-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder.go
71 lines (62 loc) · 1.34 KB
/
builder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package fproto_gowrap
import (
"fmt"
"strings"
)
// Helper to build idented text files
type Builder struct {
builder strings.Builder
indent string
}
func NewBuilder() *Builder {
return &Builder{}
}
// In Indents the output one tab stop.
func (g *Builder) In() { g.indent += "\t" }
// Out unindents the output one tab stop.
func (g *Builder) Out() {
if len(g.indent) > 0 {
g.indent = g.indent[1:]
}
}
// Writes a single byte
func (g *Builder) WriteByte(c byte) {
g.builder.WriteByte(c)
}
// Writes a full string
func (g *Builder) WriteString(s string) {
g.builder.WriteString(s)
}
// Writes a list of values
func (g *Builder) P(str ...interface{}) {
g.WriteString(g.indent)
for _, v := range str {
switch s := v.(type) {
case string:
g.WriteString(s)
case *string:
g.WriteString(*s)
case bool:
fmt.Fprintf(&g.builder, "%t", s)
case *bool:
fmt.Fprintf(&g.builder, "%t", *s)
case int:
fmt.Fprintf(&g.builder, "%d", s)
case *int32:
fmt.Fprintf(&g.builder, "%d", *s)
case *int64:
fmt.Fprintf(&g.builder, "%d", *s)
case float64:
fmt.Fprintf(&g.builder, "%g", s)
case *float64:
fmt.Fprintf(&g.builder, "%g", *s)
default:
panic(fmt.Sprintf("unknown type in printer: %T", v))
}
}
g.WriteByte('\n')
}
// Returns the content as a string
func (g *Builder) String() string {
return g.builder.String()
}