Skip to content

Commit 1e40e18

Browse files
committed
Add SnippetWriter .Dup() and .Append()
This allows generators to "fork" a SnippetWriter with a buffer, emit into it, and then conditionally merge it back into the parent (e.g. if it was not empty, the parent might emit some pre- and/or post-logic).
1 parent a0386bf commit 1e40e18

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

v2/generator/snippet_writer.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,16 @@ func (s *SnippetWriter) Out() io.Writer {
152152
func (s *SnippetWriter) Error() error {
153153
return s.err
154154
}
155+
156+
// Dup creates an exact duplicate SnippetWriter with a different io.Writer.
157+
func (s *SnippetWriter) Dup(w io.Writer) *SnippetWriter {
158+
ret := *s
159+
ret.w = w
160+
return &ret
161+
}
162+
163+
// Append adds the contents of the io.Reader to this SnippetWriter's buffer.
164+
func (s *SnippetWriter) Append(r io.Reader) error {
165+
_, err := io.Copy(s.w, r)
166+
return err
167+
}

0 commit comments

Comments
 (0)