-
Notifications
You must be signed in to change notification settings - Fork 0
/
yaml.go
176 lines (155 loc) · 4.61 KB
/
yaml.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package el
import (
"io"
"github.com/albertjin/el/internal/yaml"
)
type YamlKind int
const (
YamlKindMap YamlKind = 1
YamlKindList YamlKind = 2
)
type YamlLog struct {
lines []yaml.Line
kind YamlKind
errCount int
}
func YamlLogMap() *YamlLog {
return &YamlLog{kind: YamlKindMap}
}
func YamlLogList() *YamlLog {
return &YamlLog{kind: YamlKindList}
}
func (l *YamlLog) KV(key interface{}, value string) {
l.lines = append(l.lines, yaml.NewText(key, value))
}
func (l *YamlLog) V(value string) {
l.lines = append(l.lines, yaml.NewText(nil, value))
}
func (l *YamlLog) E(key interface{}, err error) {
l.lines = append(l.lines, yaml.NewError(key, err))
l.errCount++
}
func (l *YamlLog) PushMap(key interface{}) *YamlLog {
n := &YamlLog{kind: YamlKindMap}
l.lines = append(l.lines, yaml.NewLine(key, n))
return n
}
func (l *YamlLog) PushList(key interface{}) *YamlLog {
n := &YamlLog{kind: YamlKindList}
l.lines = append(l.lines, yaml.NewLine(key, n))
return n
}
func (l *YamlLog) HasErr() bool {
if l.errCount > 0 {
return true
}
for _, line := range l.lines {
if line.Line.HasErr() {
return true
}
}
return false
}
func (l *YamlLog) Output(context interface{}, prefix string, w io.Writer) {
p0 := prefix
p4 := p0 + " "
p2 := p4[:len(p0)+2]
br := []byte("\n")
switch l.kind {
case YamlKindMap:
for i, line := range l.lines {
if i > 0 {
_, _ = w.Write([]byte(p0))
}
if key := line.MakeKey(context); len(key) > 0 {
_, _ = w.Write([]byte(key))
} else {
_, _ = w.Write([]byte("'':"))
}
switch x := line.Line.(type) {
case *YamlLog:
var a, b string
switch x.kind {
case YamlKindMap:
a, b = p2, " {}\n"
case YamlKindList:
a, b = p0, " []\n"
}
if len(x.lines) > 0 {
_, _ = w.Write(br)
_, _ = w.Write([]byte(a))
x.Output(context, a, w)
} else {
_, _ = w.Write([]byte(b))
}
case yaml.Error:
_, _ = w.Write(br)
_, _ = w.Write([]byte(p0))
x.Output(context, p0, w)
_, _ = w.Write(br)
default:
x.Output(context, p0, w)
_, _ = w.Write(br)
}
}
case YamlKindList:
for i, line := range l.lines {
key := line.MakeKey(context)
switch x := line.Line.(type) {
case *YamlLog:
var a, b, c string
switch x.kind {
case YamlKindMap:
a, b, c = p4, " {}\n", "{}\n"
case YamlKindList:
a, b, c = p2, " []\n", "[]\n"
}
if i > 0 {
_, _ = w.Write([]byte(p0))
}
_, _ = w.Write([]byte("- "))
if len(key) > 0 {
_, _ = w.Write([]byte(key))
if len(x.lines) > 0 {
_, _ = w.Write(br)
_, _ = w.Write([]byte(a))
x.Output(context, a, w)
} else {
_, _ = w.Write([]byte(b))
}
} else {
if len(x.lines) > 0 {
x.Output(context, p2, w)
} else {
_, _ = w.Write([]byte(c))
}
}
case yaml.Error:
if i > 0 {
_, _ = w.Write([]byte(p0))
}
_, _ = w.Write([]byte("- "))
if len(key) > 0 {
_, _ = w.Write([]byte(key))
_, _ = w.Write(br)
_, _ = w.Write([]byte(p2))
}
x.Output(context, p2, w)
_, _ = w.Write(br)
default:
if i > 0 {
_, _ = w.Write([]byte(p0))
}
if len(key) > 0 {
_, _ = w.Write([]byte("- "))
_, _ = w.Write([]byte(key))
x.Output(context, p2, w)
} else {
_, _ = w.Write([]byte("-"))
x.Output(context, p0, w)
}
_, _ = w.Write(br)
}
}
}
}