From 5412e5e1081a6f2d1192e2441f559b12fff63d23 Mon Sep 17 00:00:00 2001 From: Brandon Dyck Date: Sat, 31 Aug 2019 10:46:54 -0600 Subject: [PATCH] Got rid of extra cast to String --- attribute/generated.go | 56 ++++++++++++++++---------------- attribute/generated_test.go | 5 --- attribute/string.go | 7 ---- attribute/value_types.go | 6 ++++ attribute/value_types_test.go | 5 +++ internal/codegen/codegen.go | 61 ++++++++++++++++++----------------- 6 files changed, 70 insertions(+), 70 deletions(-) delete mode 100644 attribute/string.go diff --git a/attribute/generated.go b/attribute/generated.go index 3cd3333..8e0c05b 100644 --- a/attribute/generated.go +++ b/attribute/generated.go @@ -9,7 +9,7 @@ import "gitlab.codemonkeysoftware.net/b/hatmill" func Accept(value ...string) hatmill.Attrib { return hatmill.Attrib{ Key: "accept", - Value: String(CommaList(value).String()), + Value: CommaList(value), } } @@ -17,7 +17,7 @@ func Accept(value ...string) hatmill.Attrib { func AcceptCharset(value ...string) hatmill.Attrib { return hatmill.Attrib{ Key: "accept-charset", - Value: String(SpaceList(value).String()), + Value: SpaceList(value), } } @@ -101,7 +101,7 @@ func Cite(value string) hatmill.Attrib { func Class(value ...string) hatmill.Attrib { return hatmill.Attrib{ Key: "class", - Value: String(SpaceList(value).String()), + Value: SpaceList(value), } } @@ -109,7 +109,7 @@ func Class(value ...string) hatmill.Attrib { func Cols(value int) hatmill.Attrib { return hatmill.Attrib{ Key: "cols", - Value: String(Int(value).String()), + Value: Int(value), } } @@ -117,7 +117,7 @@ func Cols(value int) hatmill.Attrib { func Colspan(value int) hatmill.Attrib { return hatmill.Attrib{ Key: "colspan", - Value: String(Int(value).String()), + Value: Int(value), } } @@ -133,7 +133,7 @@ func Content(value string) hatmill.Attrib { func Contenteditable(value bool) hatmill.Attrib { return hatmill.Attrib{ Key: "contenteditable", - Value: String(Bool(value).String()), + Value: Bool(value), } } @@ -225,7 +225,7 @@ func Download(value string) hatmill.Attrib { func Draggable(value bool) hatmill.Attrib { return hatmill.Attrib{ Key: "draggable", - Value: String(Bool(value).String()), + Value: Bool(value), } } @@ -241,7 +241,7 @@ func Enctype(value string) hatmill.Attrib { func For(value ...string) hatmill.Attrib { return hatmill.Attrib{ Key: "for", - Value: String(SpaceList(value).String()), + Value: SpaceList(value), } } @@ -273,7 +273,7 @@ func Formmethod(value string) hatmill.Attrib { func Headers(value ...string) hatmill.Attrib { return hatmill.Attrib{ Key: "headers", - Value: String(SpaceList(value).String()), + Value: SpaceList(value), } } @@ -281,7 +281,7 @@ func Headers(value ...string) hatmill.Attrib { func Height(value int) hatmill.Attrib { return hatmill.Attrib{ Key: "height", - Value: String(Int(value).String()), + Value: Int(value), } } @@ -296,7 +296,7 @@ func Hidden() hatmill.Attrib { func High(value float32) hatmill.Attrib { return hatmill.Attrib{ Key: "high", - Value: String(Float(value).String()), + Value: Float(value), } } @@ -390,7 +390,7 @@ func Loop() hatmill.Attrib { func Low(value float32) hatmill.Attrib { return hatmill.Attrib{ Key: "low", - Value: String(Float(value).String()), + Value: Float(value), } } @@ -398,7 +398,7 @@ func Low(value float32) hatmill.Attrib { func Max(value float32) hatmill.Attrib { return hatmill.Attrib{ Key: "max", - Value: String(Float(value).String()), + Value: Float(value), } } @@ -406,7 +406,7 @@ func Max(value float32) hatmill.Attrib { func Maxlength(value int) hatmill.Attrib { return hatmill.Attrib{ Key: "maxlength", - Value: String(Int(value).String()), + Value: Int(value), } } @@ -430,7 +430,7 @@ func Method(value string) hatmill.Attrib { func Min(value float32) hatmill.Attrib { return hatmill.Attrib{ Key: "min", - Value: String(Float(value).String()), + Value: Float(value), } } @@ -438,7 +438,7 @@ func Min(value float32) hatmill.Attrib { func Minlength(value int) hatmill.Attrib { return hatmill.Attrib{ Key: "minlength", - Value: String(Int(value).String()), + Value: Int(value), } } @@ -482,7 +482,7 @@ func Open() hatmill.Attrib { func Optimum(value float32) hatmill.Attrib { return hatmill.Attrib{ Key: "optimum", - Value: String(Float(value).String()), + Value: Float(value), } } @@ -498,7 +498,7 @@ func Pattern(value string) hatmill.Attrib { func Ping(value ...string) hatmill.Attrib { return hatmill.Attrib{ Key: "ping", - Value: String(SpaceList(value).String()), + Value: SpaceList(value), } } @@ -545,7 +545,7 @@ func Referrerpolicy(value string) hatmill.Attrib { func Rel(value ...string) hatmill.Attrib { return hatmill.Attrib{ Key: "rel", - Value: String(SpaceList(value).String()), + Value: SpaceList(value), } } @@ -567,7 +567,7 @@ func Reversed() hatmill.Attrib { func Rows(value int) hatmill.Attrib { return hatmill.Attrib{ Key: "rows", - Value: String(Int(value).String()), + Value: Int(value), } } @@ -575,7 +575,7 @@ func Rows(value int) hatmill.Attrib { func Rowspan(value int) hatmill.Attrib { return hatmill.Attrib{ Key: "rowspan", - Value: String(Int(value).String()), + Value: Int(value), } } @@ -583,7 +583,7 @@ func Rowspan(value int) hatmill.Attrib { func Sandbox(value ...string) hatmill.Attrib { return hatmill.Attrib{ Key: "sandbox", - Value: String(SpaceList(value).String()), + Value: SpaceList(value), } } @@ -614,7 +614,7 @@ func Shape(value string) hatmill.Attrib { func Size(value int) hatmill.Attrib { return hatmill.Attrib{ Key: "size", - Value: String(Int(value).String()), + Value: Int(value), } } @@ -630,7 +630,7 @@ func Sizes(value string) hatmill.Attrib { func Span(value int) hatmill.Attrib { return hatmill.Attrib{ Key: "span", - Value: String(Int(value).String()), + Value: Int(value), } } @@ -638,7 +638,7 @@ func Span(value int) hatmill.Attrib { func Spellcheck(value bool) hatmill.Attrib { return hatmill.Attrib{ Key: "spellcheck", - Value: String(Bool(value).String()), + Value: Bool(value), } } @@ -670,7 +670,7 @@ func Srclang(value string) hatmill.Attrib { func Srcset(value ...string) hatmill.Attrib { return hatmill.Attrib{ Key: "srcset", - Value: String(CommaList(value).String()), + Value: CommaList(value), } } @@ -694,7 +694,7 @@ func Style(value string) hatmill.Attrib { func Tabindex(value int) hatmill.Attrib { return hatmill.Attrib{ Key: "tabindex", - Value: String(Int(value).String()), + Value: Int(value), } } @@ -742,7 +742,7 @@ func Value(value string) hatmill.Attrib { func Width(value int) hatmill.Attrib { return hatmill.Attrib{ Key: "width", - Value: String(Int(value).String()), + Value: Int(value), } } diff --git a/attribute/generated_test.go b/attribute/generated_test.go index 4a9fef0..6352821 100644 --- a/attribute/generated_test.go +++ b/attribute/generated_test.go @@ -14,11 +14,6 @@ func testAttribValue(t *testing.T, attrib hatmill.Attrib, expectedValue string) } } -func TestString(t *testing.T) { - const s = "abcdefg" - testAttribValue(t, attribute.Id(s), s) -} - func TestBool(t *testing.T) { testAttribValue(t, attribute.Disabled(), "") } diff --git a/attribute/string.go b/attribute/string.go deleted file mode 100644 index 8eb46fe..0000000 --- a/attribute/string.go +++ /dev/null @@ -1,7 +0,0 @@ -package attribute - -type String string - -func (s String) String() string { - return string(s) -} diff --git a/attribute/value_types.go b/attribute/value_types.go index 001b49e..84ad47f 100644 --- a/attribute/value_types.go +++ b/attribute/value_types.go @@ -32,3 +32,9 @@ type Bool bool func (b Bool) String() string { return strconv.FormatBool(bool(b)) } + +type String string + +func (s String) String() string { + return string(s) +} diff --git a/attribute/value_types_test.go b/attribute/value_types_test.go index d2adf31..38b46de 100644 --- a/attribute/value_types_test.go +++ b/attribute/value_types_test.go @@ -76,3 +76,8 @@ func TestExplicitBool(t *testing.T) { expectEqualStrings(t, attribute.Bool(false).String(), "false") }) } + +func TestString(t *testing.T) { + const s = "abcdefg" + expectEqualStrings(t, attribute.String(s).String(), s) +} diff --git a/internal/codegen/codegen.go b/internal/codegen/codegen.go index 3cc2c8d..3f6f54c 100644 --- a/internal/codegen/codegen.go +++ b/internal/codegen/codegen.go @@ -20,54 +20,55 @@ func identifier(s string) string { } type AttribTypeInfo struct { - Template string + Template AttribTemplate } -func simpleTemplate(paramType, convertExpr string) string { - conversion := fmt.Sprintf(convertExpr, "value") - return fmt.Sprintf(`func %%s(value %s) hatmill.Attrib { - return hatmill.Attrib{ - Key: "%%s", - Value: String(%s), - } +type AttribTemplate func(identifier, name string) string + +func simpleTemplate(paramType, toType string) AttribTemplate { + return func(identifier, name string) string { + return fmt.Sprintf( + `func %s(value %s) hatmill.Attrib { + return hatmill.Attrib{ + Key: "%s", + Value: %s(value), + } + } + `, identifier, paramType, name, toType) } - `, paramType, conversion) } -func listTemplate(separator string) string { - return fmt.Sprintf(`func %%s(value ...string) hatmill.Attrib { - return hatmill.Attrib{ - Key: "%%s", - Value: String(strings.Join(value, "%s")), - } - } - `, separator) -} - -var attribTypes = map[string]AttribTypeInfo{ - "string": {Template: simpleTemplate("string", "%s")}, - "bool": { - Template: `func %s() hatmill.Attrib { +func boolTemplate(identifier, name string) string { + return fmt.Sprintf( + `func %s() hatmill.Attrib { return hatmill.Attrib{ Key: "%s", } } - `, + `, identifier, name) +} + +var attribTypes = map[string]AttribTypeInfo{ + "string": { + Template: simpleTemplate("string", "String"), + }, + "bool": { + Template: boolTemplate, }, "explicit bool": { - Template: simpleTemplate("bool", "Bool(%s).String()"), + Template: simpleTemplate("bool", "Bool"), }, "int": { - Template: simpleTemplate("int", "Int(%s).String()"), + Template: simpleTemplate("int", "Int"), }, "float": { - Template: simpleTemplate("float32", "Float(%s).String()"), + Template: simpleTemplate("float32", "Float"), }, "space list": { - Template: simpleTemplate("...string", "SpaceList(%s).String()"), + Template: simpleTemplate("...string", "SpaceList"), }, "comma list": { - Template: simpleTemplate("...string", "CommaList(%s).String()"), + Template: simpleTemplate("...string", "CommaList"), }, } @@ -91,7 +92,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), + Source: comment + template(ident, name), } }