-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy patharray.go
204 lines (171 loc) · 5.31 KB
/
array.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package prettyconsole
import (
"bytes"
"time"
"go.uber.org/zap/zapcore"
)
// Test interface conformance
var _ zapcore.ArrayEncoder = (*prettyConsoleEncoder)(nil)
func (e *prettyConsoleEncoder) AppendComplex64(v complex64) { e.appendComplex(complex128(v), 32) }
func (e *prettyConsoleEncoder) AppendComplex128(v complex128) { e.appendComplex(v, 64) }
func (e *prettyConsoleEncoder) AppendFloat32(v float32) { e.appendFloat(float64(v), 32) }
func (e *prettyConsoleEncoder) AppendFloat64(v float64) { e.appendFloat(v, 64) }
func (e *prettyConsoleEncoder) AppendInt(v int) { e.AppendInt64(int64(v)) }
func (e *prettyConsoleEncoder) AppendInt32(v int32) { e.AppendInt64(int64(v)) }
func (e *prettyConsoleEncoder) AppendInt16(v int16) { e.AppendInt64(int64(v)) }
func (e *prettyConsoleEncoder) AppendInt8(v int8) { e.AppendInt64(int64(v)) }
func (e *prettyConsoleEncoder) AppendUint(v uint) { e.AppendUint64(uint64(v)) }
func (e *prettyConsoleEncoder) AppendUint32(v uint32) { e.AppendUint64(uint64(v)) }
func (e *prettyConsoleEncoder) AppendUint16(v uint16) { e.AppendUint64(uint64(v)) }
func (e *prettyConsoleEncoder) AppendUint8(v uint8) { e.AppendUint64(uint64(v)) }
func (e *prettyConsoleEncoder) AppendUintptr(v uintptr) { e.AppendUint64(uint64(v)) }
func (e *prettyConsoleEncoder) AppendBool(b bool) {
e.addSeparator()
e.buf.AppendBool(b)
e.inList = true
e.listSep = e._listSepComma
}
func (e *prettyConsoleEncoder) AppendByteString(bytes []byte) {
e.addSeparator()
e.appendSafeByte(bytes)
e.inList = true
e.listSep = e._listSepComma
}
func (e *prettyConsoleEncoder) appendComplex(c complex128, precision int) {
e.addSeparator()
// Cast to a platform-independent, fixed-size type.
r, i := real(c), imag(c)
// Because we're always in a quoted string, we can use strconv without
// special-casing NaN and +/-Inf.
e.buf.AppendFloat(r, precision)
// If imaginary part is less than 0, minus (-) sign is added by default
// by AppendFloat.
if i >= 0 {
e.buf.AppendByte('+')
}
e.buf.AppendFloat(i, precision)
e.buf.AppendByte('i')
e.inList = true
e.listSep = e._listSepComma
}
func (e *prettyConsoleEncoder) appendFloat(f float64, precision int) {
e.addSeparator()
e.buf.AppendFloat(f, precision)
e.inList = true
e.listSep = e._listSepComma
}
func (e *prettyConsoleEncoder) AppendInt64(i int64) {
e.addSeparator()
e.buf.AppendInt(i)
e.inList = true
e.listSep = e._listSepComma
}
func (e *prettyConsoleEncoder) AppendString(s string) {
e.addSeparator()
e.addSafeString(s)
e.inList = true
e.listSep = e._listSepComma
}
func (e *prettyConsoleEncoder) AppendUint64(u uint64) {
e.addSeparator()
e.buf.AppendUint(u)
e.inList = true
e.listSep = e._listSepComma
}
func (e *prettyConsoleEncoder) AppendDuration(duration time.Duration) {
e.addSeparator()
cur := e.buf.Len()
e.cfg.EncodeDuration(duration, e)
if cur == e.buf.Len() {
// User-supplied EncodeDuration is a no-op. Fall back to nanoseconds to keep
// JSON valid.
e.buf.AppendInt(int64(duration))
}
e.inList = true
e.listSep = e._listSepComma
}
func (e *prettyConsoleEncoder) AppendTime(t time.Time) {
e.addSeparator()
cur := e.buf.Len()
e.cfg.EncodeTime(t, e)
if cur == e.buf.Len() {
// User-supplied EncodeTime is a no-op. Fall back to RFC3339
e.buf.AppendTime(t, time.RFC3339)
}
e.inList = true
e.listSep = e._listSepComma
}
func (e *prettyConsoleEncoder) AppendArray(marshaler zapcore.ArrayMarshaler) error {
e.addSeparator()
enc := e.clone()
enc.OpenNamespace("")
enc.colorizeAtLevel("[")
enc.inList = false
l := enc.buf.Len()
if err := marshaler.MarshalLogArray(enc); err != nil {
return err
}
if bytes.ContainsRune(enc.buf.Bytes()[l:], '\n') {
enc.buf.AppendString(e.cfg.LineEnding)
for ii := 0; ii < enc.namespaceIndent-1; ii++ {
enc.buf.AppendByte(' ')
}
}
enc.colorizeAtLevel("]")
_, _ = e.buf.Write(enc.buf.Bytes())
putPrettyConsoleEncoder(enc)
e.inList = true
e.listSep = e._listSepComma
return nil
}
func (e *prettyConsoleEncoder) AppendObject(marshaler zapcore.ObjectMarshaler) error {
e.addSeparator()
enc := e.clone()
enc.OpenNamespace("")
enc.colorizeAtLevel("{")
enc.inList = false
enc.keyPrefix = ""
l := enc.buf.Len()
if err := marshaler.MarshalLogObject(enc); err != nil {
return err
}
if bytes.ContainsRune(enc.buf.Bytes()[l:], '\n') {
enc.buf.AppendString(e.cfg.LineEnding)
for ii := 0; ii < enc.namespaceIndent-1; ii++ {
enc.buf.AppendByte(' ')
}
}
enc.colorizeAtLevel("}")
_, _ = e.buf.Write(enc.buf.Bytes())
putPrettyConsoleEncoder(enc)
e.inList = true
e.listSep = e._listSepComma
return nil
}
func (e *prettyConsoleEncoder) AppendReflected(value interface{}) error {
e.addSeparator()
enc := e.clone()
enc.OpenNamespace("")
enc.keyPrefix = ""
enc.inList = false
l := enc.buf.Len()
iw := indentingWriter{
buf: enc.buf,
indent: enc.namespaceIndent,
lineEnding: []byte(e.cfg.LineEnding),
}
if err := e.cfg.NewReflectedEncoder(iw).Encode(value); err != nil {
return err
}
if l-enc.buf.Len() == 0 {
// User-supplied reflectedEncoder is a no-op. Fall back to dd
if err := defaultReflectedEncoder(iw).Encode(value); err != nil {
return err
}
}
_, _ = e.buf.Write(enc.buf.Bytes())
putPrettyConsoleEncoder(enc)
e.inList = true
e.listSep = e._listSepComma
return nil
}