-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
65 lines (54 loc) · 1.29 KB
/
main.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
package main
import (
"encoding/json"
"fmt"
schema "github.com/Kangaroux/go-map-schema"
)
// Address is the model we are using as nested for Person.
type Address struct {
Country string `json:"country"`
City string `json:"city"`
AddressLine string `json:"address_line"`
}
// Person is the model we are using.
type Person struct {
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Age int `json:"age"`
Address Address `json:"address"`
}
// Response is used to generate a JSON response.
type Response struct {
Errors error `json:"errors,omitempty"`
OK bool `json:"ok"`
}
func main() {
input := `{
"first_name": "Jessie",
"age": "26",
"address": {
"country": "US",
"city": null
}
}`
m := make(map[string]interface{})
p := Person{}
// Load the JSON into a map.
if err := json.Unmarshal([]byte(input), &m); err != nil {
panic(err)
}
// Check the types.
r, err := schema.CompareMapToStruct(&p, m, nil)
if err != nil {
panic(err)
}
typeErrors := r.Errors()
resp := &Response{
OK: typeErrors == nil,
Errors: typeErrors,
}
respJson, _ := json.MarshalIndent(resp, "", " ")
fmt.Println("missing fields: ", r.MissingFields)
fmt.Println("mismatched fields:", r.MismatchedFields)
fmt.Println(string(respJson))
}