Skip to content

Commit 3b05ca7

Browse files
authored
Merge pull request #272 from thockin/master
Add SnippetWriter.Dup(), fix bug in Args.With()
2 parents a0386bf + 7a656db commit 3b05ca7

File tree

2 files changed

+78
-11
lines changed

2 files changed

+78
-11
lines changed

v2/generator/snippet_writer.go

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -123,25 +123,28 @@ func (s *SnippetWriter) Do(format string, args interface{}) *SnippetWriter {
123123
// SnippetWriter.Do.
124124
type Args map[interface{}]interface{}
125125

126-
// With makes a copy of a and adds the given key, value pair.
126+
// With makes a copy of a and adds the given key, value pair. If key overlaps,
127+
// the new value wins.
127128
func (a Args) With(key, value interface{}) Args {
128-
a2 := Args{key: value}
129+
result := Args{}
129130
for k, v := range a {
130-
a2[k] = v
131+
result[k] = v
131132
}
132-
return a2
133+
result[key] = value
134+
return result
133135
}
134136

135-
// WithArgs makes a copy of a and adds the given arguments.
137+
// WithArgs makes a copy of a and adds the given arguments. If any keys
138+
// overlap, the values from rhs win.
136139
func (a Args) WithArgs(rhs Args) Args {
137-
a2 := Args{}
138-
for k, v := range rhs {
139-
a2[k] = v
140-
}
140+
result := Args{}
141141
for k, v := range a {
142-
a2[k] = v
142+
result[k] = v
143+
}
144+
for k, v := range rhs {
145+
result[k] = v
143146
}
144-
return a2
147+
return result
145148
}
146149

147150
func (s *SnippetWriter) Out() io.Writer {
@@ -152,3 +155,16 @@ func (s *SnippetWriter) Out() io.Writer {
152155
func (s *SnippetWriter) Error() error {
153156
return s.err
154157
}
158+
159+
// Dup creates an exact duplicate SnippetWriter with a different io.Writer.
160+
func (s *SnippetWriter) Dup(w io.Writer) *SnippetWriter {
161+
ret := *s
162+
ret.w = w
163+
return &ret
164+
}
165+
166+
// Append adds the contents of the io.Reader to this SnippetWriter's buffer.
167+
func (s *SnippetWriter) Append(r io.Reader) error {
168+
_, err := io.Copy(s.w, r)
169+
return err
170+
}

v2/generator/snippet_writer_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,54 @@ func TestSnippetWriter(t *testing.T) {
6969
}
7070
}
7171
}
72+
73+
func TestArgsWith(t *testing.T) {
74+
orig := generator.Args{
75+
"a": "aaa",
76+
"b": "bbb",
77+
}
78+
79+
withC := orig.With("c", "ccc")
80+
if len(orig) != 2 {
81+
t.Errorf("unexpected change to 'orig': %v", orig)
82+
}
83+
if len(withC) != 3 {
84+
t.Errorf("expected 'withC' to have 3 values: %v", withC)
85+
}
86+
87+
withDE := withC.WithArgs(generator.Args{
88+
"d": "ddd",
89+
"e": "eee",
90+
})
91+
if len(orig) != 2 {
92+
t.Errorf("unexpected change to 'orig': %v", orig)
93+
}
94+
if len(withC) != 3 {
95+
t.Errorf("unexpected change to 'withC': %v", orig)
96+
}
97+
if len(withDE) != 5 {
98+
t.Errorf("expected 'withDE' to have 5 values: %v", withC)
99+
}
100+
101+
withNewA := orig.With("a", "AAA")
102+
if orig["a"] != "aaa" {
103+
t.Errorf("unexpected change to 'orig': %v", orig)
104+
}
105+
if withNewA["a"] != "AAA" {
106+
t.Errorf("expected 'withNewA[\"a\"]' to be \"AAA\": %v", withNewA)
107+
}
108+
109+
withNewAB := orig.WithArgs(generator.Args{
110+
"a": "AAA",
111+
"b": "BBB",
112+
})
113+
if orig["a"] != "aaa" || orig["b"] != "bbb" {
114+
t.Errorf("unexpected change to 'orig': %v", orig)
115+
}
116+
if withNewAB["a"] != "AAA" {
117+
t.Errorf("expected 'withNewAB[\"a\"]' to be \"AAA\": %v", withNewAB)
118+
}
119+
if withNewAB["b"] != "BBB" {
120+
t.Errorf("expected 'withNewAB[\"b\"]' to be \"BBB\": %v", withNewAB)
121+
}
122+
}

0 commit comments

Comments
 (0)