-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmissingkeys.go
243 lines (229 loc) · 7.5 KB
/
missingkeys.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// missingkeys.go - check JSON object against struct definition
// Copyright © 2016-2019 Charles Banning. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package checkjson
import (
"encoding/json"
"reflect"
"strings"
)
type skipmems struct {
val string
depth int
}
// Slice of dot-notation struct fields that can be missing in JSON object.
var skipmembers = []skipmems{}
// SetMembersToIgnore creates a list of exported struct field names that should not be checked
// for as keys in the JSON object. For hierarchical struct members provide the full path for
// the member name using dot-notation. Calling SetMembersToIgnore with no arguments -
// SetMembersToIgnore() - clears the list.
func SetMembersToIgnore(s ...string) {
if len(s) == 0 {
skipmembers = skipmembers[:0]
return
}
skipmembers = make([]skipmems, len(s))
for i, v := range s {
skipmembers[i] = skipmems{strings.ToLower(v), len(strings.Split(v, "."))}
}
}
// Should we ignore "omitempty" struct tags. By default accept tag.
var omitemptyOK = true
// IgnoreOmitemptyTag determines whether a `json:",omitempty"` tag is recognized or
// not with respect to the JSON object. By default MissingJSONKeys will not include
// struct fields that are tagged with "omitempty" in the list of missing JSON keys.
// If the function is toggled or passed the optional argument 'false' then missing
// JSON keys may include those for struct fields with the 'omitempty' JSON tag.
//
// Calling IgnoreOmitemptyTag with no arguments toggles the handling on/off. If
// the alternative argument is passed, then the argument value determines the
// "omitempty" handling behavior.
func IgnoreOmitemptyTag(ok ...bool) {
if len(ok) == 0 {
omitemptyOK = !omitemptyOK
return
}
omitemptyOK = ok[0]
}
// MissingJSONKeys returns a list of fields of a struct that will NOT be set
// by unmarshaling the JSON object; rather, they will assume their default
// values. For nested structs, field labels are the dot-notation hierachical
// path for the missing JSON key. Specific struct fields can be igored
// when scanning the JSON object by declaring them using SetMembersToIgnore().
// (NOTE: JSON object keys and tags are treated as case insensitive, i.e., there
// is no distiction between "keylabel":"value" and "Keylabel":"value" and
// "keyLabel":"value".)
//
// By default keys in the JSON object that are associated with struct fields that
// have JSON tag "-" are ignored. If the "omitempty" attribute is included in the
// struct field tag they are by default also not included in the returned slice.
// IgnoreOmitemptyTag(false) can be called to override the handling of "omitempty"
// tags - this might be useful if you want to find the "omitempty" fields that
// are not set by decoding the JSON object.
//
// If the struct has a member struct with `checkjson:"norecurse"` tag, then it is not scanned.
func MissingJSONKeys(b []byte, val interface{}) ([]string, error) {
s := make([]string, 0)
m := make(map[string]interface{})
if err := json.Unmarshal(b, &m); err != nil {
return s, ResolveJSONError(b, err)
}
checkMembers(m, reflect.ValueOf(val), &s, "")
return s, nil
}
// cmem is the parent struct member for nested structs
// If the struct has a member struct with `checkjson:"norecurse"` tag, then it is not scanned.
func checkMembers(mv interface{}, val reflect.Value, s *[]string, cmem string) {
// 1. Convert any pointer value.
if val.Kind() == reflect.Ptr {
val = reflect.Indirect(val) // convert ptr to struc
}
// zero Value?
if !val.IsValid() {
return
}
typ := val.Type()
// json.RawMessage is a []byte/[]uint8 and has Kind() == reflect.Slice
if typ.Name() == "RawMessage" {
return
}
// 2. If its a slice then 'mv' should hold a []interface{} value.
// Loop through the members of 'mv' and see that they are valid relative
// to the <T> of val []<T>.
if typ.Kind() == reflect.Slice {
tval := typ.Elem()
if tval.Kind() == reflect.Ptr {
tval = tval.Elem()
}
// slice may be nil, so create a Value of it's type
// 'mv' must be of type []interface{}
sval := reflect.New(tval)
slice, ok := mv.([]interface{})
if !ok {
// encodiong/json must have a JSON array value to decode
// unlike encoding/xml which will decode a list of elements
// to a singleton or vise-versa.
*s = append(*s, typ.Name())
return
}
// 2.1. Check members of JSON array.
// This forces all of them to be regular and w/o typos in key labels.
for _, sl := range slice {
// cmem is the member name for the slice - []<T> - value
checkMembers(sl, sval, s, cmem)
}
return // done with reflect.Slice value
}
// 3a. Ignore anything that's not a struct.
if typ.Kind() != reflect.Struct {
return // just ignore it - don't look for k:v pairs
}
// 3b. map value must represent k:v pairs
mm, ok := mv.(map[string]interface{})
if !ok {
*s = append(*s, typ.Name())
}
// 3c. Coerce keys to lower case.
mkeys := make(map[string]interface{}, len(mm))
for k, v := range mm {
mkeys[strings.ToLower(k)] = v
}
// 4. Build the list of struct field name:value
// We make every key (field) label look like an exported label - "Fieldname".
// If there is a JSON tag it is used instead of the field label, and saved to
// insure that the spec'd tag matches the JSON key exactly.
type fieldSpec struct {
name string
val reflect.Value
tag string
omitempty bool
norecurse bool
}
fields := make([]*fieldSpec, 0) // use a list so members are in sequence
var tag string
var oempty bool
var norecurse bool
for i := 0; i < val.NumField(); i++ {
tag = ""
oempty = false
norecurse = false
if len(typ.Field(i).PkgPath) > 0 {
continue // field is NOT exported
}
t := typ.Field(i).Tag.Get("json")
tags := strings.Split(t, ",")
tag = strings.ToLower(tags[0])
// handle ignore member JSON tag, "-"
if tag == "-" {
continue
}
// scan rest of tags for "omitempty"
for _, v := range tags[1:] {
if v == "omitempty" {
oempty = true
break
}
}
t = typ.Field(i).Tag.Get("checkjson")
if t == "norecurse" {
norecurse = true
break
}
fields = append(fields, &fieldSpec{typ.Field(i).Name, val.Field(i), tag, oempty, norecurse})
}
// 5. check that field names/tags have corresponding map key
// var ok bool
var v interface{}
// var err error
cmemdepth := 1
if len(cmem) > 0 {
cmemdepth = len(strings.Split(cmem, ".")) + 1 // struct hierarchy
}
lcmem := strings.ToLower(cmem)
name := ""
for _, field := range fields {
lm := strings.ToLower(field.name)
for _, sm := range skipmembers {
// skip any skipmembers values that aren't at same depth
if cmemdepth != sm.depth {
continue
}
if len(cmem) > 0 {
if lcmem+`.`+lm == sm.val {
goto next
}
} else if lm == sm.val {
goto next
}
}
if len(field.tag) > 0 {
name = field.tag
v, ok = mkeys[field.tag]
} else {
name = field.name
v, ok = mkeys[lm]
}
// If map key is missing, then record it
// if there's no omitempty tag or we're ignoring omitempty tag.
if !ok && (!field.omitempty || !omitemptyOK) {
if len(cmem) > 0 {
// *s = append(*s, cmem+`.`+field.name)
*s = append(*s, cmem+`.`+name)
} else {
// *s = append(*s, field.name)
*s = append(*s, name)
}
goto next // don't drill down further; no key in JSON object
}
if field.norecurse {
goto next // don't drill down further
}
if len(cmem) > 0 {
checkMembers(v, field.val, s, cmem+`.`+name)
} else {
checkMembers(v, field.val, s, name)
}
next:
}
}