-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint.go
212 lines (185 loc) · 4.1 KB
/
print.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
package goslices
import (
"fmt"
"reflect"
)
const (
sep string = "----"
ind string = " |"
tSep string = "****"
)
// print a slice with a newline
func Println[T ~[]V, V any](s *T) {
if IsEmpty(s) {
fmt.Println(sep)
fmt.Println("[]")
fmt.Printf("***\nType: %T\nLength: %d\n", *s, len(*s))
fmt.Println(sep)
return
}
fmt.Println(sep)
for _, v := range *s {
t := reflect.TypeOf(v)
switch t.Kind() {
case reflect.Map:
temp := reflect.ValueOf(v)
n := make(map[interface{}]interface{})
i := true
var kType reflect.Kind
var vType reflect.Kind
for _, k := range temp.MapKeys() {
if i {
i = false
kType = k.Kind()
vType = temp.MapIndex(k).Kind()
}
n[k] = temp.MapIndex(k).Interface()
}
mPrintln(&n, kType, vType, "")
case reflect.Slice:
temp := reflect.ValueOf(v)
n := make([]interface{}, temp.Len())
var eType reflect.Kind
for i := 0; i < temp.Len(); i++ {
if i == 0 {
eType = temp.Index(i).Kind()
}
n[i] = temp.Index(i).Interface()
}
sPrintln(&n, eType, "")
default:
fmt.Printf("%#v\n", v)
}
}
fmt.Printf("\n***\nType: %T\nLength: %d\n", *s, len(*s))
fmt.Println(sep)
}
func mPrintln(m *map[interface{}]interface{}, kType reflect.Kind, vType reflect.Kind, indent string) {
if len(indent) > 1000 {
return
}
indent += ind
var kTypeStr string
var vTypeStr string
if kType == reflect.Interface {
kTypeStr = "interface{}"
} else {
kTypeStr = kType.String()
}
if vType == reflect.Interface {
vTypeStr = "interface{}"
} else {
vTypeStr = vType.String()
}
length := len(*m)
if length == 0 {
fmt.Printf(">> empty 0\n")
return
}
fmt.Printf(">> map[%s]%s %d\n",
kTypeStr, vTypeStr, length)
for k, v := range *m {
t := reflect.TypeOf(v)
// fmt.Println(t.Kind().String())
switch t.Kind() {
case reflect.Map:
fmt.Printf("%v%v ", indent, k)
temp := reflect.ValueOf(v)
n := make(map[interface{}]interface{})
i := true
var kType reflect.Kind
var vType reflect.Kind
for _, k := range temp.MapKeys() {
if i {
i = false
kType = k.Kind()
vType = temp.MapIndex(k).Kind()
}
n[k] = temp.MapIndex(k).Interface()
}
mPrintln(&n, kType, vType, indent)
case reflect.Slice:
fmt.Printf("%v%v ", indent, k)
temp := reflect.ValueOf(v)
n := make([]interface{}, temp.Len())
var eType reflect.Kind
for i := 0; i < temp.Len(); i++ {
if i == 0 {
eType = temp.Index(i).Kind()
}
n[i] = temp.Index(i).Interface()
}
sPrintln(&n, eType, indent)
default:
fmt.Print(indent)
fmt.Printf("%v: %#v\n", k, v)
}
}
}
func sPrintln(s *[]interface{}, eType reflect.Kind, indent string) {
if len(indent) > 1000 {
return
}
indent += ind
var eTypeStr string
if eType == reflect.Interface {
eTypeStr = "interface{}"
} else {
eTypeStr = eType.String()
}
length := len(*s)
if length == 0 {
fmt.Printf(">> []empty 0\n")
return
}
fmt.Printf(">> []%s %d\n",
eTypeStr, length)
for _, e := range *s {
t := reflect.TypeOf(e)
switch t.Kind() {
case reflect.Map:
fmt.Print(indent)
temp := reflect.ValueOf(e)
n := make(map[interface{}]interface{})
i := true
var kType reflect.Kind
var vType reflect.Kind
for _, k := range temp.MapKeys() {
if i {
i = false
kType = k.Kind()
vType = temp.MapIndex(k).Kind()
}
n[k] = temp.MapIndex(k).Interface()
}
mPrintln(&n, kType, vType, indent)
case reflect.Slice:
fmt.Print(indent)
temp := reflect.ValueOf(e)
n := make([]interface{}, temp.Len())
var eType reflect.Kind
for i := 0; i < temp.Len(); i++ {
if i == 0 {
eType = temp.Index(i).Kind()
}
n[i] = temp.Index(i).Interface()
}
sPrintln(&n, eType, indent)
default:
fmt.Print(indent)
fmt.Printf("%#v\n", e)
}
}
}
// print a slice
func Print[T ~[]V, V any](s *T) {
length := len(*s)
if length == 0 {
fmt.Printf("\n***\nType: %T\nLength: %d\n***\n", *s, length)
return
}
first := (*s)[0]
last := (*s)[length-1]
fmt.Printf("%v\n", *s)
fmt.Printf("\n***\nType: %T\nLength: %d\nFirst: %#v\nLast: %#v\n***\n", first, length, first, last)
}