From 4aacd6e7225f1bdc483446e96394fd1a8fe4e6b5 Mon Sep 17 00:00:00 2001 From: Brandon Dyck Date: Wed, 29 May 2019 22:26:36 -0600 Subject: [PATCH] Added comma-delimited string attrib type --- CHANGELOG.md | 4 ++-- attribute/generated.go | 8 ++++---- attribute/generated_test.go | 9 +++++++++ defs.json | 4 ++-- internal/codegen/codegen.go | 24 ++++++++++++++++-------- 5 files changed, 33 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 09af8a5..01b7620 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,8 +10,8 @@ ### Changed - Formatted code identifiers in Changelog -- Attributes `accept-charset`, `class`, `for`, `headers`, `ping`, `rel`, and -`sandbox` have variadic string parameters. +- Attributes `accept`, `accept-charset`, `class`, `for`, `headers`, `ping`, +`rel`, `srcset`, and `sandbox` have variadic string parameters. ## [0.0.4] - 2019-05-27 diff --git a/attribute/generated.go b/attribute/generated.go index 17e9a9b..1bbe738 100644 --- a/attribute/generated.go +++ b/attribute/generated.go @@ -8,10 +8,10 @@ import "strings" import "strconv" // Accept sets the content types or file types allowed in a form or file input. -func Accept(value string) hatmill.Attrib { +func Accept(value ...string) hatmill.Attrib { return hatmill.Attrib{ Key: "accept", - Value: value, + Value: strings.Join(value, ","), } } @@ -677,10 +677,10 @@ func Srclang(value string) hatmill.Attrib { } // Srcset creates a "srcset" attribute -func Srcset(value string) hatmill.Attrib { +func Srcset(value ...string) hatmill.Attrib { return hatmill.Attrib{ Key: "srcset", - Value: value, + Value: strings.Join(value, ","), } } diff --git a/attribute/generated_test.go b/attribute/generated_test.go index be1a0a5..cb17874 100644 --- a/attribute/generated_test.go +++ b/attribute/generated_test.go @@ -23,6 +23,15 @@ func TestSpaceList(t *testing.T) { }) } +func TestCommaList(t *testing.T) { + t.Run("empty", func(t *testing.T) { + testAttribValue(t, attribute.Class(), "") + }) + t.Run("nonempty", func(t *testing.T) { + testAttribValue(t, attribute.Accept("alpha", "bravo", "charlie"), "alpha,bravo,charlie") + }) +} + func TestString(t *testing.T) { const s = "abcdefg" testAttribValue(t, attribute.Id(s), s) diff --git a/defs.json b/defs.json index f5a18d6..815f7f3 100644 --- a/defs.json +++ b/defs.json @@ -1,6 +1,6 @@ { "attributes": [ - {"name": "accept", "type": "string", "comment": "Accept sets the content types or file types allowed in a form or file input."}, + {"name": "accept", "type": "comma list", "comment": "Accept sets the content types or file types allowed in a form or file input."}, {"name": "accept-charset", "type": "space list"}, {"name": "action", "type": "string"}, {"name": "alt", "type": "string"}, @@ -86,7 +86,7 @@ {"name": "src", "type": "string"}, {"name": "srcdoc", "type": "string"}, {"name": "srclang", "type": "string"}, - {"name": "srcset", "type": "string"}, + {"name": "srcset", "type": "comma list"}, {"name": "start", "type": "string"}, {"name": "style", "type": "string"}, {"name": "tabindex", "type": "int"}, diff --git a/internal/codegen/codegen.go b/internal/codegen/codegen.go index 9e43cd2..67afa8a 100644 --- a/internal/codegen/codegen.go +++ b/internal/codegen/codegen.go @@ -39,6 +39,16 @@ func simpleTemplate(paramType, convertExpr string) string { `, paramType, conversion) } +func listTemplate(separator string) string { + return fmt.Sprintf(`func %%s(value ...string) hatmill.Attrib { + return hatmill.Attrib{ + Key: "%%s", + Value: strings.Join(value, "%s"), + } + } + `, separator) +} + var attribTypes = map[string]AttribTypeInfo{ "string": {Template: simpleTemplate("string", "%s")}, "bool": { @@ -62,14 +72,12 @@ var attribTypes = map[string]AttribTypeInfo{ Imports: []string{"strconv"}, }, "space list": { - Template: `func %s(value ...string) hatmill.Attrib { - return hatmill.Attrib{ - Key: "%s", - Value: strings.Join(value, " "), - } - } - `, - Imports: []string{"strings"}, + Template: listTemplate(" "), + Imports: []string{"strings"}, + }, + "comma list": { + Template: listTemplate(","), + Imports: []string{"strings"}, }, }