From e81a25f1e702a921ade047137f49f3dd32cf70c7 Mon Sep 17 00:00:00 2001 From: Brandon Dyck Date: Sat, 31 Aug 2019 10:30:17 -0600 Subject: [PATCH] Created Bool type --- attribute/generated.go | 7 +++---- attribute/generated_test.go | 9 --------- attribute/value_types.go | 6 ++++++ attribute/value_types_test.go | 9 +++++++++ internal/codegen/codegen.go | 21 +++------------------ 5 files changed, 21 insertions(+), 31 deletions(-) diff --git a/attribute/generated.go b/attribute/generated.go index e4f875e..3cd3333 100644 --- a/attribute/generated.go +++ b/attribute/generated.go @@ -4,7 +4,6 @@ package attribute import "gitlab.codemonkeysoftware.net/b/hatmill" -import "strconv" // Accept creates a "accept" attribute func Accept(value ...string) hatmill.Attrib { @@ -134,7 +133,7 @@ func Content(value string) hatmill.Attrib { func Contenteditable(value bool) hatmill.Attrib { return hatmill.Attrib{ 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 { return hatmill.Attrib{ 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 { return hatmill.Attrib{ Key: "spellcheck", - Value: String(strconv.FormatBool(value)), + Value: String(Bool(value).String()), } } diff --git a/attribute/generated_test.go b/attribute/generated_test.go index babccf2..4a9fef0 100644 --- a/attribute/generated_test.go +++ b/attribute/generated_test.go @@ -22,12 +22,3 @@ func TestString(t *testing.T) { func TestBool(t *testing.T) { 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") - }) -} diff --git a/attribute/value_types.go b/attribute/value_types.go index 5f64231..001b49e 100644 --- a/attribute/value_types.go +++ b/attribute/value_types.go @@ -26,3 +26,9 @@ type Int int func (n Int) String() string { return strconv.FormatInt(int64(n), 10) } + +type Bool bool + +func (b Bool) String() string { + return strconv.FormatBool(bool(b)) +} diff --git a/attribute/value_types_test.go b/attribute/value_types_test.go index 86e7ae7..d2adf31 100644 --- a/attribute/value_types_test.go +++ b/attribute/value_types_test.go @@ -67,3 +67,12 @@ func TestInt(t *testing.T) { 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") + }) +} diff --git a/internal/codegen/codegen.go b/internal/codegen/codegen.go index 47bc9d8..3cc2c8d 100644 --- a/internal/codegen/codegen.go +++ b/internal/codegen/codegen.go @@ -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) }