aboutsummaryrefslogtreecommitdiff
path: root/model/condition.go
blob: 0b558b4dbc77d0a3e8a27c363382b1759fe987eb (plain) (blame)
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
package model

// Compare current node with another node
func (n *Node) Compare(op Operand, node *Node) bool {
	switch op {
	case OpEq:
		return n.Value() == node.Value()
	case OpNeq:
		return n.Value() != node.Value()
	case OpLess:
		return less(n, node)
	case OpGt:
		return less(node, n)
	case OpLessEq:
		return less(n, node) || n.Value() == node.Value()
	case OpGtEq:
		return less(node, n) || n.Value() == node.Value()
	case OpIn:
		if n.Type != ArrayNode {
			return false
		}
		for _, v := range n.arrayValue {
			if v.Value() == node.Value() {
				return true
			}
		}
	}
	return false
}

func less(n1 *Node, n2 *Node) bool {
	if n1.Type != n2.Type {
		return false
	}
	switch n1.Type {
	case NumberNode:
		return n1.numberValue < n2.numberValue
	case StringNode:
		return n1.stringValue < n2.stringValue
	default:
		return false
	}
}

type Operand int

const (
	OpEq Operand = iota
	OpNeq
	OpLess
	OpLessEq
	OpGt
	OpGtEq
	OpIn
)