Repository: maragudk/gomponents
Pull request #14 (2020-09-23 18:21:54)

Make attr.Classes a map type
This makes the usage syntax prettier. Instead of `attr.Classes(map[string]bool{})`, we can just use `attr.Classes{}`.

codecov[bot]: # [Codecov](https://codecov.io/gh/maragudk/gomponents/pull/14?src=pr&el=h1) Report
> Merging [#14](https://codecov.io/gh/maragudk/gomponents/pull/14?src=pr&el=desc) into [master](https://codecov.io/gh/maragudk/gomponents/commit/c832941edb5e896a893c663e83aba7088ea40e6c?el=desc) will **not change** coverage.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/maragudk/gomponents/pull/14/graphs/tree.svg?width=650&height=150&src=pr&token=JYN032TYAC)](https://codecov.io/gh/maragudk/gomponents/pull/14?src=pr&el=tree)

```diff
@@            Coverage Diff            @@
##            master       #14   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            3         3           
  Lines           80        81    +1     
=========================================
+ Hits            80        81    +1     
```


| [Impacted Files](https://codecov.io/gh/maragudk/gomponents/pull/14?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [attr/attributes.go](https://codecov.io/gh/maragudk/gomponents/pull/14/diff?src=pr&el=tree#diff-YXR0ci9hdHRyaWJ1dGVzLmdv) | `100.00% <100.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/maragudk/gomponents/pull/14?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/maragudk/gomponents/pull/14?src=pr&el=footer). Last update [c832941...a433038](https://codecov.io/gh/maragudk/gomponents/pull/14?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/attr/attributes.go b/attr/attributes.go
index 7ab27e4..56fcd74 100644
--- a/attr/attributes.go
+++ b/attr/attributes.go
@@ -19,15 +19,23 @@ func Class(v string) g.Node {
 	return g.Attr("class", v)
 }
 
-// Classes returns an attribute with name "class" and the value being a sorted, space-separated string of all the keys,
-// for which the corresponding value is true.
-func Classes(cs map[string]bool) g.Node {
+// Classes is a map of strings to booleans, which Renders to an attribute with name "class".
+// The attribute value is a sorted, space-separated string of all the map keys,
+// for which the corresponding map value is true.
+type Classes map[string]bool
+
+func (c Classes) Render() string {
 	var included []string
-	for c, include := range cs {
+	for c, include := range c {
 		if include {
 			included = append(included, c)
 		}
 	}
 	sort.Strings(included)
-	return g.Attr("class", strings.Join(included, " "))
+	return g.Attr("class", strings.Join(included, " ")).Render()
+}
+
+// String satisfies fmt.Stringer.
+func (c Classes) String() string {
+	return c.Render()
 }


Test patch:
diff --git a/attr/attributes_test.go b/attr/attributes_test.go
index 0ad8c4e..9c7ece7 100644
--- a/attr/attributes_test.go
+++ b/attr/attributes_test.go
@@ -21,11 +21,18 @@ func TestClass(t *testing.T) {
 
 func TestClasses(t *testing.T) {
 	t.Run("given a map, returns sorted keys from the map with value true", func(t *testing.T) {
-		assert.Equal(t, ` class="boheme-hat hat partyhat"`, attr.Classes(map[string]bool{
+		assert.Equal(t, ` class="boheme-hat hat partyhat"`, attr.Classes{
 			"boheme-hat": true,
 			"hat":        true,
 			"partyhat":   true,
 			"turtlehat":  false,
-		}))
+		})
+	})
+
+	t.Run("also works with fmt", func(t *testing.T) {
+		a := attr.Classes{"hat": true}
+		if a.String() != ` class="hat"` {
+			t.FailNow()
+		}
 	})
 }