2022-10-18 12:37:50 +00:00
|
|
|
package codegen
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"text/template"
|
|
|
|
|
2023-05-24 11:01:50 +00:00
|
|
|
"git.kmsign.com/royalcat/repogen/internal/code"
|
2022-10-18 12:37:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const functionTemplate = `
|
|
|
|
func {{.Name}}({{.GenParams}}){{.GenReturns}} {
|
2023-04-18 13:21:46 +00:00
|
|
|
{{.Body.Code}}
|
2022-10-18 12:37:50 +00:00
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
// FunctionBuilder is an implementer of a function.
|
|
|
|
type FunctionBuilder struct {
|
|
|
|
Name string
|
|
|
|
Params []code.Param
|
|
|
|
Returns []code.Type
|
2023-04-18 13:21:46 +00:00
|
|
|
Body FunctionBody
|
2022-10-18 12:37:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Impl writes function declatation code to the buffer.
|
|
|
|
func (fb FunctionBuilder) Impl(buffer *bytes.Buffer) error {
|
|
|
|
tmpl, err := template.New("function").Parse(functionTemplate)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// writing to a buffer should not cause errors.
|
|
|
|
_ = tmpl.Execute(buffer, fb)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fb FunctionBuilder) GenParams() string {
|
|
|
|
return generateParams(fb.Params)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fb FunctionBuilder) GenReturns() string {
|
|
|
|
return generateReturns(fb.Returns)
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateParams(params []code.Param) string {
|
|
|
|
var paramList []string
|
|
|
|
for _, param := range params {
|
|
|
|
paramList = append(
|
|
|
|
paramList,
|
|
|
|
fmt.Sprintf("%s %s", param.Name, param.Type.Code()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return strings.Join(paramList, ", ")
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateReturns(returns []code.Type) string {
|
|
|
|
if len(returns) == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(returns) == 1 {
|
|
|
|
return " " + returns[0].Code()
|
|
|
|
}
|
|
|
|
|
|
|
|
var returnList []string
|
|
|
|
for _, ret := range returns {
|
|
|
|
returnList = append(returnList, ret.Code())
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf(" (%s)", strings.Join(returnList, ", "))
|
|
|
|
}
|