Created Bool type
This commit is contained in:
parent
9f7f25a89e
commit
e81a25f1e7
@ -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()),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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")
|
||||
})
|
||||
}
|
||||
|
@ -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))
|
||||
}
|
||||
|
@ -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")
|
||||
})
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user