-
Notifications
You must be signed in to change notification settings - Fork 1
/
param.go
39 lines (35 loc) · 1.09 KB
/
param.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
package filterbuilder
import (
"fmt"
"reflect"
)
// Param is a single field's parameter for the query.
type Param struct {
FieldType reflect.Kind
FieldName string
Operator string
Value interface{}
}
// Validate runs all the validation checks on the query.
func (q *Param) Validate() error {
if !q.IsValidOperator(q.FieldType, q.Operator) {
return fmt.Errorf("invalid/unsupported operator %s for field %s", q.Operator, q.FieldName)
}
return nil
}
// IsValidOperator returns true if the operator is valid for the field type.
func (q *Param) IsValidOperator(fieldType reflect.Kind, operator string) bool {
switch fieldType {
case reflect.String:
return operator == Regex || operator == Equals || operator == In
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return operator == Equals || operator == GreaterThan || operator == LessThan ||
operator == GreaterThanOrEqualTo || operator == LessThanOrEqualTo || operator == In
case reflect.Bool:
return operator == Equals || operator == In
case reflect.Slice:
return operator == In
default:
return false
}
}