-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunmarshal.go
More file actions
88 lines (71 loc) · 2.2 KB
/
unmarshal.go
File metadata and controls
88 lines (71 loc) · 2.2 KB
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
package additionaljson
import (
"encoding/json"
"errors"
"fmt"
"reflect"
"strings"
)
var DefaultUnmarshaler = Unmarshaler{UnmarshalFunc: json.Unmarshal}
var UnexpectedTypeErr = errors.New("failed to unmarshal json: interface to unmarshal into must be a pointer to a struct")
type Unmarshaler struct {
UnmarshalFunc func([]byte, interface{}) error
}
func (um Unmarshaler) Unmarshal(data []byte, i interface{}) error {
v := reflect.ValueOf(i)
if v.Kind() != reflect.Ptr || v.Elem().Kind() != reflect.Struct { //pointer to struct
return UnexpectedTypeErr
}
err := um.UnmarshalFunc(data, i)
if err != nil {
return fmt.Errorf("failed to unmarshal json: %w", err)
}
declaredJSONFields := listDeclaredJSONFieldNames(v.Elem())
for i := 0; i < v.Elem().Type().NumField(); i++ {
f := v.Elem().Type().Field(i)
ajTag, found := f.Tag.Lookup("aj")
if !found {
continue
}
if ajTag == "all" {
err := json.Unmarshal(data, v.Elem().Field(i).Addr().Interface())
if err != nil {
return fmt.Errorf("failed to unmarshal json field with tag %q: %w", ajTag, err)
}
} else if ajTag == "other" {
var tmp map[string]json.RawMessage
err := json.Unmarshal(data, &tmp)
if err != nil {
return fmt.Errorf("failed to unmarshal generic json field-map for tag %q: %w", ajTag, err)
}
//drop declared fields
for _, f := range declaredJSONFields {
delete(tmp, f)
}
//return err can be ignored cause:
// - json.RawMessage{...}.MarshalJSON() does never respond with an error
// - map keys (string) cannot be nil cause (syntax error at unmarshal step)
b, _ := json.Marshal(tmp)
err = json.Unmarshal(b, v.Elem().Field(i).Addr().Interface())
if err != nil {
return fmt.Errorf("failed to unmarshal json field with tag %q: %w", ajTag, err)
}
}
}
return nil
}
func listDeclaredJSONFieldNames(v reflect.Value) []string {
var declaredJSONFields []string
for i := 0; i < v.NumField(); i++ {
jsonTag, found := v.Type().Field(i).Tag.Lookup("json")
if !found {
continue
}
splitTag := strings.Split(jsonTag, ",")
if len(splitTag) == 0 || splitTag[0] == "-" {
continue
}
declaredJSONFields = append(declaredJSONFields, jsonTag)
}
return declaredJSONFields
}