Created Bool type

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

View File

@ -4,7 +4,6 @@
package attribute package attribute
import "gitlab.codemonkeysoftware.net/b/hatmill" import "gitlab.codemonkeysoftware.net/b/hatmill"
import "strconv"
// Accept creates a "accept" attribute // Accept creates a "accept" attribute
func Accept(value ...string) hatmill.Attrib { func Accept(value ...string) hatmill.Attrib {
@ -134,7 +133,7 @@ func Content(value string) hatmill.Attrib {
func Contenteditable(value bool) hatmill.Attrib { func Contenteditable(value bool) hatmill.Attrib {
return hatmill.Attrib{ return hatmill.Attrib{
Key: "contenteditable", Key: "contenteditable",
Value: String(strconv.FormatBool(value)), Value: String(Bool(value).String()),
} }
} }
@ -226,7 +225,7 @@ func Download(value string) hatmill.Attrib {
func Draggable(value bool) hatmill.Attrib { func Draggable(value bool) hatmill.Attrib {
return hatmill.Attrib{ return hatmill.Attrib{
Key: "draggable", Key: "draggable",
Value: String(strconv.FormatBool(value)), Value: String(Bool(value).String()),
} }
} }
@ -639,7 +638,7 @@ func Span(value int) hatmill.Attrib {
func Spellcheck(value bool) hatmill.Attrib { func Spellcheck(value bool) hatmill.Attrib {
return hatmill.Attrib{ return hatmill.Attrib{
Key: "spellcheck", Key: "spellcheck",
Value: String(strconv.FormatBool(value)), Value: String(Bool(value).String()),
} }
} }

View File

@ -22,12 +22,3 @@ func TestString(t *testing.T) {
func TestBool(t *testing.T) { func TestBool(t *testing.T) {
testAttribValue(t, attribute.Disabled(), "") testAttribValue(t, attribute.Disabled(), "")
} }
func TestExplicitBool(t *testing.T) {
t.Run("true", func(t *testing.T) {
testAttribValue(t, attribute.Draggable(true), "true")
})
t.Run("false", func(t *testing.T) {
testAttribValue(t, attribute.Draggable(false), "false")
})
}

View File

@ -26,3 +26,9 @@ type Int int
func (n Int) String() string { func (n Int) String() string {
return strconv.FormatInt(int64(n), 10) return strconv.FormatInt(int64(n), 10)
} }
type Bool bool
func (b Bool) String() string {
return strconv.FormatBool(bool(b))
}

View File

@ -67,3 +67,12 @@ func TestInt(t *testing.T) {
expectEqualStrings(t, attribute.Int(-45).String(), "-45") expectEqualStrings(t, attribute.Int(-45).String(), "-45")
}) })
} }
func TestExplicitBool(t *testing.T) {
t.Run("true", func(t *testing.T) {
expectEqualStrings(t, attribute.Bool(true).String(), "true")
})
t.Run("false", func(t *testing.T) {
expectEqualStrings(t, attribute.Bool(false).String(), "false")
})
}

View File

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