-
Notifications
You must be signed in to change notification settings - Fork 13
/
callexpr.go
83 lines (72 loc) · 1.93 KB
/
callexpr.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
package filter
import (
"errors"
"strings"
)
// CallType - Call type for filter condition
type CallType int
// Constants for Call type
const (
GETVALUE CallType = iota
MATCHREGEX
CONTAINS
EXISTS
ANY
)
var callTypeMap = map[string]CallType{
"any": ANY,
"exists": EXISTS,
"contains": CONTAINS,
"matchregex": MATCHREGEX,
}
// CallExpr - Interface for call expression in filter condition
type CallExpr interface {
GetType() CallType
Execute(data Data) (interface{}, error)
String() string
}
// NewCallExpr - Factory method for creating CallExpr
func newCallExpr(callType CallType, filterType, name string, arguments []interface{}) (callExpr CallExpr, err error) {
if (callType == ANY || callType == EXISTS) && len(arguments) != 0 {
return nil, errors.New("Syntax Error, unrecognized argument(s)")
}
if callType == CONTAINS || callType == MATCHREGEX {
if len(arguments) == 0 {
return nil, errors.New("Syntax Error, missing argument")
}
if len(arguments) != 1 {
return nil, errors.New("Syntax Error, unrecognized argument(s)")
}
}
callTypeSupported := false
for _, supportedCallType := range supportedExpr {
if supportedCallType == callType {
callTypeSupported = true
break
}
}
if !callTypeSupported {
return nil, errors.New("Syntax Error, unsupported condition")
}
switch callType {
case GETVALUE:
callExpr = newValueExpr(filterType, name)
case EXISTS:
callExpr = newExistsExpr(filterType, name)
case ANY:
callExpr = newAnyExpr(filterType)
case CONTAINS:
callExpr = newContainsExpr(filterType, name, arguments[0].(string))
case MATCHREGEX:
callExpr, err = newMatchRegExExpr(filterType, name, arguments[0].(string))
}
return
}
// GetCallType - Converts a string to its corresponding call type.
func GetCallType(callTypeString string) (callType CallType, err error) {
callType, ok := callTypeMap[strings.ToLower(callTypeString)]
if !ok {
err = errors.New("Unsupported call")
}
return
}