-
Notifications
You must be signed in to change notification settings - Fork 4
/
operator.go
115 lines (106 loc) · 2.77 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
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
package actions
import (
"errors"
"fmt"
"github.com/teamkeel/keel/proto"
"github.com/teamkeel/keel/schema/parser"
)
// An ActionOperator gives a symbolic, machine-readable name to each
// of the comparison operators that Keel Actions work with at a CONCEPTUAL
// level.
//
// By design, the ActionOperator has no knowledge (in of itself) of how these
// might be expressed in schema's or in request inputs, or in expressions for
// example.
type ActionOperator int
const (
Unknown ActionOperator = iota
After
Before
Contains
Equals
EndsWith
GreaterThan
GreaterThanEquals
LessThan
LessThanEquals
NotContains
NotEquals
NotOneOf
OneOf
OnOrAfter
OnOrBefore
StartsWith
)
// graphQlOperatorToActionOperator converts the conditional operators that are used
// in GraphQL request input structures (such as "lessThanOrEquals") to its symbolic constant,
// machine-readable, ActionOperator value.
func graphQlOperatorToActionOperator(in string) (out ActionOperator, err error) {
switch in {
case "equals":
return Equals, nil
case "notEquals":
return NotEquals, nil
case "startsWith":
return StartsWith, nil
case "endsWith":
return EndsWith, nil
case "contains":
return Contains, nil
case "oneOf":
return OneOf, nil
case "lessThan":
return LessThan, nil
case "lessThanOrEquals":
return LessThanEquals, nil
case "greaterThan":
return GreaterThan, nil
case "greaterThanOrEquals":
return GreaterThanEquals, nil
case "before":
return Before, nil
case "after":
return After, nil
case "onOrBefore":
return OnOrBefore, nil
case "onOrAfter":
return OnOrAfter, nil
default:
return out, fmt.Errorf("unrecognized operator: %s", in)
}
}
// expressionOperatorToActionOperator converts the conditional operators that are used
// in Keel Expressions (such as ">=") to its symbolic constant,
// machine-readable, ActionOperator value.
func expressionOperatorToActionOperator(in string) (out ActionOperator, err error) {
switch in {
case parser.OperatorEquals:
return Equals, nil
case parser.OperatorNotEquals:
return NotEquals, nil
case parser.OperatorGreaterThanOrEqualTo:
return GreaterThanEquals, nil
case parser.OperatorLessThanOrEqualTo:
return LessThanEquals, nil
case parser.OperatorLessThan:
return LessThan, nil
case parser.OperatorGreaterThan:
return GreaterThan, nil
case parser.OperatorIn:
return OneOf, nil
case parser.OperatorNotIn:
return NotOneOf, nil
default:
return Unknown, fmt.Errorf("this is not a recognized conditional operator: %s", in)
}
}
func toSql(o proto.OrderDirection) (string, error) {
switch o {
case proto.OrderDirection_ORDER_DIRECTION_ASCENDING:
return "ASC", nil
case proto.OrderDirection_ORDER_DIRECTION_DECENDING:
return "DESC", nil
default:
return "", errors.New("cannot parse ORDER_DIRECTION_UNKNOWN")
}
}