internal/prnt: doc exported symbols (#9)
This commit is contained in:
@@ -5,36 +5,47 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Generator provides convenience methods for code generators. In particular it
|
||||||
|
// provides fmt-like methods which print to an internal buffer. It also allows
|
||||||
|
// any errors to be stored so they can be checked at the end, rather than having
|
||||||
|
// error checks obscuring the code generation.
|
||||||
type Generator struct {
|
type Generator struct {
|
||||||
buf bytes.Buffer
|
buf bytes.Buffer
|
||||||
err error
|
err error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Printf prints to the internal buffer.
|
||||||
func (g *Generator) Printf(format string, args ...interface{}) {
|
func (g *Generator) Printf(format string, args ...interface{}) {
|
||||||
if g.err != nil {
|
if g.err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if _, err := fmt.Fprintf(&g.buf, format, args...); err != nil {
|
_, err := fmt.Fprintf(&g.buf, format, args...)
|
||||||
g.AddError(err)
|
g.AddError(err)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NL prints a new line.
|
||||||
func (g *Generator) NL() {
|
func (g *Generator) NL() {
|
||||||
g.Printf("\n")
|
g.Printf("\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Comment writes comment lines prefixed with "// ".
|
||||||
func (g *Generator) Comment(lines ...string) {
|
func (g *Generator) Comment(lines ...string) {
|
||||||
for _, line := range lines {
|
for _, line := range lines {
|
||||||
g.Printf("// %s\n", line)
|
g.Printf("// %s\n", line)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AddError records an error in code generation. The first non-nil error will
|
||||||
|
// prevent printing operations from writing anything else, and the error will be
|
||||||
|
// returned from Result().
|
||||||
func (g *Generator) AddError(err error) {
|
func (g *Generator) AddError(err error) {
|
||||||
if err != nil && g.err == nil {
|
if err != nil && g.err == nil {
|
||||||
g.err = err
|
g.err = err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Result returns the printed bytes. If any error was recorded with AddError
|
||||||
|
// during code generation, the first such error will be returned here.
|
||||||
func (g *Generator) Result() ([]byte, error) {
|
func (g *Generator) Result() ([]byte, error) {
|
||||||
return g.buf.Bytes(), g.err
|
return g.buf.Bytes(), g.err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user