-
Notifications
You must be signed in to change notification settings - Fork 0
/
polygo.go
116 lines (92 loc) · 2.25 KB
/
polygo.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
package polygo
import (
"encoding/json"
"errors"
"fmt"
"reflect"
"github.com/tidwall/gjson"
)
type Decoder[T any] struct {
Type T
FieldName string
TypeMap map[string]reflect.Type
}
func NewDecoder[T any](field string) *Decoder[T] {
return &Decoder[T]{
FieldName: field,
TypeMap: map[string]reflect.Type{},
}
}
func (d *Decoder[T]) Register(value string, v any) *Decoder[T] {
d.TypeMap[value] = reflect.TypeOf(v)
return d
}
func (d *Decoder[T]) UnmarshalArray(b []byte) ([]T, error) {
res := gjson.ParseBytes(b)
return d.unmarshalArray("", res)
}
func (d *Decoder[T]) UnmarshalInnerArray(path string, b []byte) ([]T, error) {
res := gjson.ParseBytes(b)
return d.unmarshalArray(path, res)
}
func (d *Decoder[T]) unmarshalArray(path string, res gjson.Result) ([]T, error) {
if path != "" {
res = res.Get(path)
}
if !res.IsArray() {
return nil, errors.New("object is not an array")
}
arr := []T{}
var err error
res.ForEach(func(key, value gjson.Result) bool {
a, errParse := d.unmarshalObject("", value)
if errParse != nil {
err = errParse
return false
}
arr = append(arr, a)
return true
})
return arr, err
}
func (d *Decoder[T]) UnmarshalObject(b []byte) (T, error) {
res := gjson.ParseBytes(b)
return d.unmarshalObject("", res)
}
func (d *Decoder[T]) UnmarshalInnerObject(path string, b []byte) (T, error) {
res := gjson.ParseBytes(b)
return d.unmarshalObject(path, res)
}
func (d *Decoder[T]) unmarshalObject(path string, res gjson.Result) (T, error) {
var zero T
if path != "" {
res = res.Get(path)
}
if res.IsArray() {
return zero, errors.New("cannot unmarshal object: JSON is array")
}
fieldValue := res.Get(d.FieldName).String()
// TODO check if string
if fieldValue == "" {
return zero, fmt.Errorf("field '%s' not found", d.FieldName)
}
matchedType, found := d.TypeMap[fieldValue]
if !found {
return zero, fmt.Errorf("type '%s' not registered", fieldValue)
}
var v reflect.Value
if matchedType.Kind() == reflect.Ptr {
v = reflect.New(matchedType.Elem())
} else {
v = reflect.New(matchedType)
}
err := json.Unmarshal([]byte(res.Raw), v.Interface())
if err != nil {
return zero, err
}
a, ok := v.Interface().(T)
if !ok {
return zero, errors.New("error casting")
}
return a, nil
}