/
filter.go
184 lines (161 loc) · 4.42 KB
/
filter.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
177
178
179
180
181
182
183
184
// Copyright 2022 The PipeCD Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package filedb
import (
"encoding/json"
"fmt"
"reflect"
"strings"
"unicode"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
"github.com/pipe-cd/pipecd/pkg/datastore"
)
type filterable interface {
Match(e interface{}, filters []datastore.ListFilter) (bool, error)
}
func filter(col datastore.Collection, e interface{}, filters []datastore.ListFilter) (bool, error) {
// Always pass, if there is no filter.
if len(filters) == 0 {
return true, nil
}
// If the collection implement filterable interface, use it.
fcol, ok := col.(filterable)
if ok {
return fcol.Match(e, filters)
}
pe, ok := e.(proto.Message)
if !ok {
return false, datastore.ErrUnsupported
}
// remarshal entity as map[string]interface{} struct.
m := protojson.MarshalOptions{EmitUnpopulated: true}
raw, err := m.Marshal(pe)
if err != nil {
return false, err
}
var omap map[string]interface{}
if err := json.Unmarshal(raw, &omap); err != nil {
return false, err
}
for _, filter := range filters {
field := normalizeFirstChar(filter.Field)
if strings.Contains(field, ".") {
// TODO: Handle nested field name such as SyncState.Status.
return false, datastore.ErrUnsupported
}
val, ok := omap[field]
// If the object does not contain given field name in filter, return false immidiately.
if !ok {
return false, nil
}
cmp, err := compare(val, filter.Value, filter.Operator)
if err != nil {
return false, err
}
if !cmp {
return false, nil
}
}
return true, nil
}
func compare(val, operand interface{}, op datastore.Operator) (bool, error) {
var valNum, operandNum int64
switch v := val.(type) {
case int, int8, int16, int32, int64:
valNum = reflect.ValueOf(v).Int()
case uint, uint8, uint16, uint32:
valNum = int64(reflect.ValueOf(v).Uint())
default:
if op.IsNumericOperator() {
return false, fmt.Errorf("value of type unsupported")
}
}
switch o := operand.(type) {
case int, int8, int16, int32, int64:
operandNum = reflect.ValueOf(o).Int()
case uint, uint8, uint16, uint32:
operandNum = int64(reflect.ValueOf(o).Uint())
default:
if op.IsNumericOperator() {
return false, fmt.Errorf("operand of type unsupported")
}
}
switch op {
case datastore.OperatorEqual:
return val == operand, nil
case datastore.OperatorNotEqual:
return val != operand, nil
case datastore.OperatorGreaterThan:
return valNum > operandNum, nil
case datastore.OperatorGreaterThanOrEqual:
return valNum >= operandNum, nil
case datastore.OperatorLessThan:
return valNum < operandNum, nil
case datastore.OperatorLessThanOrEqual:
return valNum <= operandNum, nil
case datastore.OperatorIn:
os, err := makeSliceOfInterfaces(operand)
if err != nil {
return false, fmt.Errorf("operand error: %w", err)
}
for _, o := range os {
if o == val {
return true, nil
}
}
return false, nil
case datastore.OperatorNotIn:
os, err := makeSliceOfInterfaces(operand)
if err != nil {
return false, fmt.Errorf("operand error: %w", err)
}
for _, o := range os {
if o == val {
return false, nil
}
}
return true, nil
case datastore.OperatorContains:
vs, err := makeSliceOfInterfaces(val)
if err != nil {
return false, fmt.Errorf("value error: %w", err)
}
for _, v := range vs {
if v == operand {
return true, nil
}
}
return false, nil
default:
return false, datastore.ErrUnsupported
}
}
func makeSliceOfInterfaces(v interface{}) ([]interface{}, error) {
rv := reflect.ValueOf(v)
if rv.Kind() != reflect.Slice && rv.Kind() != reflect.Array {
return nil, fmt.Errorf("value is not a slide or array")
}
vs := make([]interface{}, rv.Len())
for i := 0; i < rv.Len(); i++ {
vs[i] = rv.Index(i).Interface()
}
return vs, nil
}
func normalizeFirstChar(key string) string {
for i, v := range key {
return string(unicode.ToLower(v)) + key[i+1:]
}
return ""
}