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