Refactored codegen templates

This commit is contained in:
Brandon Dyck 2019-04-27 15:26:59 -06:00
parent 141a1a3219
commit fe2baa121f

View File

@ -24,61 +24,37 @@ type AttribTypeInfo struct {
Imports []string
}
func simpleTemplate(paramType, convertExpr string) string {
conversion := fmt.Sprintf(convertExpr, "value")
return fmt.Sprintf(`func %%s(value %s) hatmill.Attrib {
return hatmill.Attrib{
Key: "%%s",
Value: %s,
}
}
`, paramType, conversion)
}
var attribTypes = map[string]AttribTypeInfo{
"string": {
Template: `// %[1]s creates a "%[2]s" attribute
func %[1]s(value string) hatmill.Attrib {
return hatmill.Attrib{
Key: "%[2]s",
Value: value,
}
}
`,
},
"string": {Template: simpleTemplate("string", "%s")},
"bool": {
Template: `// %[1]s creates a "%[2]s" attribute
func %[1]s() hatmill.Attrib {
Template: `func %s() hatmill.Attrib {
return hatmill.Attrib{
Key: "%[2]s",
Key: "%s",
}
}
`,
},
"explicit bool": {
Template: `// %[1]s creates a "%[2]s" attribute
func %[1]s(value bool) hatmill.Attrib {
return hatmill.Attrib{
Key: "%[2]s",
Value: strconv.FormatBool(value),
}
}
`,
Template: simpleTemplate("bool", "strconv.FormatBool(%s)"),
Imports: []string{"strconv"},
},
"int": {
Template: `// %[1]s creates a "%[2]s" attribute
func %[1]s(value int) hatmill.Attrib {
return hatmill.Attrib{
Key: "%[2]s",
Value: strconv.FormatInt(int64(value), 10),
}
}
`,
Template: simpleTemplate("int", "strconv.FormatInt(int64(%s), 10)"),
Imports: []string{"strconv"},
},
"float": {
Template: `// %[1]s creates a "%[2]s" attribute
func %[1]s(value float32) hatmill.Attrib {
return hatmill.Attrib{
Key: "%[2]s",
Value: strconv.FormatFloat(float64(value), 'G', -1, 32),
}
}
`,
Template: simpleTemplate("float32", "strconv.FormatFloat(float64(%s), 'G', -1, 32)"),
Imports: []string{"strconv"},
},
}
@ -99,9 +75,12 @@ type AttribSpec struct {
}
func (spec AttribSpec) Generate() Def {
name := spec.Name
ident := identifier(spec.Name)
comment := fmt.Sprintf("// %s creates a \"%s\" attribute\n", ident, name)
template := attribTypes[spec.Type].Template
return Def{
Source: fmt.Sprintf(template, identifier(spec.Name), spec.Name),
Source: comment + fmt.Sprintf(template, ident, name),
Imports: attribTypes[spec.Type].Imports,
}
}
@ -113,12 +92,11 @@ type ElemSpec struct {
func (spec ElemSpec) Generate() Def {
const (
parentTemplate = `// %[1]s creates a <%[2]s> element.
func %[1]s(attribs ...hatmill.Attrib) func(children ...hatmill.Term) hatmill.ParentElement {
parentTemplate = `func %s(attribs ...hatmill.Attrib) func(children ...hatmill.Term) hatmill.ParentElement {
return func(children ...hatmill.Term) hatmill.ParentElement {
return hatmill.ParentElement{
VoidElement: hatmill.VoidElement{
TagName: "%[2]s",
TagName: "%s",
Attribs: attribs,
},
Children: children,
@ -126,22 +104,24 @@ func (spec ElemSpec) Generate() Def {
}
}
`
voidTemplate = `// %[1]s creates a <%[2]s> element.
func %[1]s(attribs ...hatmill.Attrib) hatmill.VoidElement {
voidTemplate = `func %s(attribs ...hatmill.Attrib) hatmill.VoidElement {
return hatmill.VoidElement{
TagName: "%[2]s",
TagName: "%s",
Attribs: attribs,
}
}
`
)
name := spec.Name
ident := identifier(spec.Name)
comment := fmt.Sprintf("// %s creates a <%s> element.\n", ident, name)
template := parentTemplate
if spec.Void {
template = voidTemplate
}
return Def{
Source: fmt.Sprintf(template, identifier(spec.Name), spec.Name),
Source: comment + fmt.Sprintf(template, ident, name),
}
}