-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonobj.go
298 lines (250 loc) · 6.01 KB
/
jsonobj.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
package jsonobj
import (
"encoding/json"
"fmt"
"reflect"
"strings"
)
const (
CamelCase = NameConv(iota)
SnakeCase
LowerCase
UpperCase
)
var (
ErrUnkNameConv = fmt.Errorf("unknown name convention")
)
// NameConv defines a custom type for identifying
// naming convention
type NameConv int
// Options to apply modification on JSONObject fields
type Options struct {
FieldNameConvention NameConv
}
// Field structure definition
type Field struct {
Name string
Value any
}
// JSONObject structure definition
type JSONObject struct {
fields []Field
cache map[string]int
Options *Options
}
func (o *JSONObject) newChild() (new *JSONObject) {
new = New()
new.Options = o.Options
return
}
func (o *JSONObject) newFromValue(v reflect.Value) (new *JSONObject) {
new = o.newChild()
new.fromStruct(v)
return
}
func isBaseType(v reflect.Value) bool {
switch v.Kind() {
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.String, reflect.Bool:
return true
default:
return false
}
}
func resolve(v reflect.Value) reflect.Value {
var t reflect.Type
// we deref type until we get something not Ptr
for t = v.Type(); ; t = v.Type() {
if t.Kind() != reflect.Ptr && t.Kind() != reflect.Interface {
break
}
v = v.Elem()
}
return v
}
func (o *JSONObject) convertSlice(slice reflect.Value) (s []interface{}) {
if slice.Kind() != reflect.Slice {
panic("can convert only slice")
}
for i := 0; i < slice.Len(); i++ {
v := slice.Index(i)
v = resolve(v)
if isBaseType(v) {
s = append(s, v.Interface())
continue
}
switch v.Kind() {
case reflect.Struct, reflect.Ptr:
s = append(s, o.newFromValue(v))
case reflect.Slice:
s = append(s, o.convertSlice(v))
case reflect.Map:
s = append(s, o.newFromMapValue(v))
default:
panic("not handled")
}
}
return
}
func (o *JSONObject) fromMapValue(m reflect.Value) {
iter := m.MapRange()
for iter.Next() {
k := iter.Key()
v := iter.Value()
if k.Kind() != reflect.String {
panic("only map with string key are supported")
}
fieldName := k.Interface().(string)
v = resolve(v)
if isBaseType(v) {
o.SetField(fieldName, v.Interface())
continue
}
switch v.Kind() {
case reflect.Struct, reflect.Ptr:
o.SetField(fieldName, o.newFromValue(v))
case reflect.Slice:
o.SetField(fieldName, o.convertSlice(v))
case reflect.Map:
o.SetField(fieldName, o.newFromMapValue(v))
default:
panic("not handled")
}
}
}
func (o *JSONObject) newFromMapValue(m reflect.Value) (new *JSONObject) {
new = o.newChild()
new.fromMapValue(m)
return
}
func (o *JSONObject) fromStruct(v reflect.Value) {
v = resolve(v)
t := v.Type()
if t.Kind() != reflect.Struct {
panic("not a structure")
}
for i := 0; i < v.NumField(); i++ {
field := v.Field(i)
fieldType := t.Field(i)
fieldName := fieldType.Name
if !fieldType.IsExported() {
continue
}
if isBaseType(field) {
o.SetField(fieldName, field.Interface())
continue
}
v = resolve(v)
switch field.Kind() {
case reflect.Struct, reflect.Ptr:
// this is an embedded structure
if fieldType.Anonymous {
o.fromStruct(field)
} else {
o.SetField(fieldName, o.newFromValue(field))
}
case reflect.Slice:
o.SetField(fieldName, o.convertSlice(field))
case reflect.Map:
o.SetField(fieldName, o.newFromMapValue(field))
default:
panic("not handled")
}
}
}
// New creates an empty new JSONObject
func New() *JSONObject {
return &JSONObject{
Options: &Options{},
cache: make(map[string]int),
}
}
// FromMap creates a new JSONObject from a structure
func FromStruct(s any) (o *JSONObject) {
o = New()
return o.newFromValue(reflect.ValueOf(s))
}
// FromMap creates a new JSONObject with options from a structure
func FromStructWithOptions(s any, opt Options) (o *JSONObject) {
o = New()
o.Options = &opt
o.fromStruct(reflect.ValueOf(s))
return
}
// FromMap creates a new JSONObject from a map
func FromMap(m map[string]any) (o *JSONObject) {
o = New()
o.fromMapValue(reflect.ValueOf(m))
return
}
// json marshaling implementation
func (o JSONObject) MarshalJSON() (out []byte, err error) {
// object opening
out = append(out, '{')
for i, f := range o.fields {
var name []byte
var value []byte
name, err = json.Marshal(f.Name)
if err != nil {
return
}
value, err = json.Marshal(f.Value)
if err != nil {
return
}
out = append(out, name...)
out = append(out, ':')
out = append(out, value...)
if i != len(o.fields)-1 {
out = append(out, ',')
}
}
// object closing
out = append(out, '}')
return
}
// ConvertSlice converts any slice to a JSONObject ready slice.
// It is useful to recursively convert slice of structures, so that
// options are also applied to slice elements. On slices containing
// base types, this method has no effect.
func (o *JSONObject) ConvertSlice(slice any) (s []any) {
return o.convertSlice(reflect.ValueOf(slice))
}
// SetField sets a field of the object. If the field is
// already existing, value is replaced
func (o *JSONObject) SetField(name string, value any) {
if o.Options != nil {
switch o.Options.FieldNameConvention {
case SnakeCase:
name = camelToSnake(name)
case LowerCase:
name = strings.ToLower(name)
case UpperCase:
name = strings.ToUpper(name)
}
}
// we replace value if field alread there
if i, ok := o.cache[name]; ok {
o.fields[i].Value = value
return
}
o.fields = append(o.fields, Field{name, value})
o.cache[name] = len(o.fields)
}
// HasField returns true if field is present in JSONObject
func (o *JSONObject) HasField(name string) bool {
_, ok := o.cache[name]
return ok
}
// GetField returns field's value if any. It panics if field is not present
func (o *JSONObject) GetField(name string) any {
if i, ok := o.cache[name]; ok {
return o.fields[i].Value
}
panic("unknown field")
}
// IsEmpty checks wether the JSONObject is empty
func (o *JSONObject) IsEmpty() bool {
return len(o.fields) == 0
}