/
token.go
318 lines (271 loc) · 6.8 KB
/
token.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
package gop
import (
"encoding/base64"
"fmt"
"reflect"
"sort"
"strconv"
"time"
"unicode"
"unicode/utf8"
"github.com/ysmood/got/lib/utils"
)
// Type of token
type Type int
const (
// Nil type
Nil Type = iota
// Bool type
Bool
// Number type
Number
// Float type
Float
// Complex type
Complex
// String type
String
// Byte type
Byte
// Rune type
Rune
// Chan type
Chan
// Func type
Func
// UnsafePointer type
UnsafePointer
// TypeName type
TypeName
// ParenOpen type
ParenOpen
// ParenClose type
ParenClose
// PointerOpen type
PointerOpen
// PointerClose type
PointerClose
// PointerCyclic type
PointerCyclic
// SliceOpen type
SliceOpen
// SliceItem type
SliceItem
// InlineComma type
InlineComma
// Comma type
Comma
// SliceClose type
SliceClose
// MapOpen type
MapOpen
// MapKey type
MapKey
// Colon type
Colon
// MapClose type
MapClose
// StructOpen type
StructOpen
// StructKey type
StructKey
// StructField type
StructField
// StructClose type
StructClose
)
// Token represents a symbol in value layout
type Token struct {
Type Type
Literal string
}
// Tokenize a random Go value
func Tokenize(v interface{}) []*Token {
return tokenize(seen{}, reflect.ValueOf(v))
}
// ToPtr converts a Go value to its pointer
func ToPtr(interface{}) interface{} {
return nil
}
// Cyclic reference
func Cyclic(uintptr) interface{} {
return nil
}
// Base64 from string
func Base64(string) []byte {
return nil
}
type seen map[interface{}]struct{}
func tokenize(sn seen, v reflect.Value) []*Token {
ts := []*Token{}
t := &Token{Nil, ""}
if v.Kind() == reflect.Invalid {
t.Literal = "nil"
return append(ts, t)
} else if r, ok := v.Interface().(rune); ok && unicode.IsGraphic(r) {
return append(ts, tokenizeRune(t, r))
} else if b, ok := v.Interface().(byte); ok {
return append(ts, tokenizeByte(t, b))
} else if tt, ok := v.Interface().(time.Time); ok {
return tokenizeTime(tt)
}
switch v.Kind() {
case reflect.Interface:
ts = append(ts, tokenize(sn, v.Elem())...)
case reflect.Slice, reflect.Array:
if data, ok := v.Interface().([]byte); ok {
ts = append(ts, tokenizeBytes(data)...)
break
} else {
ts = append(ts, &Token{TypeName, v.Type().String()})
}
ts = append(ts, &Token{SliceOpen, "{"})
for i := 0; i < v.Len(); i++ {
el := v.Index(i)
ts = append(ts, &Token{SliceItem, ""})
ts = append(ts, tokenize(sn, el)...)
ts = append(ts, &Token{Comma, ","})
}
ts = append(ts, &Token{SliceClose, "}"})
case reflect.Map:
ts = append(ts, &Token{TypeName, v.Type().String()})
ts = append(ts, &Token{MapOpen, "{"})
keys := v.MapKeys()
sort.Slice(keys, func(i, j int) bool {
return utils.Compare(keys[i], keys[j]) < 0
})
for _, k := range keys {
ts = append(ts, &Token{MapKey, ""})
ts = append(ts, tokenize(sn, k)...)
ts = append(ts, &Token{Colon, ":"})
ts = append(ts, tokenize(sn, v.MapIndex(k))...)
ts = append(ts, &Token{Comma, ","})
}
ts = append(ts, &Token{MapClose, "}"})
case reflect.Struct:
t := v.Type()
ts = append(ts, &Token{TypeName, t.String()})
ts = append(ts, &Token{StructOpen, "{"})
for i := 0; i < v.NumField(); i++ {
ts = append(ts, &Token{StructKey, ""})
ts = append(ts, &Token{StructField, t.Field(i).Name})
f := v.Field(i)
if !f.CanInterface() {
f = GetPrivateField(v, i)
}
ts = append(ts, &Token{Colon, ":"})
ts = append(ts, tokenize(sn, f)...)
ts = append(ts, &Token{Comma, ","})
}
ts = append(ts, &Token{StructClose, "}"})
case reflect.Bool:
t.Type = Bool
if v.Bool() {
t.Literal = "true"
} else {
t.Literal = "false"
}
ts = append(ts, t)
case reflect.Int:
t.Type = Number
t.Literal = strconv.FormatInt(v.Int(), 10)
ts = append(ts, t)
case 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,
reflect.Uintptr:
ts = append(ts, &Token{TypeName, v.Type().Name()})
ts = append(ts, &Token{ParenOpen, "("})
t.Type = Number
t.Literal = fmt.Sprintf("%v", v.Interface())
ts = append(ts, t)
ts = append(ts, &Token{ParenClose, ")"})
case reflect.String:
t.Type = String
t.Literal = fmt.Sprintf("%#v", v.Interface())
ts = append(ts, t)
case reflect.Chan:
t.Type = Chan
if v.Cap() == 0 {
t.Literal = fmt.Sprintf("make(chan %s)", v.Type().Elem().Name())
} else {
t.Literal = fmt.Sprintf("make(chan %s, %d)", v.Type().Elem().Name(), v.Cap())
}
ts = append(ts, t)
case reflect.Func:
t.Type = Func
t.Literal = fmt.Sprintf("(%s)(nil)", v.Type().String())
ts = append(ts, t)
case reflect.Ptr:
ts = append(ts, tokenizePtr(sn, v)...)
case reflect.UnsafePointer:
t.Type = UnsafePointer
t.Literal = fmt.Sprintf("unsafe.Pointer(uintptr(%v))", v.Interface())
ts = append(ts, t)
}
return ts
}
func tokenizeRune(t *Token, r rune) *Token {
t.Type = Rune
t.Literal = fmt.Sprintf("'%s'", string(r))
return t
}
func tokenizeByte(t *Token, b byte) *Token {
t.Type = Byte
if unicode.IsGraphic(rune(b)) {
t.Literal = fmt.Sprintf("byte('%s')", string(b))
} else {
t.Literal = fmt.Sprintf("byte(0x%x)", b)
}
return t
}
func tokenizeTime(t time.Time) []*Token {
ts := []*Token{}
ts = append(ts, &Token{TypeName, "time.Parse"})
ts = append(ts, &Token{ParenOpen, "("})
ts = append(ts, &Token{TypeName, "time.RFC3339Nano"})
ts = append(ts, &Token{InlineComma, ","})
ts = append(ts, &Token{String, `"` + t.Format(time.RFC3339Nano) + `"`})
ts = append(ts, &Token{ParenClose, ")"})
return ts
}
func tokenizeBytes(data []byte) []*Token {
ts := []*Token{}
if utf8.Valid(data) {
ts = append(ts, &Token{TypeName, "[]byte"})
ts = append(ts, &Token{ParenOpen, "("})
ts = append(ts, &Token{String, fmt.Sprintf("%#v", string(data))})
ts = append(ts, &Token{ParenClose, ")"})
return ts
}
ts = append(ts, &Token{ParenOpen, "gop.Base64("})
ts = append(ts, &Token{String, fmt.Sprintf("%#v", base64.StdEncoding.EncodeToString(data))})
ts = append(ts, &Token{ParenClose, ")"})
return ts
}
func tokenizePtr(sn seen, v reflect.Value) []*Token {
ts := []*Token{}
if v.Elem().Kind() == reflect.Invalid {
ts = append(ts, &Token{Nil, fmt.Sprintf("(%s)(nil)", v.Type().String())})
return ts
}
if _, has := sn[v.Interface()]; has {
ts = append(ts, &Token{
PointerCyclic,
fmt.Sprintf("gop.Cyclic(0x%x).(%s)", v.Pointer(), v.Type().String()),
})
return ts
}
sn[v.Interface()] = struct{}{}
switch v.Elem().Kind() {
case reflect.Struct, reflect.Map, reflect.Slice, reflect.Array:
ts = append(ts, &Token{TypeName, "&"})
ts = append(ts, tokenize(sn, v.Elem())...)
default:
ts = append(ts, &Token{PointerOpen, "gop.ToPtr("})
ts = append(ts, tokenize(sn, v.Elem())...)
ts = append(ts, &Token{PointerOpen, fmt.Sprintf(").(%s)", v.Type().String())})
}
return ts
}