-
Notifications
You must be signed in to change notification settings - Fork 0
/
executor.go
243 lines (205 loc) · 5.56 KB
/
executor.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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package pl
import (
"errors"
"fmt"
"reflect"
)
type fnNode struct {
name string
args []any
has_nested bool
}
type Executor struct {
Funcs FuncMap
Convs ConvMap
}
func NewExecutor() *Executor {
return &Executor{
Funcs: NewFuncMap(),
Convs: NewConvMap(),
}
}
func (e *Executor) ExecuteExpr(expr string, data any) ([]any, error) {
pl, err := ParseString(expr)
if err != nil {
return nil, err
}
return e.Execute(pl, data)
}
func (e *Executor) Execute(pl *Pl, data any) ([]any, error) {
args_prev := []any{}
for i, fn := range pl.Funcs {
err := func() error {
f, ok := e.Funcs[fn.Name]
if !ok {
return errors.New("not defined")
}
fnode, err := e.evaluateFn(fn, data)
if err != nil {
return err
}
args := make([]any, 0, len(fnode.args)+len(args_prev))
if !fnode.has_nested {
args = append(args, fnode.args...)
} else {
for i, arg := range fnode.args {
nested, ok := arg.(*Pl)
if !ok {
args = append(args, arg)
continue
}
rst, err := e.Execute(nested, data)
if err != nil {
return fmt.Errorf("arg[%d]: %w", i, err)
}
args = append(args, rst...)
}
}
args = append(args, args_prev...)
rst, err := e.invokeFn(f, args)
if err != nil {
return err
}
if reflect.TypeOf(rst).Kind() != reflect.Slice {
args_prev = []any{rst}
} else {
v := reflect.ValueOf(rst)
args_prev = make([]any, v.Len())
for i := 0; i < v.Len(); i++ {
args_prev[i] = v.Index(i).Interface()
}
}
return nil
}()
if err != nil {
return nil, fmt.Errorf("fn[%d] %s: %w", i, fn.Name, err)
}
}
return args_prev, nil
}
func (e *Executor) evaluateFn(fn *Fn, data any) (*fnNode, error) {
rst := &fnNode{name: fn.Name, args: make([]any, len(fn.Args))}
for i, arg := range fn.Args {
if arg.String != nil {
rst.args[i] = *arg.String
} else if arg.Float != nil {
rst.args[i] = *arg.Float
} else if arg.Int != nil {
rst.args[i] = *arg.Int
} else if arg.Ref != nil {
arg, err := Resolve(data, arg.Ref)
if err != nil {
return nil, fmt.Errorf("arg[%d]: reference: %w", i, err)
}
rst.args[i] = arg
} else if arg.Nested != nil {
rst.args[i] = arg.Nested
rst.has_nested = true
} else {
return nil, fmt.Errorf("arg[%d]: empty value", i)
}
}
return rst, nil
}
func (e *Executor) convert(out reflect.Type, in reflect.Type, v reflect.Value) (any, error) {
rst, err := e.Convs.Convert(out, in, v)
if err == nil {
return rst, nil
}
if !errors.Is(err, ErrNotFound) {
return nil, err
}
// Try to convert through string.
var (
to_string func() (reflect.Value, error) = nil
from_string func(v reflect.Value) (any, error) = nil
)
if out.Kind() == reflect.String {
from_string = func(v reflect.Value) (any, error) { return v.String(), nil }
} else if convs, ok := e.Convs[string_t]; !ok {
return nil, ErrNotFound
} else if conv, ok := convs[out]; !ok {
return nil, ErrNotFound
} else {
from_string = func(v reflect.Value) (any, error) { return conv(v) }
}
if in.Kind() == reflect.String {
// Already a string.
panic("e.Convs.Convert must be done before")
} else if convs, ok := e.Convs[in]; ok {
// Has conversion to string?
if conv, ok := convs[string_t]; ok {
to_string = func() (reflect.Value, error) {
rst, err := conv(v)
return reflect.ValueOf(rst), err
}
}
}
// No conversion to string found.
if to_string == nil {
// Has stringer?
if s, ok := v.Interface().(interface{ String() string }); ok {
to_string = func() (reflect.Value, error) { return reflect.ValueOf(s.String()), nil }
} else {
return nil, ErrNotFound
}
}
tmp, err := to_string()
if err != nil {
return nil, fmt.Errorf("failed to convert to intermediate type string from argument type: %w", err)
}
rst, err = from_string(tmp)
if err != nil {
return nil, fmt.Errorf("failed to convert to parameter type from intermediate type string: %w", err)
}
return rst, nil
}
func (e *Executor) invokeFn(fn any, args []any) (any, error) {
fv := reflect.ValueOf(fn)
ft := fv.Type()
// Check if the number of returned values is valid.
if n := ft.NumOut(); n > 2 || n == 0 {
return nil, fmt.Errorf("function have to return one or two values but %d values are returned", n)
} else if n == 2 && !ft.Out(1).Implements(reflect.TypeOf((*error)(nil)).Elem()) {
return nil, fmt.Errorf("type of second return value of the function must be an error but it was %s", ft.Out(1).Name())
}
// Check if the number of argument is fit.
num_fixed_args := ft.NumIn()
if ft.IsVariadic() {
num_fixed_args--
if len(args) < num_fixed_args {
return nil, fmt.Errorf("expected at least %d args but %d args are given", num_fixed_args, len(args))
}
} else if len(args) != num_fixed_args {
return nil, fmt.Errorf("expected %d args but %d args are given", len(args), num_fixed_args)
}
input_args := make([]reflect.Value, len(args))
for i, arg := range args {
j := i
if i >= num_fixed_args {
j = num_fixed_args
}
t_arg := reflect.TypeOf(arg)
t_in := ft.In(j)
if i >= num_fixed_args {
t_in = t_in.Elem()
}
if t_arg.AssignableTo(t_in) {
input_args[i] = reflect.ValueOf(arg)
continue
}
v_arg := reflect.ValueOf(arg)
v, err := e.convert(t_in, t_arg, v_arg)
if err != nil {
return nil, fmt.Errorf("arg[%d]: convert to %s from %s: %w", i, t_in.String(), t_arg.String(), err)
}
input_args[i] = reflect.ValueOf(v)
}
rst := fv.Call(input_args)
if len(rst) == 1 || (len(rst) == 2 && rst[1].IsNil()) {
return rst[0].Interface(), nil
} else {
err := rst[1].Interface().(error)
return rst[0].Interface(), err
}
}