/
operator.go
54 lines (47 loc) · 1.33 KB
/
operator.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
package search
import "github.com/tidwall/gjson"
type Operator string
const (
// Or Operator
//
// For example, a query value of capital of Hungary is interpreted as
// capital OR of OR Hungary.
Or Operator = "OR"
// And Operator
//
// For example, a query value of capital of Hungary is interpreted as capital
// AND of AND Hungary.
And Operator = "AND"
)
// WithOperator is a query with the operator param
//
// Boolean logic used to interpret text in the query value.
type WithOperator interface {
// Operator is the boolean logic used to interpret text in the query value.
// Defaults to Or
Operator() Operator
// SetOperator sets the Operator to v
SetOperator(v Operator)
}
// OperatorParam is a query mixin that adds the operator param
type OperatorParam struct {
OperatorValue *Operator `json:"operator,omitempty" bson:"operator,omitempty"`
}
// Operator is the boolean logic used to interpret text in the query value.
// Defaults to Or
func (o OperatorParam) Operator() Operator {
if o.OperatorValue != nil {
return *o.OperatorValue
}
return Or
}
// SetOperator sets the Operator to v
func (o *OperatorParam) SetOperator(v Operator) {
o.OperatorValue = &v
}
func unmarshalOperatorParam(value gjson.Result, target interface{}) error {
if a, ok := target.(WithOperator); ok {
a.SetOperator(Operator(value.Str))
}
return nil
}