blob: bd7fcaa527161383421322376fac742c115ae0b4 [file] [log] [blame]
khenaidooa46458b2021-12-15 16:50:44 -05001package dynamic
2
3import "bytes"
4
5type indentBuffer struct {
6 bytes.Buffer
7 indent string
8 indentCount int
9 comma bool
10}
11
12func (b *indentBuffer) start() error {
13 if b.indentCount >= 0 {
14 b.indentCount++
15 return b.newLine(false)
16 }
17 return nil
18}
19
20func (b *indentBuffer) sep() error {
21 if b.indentCount >= 0 {
22 _, err := b.WriteString(": ")
23 return err
24 } else {
25 return b.WriteByte(':')
26 }
27}
28
29func (b *indentBuffer) end() error {
30 if b.indentCount >= 0 {
31 b.indentCount--
32 return b.newLine(false)
33 }
34 return nil
35}
36
37func (b *indentBuffer) maybeNext(first *bool) error {
38 if *first {
39 *first = false
40 return nil
41 } else {
42 return b.next()
43 }
44}
45
46func (b *indentBuffer) next() error {
47 if b.indentCount >= 0 {
48 return b.newLine(b.comma)
49 } else if b.comma {
50 return b.WriteByte(',')
51 } else {
52 return b.WriteByte(' ')
53 }
54}
55
56func (b *indentBuffer) newLine(comma bool) error {
57 if comma {
58 err := b.WriteByte(',')
59 if err != nil {
60 return err
61 }
62 }
63
64 err := b.WriteByte('\n')
65 if err != nil {
66 return err
67 }
68
69 for i := 0; i < b.indentCount; i++ {
70 _, err := b.WriteString(b.indent)
71 if err != nil {
72 return err
73 }
74 }
75 return nil
76}