-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpression.go
More file actions
125 lines (100 loc) · 2.92 KB
/
expression.go
File metadata and controls
125 lines (100 loc) · 2.92 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package main
import (
"fmt"
"strings"
"github.com/oliveagle/jsonpath"
)
type Expression struct {
Action string
Left, Right *Operand
Operator Operator
IsGroupEnabled bool `json:"group"`
}
type Type string
type Operand struct {
Type Type `json:"dtype"`
Value interface{}
FieldName string `json:"field"`
}
func (t Type) IsSliceType() bool {
return strings.Contains(string(t), "list")
}
func (t Type) IsInt() bool {
return strings.Contains(string(t), "int")
}
func (t Type) IsString() bool {
return strings.Contains(string(t), "string")
}
// SetMissingFields lookups jsonData structure to get needed values.
func (e *Expression) SetMissingFields(jsonData interface{}) error {
if e.Left.FieldName != "" {
err := e.Left.setMissingField(jsonData)
if err != nil {
return err
}
}
if e.Right.FieldName != "" {
err := e.Right.setMissingField(jsonData)
if err != nil {
return err
}
}
return nil
}
func (e Expression) toString() (string, error) {
err := e.checkOperandsTypes()
if err != nil {
return "", err
}
if e.Action == "in" {
return fmt.Sprintf("%s %s ", in(e.Left.Value, e.Right.Value.([]interface{})), e.Operator), nil
}
return fmt.Sprintf("(%v %s %v) %s ", e.Left.Value, e.Action, e.Right.Value, e.Operator), nil
}
func (e Expression) checkOperandsTypes() error {
// string and int couldn't be compared
if e.Left.Type.IsString() && e.Right.Type.IsInt() ||
e.Left.Type.IsInt() && e.Right.Type.IsString() {
return fmt.Errorf("bad operand's types provided: %s and %s", e.Left.Type, e.Right.Type)
}
// check that action suits provided types.
valid := isActionValid(e.Action, e.Left.Type, e.Right.Type)
if !valid {
return fmt.Errorf("action %s couldn't be performed on types: %s and %s", e.Action, e.Left.Type, e.Right.Type)
}
return nil
}
// is actionValid checks if action could be performed on the specified types.
// leftOperandType and rightOperandType are types of expression operands.
// Assume, that they both contain string or both contain int.
func isActionValid(action string, leftOperandType, rightOperandType Type) bool {
// left operand has a slice type. There are no available actions for this case.
if leftOperandType.IsSliceType() {
return false
}
if rightOperandType.IsSliceType() {
return action == "in"
}
switch leftOperandType {
case "int":
return action == "<" || action == "<=" || action == "==" || action == ">=" || action == ">"
case "string":
return action == "=="
default:
// unknown type
return false
}
}
func (o *Operand) setMissingField(jsonData interface{}) error {
o.prepareFieldNameForLookup()
res, err := jsonpath.JsonPathLookup(jsonData, o.FieldName)
if err != nil {
return err
}
o.Value = res
return nil
}
// prepareFieldNameForLookup sets Operand's FieldName for the format of the jsonpath lookup library.
func (o *Operand) prepareFieldNameForLookup() {
o.FieldName = "$." + strings.ReplaceAll(o.FieldName, "#", "[:]")
}