diff --git a/attribute/generated.go b/attribute/generated.go index 7177b30..57c9dcb 100644 --- a/attribute/generated.go +++ b/attribute/generated.go @@ -297,7 +297,7 @@ func Hidden() hatmill.Attrib { func High(value float32) hatmill.Attrib { return hatmill.Attrib{ Key: "high", - Value: String(strconv.FormatFloat(float64(value), 'G', -1, 32)), + Value: String(Float(value).String()), } } @@ -391,7 +391,7 @@ func Loop() hatmill.Attrib { func Low(value float32) hatmill.Attrib { return hatmill.Attrib{ Key: "low", - Value: String(strconv.FormatFloat(float64(value), 'G', -1, 32)), + Value: String(Float(value).String()), } } @@ -399,7 +399,7 @@ func Low(value float32) hatmill.Attrib { func Max(value float32) hatmill.Attrib { return hatmill.Attrib{ Key: "max", - Value: String(strconv.FormatFloat(float64(value), 'G', -1, 32)), + Value: String(Float(value).String()), } } @@ -431,7 +431,7 @@ func Method(value string) hatmill.Attrib { func Min(value float32) hatmill.Attrib { return hatmill.Attrib{ Key: "min", - Value: String(strconv.FormatFloat(float64(value), 'G', -1, 32)), + Value: String(Float(value).String()), } } @@ -483,7 +483,7 @@ func Open() hatmill.Attrib { func Optimum(value float32) hatmill.Attrib { return hatmill.Attrib{ Key: "optimum", - Value: String(strconv.FormatFloat(float64(value), 'G', -1, 32)), + Value: String(Float(value).String()), } } diff --git a/attribute/generated_test.go b/attribute/generated_test.go index a988952..df7ecd3 100644 --- a/attribute/generated_test.go +++ b/attribute/generated_test.go @@ -43,24 +43,3 @@ func TestInt(t *testing.T) { testAttribValue(t, attribute.Tabindex(-45), "-45") }) } - -func TestFloat(t *testing.T) { - for _, testcase := range []struct { - name string - f float32 - value string - }{ - {name: "zero", f: 0, value: "0"}, - {name: "integral", f: 45, value: "45"}, - {name: "positive fractional, small positive exponent", f: 12.55, value: "12.55"}, - {name: "positive fractional, large positive exponent", f: 1.55E+12, value: "1.55E+12"}, - {name: "positive fractional, large negative exponent", f: 1.55E-12, value: "1.55E-12"}, - {name: "negative fractional, small positive exponent", f: -12.55, value: "-12.55"}, - {name: "negative fractional, large positive exponent", f: -1.55E+12, value: "-1.55E+12"}, - {name: "negative fractional, large negative exponent", f: -1.55E-12, value: "-1.55E-12"}, - } { - t.Run(testcase.name, func(t *testing.T) { - testAttribValue(t, attribute.Min(testcase.f), testcase.value) - }) - } -} diff --git a/attribute/value_types.go b/attribute/value_types.go index a66604f..bc7a6bd 100644 --- a/attribute/value_types.go +++ b/attribute/value_types.go @@ -1,6 +1,7 @@ package attribute import "strings" +import "strconv" type SpaceList []string @@ -13,3 +14,9 @@ type CommaList []string func (l CommaList) String() string { return strings.Join(l, ",") } + +type Float float32 + +func (f Float) String() string { + return strconv.FormatFloat(float64(f), 'G', -1, 32) +} diff --git a/attribute/value_types_test.go b/attribute/value_types_test.go index f5ed050..14028e4 100644 --- a/attribute/value_types_test.go +++ b/attribute/value_types_test.go @@ -34,3 +34,24 @@ func TestCommaList(t *testing.T) { expectEqualStrings(t, actual, expected) }) } + +func TestFloat(t *testing.T) { + for _, testcase := range []struct { + name string + f attribute.Float + s string + }{ + {name: "zero", f: 0, s: "0"}, + {name: "integral", f: 45, s: "45"}, + {name: "positive fractional, small positive exponent", f: 12.55, s: "12.55"}, + {name: "positive fractional, large positive exponent", f: 1.55E+12, s: "1.55E+12"}, + {name: "positive fractional, large negative exponent", f: 1.55E-12, s: "1.55E-12"}, + {name: "negative fractional, small positive exponent", f: -12.55, s: "-12.55"}, + {name: "negative fractional, large positive exponent", f: -1.55E+12, s: "-1.55E+12"}, + {name: "negative fractional, large negative exponent", f: -1.55E-12, s: "-1.55E-12"}, + } { + t.Run(testcase.name, func(t *testing.T) { + expectEqualStrings(t, testcase.f.String(), testcase.s) + }) + } +} diff --git a/internal/codegen/codegen.go b/internal/codegen/codegen.go index 6b79ce9..7e3bfa2 100644 --- a/internal/codegen/codegen.go +++ b/internal/codegen/codegen.go @@ -64,8 +64,7 @@ var attribTypes = map[string]AttribTypeInfo{ Imports: []string{"strconv"}, }, "float": { - Template: simpleTemplate("float32", "strconv.FormatFloat(float64(%s), 'G', -1, 32)"), - Imports: []string{"strconv"}, + Template: simpleTemplate("float32", "Float(%s).String()"), }, "space list": { Template: simpleTemplate("...string", "SpaceList(%s).String()"),