-
Notifications
You must be signed in to change notification settings - Fork 0
/
Not.go
69 lines (60 loc) · 2.02 KB
/
Not.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
// =================================================================
//
// Copyright (C) 2019 Spatial Current, Inc. - All Rights Reserved
// Released as open source under the MIT License. See LICENSE file.
//
// =================================================================
package dfl
import (
"github.com/pkg/errors"
"github.com/spatialcurrent/go-dfl/pkg/dfl/builder"
)
// Not is a UnaryOperator that inverts the boolean value of the children Node.
type Not struct {
*UnaryOperator
}
// Dfl returns the DFL representation of this node (and its children nodes)
func (n Not) Dfl(quotes []string, pretty bool, tabs int) string {
b := builder.New(quotes, tabs).Pretty(pretty).Op("not").Right(n.Node)
if pretty {
b = b.Indent(tabs)
}
return b.Dfl()
}
// Sql returns the SQL representation of this node as a string
func (n Not) Sql(pretty bool, tabs int) string {
panic("Not is not supported yet!")
}
// Map returns a map representation of this node (and its children nodes)
func (n Not) Map() map[string]interface{} {
return map[string]interface{}{
"op": "not",
"node": n.Node.Map(),
}
}
// Compile returns a compiled version of this node.
// If the the child Node is compiled as a boolean Literal, then returns an inverse Literal Node.
// Otherwise returns a clone of this node.
func (n Not) Compile() Node {
child := n.Node.Compile()
switch child.(type) {
case Literal:
switch child.(Literal).Value.(type) {
case bool:
return Literal{Value: !child.(Literal).Value.(bool)}
}
}
return Not{&UnaryOperator{Node: child}}
}
// Evaluate evaluates this node within a context and returns the bool result, and error if any.
func (n Not) Evaluate(vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, interface{}, error) {
vars, v, err := n.Node.Evaluate(vars, ctx, funcs, quotes)
if err != nil {
return vars, false, err
}
switch v.(type) {
case bool:
return vars, !(v.(bool)), nil
}
return vars, false, errors.New("Error evaluating expression " + n.Dfl(quotes, false, 0))
}