-
Notifications
You must be signed in to change notification settings - Fork 1
/
strictjson.go
78 lines (75 loc) · 1.85 KB
/
strictjson.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
package strictjson
import (
"encoding/json"
"fmt"
"reflect"
"strings"
)
func checkErr(e error) error {
return fmt.Errorf("strictjson: Check(%s)", e)
}
// Check checks that all of `obj`'s struct fields are provided by the JSON
// object encoded in `b`.
//
// If there are any fields indicated by a `json` tag that are not present
// in the object encoded in `b`, an error will be returned indicating the
// missing fields.
//
// If obj is not a struct, an error will be returned.
//
// If a struct field has an empty `json` tag, or an omitempty tag, the absence
// of a corresponding key in b will not be considered an error.
//
// The omit variadic argument can be used to omit fields from checking, so that
// only certain fields in a struct are checked.
//
// Example:
//
// type Foo struct {
// A int `json:"a"`
// B string `json:"b"`
// }
//
// func (f *Foo) UnmarshalJSON(b []byte) error {
// type __ Foo
// var g __
// if err := Check(b, f); err != nil {
// return err
// }
// if err := json.Unmarshal(b, &g); err != nil {
// return err
// }
// *f = Foo(g)
// return nil
// }
//
func Check(b []byte, obj interface{}, omit ...string) error {
v := reflect.ValueOf(obj)
for v.Kind() == reflect.Ptr {
v = reflect.Indirect(v)
}
if v.Kind() != reflect.Struct {
return checkErr(fmt.Errorf("non-struct %s", v.Kind()))
}
m := make(map[string]*json.RawMessage)
if err := json.Unmarshal(b, &m); err != nil {
return checkErr(err)
}
for _, o := range omit {
m[o] = nil
}
var errors []string
vt := v.Type()
for i := 0; i < v.NumField(); i++ {
field := vt.Field(i)
if tag := field.Tag.Get("json"); tag == "" || strings.Contains(tag, ",omitempty") {
continue
} else if _, ok := m[tag]; !ok {
errors = append(errors, tag)
}
}
if errors != nil {
return fmt.Errorf("missing fields: %v", errors)
}
return nil
}