-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmatch.go
176 lines (144 loc) · 4.28 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package rio
import (
"context"
"encoding/json"
"fmt"
"reflect"
"strings"
"github.com/hungdv136/rio/internal/log"
"github.com/hungdv136/rio/internal/util"
)
var matchingFunctions = map[OperatorName]matchingFunc{
OpEqualTo: executeEqualToOperator,
OpContaining: executeContainingOperator,
OpNotContaining: executeNotContainingOperator,
OpRegex: executeRegexOperator,
OpStartWith: executeStartWithOperator,
OpEndWith: executeEndWithOperator,
OpLength: executeLengthOperator,
OpEmpty: executeEmptyOperator,
OpNotEmpty: executeNotEmptyOperator,
}
type matchingFunc func(ctx context.Context, op Operator, value interface{}) (bool, error)
// Match compares input value with predefined operator
func Match(ctx context.Context, op Operator, value interface{}) (bool, error) {
matchFunc, ok := matchingFunctions[op.Name]
if !ok {
err := fmt.Errorf("unsupported operator %s", op.Name)
log.Error(ctx, err)
return false, err
}
return matchFunc(ctx, op, value)
}
func executeEqualToOperator(ctx context.Context, op Operator, value interface{}) (bool, error) {
switch opVal := op.Value.(type) {
case json.Number:
vFloat64, err := opVal.Float64()
if err != nil {
log.Error(ctx, err)
return false, err
}
if val, ok := getFloat64(value); ok {
return vFloat64 == val, nil
}
case float64, float32:
opVal64, ok := getFloat64(opVal)
if !ok {
return false, nil
}
if val, ok := getFloat64(value); ok {
return val == opVal64, nil
}
case int, int16, int64, int32, uint, uint16, uint32, uint64:
opVal64, ok := getInt64(opVal)
if !ok {
return false, nil
}
if val, ok := getInt64(value); ok {
return opVal64 == val, nil
}
case string:
if val, ok := value.(string); ok {
return opVal == val, nil
}
return opVal == util.ToString(value), nil
}
return reflect.DeepEqual(op.Value, value), nil
}
func executeContainingOperator(ctx context.Context, op Operator, value interface{}) (bool, error) {
ok, found := containsElement(value, op.Value)
if !ok {
err := fmt.Errorf("unsupported data type - %T", value)
log.Error(ctx, err)
return false, err
}
return found, nil
}
func executeNotContainingOperator(ctx context.Context, op Operator, value interface{}) (bool, error) {
contain, err := executeContainingOperator(ctx, op, value)
return !contain, err
}
func executeRegexOperator(ctx context.Context, op Operator, value interface{}) (bool, error) {
s, ok := op.Value.(string)
if !ok {
err := fmt.Errorf("unsupported data type %s - %T", op.String(), value)
log.Error(ctx, err)
return false, err
}
actualVal, ok := value.(string)
if !ok {
err := fmt.Errorf("incompatible string type %s - %T", op.String(), value)
log.Error(ctx, err)
return false, err
}
r, err := defaultRegexCompiler.compile(ctx, s)
if err != nil {
return false, err
}
return r.MatchString(actualVal), nil
}
func executeStartWithOperator(ctx context.Context, op Operator, value interface{}) (bool, error) {
s, ok := op.Value.(string)
if !ok {
err := fmt.Errorf("unsupported data type %s - %T", op.String(), value)
log.Error(ctx, err)
return false, err
}
actualVal, ok := value.(string)
if !ok {
err := fmt.Errorf("incompatible string type %s - %T", op.String(), value)
log.Error(ctx, err)
return false, err
}
return strings.HasPrefix(actualVal, s), nil
}
func executeEndWithOperator(ctx context.Context, op Operator, value interface{}) (bool, error) {
s, ok := op.Value.(string)
if !ok {
err := fmt.Errorf("unsupported data type %s - %T", op.String(), value)
log.Error(ctx, err)
return false, err
}
actualVal, ok := value.(string)
if !ok {
err := fmt.Errorf("incompatible string type %s - %T", op.String(), value)
log.Error(ctx, err)
return false, err
}
return strings.HasSuffix(actualVal, s), nil
}
func executeLengthOperator(ctx context.Context, op Operator, value interface{}) (bool, error) {
l, ok := getLen(value)
if !ok {
err := fmt.Errorf("unsupported data type %T", value)
log.Error(ctx, err)
return false, err
}
return l == op.Value.(int), nil
}
func executeEmptyOperator(_ context.Context, _ Operator, value interface{}) (bool, error) {
return isEmpty(value), nil
}
func executeNotEmptyOperator(_ context.Context, _ Operator, value interface{}) (bool, error) {
return !isEmpty(value), nil
}