From 9f7f25a89e8cdf58ba7664c57a79fe76a91552da Mon Sep 17 00:00:00 2001 From: Brandon Dyck Date: Sat, 31 Aug 2019 10:20:58 -0600 Subject: [PATCH] Created Int type --- attribute/generated.go | 22 +++++++++++----------- attribute/generated_test.go | 12 ------------ attribute/value_types.go | 6 ++++++ attribute/value_types_test.go | 12 ++++++++++++ internal/codegen/codegen.go | 3 +-- 5 files changed, 30 insertions(+), 25 deletions(-) diff --git a/attribute/generated.go b/attribute/generated.go index 57c9dcb..e4f875e 100644 --- a/attribute/generated.go +++ b/attribute/generated.go @@ -110,7 +110,7 @@ func Class(value ...string) hatmill.Attrib { func Cols(value int) hatmill.Attrib { return hatmill.Attrib{ Key: "cols", - Value: String(strconv.FormatInt(int64(value), 10)), + Value: String(Int(value).String()), } } @@ -118,7 +118,7 @@ func Cols(value int) hatmill.Attrib { func Colspan(value int) hatmill.Attrib { return hatmill.Attrib{ Key: "colspan", - Value: String(strconv.FormatInt(int64(value), 10)), + Value: String(Int(value).String()), } } @@ -282,7 +282,7 @@ func Headers(value ...string) hatmill.Attrib { func Height(value int) hatmill.Attrib { return hatmill.Attrib{ Key: "height", - Value: String(strconv.FormatInt(int64(value), 10)), + Value: String(Int(value).String()), } } @@ -407,7 +407,7 @@ func Max(value float32) hatmill.Attrib { func Maxlength(value int) hatmill.Attrib { return hatmill.Attrib{ Key: "maxlength", - Value: String(strconv.FormatInt(int64(value), 10)), + Value: String(Int(value).String()), } } @@ -439,7 +439,7 @@ func Min(value float32) hatmill.Attrib { func Minlength(value int) hatmill.Attrib { return hatmill.Attrib{ Key: "minlength", - Value: String(strconv.FormatInt(int64(value), 10)), + Value: String(Int(value).String()), } } @@ -568,7 +568,7 @@ func Reversed() hatmill.Attrib { func Rows(value int) hatmill.Attrib { return hatmill.Attrib{ Key: "rows", - Value: String(strconv.FormatInt(int64(value), 10)), + Value: String(Int(value).String()), } } @@ -576,7 +576,7 @@ func Rows(value int) hatmill.Attrib { func Rowspan(value int) hatmill.Attrib { return hatmill.Attrib{ Key: "rowspan", - Value: String(strconv.FormatInt(int64(value), 10)), + Value: String(Int(value).String()), } } @@ -615,7 +615,7 @@ func Shape(value string) hatmill.Attrib { func Size(value int) hatmill.Attrib { return hatmill.Attrib{ Key: "size", - Value: String(strconv.FormatInt(int64(value), 10)), + Value: String(Int(value).String()), } } @@ -631,7 +631,7 @@ func Sizes(value string) hatmill.Attrib { func Span(value int) hatmill.Attrib { return hatmill.Attrib{ Key: "span", - Value: String(strconv.FormatInt(int64(value), 10)), + Value: String(Int(value).String()), } } @@ -695,7 +695,7 @@ func Style(value string) hatmill.Attrib { func Tabindex(value int) hatmill.Attrib { return hatmill.Attrib{ Key: "tabindex", - Value: String(strconv.FormatInt(int64(value), 10)), + Value: String(Int(value).String()), } } @@ -743,7 +743,7 @@ func Value(value string) hatmill.Attrib { func Width(value int) hatmill.Attrib { return hatmill.Attrib{ Key: "width", - Value: String(strconv.FormatInt(int64(value), 10)), + Value: String(Int(value).String()), } } diff --git a/attribute/generated_test.go b/attribute/generated_test.go index df7ecd3..babccf2 100644 --- a/attribute/generated_test.go +++ b/attribute/generated_test.go @@ -31,15 +31,3 @@ func TestExplicitBool(t *testing.T) { testAttribValue(t, attribute.Draggable(false), "false") }) } - -func TestInt(t *testing.T) { - t.Run("zero", func(t *testing.T) { - testAttribValue(t, attribute.Height(0), "0") - }) - t.Run("positive", func(t *testing.T) { - testAttribValue(t, attribute.Height(45), "45") - }) - t.Run("negative", func(t *testing.T) { - testAttribValue(t, attribute.Tabindex(-45), "-45") - }) -} diff --git a/attribute/value_types.go b/attribute/value_types.go index bc7a6bd..5f64231 100644 --- a/attribute/value_types.go +++ b/attribute/value_types.go @@ -20,3 +20,9 @@ type Float float32 func (f Float) String() string { return strconv.FormatFloat(float64(f), 'G', -1, 32) } + +type Int int + +func (n Int) String() string { + return strconv.FormatInt(int64(n), 10) +} diff --git a/attribute/value_types_test.go b/attribute/value_types_test.go index 14028e4..86e7ae7 100644 --- a/attribute/value_types_test.go +++ b/attribute/value_types_test.go @@ -55,3 +55,15 @@ func TestFloat(t *testing.T) { }) } } + +func TestInt(t *testing.T) { + t.Run("zero", func(t *testing.T) { + expectEqualStrings(t, attribute.Int(0).String(), "0") + }) + t.Run("positive", func(t *testing.T) { + expectEqualStrings(t, attribute.Int(45).String(), "45") + }) + t.Run("negative", func(t *testing.T) { + expectEqualStrings(t, attribute.Int(-45).String(), "-45") + }) +} diff --git a/internal/codegen/codegen.go b/internal/codegen/codegen.go index 7e3bfa2..47bc9d8 100644 --- a/internal/codegen/codegen.go +++ b/internal/codegen/codegen.go @@ -60,8 +60,7 @@ var attribTypes = map[string]AttribTypeInfo{ Imports: []string{"strconv"}, }, "int": { - Template: simpleTemplate("int", "strconv.FormatInt(int64(%s), 10)"), - Imports: []string{"strconv"}, + Template: simpleTemplate("int", "Int(%s).String()"), }, "float": { Template: simpleTemplate("float32", "Float(%s).String()"),