-
Notifications
You must be signed in to change notification settings - Fork 0
/
Or.go
70 lines (63 loc) · 2.01 KB
/
Or.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
// =================================================================
//
// Copyright (C) 2019 Spatial Current, Inc. - All Rights Reserved
// Released as open source under the MIT License. See LICENSE file.
//
// =================================================================
package dfl
// Or is a BinaryOperator which represents the logical boolean OR operation of left and right values.
type Or struct {
*BinaryOperator
}
func (o Or) Dfl(quotes []string, pretty bool, tabs int) string {
return o.BinaryOperator.Dfl("or", quotes, pretty, tabs)
}
// Sql returns the SQL representation of this node as a string
func (o Or) Sql(pretty bool, tabs int) string {
return o.BinaryOperator.Sql("or", pretty, tabs)
}
func (o Or) Map() map[string]interface{} {
return o.BinaryOperator.Map("or", o.Left, o.Right)
}
// Compile returns a compiled version of this node.
// If the left value and right value are both compiled as Literals, then returns the logical boolean AND operation of the left and right value.
// Otherwise, returns a clone.
func (o Or) Compile() Node {
left := o.Left.Compile()
right := o.Right.Compile()
switch left.(type) {
case Literal:
switch right.(type) {
case Literal:
switch left.(Literal).Value.(type) {
case bool:
switch right.(Literal).Value.(type) {
case bool:
return Literal{Value: (left.(Literal).Value.(bool) || right.(Literal).Value.(bool))}
}
}
}
}
return Or{&BinaryOperator{Left: left, Right: right}}
}
func (o Or) Evaluate(vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, interface{}, error) {
vars, lv, err := o.Left.Evaluate(vars, ctx, funcs, quotes)
if err != nil {
return vars, false, err
}
switch lv.(type) {
case bool:
if lv.(bool) {
return vars, true, nil
}
vars, rv, err := o.Right.Evaluate(vars, ctx, funcs, quotes)
if err != nil {
return vars, false, err
}
switch rv.(type) {
case bool:
return vars, rv.(bool), nil
}
}
return vars, false, &ErrorEvaluate{Node: o, Quotes: quotes}
}