Created Bool type

This commit is contained in:
2019-08-31 10:30:17 -06:00
parent 9f7f25a89e
commit e81a25f1e7
5 changed files with 21 additions and 31 deletions

View File

@ -21,7 +21,6 @@ func identifier(s string) string {
type AttribTypeInfo struct {
Template string
Imports []string
}
func simpleTemplate(paramType, convertExpr string) string {
@ -56,8 +55,7 @@ var attribTypes = map[string]AttribTypeInfo{
`,
},
"explicit bool": {
Template: simpleTemplate("bool", "strconv.FormatBool(%s)"),
Imports: []string{"strconv"},
Template: simpleTemplate("bool", "Bool(%s).String()"),
},
"int": {
Template: simpleTemplate("int", "Int(%s).String()"),
@ -75,8 +73,7 @@ var attribTypes = map[string]AttribTypeInfo{
// Def represents a top-level definition and its required imports.
type Def struct {
Source string
Imports []string
Source string
}
type Spec interface {
@ -94,8 +91,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),
Imports: attribTypes[spec.Type].Imports,
Source: comment + fmt.Sprintf(template, ident, name),
}
}
@ -149,17 +145,6 @@ func Render(defs []Def, pkgName string) ([]byte, error) {
buf.WriteString(`import "gitlab.codemonkeysoftware.net/b/hatmill"
`)
// Print each import only once.
imports := make(map[string]struct{})
for _, def := range defs {
for _, imp := range def.Imports {
imports[imp] = struct{}{}
}
}
for imp := range imports {
fmt.Fprintf(buf, "import \"%s\"\n", imp)
}
for _, def := range defs {
buf.WriteString(def.Source)
}