This repository has been archived by the owner on Nov 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter-field.go
288 lines (245 loc) · 7.87 KB
/
filter-field.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package filters
import (
"fmt"
aerrors "github.com/neuronlabs/neuron/errors"
"github.com/neuronlabs/neuron/i18n"
"github.com/neuronlabs/neuron/internal"
"github.com/neuronlabs/neuron/internal/models"
"golang.org/x/text/language"
"reflect"
"strings"
)
// FilterField is a field that contains information about filters
type FilterField struct {
structField *models.StructField
// Key is used for the map type filters as a nested argument
key string
// AttrFilters are the filter values for given attribute FilterField
values []*OpValuePair
// if given filterField is a relationship type it should be filter by it's
// subfields (for given relation type).
// Relationships are the filter values for given relationship FilterField
nested []*FilterField
raw string
}
// ToRaw returns raw string filter
func (f *FilterField) Raw() string {
return ""
}
// Key returns the filter key if set
func (f *FilterField) Key() string {
return f.key
}
// AddValues adds provided values to the filter values
func (f *FilterField) AddValues(values ...*OpValuePair) {
f.values = append(f.values, values...)
}
// StructField returns the filter's StructField
func (f *FilterField) StructField() *models.StructField {
return f.structField
}
// NestedFields returns nested fields for given filter
func (f *FilterField) NestedFields() []*FilterField {
return f.nested
}
// Values return filter values
func (f *FilterField) Values() []*OpValuePair {
return f.values
}
// NewFilter creates the filterField for given stuct field and values
func NewFilter(s *models.StructField, values ...*OpValuePair) *FilterField {
return &FilterField{structField: s, values: values}
}
// FilterAppendvalues adds the values to the given filter field
func FilterAppendValues(f *FilterField, values ...*OpValuePair) {
f.values = append(f.values, values...)
}
// FilterOpValuePairs returns the values of the provided filter field.
func FilterOpValuePairs(f *FilterField) []*OpValuePair {
return f.values
}
// CopyFilter copies given FilterField with it's values
func CopyFilter(f *FilterField) *FilterField {
return f.copy()
}
// GetOrCreateNestedFilter gets the filter field for given field
// If the field is a key returns the filter by key
func GetOrCreateNestedFilter(f *FilterField, field interface{}) *FilterField {
return f.getOrCreateNested(field)
}
func NestedFields(f *FilterField) []*FilterField {
return f.nested
}
// AddNestedField adds the nested field value
func (f *FilterField) AddNestedField(nested *FilterField) {
addNestedField(f, nested)
}
// AddNestedField adds nested filterfield for the filter 'f'
func AddNestedField(f, nested *FilterField) {
addNestedField(f, nested)
}
// AddsNestedField for given FilterField
func addNestedField(f, nested *FilterField) {
// check if there already exists a nested filter
for _, nf := range f.nested {
if nf.structField == nested.structField {
// Append the values to the given filter
nf.values = append(nf.values, nested.values...)
return
}
}
f.nested = append(f.nested, nested)
return
}
// SetValues sets the filter values for provided field, it's operator and possible i18n Support
func (f *FilterField) SetValues(
values []string,
op *Operator,
sup *i18n.Support,
) (errObj *aerrors.ApiError) {
var (
er error
opInvalid = func() {
errObj = aerrors.ErrUnsupportedQueryParameter.Copy()
errObj.Detail = fmt.Sprintf("The filter operator: '%s' is not supported for the field: '%s'.", op, f.structField.ApiName())
}
)
t := f.structField.GetDereferencedType()
// create new FilterValue
fv := new(OpValuePair)
fv.operator = op
// Add and check all values for given field type
switch f.structField.FieldKind() {
case models.KindPrimary, models.KindForeignKey:
switch t.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
if op.Id > OpLessEqual.Id {
opInvalid()
return
}
case reflect.String:
if !op.isBasic() {
opInvalid()
return
}
}
for _, value := range values {
fieldValue := reflect.New(t).Elem()
er = setPrimaryField(value, fieldValue)
if er != nil {
errObj = aerrors.ErrInvalidQueryParameter.Copy()
errObj.Detail = fmt.Sprintf("Invalid filter value for primary field in collection: '%s'. %s. ", f.structField.Struct().Collection(), er)
return
}
fv.Values = append(fv.Values, fieldValue.Interface())
}
f.values = append(f.values, fv)
// if it is of integer type check which kind of it
case models.KindAttribute:
switch t.Kind() {
case reflect.String:
default:
if op.isStringOnly() {
opInvalid()
return
}
}
if f.structField.IsLanguage() {
switch op {
case OpIn, OpEqual, OpNotIn, OpNotEqual:
if sup != nil {
for i, value := range values {
tag, err := language.Parse(value)
if err != nil {
switch v := err.(type) {
case language.ValueError:
errObj = aerrors.ErrLanguageNotAcceptable.Copy()
errObj.Detail = fmt.Sprintf("The value: '%s' for the '%s' filter field within the collection '%s', is not a valid language. Cannot recognize subfield: '%s'.", value, f.structField.ApiName(),
f.structField.Struct().Collection(), v.Subtag())
return
default:
errObj = aerrors.ErrInvalidQueryParameter.Copy()
errObj.Detail = fmt.Sprintf("The value: '%v' for the '%s' filter field within the collection '%s' is not syntetatically valid.", value, f.structField.ApiName(), f.structField.Struct().Collection())
return
}
}
if op == OpEqual {
var confidence language.Confidence
tag, _, confidence = sup.Matcher.Match(tag)
if confidence <= language.Low {
errObj = aerrors.ErrLanguageNotAcceptable.Copy()
errObj.Detail = fmt.Sprintf("The value: '%s' for the '%s' filter field within the collection '%s' does not match any supported langauges. The server supports following langauges: %s", value, f.structField.ApiName(), f.structField.Struct().Collection(),
strings.Join(sup.PrettyLanguages(), internal.AnnotationSeperator),
)
return
}
}
b, _ := tag.Base()
values[i] = b.String()
}
}
default:
errObj = aerrors.ErrInvalidQueryParameter.Copy()
errObj.Detail = fmt.Sprintf("Provided operator: '%s' for the language field is not acceptable", op.String())
return
}
}
for _, value := range values {
fieldValue := reflect.New(t).Elem()
er = setAttributeField(value, fieldValue)
if er != nil {
errObj = aerrors.ErrInvalidQueryParameter.Copy()
errObj.Detail = fmt.Sprintf("Invalid filter value for the attribute field: '%s' for collection: '%s'. %s.", f.structField.ApiName(), f.structField.Struct().Collection(), er)
return
}
fv.Values = append(fv.Values, fieldValue.Interface())
}
f.values = append(f.values, fv)
default:
}
return
}
func (f *FilterField) copy() *FilterField {
dst := &FilterField{structField: f.structField}
if len(f.values) != 0 {
dst.values = make([]*OpValuePair, len(f.values))
for i, value := range f.values {
dst.values[i] = value.copy()
}
}
if len(f.nested) != 0 {
dst.nested = make([]*FilterField, len(f.nested))
for i, value := range f.nested {
dst.nested[i] = value.copy()
}
}
return dst
}
// getNested gets the nested field by it's key or structfield
func (f *FilterField) getOrCreateNested(field interface{}) *FilterField {
switch t := field.(type) {
case string:
for _, nested := range f.nested {
if nested.key == t {
return nested
}
}
nested := &FilterField{key: t}
f.nested = append(f.nested, nested)
return nested
case *models.StructField:
for _, nested := range f.nested {
if nested.structField == t {
return nested
}
}
nested := &FilterField{structField: t}
f.nested = append(f.nested, nested)
return nested
default:
return nil
// invalid field type
}
return nil
}