forked from ohler55/ojg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decompose.go
294 lines (281 loc) · 6.16 KB
/
decompose.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
// Copyright (c) 2020, Peter Ohler, All rights reserved.
package alt
import (
"encoding/base64"
"fmt"
"math"
"reflect"
"time"
"github.com/khaf/ojg"
)
// 23 for fraction in IEEE 754 which amounts to 7 significant digits. Use base
// 10 so that numbers look correct when displayed in base 10.
const fracMax = 10000000.0
func decompose(v any, opt *Options) any {
switch tv := v.(type) {
case nil, bool, int64, float64, string:
case int:
v = int64(tv)
case int8:
v = int64(tv)
case int16:
v = int64(tv)
case int32:
v = int64(tv)
case uint:
v = int64(tv)
case uint8:
v = int64(tv)
case uint16:
v = int64(tv)
case uint32:
v = int64(tv)
case uint64:
v = int64(tv)
case float32:
// This small rounding makes the conversion from 32 bit to 64 bit
// display nicer.
f, i := math.Frexp(float64(tv))
f = float64(int64(f*fracMax)) / fracMax
v = math.Ldexp(f, i)
case []any:
a := make([]any, len(tv))
for i, m := range tv {
a[i] = decompose(m, opt)
}
v = a
case map[string]any:
o := map[string]any{}
for k, m := range tv {
if mv := decompose(m, opt); mv != nil || !opt.OmitNil {
if mv != nil || !opt.OmitNil {
o[k] = mv
}
}
}
v = o
case []byte:
switch opt.BytesAs {
case ojg.BytesAsBase64:
v = base64.StdEncoding.EncodeToString(tv)
case ojg.BytesAsArray:
a := make([]any, len(tv))
for i, m := range tv {
a[i] = decompose(m, opt)
}
v = a
default:
v = string(tv)
}
case time.Time:
v = opt.DecomposeTime(tv)
default:
if simp, _ := v.(Simplifier); simp != nil {
return decompose(simp.Simplify(), opt)
}
return reflectValue(reflect.ValueOf(v), v, opt)
}
return v
}
func alter(v any, opt *Options) any {
switch tv := v.(type) {
case bool, nil, int64, float64, string, time.Time:
case int:
v = int64(tv)
case int8:
v = int64(tv)
case int16:
v = int64(tv)
case int32:
v = int64(tv)
case uint:
v = int64(tv)
case uint8:
v = int64(tv)
case uint16:
v = int64(tv)
case uint32:
v = int64(tv)
case uint64:
v = int64(tv)
case float32:
// This small rounding makes the conversion from 32 bit to 64 bit
// display nicer.
f, i := math.Frexp(float64(tv))
f = float64(int64(f*fracMax)) / fracMax
v = math.Ldexp(f, i)
case []any:
for i, m := range tv {
tv[i] = alter(m, opt)
}
case map[string]any:
for k, m := range tv {
if mv := alter(m, opt); mv != nil || !opt.OmitNil {
if mv != nil || !opt.OmitNil {
tv[k] = mv
}
}
}
case []byte:
switch opt.BytesAs {
case ojg.BytesAsBase64:
v = base64.StdEncoding.EncodeToString(tv)
case ojg.BytesAsArray:
a := make([]any, len(tv))
for i, m := range tv {
a[i] = decompose(m, opt)
}
v = a
default:
v = string(tv)
}
default:
if simp, _ := v.(Simplifier); simp != nil {
return alter(simp.Simplify(), opt)
}
return reflectValue(reflect.ValueOf(v), v, opt)
}
return v
}
func reflectValue(rv reflect.Value, val any, opt *Options) (v any) {
switch rv.Kind() {
case reflect.Invalid, reflect.Uintptr, reflect.UnsafePointer, reflect.Chan, reflect.Func, reflect.Interface:
v = nil
case reflect.Complex64, reflect.Complex128:
v = reflectComplex(rv, opt)
case reflect.Map:
v = reflectMap(rv, opt)
case reflect.Ptr:
elem := rv.Elem()
if elem.IsValid() && elem.CanInterface() {
v = reflectValue(elem, elem.Interface(), opt)
} else {
v = nil
}
case reflect.Slice, reflect.Array:
v = reflectArray(rv, opt)
case reflect.Struct:
v = reflectStruct(rv, val, opt)
case reflect.String:
v = rv.String()
case reflect.Bool:
v = rv.Bool()
case reflect.Float32, reflect.Float64:
v = rv.Float()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
v = rv.Int()
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
v = rv.Uint()
}
return
}
func reflectStruct(rv reflect.Value, val any, opt *Options) any {
if !rv.CanAddr() {
return reflectEmbed(rv, val, opt)
}
obj := map[string]any{}
si := getSinfo(val)
t := si.rt
if 0 < len(opt.CreateKey) {
if opt.FullTypePath {
obj[opt.CreateKey] = t.PkgPath() + "/" + t.Name()
} else {
obj[opt.CreateKey] = t.Name()
}
}
fields := si.getFields(opt)
addr := rv.UnsafeAddr()
for _, fi := range fields {
if v, fv, omit := fi.value(fi, rv, addr); !omit {
if fv.IsValid() {
if opt.NestEmbed && fv.Kind() == reflect.Struct {
v = reflectEmbed(fv, v, opt)
} else {
v = decompose(v, opt)
}
if !opt.OmitNil || v != nil {
obj[fi.key] = v
}
} else if !opt.OmitNil || v != nil {
obj[fi.key] = v
}
}
}
return obj
}
func reflectEmbed(rv reflect.Value, val any, opt *Options) any {
obj := map[string]any{}
si := getSinfo(val)
t := si.rt
if 0 < len(opt.CreateKey) {
if opt.FullTypePath {
obj[opt.CreateKey] = t.PkgPath() + "/" + t.Name()
} else {
obj[opt.CreateKey] = t.Name()
}
}
fields := si.getFields(opt)
for _, fi := range fields {
if v, fv, omit := fi.ivalue(fi, rv, 0); !omit {
if fv.IsValid() {
if opt.NestEmbed && fv.Kind() == reflect.Struct {
v = reflectEmbed(fv, v, opt)
} else {
v = decompose(v, opt)
}
if !opt.OmitNil || v != nil {
obj[fi.key] = v
}
} else if !opt.OmitNil || v != nil {
obj[fi.key] = v
}
}
}
return obj
}
func reflectComplex(rv reflect.Value, opt *Options) any {
c := rv.Complex()
obj := map[string]any{
"real": real(c),
"imag": imag(c),
}
if 0 < len(opt.CreateKey) {
obj[opt.CreateKey] = "complex"
}
return obj
}
func reflectMap(rv reflect.Value, opt *Options) any {
obj := map[string]any{}
it := rv.MapRange()
for it.Next() {
k := it.Key().Interface()
var g any
vv := it.Value()
if !isNil(vv) {
g = decompose(vv.Interface(), opt)
}
if g != nil || !opt.OmitNil {
if ks, ok := k.(string); ok {
obj[ks] = g
} else {
obj[fmt.Sprint(k)] = g
}
}
}
return obj
}
func reflectArray(rv reflect.Value, opt *Options) any {
size := rv.Len()
a := make([]any, size)
for i := size - 1; 0 <= i; i-- {
a[i] = decompose(rv.Index(i).Interface(), opt)
}
return a
}
func isNil(rv reflect.Value) bool {
switch rv.Kind() {
case reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
return rv.IsNil()
}
return false
}