summaryrefslogtreecommitdiff
path: root/vendor/github.com/jhump/protoreflect/dynamic/indent.go
blob: bd7fcaa527161383421322376fac742c115ae0b4 (plain)
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
72
73
74
75
76
package dynamic

import "bytes"

type indentBuffer struct {
	bytes.Buffer
	indent      string
	indentCount int
	comma       bool
}

func (b *indentBuffer) start() error {
	if b.indentCount >= 0 {
		b.indentCount++
		return b.newLine(false)
	}
	return nil
}

func (b *indentBuffer) sep() error {
	if b.indentCount >= 0 {
		_, err := b.WriteString(": ")
		return err
	} else {
		return b.WriteByte(':')
	}
}

func (b *indentBuffer) end() error {
	if b.indentCount >= 0 {
		b.indentCount--
		return b.newLine(false)
	}
	return nil
}

func (b *indentBuffer) maybeNext(first *bool) error {
	if *first {
		*first = false
		return nil
	} else {
		return b.next()
	}
}

func (b *indentBuffer) next() error {
	if b.indentCount >= 0 {
		return b.newLine(b.comma)
	} else if b.comma {
		return b.WriteByte(',')
	} else {
		return b.WriteByte(' ')
	}
}

func (b *indentBuffer) newLine(comma bool) error {
	if comma {
		err := b.WriteByte(',')
		if err != nil {
			return err
		}
	}

	err := b.WriteByte('\n')
	if err != nil {
		return err
	}

	for i := 0; i < b.indentCount; i++ {
		_, err := b.WriteString(b.indent)
		if err != nil {
			return err
		}
	}
	return nil
}