Got rid of extra cast to String

This commit is contained in:
Brandon Dyck 2019-08-31 10:46:54 -06:00
parent e81a25f1e7
commit 5412e5e108
6 changed files with 70 additions and 70 deletions

View File

@ -9,7 +9,7 @@ import "gitlab.codemonkeysoftware.net/b/hatmill"
func Accept(value ...string) hatmill.Attrib { func Accept(value ...string) hatmill.Attrib {
return hatmill.Attrib{ return hatmill.Attrib{
Key: "accept", 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 { func AcceptCharset(value ...string) hatmill.Attrib {
return hatmill.Attrib{ return hatmill.Attrib{
Key: "accept-charset", 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 { func Class(value ...string) hatmill.Attrib {
return hatmill.Attrib{ return hatmill.Attrib{
Key: "class", 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 { func Cols(value int) hatmill.Attrib {
return hatmill.Attrib{ return hatmill.Attrib{
Key: "cols", 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 { func Colspan(value int) hatmill.Attrib {
return hatmill.Attrib{ return hatmill.Attrib{
Key: "colspan", 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 { func Contenteditable(value bool) hatmill.Attrib {
return hatmill.Attrib{ return hatmill.Attrib{
Key: "contenteditable", 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 { func Draggable(value bool) hatmill.Attrib {
return hatmill.Attrib{ return hatmill.Attrib{
Key: "draggable", 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 { func For(value ...string) hatmill.Attrib {
return hatmill.Attrib{ return hatmill.Attrib{
Key: "for", 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 { func Headers(value ...string) hatmill.Attrib {
return hatmill.Attrib{ return hatmill.Attrib{
Key: "headers", 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 { func Height(value int) hatmill.Attrib {
return hatmill.Attrib{ return hatmill.Attrib{
Key: "height", Key: "height",
Value: String(Int(value).String()), Value: Int(value),
} }
} }
@ -296,7 +296,7 @@ func Hidden() hatmill.Attrib {
func High(value float32) hatmill.Attrib { func High(value float32) hatmill.Attrib {
return hatmill.Attrib{ return hatmill.Attrib{
Key: "high", Key: "high",
Value: String(Float(value).String()), Value: Float(value),
} }
} }
@ -390,7 +390,7 @@ func Loop() hatmill.Attrib {
func Low(value float32) hatmill.Attrib { func Low(value float32) hatmill.Attrib {
return hatmill.Attrib{ return hatmill.Attrib{
Key: "low", 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 { func Max(value float32) hatmill.Attrib {
return hatmill.Attrib{ return hatmill.Attrib{
Key: "max", 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 { func Maxlength(value int) hatmill.Attrib {
return hatmill.Attrib{ return hatmill.Attrib{
Key: "maxlength", 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 { func Min(value float32) hatmill.Attrib {
return hatmill.Attrib{ return hatmill.Attrib{
Key: "min", 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 { func Minlength(value int) hatmill.Attrib {
return hatmill.Attrib{ return hatmill.Attrib{
Key: "minlength", Key: "minlength",
Value: String(Int(value).String()), Value: Int(value),
} }
} }
@ -482,7 +482,7 @@ func Open() hatmill.Attrib {
func Optimum(value float32) hatmill.Attrib { func Optimum(value float32) hatmill.Attrib {
return hatmill.Attrib{ return hatmill.Attrib{
Key: "optimum", 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 { func Ping(value ...string) hatmill.Attrib {
return hatmill.Attrib{ return hatmill.Attrib{
Key: "ping", 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 { func Rel(value ...string) hatmill.Attrib {
return hatmill.Attrib{ return hatmill.Attrib{
Key: "rel", Key: "rel",
Value: String(SpaceList(value).String()), Value: SpaceList(value),
} }
} }
@ -567,7 +567,7 @@ func Reversed() hatmill.Attrib {
func Rows(value int) hatmill.Attrib { func Rows(value int) hatmill.Attrib {
return hatmill.Attrib{ return hatmill.Attrib{
Key: "rows", 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 { func Rowspan(value int) hatmill.Attrib {
return hatmill.Attrib{ return hatmill.Attrib{
Key: "rowspan", 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 { func Sandbox(value ...string) hatmill.Attrib {
return hatmill.Attrib{ return hatmill.Attrib{
Key: "sandbox", 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 { func Size(value int) hatmill.Attrib {
return hatmill.Attrib{ return hatmill.Attrib{
Key: "size", 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 { func Span(value int) hatmill.Attrib {
return hatmill.Attrib{ return hatmill.Attrib{
Key: "span", 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 { func Spellcheck(value bool) hatmill.Attrib {
return hatmill.Attrib{ return hatmill.Attrib{
Key: "spellcheck", 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 { func Srcset(value ...string) hatmill.Attrib {
return hatmill.Attrib{ return hatmill.Attrib{
Key: "srcset", 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 { func Tabindex(value int) hatmill.Attrib {
return hatmill.Attrib{ return hatmill.Attrib{
Key: "tabindex", 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 { func Width(value int) hatmill.Attrib {
return hatmill.Attrib{ return hatmill.Attrib{
Key: "width", Key: "width",
Value: String(Int(value).String()), Value: Int(value),
} }
} }

View File

@ -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) { func TestBool(t *testing.T) {
testAttribValue(t, attribute.Disabled(), "") testAttribValue(t, attribute.Disabled(), "")
} }

View File

@ -1,7 +0,0 @@
package attribute
type String string
func (s String) String() string {
return string(s)
}

View File

@ -32,3 +32,9 @@ type Bool bool
func (b Bool) String() string { func (b Bool) String() string {
return strconv.FormatBool(bool(b)) return strconv.FormatBool(bool(b))
} }
type String string
func (s String) String() string {
return string(s)
}

View File

@ -76,3 +76,8 @@ func TestExplicitBool(t *testing.T) {
expectEqualStrings(t, attribute.Bool(false).String(), "false") expectEqualStrings(t, attribute.Bool(false).String(), "false")
}) })
} }
func TestString(t *testing.T) {
const s = "abcdefg"
expectEqualStrings(t, attribute.String(s).String(), s)
}

View File

@ -20,54 +20,55 @@ func identifier(s string) string {
} }
type AttribTypeInfo struct { type AttribTypeInfo struct {
Template string Template AttribTemplate
} }
func simpleTemplate(paramType, convertExpr string) string { type AttribTemplate func(identifier, name string) string
conversion := fmt.Sprintf(convertExpr, "value")
return fmt.Sprintf(`func %%s(value %s) hatmill.Attrib { func simpleTemplate(paramType, toType string) AttribTemplate {
return func(identifier, name string) string {
return fmt.Sprintf(
`func %s(value %s) hatmill.Attrib {
return hatmill.Attrib{ return hatmill.Attrib{
Key: "%%s", Key: "%s",
Value: String(%s), Value: %s(value),
} }
} }
`, paramType, conversion) `, identifier, paramType, name, toType)
}
} }
func listTemplate(separator string) string { func boolTemplate(identifier, name string) string {
return fmt.Sprintf(`func %%s(value ...string) hatmill.Attrib { return fmt.Sprintf(
return hatmill.Attrib{ `func %s() 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 {
return hatmill.Attrib{ return hatmill.Attrib{
Key: "%s", Key: "%s",
} }
} }
`, `, identifier, name)
}
var attribTypes = map[string]AttribTypeInfo{
"string": {
Template: simpleTemplate("string", "String"),
},
"bool": {
Template: boolTemplate,
}, },
"explicit bool": { "explicit bool": {
Template: simpleTemplate("bool", "Bool(%s).String()"), Template: simpleTemplate("bool", "Bool"),
}, },
"int": { "int": {
Template: simpleTemplate("int", "Int(%s).String()"), Template: simpleTemplate("int", "Int"),
}, },
"float": { "float": {
Template: simpleTemplate("float32", "Float(%s).String()"), Template: simpleTemplate("float32", "Float"),
}, },
"space list": { "space list": {
Template: simpleTemplate("...string", "SpaceList(%s).String()"), Template: simpleTemplate("...string", "SpaceList"),
}, },
"comma list": { "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) 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 + template(ident, name),
} }
} }