-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexpression.go
More file actions
182 lines (153 loc) · 3.47 KB
/
Copy pathexpression.go
File metadata and controls
182 lines (153 loc) · 3.47 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package goparser
import (
"encoding/json"
jsoniter "github.com/json-iterator/go"
"github.com/pkg/errors"
)
// Jsoniter 别名
var Jsoniter = jsoniter.ConfigCompatibleWithStandardLibrary
// ChildrenItem ...
type ChildrenItem struct {
Op string `json:"op"`
Field string `json:"field"`
Value interface{} `json:"value"`
}
// 逻辑运算符定义
const (
LogicAnd = "&&" // 逻辑且
LogicOr = "||" // 逻辑或
LogicNot = "!" // 逻辑非
LogicAndName = "AND" // 逻辑且标识
LogicOrName = "OR" // 逻辑或标识
LogicNotName = "NOT" // 逻辑非标识
)
// LogicMaps 逻辑运算符标识转换字典
var LogicMaps = map[string]string{
LogicAndName: LogicAnd,
LogicOrName: LogicOr,
LogicNotName: LogicNot,
}
// Expression 基于JSON生成表达式字符串
func Expression(exp map[string]interface{}) (string, error) {
if len(exp) < 2 {
return "", errors.New("invalid params")
}
child, ok := exp["children"]
if !ok {
return "", errors.New("invalid params filed[child]")
}
childs, ok2 := child.([]interface{})
if !ok2 {
return "", errors.New("childs convert array fail")
}
var con string = ""
if v, has := LogicMaps[exp["connector"].(string)]; has {
con = v
}
res := ""
for _, item := range childs {
val, ok := item.(map[string]interface{})
if !ok {
return "", errors.New("childs convert array fail")
}
ibyte, err := Jsoniter.Marshal(item)
if err != nil {
return "", err
}
if _, ok := val["children"]; ok {
ss, err := Expression(val)
if err != nil {
return "", err
}
if con == LogicNot {
res = LogicNot + ss
} else {
if len(res) == 0 {
res = ss
} else {
res = StringBuilder("(", res, " ", con, " ", ss, ")")
}
}
} else {
var child ChildrenItem
if err := json.Unmarshal(ibyte, &child); err != nil {
return "", err
}
t := convert(child)
if len(res) == 0 {
res = t
} else {
res = StringBuilder("(", res, " ", con, " ", t, ")")
}
}
}
return res, nil
}
// ExportFields 导出表达中参数名
func ExportFields(exp map[string]interface{}) ([]string, error) {
res := make([]string, 0)
if len(exp) < 2 {
return res, errors.New("invalid params")
}
child, ok := exp["children"]
if !ok {
return res, errors.New("invalid params filed[child]")
}
childs, ok2 := child.([]interface{})
if !ok2 {
return res, errors.New("childs convert array fail")
}
for _, item := range childs {
val, ok := item.(map[string]interface{})
if !ok {
return res, errors.New("childs convert array fail")
}
ibyte, err := Jsoniter.Marshal(item)
if err != nil {
return res, err
}
if _, ok := val["children"]; ok {
ss, err := ExportFields(val)
if err != nil {
return res, err
}
for _, v := range ss {
if !InArray(v, res) {
res = append(res, v)
}
}
} else {
var child ChildrenItem
if err := json.Unmarshal(ibyte, &child); err != nil {
return res, err
}
if !InArray(child.Field, res) {
res = append(res, child.Field)
}
}
}
return res, nil
}
// convert 结构体转换为表达式
func convert(item ChildrenItem) string {
opMap := map[string]string{
"NE": "!=",
"EQ": "==",
"GT": ">",
"LT": "<",
"GE": ">=",
"LE": "<=",
"ADD": "+",
"SUB": "-",
"MUL": "*",
"QUO": "/",
}
var val interface{}
switch item.Value.(type) {
case string:
val = StringBuilder("\"", item.Value, "\"")
default:
val = item.Value
}
return StringBuilder(item.Field, " ", opMap[item.Op], " ", val)
}