ast_expr.go
3.8 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
package astexpr
import (
"encoding/json"
"fmt"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
)
const (
TypeFunCallerExprAST = "FunCallerExprAST"
TypeBinaryExprAST = "BinaryExprAST"
TypeFieldExprAST = "FieldExprAST"
TypeValueExprAST = "ValueExprAST"
TypeNumberExprAST = "NumberExprAST"
)
type ExprAST interface {
toStr() string
}
type NumberExprAST struct {
ExprType string `json:"exprType"`
Val float64
Str string
}
type ValueExprAST struct {
ExprType string `json:"exprType"`
Val string `json:"val"`
Str string `json:"str"`
}
type FieldExprAST struct {
ExprType string `json:"exprType"`
Str string `json:"str"`
Field *domain.TableField `json:"field"`
}
type BinaryExprAST struct {
ExprType string `json:"exprType"`
Op string `json:"op"`
Lhs ExprAST `json:"lhs"`
Rhs ExprAST `json:"rhs"`
}
type FunCallerExprAST struct {
//ArrayFlag bool `json:"arrayFlag"`
ExprType string `json:"exprType"`
Name string `json:"name"`
Args []ExprAST `json:"args"`
}
func (n NumberExprAST) toStr() string {
return fmt.Sprintf(
"NumberExprAST:%s",
n.Str,
)
}
func (n ValueExprAST) toStr() string {
return fmt.Sprintf(
"ValueExprAST:%s",
n.Str,
)
}
func (n FieldExprAST) toStr() string {
return fmt.Sprintf(
"FieldExprAST:%s",
n.Str,
)
}
func (b BinaryExprAST) toStr() string {
return fmt.Sprintf(
"BinaryExprAST: (%s %s %s)",
b.Op,
b.Lhs.toStr(),
b.Rhs.toStr(),
)
}
func (n FunCallerExprAST) toStr() string {
return fmt.Sprintf(
"FunCallerExprAST:%s",
n.Name,
)
}
type CloneFunCallerExprAST struct {
//ArrayFlag bool `json:"arrayFlag"`
Name string `json:"name"`
Arg []json.RawMessage `json:"args"`
}
type CloneBinaryExprAST struct {
Op string `json:"op"`
Lhs json.RawMessage `json:"lhs"`
Rhs json.RawMessage `json:"rhs"`
}
func ExprUnmarshal(data []byte) (interface{}, error) {
var m = make(map[string]interface{})
if err := json.Unmarshal(data, &m); err != nil {
return nil, err
}
value, err := unmarshalMapInterface(m, data)
if err != nil {
return data, err
}
return value, nil
}
func unmarshalMapInterface(m map[string]interface{}, rawData []byte) (interface{}, error) {
t, ok := m["exprType"]
if !ok {
return nil, nil
}
if t == TypeFunCallerExprAST {
expr := &CloneFunCallerExprAST{}
if err := json.Unmarshal(rawData, expr); err != nil {
return nil, err
}
exprReturn := &FunCallerExprAST{Name: expr.Name, ExprType: TypeFunCallerExprAST}
for i := range expr.Arg {
subExpr, err := ExprUnmarshal(expr.Arg[i])
if err != nil {
return nil, err
}
if subExpr == nil {
continue
}
exprReturn.Args = append(exprReturn.Args, subExpr.(ExprAST))
}
return exprReturn, nil
}
if t == TypeBinaryExprAST {
expr := &CloneBinaryExprAST{}
if err := json.Unmarshal(rawData, expr); err != nil {
return nil, err
}
exprReturn := &BinaryExprAST{Op: expr.Op, ExprType: TypeBinaryExprAST}
if len(expr.Lhs) > 0 {
subExpr, err := ExprUnmarshal(expr.Lhs)
if err != nil {
return nil, err
}
if subExpr != nil {
exprReturn.Lhs = subExpr.(ExprAST)
}
}
if len(expr.Rhs) > 0 {
subExpr, err := ExprUnmarshal(expr.Rhs)
if err != nil {
return nil, err
}
if subExpr != nil {
exprReturn.Rhs = subExpr.(ExprAST)
}
}
return exprReturn, nil
}
if t == TypeFieldExprAST {
expr := &FieldExprAST{ExprType: TypeFieldExprAST}
if err := json.Unmarshal(rawData, expr); err != nil {
return nil, err
}
return expr, nil
}
if t == TypeValueExprAST {
expr := &ValueExprAST{ExprType: TypeValueExprAST}
if err := json.Unmarshal(rawData, expr); err != nil {
return nil, err
}
return expr, nil
}
return nil, fmt.Errorf("unkonw expr type '%v'", t)
}