-
Notifications
You must be signed in to change notification settings - Fork 3
/
marshal.go
138 lines (111 loc) · 3.52 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
package jsonry
import (
"encoding/json"
"fmt"
"reflect"
"code.cloudfoundry.org/jsonry/internal/tree"
"code.cloudfoundry.org/jsonry/internal/context"
"code.cloudfoundry.org/jsonry/internal/path"
)
// 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 field implements the json.Marshaler interface, then the MarshalJSON() method will be called.
//
// 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(context.Context{}, iv)
if err != nil {
return nil, err
}
return json.Marshal(m)
}
func marshalStruct(ctx context.Context, 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) {
p := path.ComputePath(f)
if !p.OmitEmpty || !in.Field(i).IsZero() {
r, err := marshal(ctx.WithField(f.Name, f.Type), in.Field(i))
if err != nil {
return nil, err
}
out.Attach(p, r)
}
}
}
return out, nil
}
func marshal(ctx context.Context, 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(ctx, input)
case kind == reflect.Interface:
r, err = marshal(ctx, input.Elem())
case basicType(kind):
r = in.Interface()
case kind == reflect.Struct:
r, err = marshalStruct(ctx, input)
case kind == reflect.Slice || kind == reflect.Array:
r, err = marshalList(ctx, input)
case kind == reflect.Map:
r, err = marshalMap(ctx, input)
default:
err = newUnsupportedTypeError(ctx, input.Type())
}
return
}
func marshalList(ctx context.Context, in reflect.Value) ([]interface{}, error) {
var out []interface{}
for i := 0; i < in.Len(); i++ {
ctx := ctx.WithIndex(i, in.Type())
r, err := marshal(ctx, in.Index(i))
if err != nil {
return nil, err
}
out = append(out, r)
}
return out, nil
}
func marshalMap(ctx context.Context, in reflect.Value) (map[string]interface{}, error) {
out := make(map[string]interface{})
iter := in.MapRange()
for iter.Next() {
k := iter.Key()
if k.Kind() != reflect.String {
return nil, newUnsupportedKeyTypeError(ctx, in.Type())
}
ctx := ctx.WithKey(k.String(), k.Type())
r, err := marshal(ctx, iter.Value())
if err != nil {
return nil, err
}
out[k.String()] = r
}
return out, nil
}
func marshalJSONMarshaler(ctx context.Context, in reflect.Value) (interface{}, error) {
t := in.MethodByName("MarshalJSON").Call(nil)
if err := checkForError(t[1]); err != nil {
return nil, fmt.Errorf("error from MarshaJSON() call at %s: %w", ctx, err)
}
var r interface{}
err := json.Unmarshal(t[0].Bytes(), &r)
if err != nil {
return nil, fmt.Errorf(`error parsing MarshaJSON() output "%s" at %s: %w`, t[0].Bytes(), ctx, err)
}
return r, nil
}