-
Notifications
You must be signed in to change notification settings - Fork 1
/
builder.go
94 lines (80 loc) · 2.33 KB
/
builder.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
package filterbuilder
import (
"fmt"
"go.mongodb.org/mongo-driver/bson"
"reflect"
)
// Builder is a helper struct for building query strings.
type Builder struct {
err error
output bson.M
}
// GetOutput returns the output of the builder if there are no errors
func (b *Builder) GetOutput() (bson.M, error) {
if b.err != nil {
return nil, b.err
}
return b.output, nil
}
// AddParam adds a param to the resulting query - Output.
func (b *Builder) AddParam(query *Param) *Builder {
if err := query.Validate(); err != nil {
b.err = err
return b
}
b.setParam(query.FieldName, query.Operator, query.Value)
return b
}
// setParam sets a param to the resulting query.
func (b *Builder) setParam(fieldName string, action string, value interface{}) *Builder {
if b.output[fieldName] != nil {
if _, ok := b.output[fieldName].(bson.M); ok {
b.output[fieldName].(bson.M)[action] = value
} else {
b.err = fmt.Errorf("field %s has incorrect type value %T", fieldName, b.output[fieldName])
}
} else {
b.output[fieldName] = bson.M{
action: value,
}
}
return b
}
// BuildQuery composes a query string from provided args for the specified storage type.
func (b *Builder) BuildQuery(filterStruct interface{}) (bson.M, error) {
reflectionVal := reflect.ValueOf(filterStruct)
reflectionType := reflectionVal.Type()
if reflectionVal.Kind() != reflect.Struct {
return nil, fmt.Errorf("filter must be a struct")
}
for i := 0; i < reflectionVal.NumField(); i++ {
fieldType := reflectionType.Field(i)
fieldVal := reflectionVal.Field(i)
fieldNameTag := fieldType.Tag.Get(lookup)
operatorTag := fieldType.Tag.Get(operator)
if operatorTag != Equals && operatorTag != Regex && operatorTag != GreaterThan && operatorTag != LessThan &&
operatorTag != GreaterThanOrEqualTo && operatorTag != LessThanOrEqualTo && operatorTag != In {
return nil, fmt.Errorf("invalid/unsupported action %s for field %s", operatorTag, fieldNameTag)
}
if fieldVal.IsZero() {
continue
}
b.AddParam(&Param{
FieldType: fieldVal.Kind(),
FieldName: fieldNameTag,
Operator: operatorTag,
Value: fieldVal.Interface(),
})
}
res, err := b.GetOutput()
if err != nil {
return nil, err
}
return res, nil
}
// NewBuilder returns a new Builder with empty Output.
func NewBuilder() *Builder {
return &Builder{
output: bson.M{},
}
}