-
Notifications
You must be signed in to change notification settings - Fork 8
/
encoder.go
435 lines (399 loc) · 10.1 KB
/
encoder.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
package dump
import (
"bytes"
"encoding/json"
"fmt"
"io"
"reflect"
"runtime"
"sort"
"strings"
)
// Encoder ensures all options to dump an object
type Encoder struct {
Formatters []KeyFormatterFunc
ExtraFields struct {
Len bool
Type bool
DetailedStruct bool
DetailedMap bool
DetailedArray bool
DeepJSON bool
UseJSONTag bool
}
ArrayJSONNotation bool
Separator string
DisableTypePrefix bool
Prefix string
writer io.Writer
}
// NewDefaultEncoder instanciate a go-dump encoder
func NewDefaultEncoder() *Encoder {
return NewEncoder(new(bytes.Buffer))
}
// NewEncoder instanciate a go-dump encoder over the writer
func NewEncoder(w io.Writer) *Encoder {
enc := &Encoder{
Formatters: []KeyFormatterFunc{
WithDefaultFormatter(),
},
Separator: ".",
writer: w,
}
return enc
}
// Fdump formats and displays the passed arguments to io.Writer w. It formats exactly the same as Dump.
func (e *Encoder) Fdump(i interface{}) (err error) {
res, err := e.ToStringMap(i)
if err != nil {
return
}
keys := []string{}
for k := range res {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
var err error
if res[k] == "" {
_, err = fmt.Fprintf(e.writer, "%s:\n", k)
} else {
_, err = fmt.Fprintf(e.writer, "%s: %s\n", k, res[k])
}
if err != nil {
return err
}
}
return nil
}
// Sdump returns a string with the passed arguments formatted exactly the same as Dump.
func (e *Encoder) Sdump(i interface{}) (string, error) {
m, err := e.ToStringMap(i)
if err != nil {
return "", err
}
res := ""
var keys []string
for k := range m {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
res += fmt.Sprintf("%s: %s\n", k, m[k])
}
return res, nil
}
func (e *Encoder) fdumpInterface(w map[string]interface{}, i interface{}, roots []string) error {
f := valueFromInterface(i)
k := reflect.ValueOf(i).Kind()
if k == reflect.Ptr && reflect.ValueOf(i).IsNil() || !validAndNotEmpty(f) {
if len(roots) == 0 {
return nil
}
k := strings.Join(sliceFormat(roots, e.Formatters), e.Separator)
var prefix string
if e.Prefix != "" {
prefix = e.Prefix + e.Separator
}
w[prefix+k] = ""
return nil
}
switch f.Kind() {
case reflect.Struct:
if e.ExtraFields.Type {
nodeType := append(roots, "__Type__")
nodeTypeFormatted := strings.Join(sliceFormat(nodeType, e.Formatters), e.Separator)
w[nodeTypeFormatted] = f.Type().Name()
}
croots := roots
if len(roots) == 0 && !e.DisableTypePrefix {
croots = append(roots, f.Type().Name())
}
if err := e.fdumpStruct(w, f, croots); err != nil {
return err
}
case reflect.Array, reflect.Slice:
if err := e.fDumpArray(w, i, roots); err != nil {
return err
}
return nil
case reflect.Map:
if e.ExtraFields.Type {
nodeType := append(roots, "__Type__")
nodeTypeFormatted := strings.Join(sliceFormat(nodeType, e.Formatters), e.Separator)
w[nodeTypeFormatted] = "Map"
}
if err := e.fDumpMap(w, i, roots); err != nil {
return err
}
return nil
default:
k := strings.Join(sliceFormat(roots, e.Formatters), e.Separator)
if e.ExtraFields.DeepJSON && (f.Kind() == reflect.String) {
if err := e.fDumpJSON(w, f.Interface().(string), roots, k); err != nil {
return err
}
} else {
var prefix string
if e.Prefix != "" {
prefix = e.Prefix + e.Separator
}
w[prefix+k] = f.Interface()
}
}
return nil
}
func (e *Encoder) fDumpJSON(w map[string]interface{}, i string, roots []string, k string) error {
var value interface{}
bodyJSONArray := []interface{}{}
// Try to parse as a json array
if err := json.Unmarshal([]byte(i), &bodyJSONArray); err != nil {
//Try to parse as a map
bodyJSONMap := map[string]interface{}{}
if err2 := json.Unmarshal([]byte(i), &bodyJSONMap); err2 == nil {
value = bodyJSONMap
} else {
value = i
}
} else {
value = bodyJSONArray
}
if value == i {
var prefix string
if e.Prefix != "" {
prefix = e.Prefix + e.Separator
}
w[prefix+k] = i
return nil
}
if err := e.fdumpInterface(w, value, roots); err != nil {
return err
}
return nil
}
func (e *Encoder) fDumpArray(w map[string]interface{}, i interface{}, roots []string) error {
f := valueFromInterface(i)
if _, ok := f.Interface().([]byte); ok {
if err := e.fdumpInterface(w, string(f.Interface().([]byte)), roots); err != nil {
return err
}
return nil
}
if e.ExtraFields.Type {
nodeType := append(roots, "__Type__")
nodeTypeFormatted := strings.Join(sliceFormat(nodeType, e.Formatters), e.Separator)
w[nodeTypeFormatted] = "Array"
}
v := reflect.ValueOf(i)
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
if e.ExtraFields.Len {
nodeLen := append(roots, "__Len__")
nodeLenFormatted := strings.Join(sliceFormat(nodeLen, e.Formatters), e.Separator)
w[nodeLenFormatted] = v.Len()
}
if e.ExtraFields.DetailedArray && len(roots) > 0 {
structKey := strings.Join(sliceFormat(roots, e.Formatters), e.Separator)
w[structKey] = i
}
for i := 0; i < v.Len(); i++ {
var l string
var croots []string
if len(roots) > 0 {
l = roots[len(roots)-1:][0]
if !e.ArrayJSONNotation {
croots = append(roots, fmt.Sprintf("%s%d", l, i))
} else {
var t = make([]string, len(roots)-1)
copy(t, roots[0:len(roots)-1])
croots = append(t, fmt.Sprintf("%s[%d]", l, i))
}
} else {
var skey = fmt.Sprintf("[%d]", i)
if !e.ArrayJSONNotation {
skey = fmt.Sprintf("%s%d", e.Prefix+l, i)
}
croots = append(roots, skey)
}
f := v.Index(i)
stringer, ok := f.Interface().(fmt.Stringer)
if ok {
k := strings.Join(sliceFormat(croots, e.Formatters), e.Separator)
var prefix string
if e.Prefix != "" {
prefix = e.Prefix
}
w[prefix+k] = stringer.String()
}
if err := e.fdumpInterface(w, f.Interface(), croots); err != nil {
return err
}
}
return nil
}
func (e *Encoder) fDumpMap(w map[string]interface{}, i interface{}, roots []string) error {
v := reflect.ValueOf(i)
keys := v.MapKeys()
var lenKeys int64
for _, k := range keys {
key := fmt.Sprintf("%v", k.Interface())
if key == "" {
continue
}
lenKeys++
croots := append(roots, key)
value := v.MapIndex(k)
f := valueFromInterface(value.Interface())
if validAndNotEmpty(f) && f.Type().Kind() == reflect.Struct {
stringer, ok := value.Interface().(fmt.Stringer)
if ok {
structKey := strings.Join(sliceFormat(croots, e.Formatters), e.Separator)
w[structKey] = stringer.String()
}
if !e.DisableTypePrefix {
croots = append(croots, f.Type().Name())
}
}
if err := e.fdumpInterface(w, value.Interface(), croots); err != nil {
return err
}
}
if e.ExtraFields.Len {
nodeLen := append(roots, "__Len__")
nodeLenFormatted := strings.Join(sliceFormat(nodeLen, e.Formatters), e.Separator)
w[nodeLenFormatted] = lenKeys
}
if e.ExtraFields.DetailedMap {
if len(roots) != 0 {
structKey := strings.Join(sliceFormat(roots, e.Formatters), e.Separator)
w[structKey] = i
}
}
return nil
}
func (e *Encoder) fdumpStruct(w map[string]interface{}, s reflect.Value, roots []string) error {
if e.ExtraFields.DetailedStruct {
if e.ExtraFields.Len {
nodeLen := append(roots, "__Len__")
nodeLenFormatted := strings.Join(sliceFormat(nodeLen, e.Formatters), e.Separator)
w[nodeLenFormatted] = s.NumField()
}
structKey := strings.Join(sliceFormat(roots, e.Formatters), e.Separator)
if s.CanInterface() && len(roots) > 1 {
w[structKey] = s.Interface()
}
}
var atLeastOneField bool
for i := 0; i < s.NumField(); i++ {
k := reflect.ValueOf(i).Kind()
if k == reflect.Ptr && reflect.ValueOf(i).IsNil() {
if len(roots) == 0 {
continue
}
k := strings.Join(sliceFormat(roots, e.Formatters), e.Separator)
w[k] = ""
atLeastOneField = true
continue
}
if !s.Field(i).CanInterface() {
continue
}
var croots []string
var keyNameComputed bool
if e.ExtraFields.UseJSONTag {
tagValues := strings.Split(s.Type().Field(i).Tag.Get("json"), ",")
if len(tagValues) > 0 && tagValues[0] != "omitempty" && tagValues[0] != "" {
croots = append(roots, tagValues[0])
keyNameComputed = true
}
}
if !keyNameComputed {
croots = append(roots, s.Type().Field(i).Name)
}
atLeastOneField = true
if err := e.fdumpInterface(w, s.Field(i).Interface(), croots); err != nil {
return err
}
}
if !atLeastOneField {
stringer, ok := s.Interface().(fmt.Stringer)
if ok {
structKey := strings.Join(sliceFormat(roots, e.Formatters), e.Separator)
w[structKey] = stringer.String()
}
}
return nil
}
// ToStringMap formats the argument as a map[string]string. It formats exactly the same as Dump.
func (e *Encoder) ToStringMap(i interface{}) (res map[string]string, err error) {
defer func() {
if r := recover(); r != nil {
if _, ok := r.(runtime.Error); ok {
panic(r)
}
err = r.(error)
buf := make([]byte, 1<<16)
runtime.Stack(buf, true)
}
}()
ires := map[string]interface{}{}
if err = e.fdumpInterface(ires, i, nil); err != nil {
return
}
res = map[string]string{}
for k, v := range ires {
res[k] = printValue(v)
}
return
}
// ToMap dumps argument as a map[string]interface{}
func (e *Encoder) ToMap(i interface{}) (res map[string]interface{}, err error) {
defer func() {
if r := recover(); r != nil {
if _, ok := r.(runtime.Error); ok {
panic(r)
}
err = r.(error)
buf := make([]byte, 1<<16)
runtime.Stack(buf, true)
}
}()
res = map[string]interface{}{}
if err = e.fdumpInterface(res, i, nil); err != nil {
return
}
return
}
func (e *Encoder) ViperKey(s string) string {
if e.Prefix != "" {
s = strings.Replace(s, e.Prefix+e.Separator, "", 1)
}
s = strings.Replace(s, e.Separator, ".", -1)
s = strings.ToLower(s)
return s
}
func printValue(i interface{}) string {
s, is := i.(string)
if is {
return s
}
ps, is := i.(*string)
if is && ps != nil {
return *ps
}
stringer, is := i.(fmt.Stringer)
if is {
return stringer.String()
}
btes, err := json.Marshal(i)
if err == nil {
compactedBuffer := new(bytes.Buffer)
err := json.Compact(compactedBuffer, btes)
if err != nil {
return string(btes)
}
return compactedBuffer.String()
}
return fmt.Sprintf("%v", i)
}