Got rid of extra cast to String
This commit is contained in:
@ -20,54 +20,55 @@ func identifier(s string) string {
|
||||
}
|
||||
|
||||
type AttribTypeInfo struct {
|
||||
Template string
|
||||
Template AttribTemplate
|
||||
}
|
||||
|
||||
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: String(%s),
|
||||
}
|
||||
type AttribTemplate func(identifier, name string) string
|
||||
|
||||
func simpleTemplate(paramType, toType string) AttribTemplate {
|
||||
return func(identifier, name string) string {
|
||||
return fmt.Sprintf(
|
||||
`func %s(value %s) hatmill.Attrib {
|
||||
return hatmill.Attrib{
|
||||
Key: "%s",
|
||||
Value: %s(value),
|
||||
}
|
||||
}
|
||||
`, identifier, paramType, name, toType)
|
||||
}
|
||||
`, paramType, conversion)
|
||||
}
|
||||
|
||||
func listTemplate(separator string) string {
|
||||
return fmt.Sprintf(`func %%s(value ...string) hatmill.Attrib {
|
||||
return hatmill.Attrib{
|
||||
Key: "%%s",
|
||||
Value: String(strings.Join(value, "%s")),
|
||||
}
|
||||
}
|
||||
`, separator)
|
||||
}
|
||||
|
||||
var attribTypes = map[string]AttribTypeInfo{
|
||||
"string": {Template: simpleTemplate("string", "%s")},
|
||||
"bool": {
|
||||
Template: `func %s() hatmill.Attrib {
|
||||
func boolTemplate(identifier, name string) string {
|
||||
return fmt.Sprintf(
|
||||
`func %s() hatmill.Attrib {
|
||||
return hatmill.Attrib{
|
||||
Key: "%s",
|
||||
}
|
||||
}
|
||||
`,
|
||||
`, identifier, name)
|
||||
}
|
||||
|
||||
var attribTypes = map[string]AttribTypeInfo{
|
||||
"string": {
|
||||
Template: simpleTemplate("string", "String"),
|
||||
},
|
||||
"bool": {
|
||||
Template: boolTemplate,
|
||||
},
|
||||
"explicit bool": {
|
||||
Template: simpleTemplate("bool", "Bool(%s).String()"),
|
||||
Template: simpleTemplate("bool", "Bool"),
|
||||
},
|
||||
"int": {
|
||||
Template: simpleTemplate("int", "Int(%s).String()"),
|
||||
Template: simpleTemplate("int", "Int"),
|
||||
},
|
||||
"float": {
|
||||
Template: simpleTemplate("float32", "Float(%s).String()"),
|
||||
Template: simpleTemplate("float32", "Float"),
|
||||
},
|
||||
"space list": {
|
||||
Template: simpleTemplate("...string", "SpaceList(%s).String()"),
|
||||
Template: simpleTemplate("...string", "SpaceList"),
|
||||
},
|
||||
"comma list": {
|
||||
Template: simpleTemplate("...string", "CommaList(%s).String()"),
|
||||
Template: simpleTemplate("...string", "CommaList"),
|
||||
},
|
||||
}
|
||||
|
||||
@ -91,7 +92,7 @@ func (spec AttribSpec) Generate() Def {
|
||||
comment := fmt.Sprintf("// %s creates a \"%s\" attribute\n", ident, name)
|
||||
template := attribTypes[spec.Type].Template
|
||||
return Def{
|
||||
Source: comment + fmt.Sprintf(template, ident, name),
|
||||
Source: comment + template(ident, name),
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user