-
Notifications
You must be signed in to change notification settings - Fork 3
/
marshal.go
172 lines (144 loc) · 4.29 KB
/
marshal.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
package jsonry
import (
"encoding/json"
"fmt"
"reflect"
"code.cloudfoundry.org/jsonry/internal/path"
"code.cloudfoundry.org/jsonry/internal/tree"
)
// Marshal converts the specified Go struct into JSON. The input must be a struct or a pointer to a struct.
// Where a field is optional, the suffix ",omitempty" can be specified. This will mean that the field will
// be omitted from the JSON output if it is a nil pointer or has zero value for the type.
// When a field is a slice or an array, a single list hint "[]" may be specified in the JSONry path so that the array
// is created at the correct position in the JSON output.
//
// If a type implements the json.Marshaler interface, then the MarshalJSON() method will be called.
//
// If a type implements the jsonry.Omissible interface, then the OmitJSONry() method will be used to
// to determine whether or not to marshal the field, overriding any `,omitempty` tags.
//
// The field type can be string, bool, int*, uint*, float*, map, slice, array or struct. JSONry is recursive.
func Marshal(in interface{}) ([]byte, error) {
iv := reflect.Indirect(reflect.ValueOf(in))
if iv.Kind() != reflect.Struct {
return nil, fmt.Errorf(`the input must be a struct, not "%s"`, iv.Kind())
}
m, err := marshalStruct(iv)
if err != nil {
return nil, err
}
return json.Marshal(m)
}
func marshalStruct(in reflect.Value) (map[string]interface{}, error) {
out := make(tree.Tree)
t := in.Type()
for i := 0; i < t.NumField(); i++ {
f := t.Field(i)
if public(f) {
path := path.ComputePath(f)
val := in.Field(i)
if shouldMarshal(path, val) {
r, err := marshal(val)
if err != nil {
return nil, wrapErrorWithFieldContext(err, f.Name, f.Type)
}
out.Attach(path, r)
}
}
}
return out, nil
}
func marshal(in reflect.Value) (r interface{}, err error) {
input := reflect.Indirect(in)
kind := input.Kind()
switch {
case kind == reflect.Invalid:
r = nil
case input.Type().Implements(reflect.TypeOf((*json.Marshaler)(nil)).Elem()):
r, err = marshalJSONMarshaler(input)
case kind == reflect.Interface:
r, err = marshal(input.Elem())
case basicType(kind):
r = in.Interface()
case kind == reflect.Struct:
r, err = marshalStruct(input)
case kind == reflect.Slice || kind == reflect.Array:
r, err = marshalList(input)
case kind == reflect.Map:
r, err = marshalMap(input)
default:
err = newUnsupportedTypeError(input.Type())
}
return
}
func marshalList(in reflect.Value) (out []interface{}, err error) {
if in.Type().Kind() == reflect.Slice && in.IsNil() {
return out, nil
}
out = make([]interface{}, in.Len())
for i := 0; i < in.Len(); i++ {
r, err := marshal(in.Index(i))
if err != nil {
return nil, wrapErrorWithIndexContext(err, i, in.Type())
}
out[i] = r
}
return out, nil
}
func marshalMap(in reflect.Value) (out map[string]interface{}, err error) {
if in.IsNil() {
return out, nil
}
out = make(map[string]interface{})
iter := in.MapRange()
for iter.Next() {
k := iter.Key()
if k.Kind() != reflect.String {
return nil, newUnsupportedKeyTypeError(in.Type())
}
r, err := marshal(iter.Value())
if err != nil {
return nil, wrapErrorWithKeyContext(err, k.String(), k.Type())
}
out[k.String()] = r
}
return out, nil
}
func marshalJSONMarshaler(in reflect.Value) (interface{}, error) {
const method = "MarshalJSON"
t := in.MethodByName(method).Call(nil)
if err := checkForError(t[1]); err != nil {
return nil, newForeignError(fmt.Sprintf("error from %s() call", method), err)
}
var r interface{}
err := json.Unmarshal(t[0].Bytes(), &r)
if err != nil {
return nil, newForeignError(fmt.Sprintf(`error parsing %s() output "%s"`, method, t[0].Bytes()), err)
}
return r, nil
}
func shouldMarshal(p path.Path, v reflect.Value) bool {
switch {
case p.OmitAlways:
return false
case v.Type().Implements(reflect.TypeOf((*Omissible)(nil)).Elem()):
return !v.MethodByName("OmitJSONry").Call(nil)[0].Bool()
case p.OmitEmpty && isEmpty(v):
return false
default:
return true
}
}
func isEmpty(v reflect.Value) bool {
k := v.Kind()
switch {
case k == reflect.Interface, k == reflect.Ptr:
return v.IsZero() || v.IsNil()
case k == reflect.String, k == reflect.Map, k == reflect.Slice, k == reflect.Array:
return v.Len() == 0
case basicType(k):
return v.IsZero()
default:
return false
}
}