-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathobject.go
52 lines (47 loc) · 1.22 KB
/
object.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
package jsonassert
import (
"encoding/json"
"strings"
)
func (a *Asserter) checkObject(path string, act, exp map[string]interface{}) {
a.tt.Helper()
if len(act) != len(exp) {
a.tt.Errorf("expected %d keys at '%s' but got %d keys", len(exp), path, len(act))
}
if unique := difference(act, exp); len(unique) != 0 {
a.tt.Errorf("unexpected object key(s) %+v found at '%s'", serialize(unique), path)
}
if unique := difference(exp, act); len(unique) != 0 {
a.tt.Errorf("expected object key(s) %+v missing at '%s'", serialize(unique), path)
}
for key := range act {
if contains(exp, key) {
a.pathassertf(path+"."+key, serialize(act[key]), serialize(exp[key]))
}
}
}
func difference(act, exp map[string]interface{}) []string {
unique := []string{}
for key := range act {
if !contains(exp, key) {
unique = append(unique, key)
}
}
return unique
}
func contains(container map[string]interface{}, candidate string) bool {
for key := range container {
if key == candidate {
return true
}
}
return false
}
func extractObject(s string) (map[string]interface{}, bool) {
s = strings.TrimSpace(s)
if s == "" {
return nil, false
}
var arr map[string]interface{}
return arr, json.Unmarshal([]byte(s), &arr) == nil
}