-
Notifications
You must be signed in to change notification settings - Fork 0
/
dump.go
387 lines (356 loc) · 8.63 KB
/
dump.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
package gojson
import (
"bytes"
"fmt"
"gojson/internal/type/reflection"
"gojson/internal/type/structx"
"reflect"
"strings"
)
type DumpOption struct {
WithType bool // 是否输出类型名
ExportedOnly bool // 是否输出不可导出字段
}
// 递归时保存的数据缓存
type dumpBuf struct {
Value interface{}
ReflectValue reflect.Value
ReflectTypeName string // 用于保存类型名
Option DumpOption
Indent string // 缩进
NewIndent string // 新的缩进,每一层增加一个缩进
Buffer *bytes.Buffer
}
const (
jsonIndent = ` ` // json格式的缩进
yamlIndent = ` ` // yaml格式的缩进
)
var (
// DefaultTrimChars
// 对照ASCII码表定义一些空白字符
// 用于分割字符串
DefaultTrimChars = string([]byte{
'\t', // 水平制表符
'\v', // 垂直制表符
'\n', // 换行符
'\r', // 回车符
'\f', // 换页符
' ', // 空格
0xA0, //  
0x00, // 空字符
0x85, // 删除符
0x1C, // 文件分隔符
})
)
func dump(values ...interface{}) {
for _, value := range values {
dumpWithOption(value, DumpOption{
WithType: false,
ExportedOnly: false,
})
}
}
func dumpWithOption(value interface{}, options DumpOption) {
buffer := bytes.NewBuffer(nil)
doDump(value, "", buffer, DumpOption{
WithType: options.WithType,
ExportedOnly: options.ExportedOnly,
})
fmt.Println(buffer.String())
}
func doDump(value interface{}, indent string, buffer *bytes.Buffer, option DumpOption) {
if value == nil {
buffer.WriteString(`<nil>`)
return
}
var reflectValue reflect.Value
if v, ok := value.(reflect.Value); ok {
reflectValue = v
if v.IsValid() && v.CanInterface() {
value = v.Interface()
} else {
if convertedValue, ok := reflection.ValueToInterface(v); ok {
value = convertedValue
}
}
} else {
reflectValue = reflect.ValueOf(value)
}
if value == nil {
buffer.WriteString(`<nil>`)
return
}
var (
reflectKind = reflectValue.Kind()
reflectTypeName = reflectValue.Type().String()
newIndent = indent + jsonIndent
)
// 统一[]byte类型名
reflectTypeName = strings.ReplaceAll(reflectTypeName, `[]uint8`, `[]byte`)
if !option.WithType {
reflectTypeName = ""
}
for reflectKind == reflect.Ptr {
reflectValue = reflectValue.Elem()
reflectKind = reflectValue.Kind()
}
var (
newDumpBuf = dumpBuf{
Value: value,
ReflectValue: reflectValue,
ReflectTypeName: reflectTypeName,
Option: option,
Indent: indent,
NewIndent: newIndent,
Buffer: buffer,
}
)
switch reflectKind {
case reflect.Slice, reflect.Array:
doDumpSlice(newDumpBuf)
case reflect.Map:
doDumpMap(newDumpBuf)
case reflect.Struct:
doDumpStruct(newDumpBuf)
case reflect.Interface:
doDump(newDumpBuf.ReflectValue.Elem(), indent, buffer, option)
case reflect.String:
doDumpString(newDumpBuf)
case reflect.Bool:
doDumpBool(newDumpBuf)
case
reflect.Int,
reflect.Int8,
reflect.Int16,
reflect.Int32,
reflect.Int64,
reflect.Uint,
reflect.Uint8,
reflect.Uint16,
reflect.Uint32,
reflect.Uint64,
reflect.Float32,
reflect.Float64,
reflect.Complex64,
reflect.Complex128:
doDumpNumber(newDumpBuf)
default:
doDumpDefault(newDumpBuf)
}
}
func doDumpSlice(buf dumpBuf) {
if b, ok := buf.Value.([]byte); ok {
if !buf.Option.WithType {
buf.Buffer.WriteString(fmt.Sprintf(`"%s"`, replaceSlashesForString(string(b))))
} else {
buf.Buffer.WriteString(fmt.Sprintf(
`%s(%d) "%s"`,
buf.ReflectTypeName,
len(string(b)),
string(b),
))
}
return
}
length := buf.ReflectValue.Len()
if length == 0 {
// 切片长度为0的情况
if !buf.Option.WithType {
buf.Buffer.WriteString("[]")
} else {
buf.Buffer.WriteString(fmt.Sprintf("%s(0) []", buf.ReflectTypeName))
}
return
}
// 打印开头
if !buf.Option.WithType {
buf.Buffer.WriteString("[\n")
} else {
buf.Buffer.WriteString(fmt.Sprintf("%s(%d) [\n", buf.ReflectTypeName, length))
}
for i := 0; i < length; i++ {
// 遍历切片
buf.Buffer.WriteString(buf.NewIndent)
doDump(buf.ReflectValue.Index(i), buf.NewIndent, buf.Buffer, buf.Option)
buf.Buffer.WriteString(",\n")
}
buf.Buffer.WriteString(fmt.Sprintf("%s]", buf.Indent))
}
func doDumpMap(buf dumpBuf) {
var mapKeys = make([]reflect.Value, 0)
for _, key := range buf.ReflectValue.MapKeys() {
// 获取map的所有key
if !key.CanInterface() {
// 该字段为不可导出字段直接跳到下一个key
continue
}
mapKey := key
mapKeys = append(mapKeys, mapKey)
}
if len(mapKeys) == 0 {
// map为空的情况
if !buf.Option.WithType {
buf.Buffer.WriteString("{}")
} else {
buf.Buffer.WriteString(fmt.Sprintf("%s(0) {}", buf.ReflectTypeName))
}
return
}
var (
maxSpaceNum = 0 // 根据key名词的最大长度确定缩进
tmpSpaceNum = 0
mapKeyStr = ""
)
for _, key := range mapKeys {
tmpSpaceNum = len(fmt.Sprintf(`%v`, key.Interface()))
if tmpSpaceNum > maxSpaceNum {
maxSpaceNum = tmpSpaceNum
}
}
if !buf.Option.WithType {
buf.Buffer.WriteString("{\n")
} else {
buf.Buffer.WriteString(fmt.Sprintf("%s(%d) {\n", buf.ReflectTypeName, len(mapKeys)))
}
for _, mapKey := range mapKeys {
// 根据key遍历map
// 输出map的key和缩进
curSpaceNum := len(fmt.Sprintf(`%v`, mapKey.Interface()))
if mapKey.Kind() == reflect.String {
mapKeyStr = fmt.Sprintf(`"%v"`, mapKey.Interface())
} else {
mapKeyStr = fmt.Sprintf(`%v`, mapKey.Interface())
}
if !buf.Option.WithType {
buf.Buffer.WriteString(fmt.Sprintf(
"%s%v:%s",
buf.NewIndent,
mapKeyStr,
strings.Repeat(" ", maxSpaceNum-curSpaceNum+1),
))
} else {
buf.Buffer.WriteString(fmt.Sprintf(
"%s%s(%v):%s",
buf.NewIndent,
mapKey.Type().String(),
mapKeyStr,
strings.Repeat(" ", maxSpaceNum-curSpaceNum+1),
))
}
// 输出map的value
doDump(buf.ReflectValue.MapIndex(mapKey), buf.NewIndent, buf.Buffer, buf.Option)
buf.Buffer.WriteString(",\n")
}
buf.Buffer.WriteString(fmt.Sprintf("%s}", buf.Indent))
}
func doDumpStruct(buf dumpBuf) {
// 获取结构体所有字段
structFields, _ := structx.Fields(structx.FieldsInput{
Data: buf.Value,
RecursiveOption: structx.RecursiveOptionEmbedded,
})
var (
maxSpaceNum = 0
tmpSpaceNum = 0
)
for _, field := range structFields {
if buf.Option.ExportedOnly && !field.IsExported() {
continue
}
tmpSpaceNum = len(field.Name())
if tmpSpaceNum > maxSpaceNum {
maxSpaceNum = tmpSpaceNum
}
}
// 打印开头
if !buf.Option.WithType {
buf.Buffer.WriteString("{\n")
} else {
buf.Buffer.WriteString(fmt.Sprintf("%s(%d) {\n",
buf.ReflectTypeName,
len(structFields)),
)
}
for _, field := range structFields {
// 遍历字段
if buf.Option.ExportedOnly && !field.IsExported() {
continue
}
curSpaceNum := len(fmt.Sprintf(`%v`, field.Name()))
buf.Buffer.WriteString(fmt.Sprintf(
"%s%s:%s",
buf.NewIndent,
field.Name(),
strings.Repeat(" ", maxSpaceNum-curSpaceNum+1),
))
// 递归遍历下一个层级
doDump(field.Value, buf.NewIndent, buf.Buffer, buf.Option)
buf.Buffer.WriteString(",\n")
}
buf.Buffer.WriteString(fmt.Sprintf("%s}", buf.Indent))
}
func doDumpNumber(buf dumpBuf) {
doDumpDefault(buf)
}
func doDumpString(buf dumpBuf) {
s := buf.ReflectValue.String()
if !buf.Option.WithType {
buf.Buffer.WriteString(fmt.Sprintf(`"%v"`, replaceSlashesForString(s)))
} else {
buf.Buffer.WriteString(fmt.Sprintf(
`%s(%d) "%v"`,
buf.ReflectTypeName,
len(s),
replaceSlashesForString(s),
))
}
}
func doDumpBool(buf dumpBuf) {
var s string
if buf.ReflectValue.Bool() {
s = `true`
} else {
s = `false`
}
if buf.Option.WithType {
s = fmt.Sprintf(`bool(%s)`, s)
}
buf.Buffer.WriteString(s)
}
func doDumpDefault(buf dumpBuf) {
var s string
if buf.ReflectValue.IsValid() && buf.ReflectValue.CanInterface() {
s = fmt.Sprintf("%v", buf.ReflectValue.Interface())
}
if s == "" {
s = fmt.Sprintf("%v", buf.Value)
}
s = trim(s, `<>`)
if !buf.Option.WithType {
buf.Buffer.WriteString(s)
} else {
buf.Buffer.WriteString(fmt.Sprintf("%s(%s)", buf.ReflectTypeName, s))
}
}
// 将字符串形式的斜杠替换成byte形式的
func replaceSlashesForString(s string) string {
return replaceByMap(s, map[string]string{
`"`: `\"`,
"\r": `\r`,
"\t": `\t`,
"\n": `\n`,
})
}
func replaceByMap(origin string, replaces map[string]string) string {
for k, v := range replaces {
origin = strings.Replace(origin, k, v, -1)
}
return origin
}
func trim(str string, characterMask ...string) string {
trimChars := DefaultTrimChars
if len(characterMask) > 0 {
trimChars += characterMask[0]
}
return strings.Trim(str, trimChars)
}