/
messagematch.go
155 lines (150 loc) · 3.59 KB
/
messagematch.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
package messagematch
import (
// "fmt"
//"github.com/kr/pretty"
// "reflect"
sameish "github.com/dana/go-value-sameish"
)
func Match(message map[string]interface{}, match map[string]interface{}) (bool, error) {
return matchMapMap(message, match)
}
func matchArrayArray(message []interface{}, match []interface{}) (bool, error) {
if len(match) > len(message) {
return false, nil
}
for index, value := range match {
switch value.(type) {
case string:
switch message[index].(type) {
case string:
didMatch := sameish.Sameish(message[index].(string), value.(string))
if !didMatch {
return didMatch, nil
}
default:
return false, nil
}
case int:
switch message[index].(type) {
case int:
didMatch := sameish.Sameish(message[index].(int), value.(int))
if !didMatch {
return didMatch, nil
}
default:
return false, nil
}
}
}
return true, nil
}
func matchArrayInt(message []interface{}, match int) (bool, error) {
for _, value := range message {
switch value.(type) {
case int:
didMatch := sameish.Sameish(value.(int), match)
if didMatch {
return true, nil
}
case string:
didMatch := sameish.Sameish(value.(string), match)
if didMatch {
return true, nil
}
default:
return false, nil
}
}
return false, nil
}
func matchMapMap(message map[string]interface{}, match map[string]interface{}) (bool, error) {
for key, value := range match {
switch value.(type) {
case string:
switch message[key].(type) {
case string:
if _, ok := message[key]; ok {
didMatch := sameish.Sameish(message[key].(string), value.(string))
if !didMatch {
return didMatch, nil
}
}
default:
return false, nil
}
case int:
switch message[key].(type) {
case []interface{}:
if _, ok := message[key]; ok {
didMatch, _ := matchArrayInt(message[key].([]interface{}), value.(int))
if !didMatch {
return didMatch, nil
}
}
case int:
if _, ok := message[key]; ok {
didMatch := sameish.Sameish(message[key].(int), value.(int))
if !didMatch {
return didMatch, nil
}
}
/* case float64: //this one is not working, maybe a problem in the sameish lib
if _, ok := message[key]; ok {
pretty.Println(message[key].(float64), value.(int))
didMatch := sameish.Sameish(message[key].(float64), value.(int))
if !didMatch {
return didMatch, nil
}
} */
case string:
if _, ok := message[key]; ok {
didMatch := sameish.Sameish(message[key].(string), value.(int))
if !didMatch {
return didMatch, nil
}
}
default:
return false, nil
}
case float64:
switch message[key].(type) {
case float64:
if _, ok := message[key]; ok {
didMatch := sameish.Sameish(message[key].(float64), value.(float64))
if !didMatch {
return didMatch, nil
}
}
default:
return false, nil
}
case []interface{}:
switch message[key].(type) {
case []interface{}:
if _, ok := message[key]; ok {
didMatch, _ := matchArrayArray(message[key].([]interface{}), value.([]interface{}))
if !didMatch {
return didMatch, nil
}
}
default:
return false, nil
}
case map[string]interface{}:
switch message[key].(type) {
case map[string]interface{}:
if _, ok := message[key]; ok {
didMatch, _ := matchMapMap(message[key].(map[string]interface{}), value.(map[string]interface{}))
if !didMatch {
return didMatch, nil
}
}
default:
return false, nil
}
default:
return false, nil
}
}
return true, nil
}