repogen/internal/codegen/struct.go

48 lines
961 B
Go
Raw Permalink Normal View History

package codegen
import (
"bytes"
"fmt"
"strings"
"text/template"
2023-05-24 11:04:29 +00:00
"git.kmsign.ru/royalcat/repogen/internal/code"
)
const structTemplate = `
type {{.Name}} struct {
{{.GenFields}}
}
`
// StructBuilder is an implementer of a struct.
type StructBuilder struct {
Name string
Fields code.StructFields
}
// Impl writes struct declatation code to the buffer.
func (sb StructBuilder) Impl(buffer *bytes.Buffer) error {
tmpl, err := template.New("struct").Parse(structTemplate)
if err != nil {
return err
}
// writing to a buffer should not cause errors.
_ = tmpl.Execute(buffer, sb)
return nil
}
func (sb StructBuilder) GenFields() string {
var fieldLines []string
for _, field := range sb.Fields {
fieldLine := fmt.Sprintf("\t%s %s", field.Name, field.Type.Code())
if len(field.Tag) > 0 {
fieldLine += fmt.Sprintf(" `%s`", string(field.Tag))
}
fieldLines = append(fieldLines, fieldLine)
}
return strings.Join(fieldLines, "\n")
}