-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprettystring.go
125 lines (120 loc) · 4.12 KB
/
prettystring.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
// Copyright 2015,2016,2017,2018,2019 SeukWon Kang ([email protected])
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package prettystring
import (
"bytes"
"fmt"
"reflect"
)
func writeIndent(buf *bytes.Buffer, n int) {
buf.WriteString("\n")
for i := 0; i < n; i++ {
buf.WriteString(" ")
// buf.WriteString("-|")
}
}
// PrettyString make struct to detailed string like python prettyprint
func PrettyString(inObj interface{}, depthLimit int) string {
var buf bytes.Buffer
formObj(&buf, reflect.ValueOf(inObj), 0, depthLimit)
buf.WriteString("\n")
return buf.String()
}
func formObj(buf *bytes.Buffer, objVal reflect.Value, depth int, depthLimit int) {
if depth > depthLimit {
fmt.Fprintf(buf, "%v %v", objVal.Type(), objVal)
return
}
switch objVal.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
fmt.Fprintf(buf, "%v %v", objVal.Type(), objVal)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
fmt.Fprintf(buf, "%v %v", objVal.Type(), objVal)
case reflect.Float64, reflect.Float32:
fmt.Fprintf(buf, "%v %v", objVal.Type(), objVal)
case reflect.Bool:
fmt.Fprintf(buf, "%v %v", objVal.Type(), objVal)
case reflect.String:
fmt.Fprintf(buf, "%v %v", objVal.Type(), objVal)
case reflect.Complex64, reflect.Complex128:
fmt.Fprintf(buf, "%v %v", objVal.Type(), objVal)
case reflect.Chan:
fmt.Fprintf(buf, "%v %v/%v", objVal.Type(), objVal.Len(), objVal.Cap())
case reflect.Func:
fmt.Fprintf(buf, "%v %v", objVal.Type(), objVal)
case reflect.Interface:
fmt.Fprintf(buf, "%v %v", objVal.Type(), objVal)
if !objVal.IsNil() {
writeIndent(buf, depth+1)
formObj(buf, objVal.Elem(), depth+1, depthLimit)
}
case reflect.Ptr:
fmt.Fprintf(buf, "%v", objVal.Type())
// fmt.Fprintf(buf, "%v %v", objVal.Type(), objVal)
if !objVal.IsNil() {
writeIndent(buf, depth+1)
formObj(buf, reflect.Indirect(objVal), depth+1, depthLimit)
}
case reflect.UnsafePointer:
fmt.Fprintf(buf, "%v %v", objVal.Type(), objVal)
case reflect.Array:
fmt.Fprintf(buf, "%v %v", objVal.Type(), objVal)
for i := 0; i < objVal.Len(); i++ {
writeIndent(buf, depth+1)
formObj(buf, objVal.Index(i), depth+1, depthLimit)
}
case reflect.Map:
fmt.Fprintf(buf, "%v %v", objVal.Type(), objVal)
iter := objVal.MapRange()
for iter.Next() {
k := iter.Key()
v := iter.Value()
writeIndent(buf, depth+1)
fmt.Fprintf(buf, "%v: ", k)
formObj(buf, v, depth+1, depthLimit)
}
case reflect.Slice:
fmt.Fprintf(buf, "%v %v", objVal.Type(), objVal)
for i := 0; i < objVal.Len(); i++ {
writeIndent(buf, depth+1)
formObj(buf, objVal.Index(i), depth+1, depthLimit)
}
case reflect.Struct:
fmt.Fprintf(buf, "%v", objVal.Type())
// fmt.Fprintf(buf, "%v %v", objVal.Type(), objVal)
for i := 0; i < objVal.NumField(); i++ {
structTypeField := objVal.Type().Field(i)
if tag, exist := structTypeField.Tag.Lookup("prettystring"); exist {
switch tag {
default:
writeIndent(buf, depth+1)
fmt.Fprintf(buf, "%v %v %v unknowntag %v", structTypeField.Name, structTypeField.Type, objVal.Field(i), tag)
continue
case "hide":
continue
case "hidevalue":
writeIndent(buf, depth+1)
fmt.Fprintf(buf, "%v %v ****", structTypeField.Name, structTypeField.Type)
continue
case "simple":
writeIndent(buf, depth+1)
fmt.Fprintf(buf, "%v %v %v", structTypeField.Name, structTypeField.Type, objVal.Field(i))
continue
}
}
writeIndent(buf, depth+1)
fmt.Fprintf(buf, "%v: ", structTypeField.Name)
formObj(buf, objVal.Field(i), depth+1, depthLimit)
}
default:
fmt.Fprintf(buf, "%v %v unprocessed", objVal.Type(), objVal)
}
}