repogen/internal/codegen/method.go
sunboyy bdb63c8129
Move function body generation into codegen package (#35)
* Move function body generation into codegen package
2023-04-18 20:21:46 +07:00

66 lines
1.4 KiB
Go

package codegen
import (
"bytes"
"fmt"
"text/template"
"github.com/sunboyy/repogen/internal/code"
)
const methodTemplate = `
func ({{.GenReceiver}}) {{.Name}}({{.GenParams}}){{.GenReturns}} {
{{.Body.Code}}
}
`
// MethodBuilder is an implementer of a method.
type MethodBuilder struct {
Receiver MethodReceiver
Name string
Params []code.Param
Returns []code.Type
Body FunctionBody
}
// MethodReceiver describes a specification of a method receiver.
type MethodReceiver struct {
Name string
Type code.SimpleType
Pointer bool
}
// Impl writes method declatation code to the buffer.
func (mb MethodBuilder) Impl(buffer *bytes.Buffer) error {
tmpl, err := template.New("function").Parse(methodTemplate)
if err != nil {
return err
}
// writing to a buffer should not cause errors.
_ = tmpl.Execute(buffer, mb)
return nil
}
func (mb MethodBuilder) GenReceiver() string {
if mb.Receiver.Name == "" {
return mb.generateReceiverType()
}
return fmt.Sprintf("%s %s", mb.Receiver.Name, mb.generateReceiverType())
}
func (mb MethodBuilder) generateReceiverType() string {
if !mb.Receiver.Pointer {
return mb.Receiver.Type.Code()
}
return code.PointerType{ContainedType: mb.Receiver.Type}.Code()
}
func (mb MethodBuilder) GenParams() string {
return generateParams(mb.Params)
}
func (mb MethodBuilder) GenReturns() string {
return generateReturns(mb.Returns)
}