/
unmarshal.go
77 lines (71 loc) · 1.96 KB
/
unmarshal.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
package djson
import (
"fmt"
"reflect"
"strings"
"time"
"github.com/tidwall/gjson"
)
// Unmarshal will try to load JSON from the given data into val. It'll read
// from the struct tags of the given val and look for the 'djson' tag which
// can supply multiple possible fields a JSON key can be. If a json key match
// with any of the tags it'll set the value.
func Unmarshal(data []byte, val interface{}) {
elem := reflect.TypeOf(val).Elem()
for i := 0; i < elem.NumField(); i++ {
process(data, val, elem, i)
}
}
func process(data []byte, val interface{}, elem reflect.Type, i int) {
defer func() {
_ = recover()
}()
field := elem.FieldByIndex([]int{i})
keys, ok := field.Tag.Lookup("djson")
if !ok {
return
}
keylist := strings.Split(keys, ",")
reverse(keylist) // to prioritize the keys in the beginning of the list
for _, key := range keylist {
result := gjson.GetBytes(data, strings.TrimSpace(key))
if !result.Exists() {
result = gjson.GetBytes(data, strings.ReplaceAll(strings.TrimSpace(key), ".", "\\."))
if !result.Exists() {
continue
}
}
value := reflect.ValueOf(result.Value())
fieldValue := reflect.ValueOf(val).Elem().FieldByIndex(field.Index)
if fieldValue.Kind() == reflect.Ptr {
if fieldValue.IsNil() {
fieldValue.Set(reflect.New(fieldValue.Type().Elem()))
}
fieldValue.Elem().Set(convert(value, fieldValue.Elem()))
} else {
fieldValue.Set(convert(value, fieldValue))
}
}
}
func convert(value, field reflect.Value) reflect.Value {
if value.Type() == field.Type() {
return value
}
switch field.Interface().(type) {
case string:
return reflect.ValueOf(fmt.Sprintf("%v", value.Interface()))
case time.Time:
if value.Kind() == reflect.String {
t, err := time.Parse(time.RFC3339, value.String())
if err == nil {
return reflect.ValueOf(t)
}
}
}
return reflect.ValueOf(nil)
}
func reverse[S ~[]T, T any](s S) {
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
}