-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
96 lines (85 loc) · 1.81 KB
/
node.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
package filter
import (
"errors"
"fmt"
)
const (
compareEq uint32 = iota
compareNotEq
compareGt
compareGte
compareLt
compareLte
compareIn
compareNotIn
logicAnd
logicOr
logicNot
)
type Node interface {
Accept(visitor Visitor)
String() string
}
type CompareNode struct {
Field string
Value interface{}
Operator uint32
}
func (n *CompareNode) Accept(visitor Visitor) {
visitor.Visit(n)
}
func (n *CompareNode) String() string {
return fmt.Sprintf("{field:%s,value:%v,operator:%d}", n.Field, n.Value, n.Operator)
}
func NewCompareNode(field string, value interface{}, operator uint32) (*CompareNode, error) {
if field == "" {
return nil, errors.New("Fieldname may not be empty")
}
switch operator {
case compareEq, compareNotEq, compareGt, compareGte, compareLt, compareLte, compareIn, compareNotIn:
return &CompareNode{
Field: field,
Value: value,
Operator: operator}, nil
}
return nil, fmt.Errorf("Operator %s is not known", operator)
}
type LogicNode struct {
LeftNode Node
RightNode Node
Operator uint32
}
func NewLogicNode(left, right Node, operator uint32) (*LogicNode, error) {
if operator != logicAnd && operator != logicOr && operator != logicNot {
return nil, errors.New("Unkown operator used")
}
return &LogicNode{
LeftNode: left,
RightNode: right,
Operator: operator,
}, nil
}
func (n *LogicNode) String() string {
node := "{\n\tleft:"
if n.LeftNode != nil {
node += n.LeftNode.String()
} else {
node += "nil"
}
node += fmt.Sprintf("\n\top:%d\n\tright:", n.Operator)
if n.RightNode != nil {
node += n.RightNode.String()
} else {
node += "nil"
}
return node + "\n}"
}
func (n *LogicNode) Accept(visitor Visitor) {
if n.LeftNode != nil {
n.LeftNode.Accept(visitor)
}
visitor.Visit(n)
if n.RightNode != nil {
n.RightNode.Accept(visitor)
}
}