-
-
Notifications
You must be signed in to change notification settings - Fork 384
/
match.go
141 lines (128 loc) · 3.4 KB
/
match.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
package pkg
import (
"encoding/json"
"errors"
"reflect"
"go.uber.org/zap"
)
// unmarshallJson returns unmarshalled JSON object.
func unmarshallJson(s string, log *zap.Logger) (interface{}, error) {
var result interface{}
if err := json.Unmarshal([]byte(s), &result); err != nil {
log.Error("cannot convert json string into json object", zap.Error(err))
return nil, err
} else {
return result, nil
}
}
func arrayToMap(arr []string) map[string]bool {
res := map[string]bool{}
for i := range arr {
res[arr[i]] = true
}
return res
}
func Match(exp, act string, noise []string, log *zap.Logger) (string, string, bool, error) {
noiseMap := arrayToMap(noise)
expected, err := unmarshallJson(exp, log)
if err != nil {
return exp, act, false, err
}
actual, err := unmarshallJson(act, log)
if err != nil {
return exp, act, false, err
}
if reflect.TypeOf(expected) != reflect.TypeOf(actual) {
return exp, act, false, nil
}
//tmp := mapClone(noiseMap)
////expected = removeNoise(expected, tmp)
//
//tmp = mapClone(noiseMap)
//actual = removeNoise(actual, tmp)
match, err := jsonMatch("", expected, actual, noiseMap)
if err != nil {
return exp, act, false, err
}
cleanExp, err := json.Marshal(expected)
if err != nil {
return exp, act, false, err
}
cleanAct, err := json.Marshal(actual)
if err != nil {
return exp, act, false, err
}
return string(cleanExp), string(cleanAct), match, nil
}
// jsonMatch returns true if expected and actual JSON objects matches(are equal).
func jsonMatch(key string, expected, actual interface{}, noiseMap map[string]bool) (bool, error) {
if reflect.TypeOf(expected) != reflect.TypeOf(actual) {
return false, errors.New("type not matched ")
}
if expected == nil && actual == nil {
return true, nil
}
x := reflect.ValueOf(expected)
prefix := ""
if key != "" {
prefix = key + "."
}
switch x.Kind() {
case reflect.Float64, reflect.String, reflect.Bool:
if expected != actual && !noiseMap[key] {
return false, nil
}
case reflect.Map:
expMap := expected.(map[string]interface{})
actMap := actual.(map[string]interface{})
for k, v := range expMap {
val, ok := actMap[k]
if !ok {
return false, nil
}
if x, er := jsonMatch(prefix+k, v, val, noiseMap); !x || er != nil {
return false, nil
}
// remove the noisy key from both expected and actual JSON.
if noiseMap[prefix+k] {
delete(expMap, prefix+k)
delete(actMap, k)
continue
}
}
// checks if there is a key which is not present in expMap but present in actMap.
for k := range actMap {
_, ok := expMap[k]
if !ok {
return false, nil
}
}
case reflect.Slice:
if noiseMap[key] {
return true, nil
}
expSlice := reflect.ValueOf(expected)
actSlice := reflect.ValueOf(actual)
if expSlice.Len() != actSlice.Len() {
return false, nil
}
isMatched := true
for i := 0; i < expSlice.Len(); i++ {
isMatchedElement := false
for j := 0; j < actSlice.Len(); j++ {
if x, err := jsonMatch(key, expSlice.Index(i).Interface(), actSlice.Index(j).Interface(), noiseMap); err == nil && x {
isMatchedElement = true
break
}
}
isMatched = isMatchedElement && isMatched
// if x, err := jsonMatch(expSlice.Index(i).Interface(), actSlice.Index(i).Interface()); err != nil || !x {
// return false, nil
// }
}
return isMatched, nil
default:
return false, errors.New("type not registered for json")
}
return true, nil
}