-
Notifications
You must be signed in to change notification settings - Fork 0
/
function_storer.go
337 lines (303 loc) · 10.8 KB
/
function_storer.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package ysgo
import (
"fmt"
"reflect"
"github.com/remieven/ysgo/internal/rng"
"github.com/remieven/ysgo/variable"
)
type functionCaller interface {
call(functionID string, args []*variable.Value) (*variable.Value, error)
}
// YarnSpinnerFunction is a Go function than can be called as a function from a YarnSpinner script.
// It can take zero, one or more input values, and returns possibly a value and/or an error.
type YarnSpinnerFunction func([]*variable.Value) (*variable.Value, error)
type functionStorer struct {
functionsByID map[string]YarnSpinnerFunction
}
func newFunctionStorer(rng *rng.RNG) *functionStorer {
storer := &functionStorer{
functionsByID: map[string]YarnSpinnerFunction{
"string": toString,
"bool": toBoolean,
"number": toFloat,
},
}
for functionID, f := range map[string]any{
"dice": dice(rng),
"random": random(rng),
"random_range": randomRange(rng),
"round": round,
"round_places": roundPlaces,
"floor": floor,
"ceil": ceil,
"inc": inc,
"dec": dec,
"decimal": decimal,
"integer": integer,
} {
if err := storer.convertAndAddFunction(functionID, f); err != nil {
panic(fmt.Errorf("failed to convert base function %s: %w", functionID, err))
}
}
return storer
}
func (storer *functionStorer) addFunction(functionID string, function YarnSpinnerFunction) {
storer.functionsByID[functionID] = function
}
func (storer *functionStorer) convertAndAddFunction(functionID string, function any) error {
yarnSpinnerFunction, err := newYarnSpinnerFunction(function)
if err != nil {
return fmt.Errorf("failed to convert function to use yarn spinner values: %w", err)
}
storer.addFunction(functionID, yarnSpinnerFunction)
return nil
}
func (storer *functionStorer) call(functionID string, args []*variable.Value) (*variable.Value, error) {
function, ok := storer.functionsByID[functionID]
if !ok {
return nil, fmt.Errorf("unknown function")
}
value, err := function(args)
if err != nil {
return nil, fmt.Errorf("execution failed: %w", err)
}
return value, nil
}
var typeError = reflect.TypeOf((*error)(nil)).Elem()
func newYarnSpinnerFunction(function any) (YarnSpinnerFunction, error) {
functionType := reflect.TypeOf(function)
if functionType.Kind() != reflect.Func {
return nil, fmt.Errorf("newYarnSpinnerFunction expects an argument which is a function")
}
returnSignature, err := checkFunctionOutputParameters(functionType)
if err != nil {
return nil, fmt.Errorf("received function has unsupported output parameters: %w", err)
}
inputConverter, err := createInputConverter(functionType)
if err != nil {
return nil, fmt.Errorf("failed to create input converter: %w", err)
}
return func(args []*variable.Value) (*variable.Value, error) {
inputParameters, err := inputConverter(args)
if err != nil {
return nil, fmt.Errorf("failed to convert input parameters: %w", err)
}
outputParameters := reflect.ValueOf(function).Call(inputParameters)
switch returnSignature {
case noReturn:
return nil, nil
case valueReturn:
value, err := getTreeValue(outputParameters[0])
if err != nil {
return nil, fmt.Errorf("failed to convert return value of function: %w", err)
}
return value, nil
case errorReturn:
if outputParameters[0].Interface() == nil {
return nil, nil
}
err, ok := outputParameters[0].Interface().(error)
if !ok {
return nil, fmt.Errorf("function did not return an error value like expected")
}
return nil, err
case valueErrorReturn:
returnedErr := outputParameters[1].Interface()
if returnedErr != nil {
if _, ok := returnedErr.(error); !ok {
return nil, fmt.Errorf("function did not return an error value like expected")
} else {
return nil, returnedErr.(error)
}
}
value, err := getTreeValue(outputParameters[0])
if err != nil {
return nil, fmt.Errorf("failed to convert return value of function: %w", err)
}
return value, nil
}
return nil, nil
}, nil
}
func isTypeConvertibleToValue(t reflect.Type) bool {
switch t.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Float32, reflect.Float64:
return true
case reflect.Bool:
return true
case reflect.String:
return true
}
return false
}
func getTreeValue(v reflect.Value) (*variable.Value, error) {
switch {
case v.CanFloat():
return variable.NewNumber(v.Float()), nil
case v.CanInt():
return variable.NewNumber(float64(v.Int())), nil
case v.Kind() == reflect.Bool:
return variable.NewBoolean(v.Bool()), nil
case v.Kind() == reflect.String:
return variable.NewString(v.String()), nil
}
return nil, fmt.Errorf("unsupported value type received when trying to create a tree value")
}
type returnSignature int
const (
noReturn returnSignature = iota
valueReturn
errorReturn
errorChanReturn
valueErrorReturn
)
func checkFunctionOutputParameters(functionType reflect.Type) (returnSignature, error) {
switch functionType.NumOut() {
case 0:
return noReturn, nil
case 1:
outputType := functionType.Out(0)
switch {
case isTypeConvertibleToValue(outputType):
return valueReturn, nil
case outputType.ConvertibleTo(typeError):
return errorReturn, nil
}
return 0, fmt.Errorf("a single output parameter must be either convertible to a yarn spinner value or an error")
case 2:
firstOutputType, secondOutputType := functionType.Out(0), functionType.Out(1)
if !isTypeConvertibleToValue(firstOutputType) {
return 0, fmt.Errorf("the first output parameter of a function with two must be convertible to a yarn spinner value")
}
if !secondOutputType.ConvertibleTo(typeError) {
return 0, fmt.Errorf("the second output parameter of a function with two must be an error")
}
return valueErrorReturn, nil
default:
return 0, fmt.Errorf("functions can have at most two output parameters")
}
}
func createInputConverter(functionType reflect.Type) (func([]*variable.Value) ([]reflect.Value, error), error) {
isVariadic := functionType.IsVariadic()
if isVariadic {
return createVariadicInputConverter(functionType)
}
numIn := functionType.NumIn()
argConverters := make([]func(*variable.Value) (reflect.Value, error), 0, numIn)
for i := 0; i < numIn; i++ {
argConverter, ok := argConverterByGoalKind[functionType.In(i).Kind()]
if !ok {
return nil, fmt.Errorf("argument number %d has an unsupported type %v", i, functionType.In(i).Kind())
}
argConverters = append(argConverters, argConverter)
}
return func(args []*variable.Value) ([]reflect.Value, error) {
if len(args) < numIn {
return nil, fmt.Errorf("received too few arguments")
} else if len(args) > numIn {
return nil, fmt.Errorf("received too many arguments")
}
inputParameters := make([]reflect.Value, 0, numIn)
for i := 0; i < numIn; i++ {
inputParameter, err := argConverters[i](args[i])
if err != nil {
return nil, fmt.Errorf("failed to convert argument number %d: %w", i, err)
}
inputParameters = append(inputParameters, inputParameter)
}
return inputParameters, nil
}, nil
}
func createVariadicInputConverter(functionType reflect.Type) (func([]*variable.Value) ([]reflect.Value, error), error) {
numIn := functionType.NumIn()
argConverters := make([]func(*variable.Value) (reflect.Value, error), 0, numIn)
for i := 0; i < numIn-1; i++ {
argConverter, ok := argConverterByGoalKind[functionType.In(i).Kind()]
if !ok {
return nil, fmt.Errorf("argument number %d has an unsupported type %v", i, functionType.In(i).Kind())
}
argConverters = append(argConverters, argConverter)
}
variadicArgsConverter, ok := argConverterByGoalKind[functionType.In(numIn-1).Elem().Kind()]
if !ok {
return nil, fmt.Errorf("argument number %d has an unsupported type %v", numIn-1, functionType.In(numIn-1).Kind())
}
return func(args []*variable.Value) ([]reflect.Value, error) {
if len(args) < numIn-1 {
return nil, fmt.Errorf("received too few arguments")
}
inputParameters := make([]reflect.Value, 0, numIn)
for i := 0; i < numIn-1; i++ {
inputParameter, err := argConverters[i](args[i])
if err != nil {
return nil, fmt.Errorf("failed to convert argument number %d: %w", i, err)
}
inputParameters = append(inputParameters, inputParameter)
}
for i := numIn - 1; i < len(args); i++ {
inputParameter, err := variadicArgsConverter(args[i])
if err != nil {
return nil, fmt.Errorf("failed to convert argument number %d: %w", i, err)
}
inputParameters = append(inputParameters, inputParameter)
}
return inputParameters, nil
}, nil
}
var argConverterByGoalKind map[reflect.Kind]func(*variable.Value) (reflect.Value, error) = map[reflect.Kind]func(*variable.Value) (reflect.Value, error){
reflect.Int: func(value *variable.Value) (reflect.Value, error) {
if value.Number == nil {
return reflect.Value{}, fmt.Errorf("expected a number value but got something else")
}
return reflect.ValueOf(int(*value.Number)), nil
},
reflect.Int8: func(value *variable.Value) (reflect.Value, error) {
if value.Number == nil {
return reflect.Value{}, fmt.Errorf("expected a number value but got something else")
}
return reflect.ValueOf(int8(*value.Number)), nil
},
reflect.Int16: func(value *variable.Value) (reflect.Value, error) {
if value.Number == nil {
return reflect.Value{}, fmt.Errorf("expected a number value but got something else")
}
return reflect.ValueOf(int16(*value.Number)), nil
},
reflect.Int32: func(value *variable.Value) (reflect.Value, error) {
if value.Number == nil {
return reflect.Value{}, fmt.Errorf("expected a number value but got something else")
}
return reflect.ValueOf(int32(*value.Number)), nil
},
reflect.Int64: func(value *variable.Value) (reflect.Value, error) {
if value.Number == nil {
return reflect.Value{}, fmt.Errorf("expected a number value but got something else")
}
return reflect.ValueOf(int64(*value.Number)), nil
},
reflect.Float32: func(value *variable.Value) (reflect.Value, error) {
if value.Number == nil {
return reflect.Value{}, fmt.Errorf("expected a number value but got something else")
}
return reflect.ValueOf(float32(*value.Number)), nil
},
reflect.Float64: func(value *variable.Value) (reflect.Value, error) {
if value.Number == nil {
return reflect.Value{}, fmt.Errorf("expected a number value but got something else")
}
return reflect.ValueOf(float64(*value.Number)), nil
},
reflect.Bool: func(value *variable.Value) (reflect.Value, error) {
if value.Boolean == nil {
return reflect.Value{}, fmt.Errorf("expected a boolean value but got something else")
}
return reflect.ValueOf(*value.Boolean), nil
},
reflect.String: func(value *variable.Value) (reflect.Value, error) {
if value.String == nil {
return reflect.Value{}, fmt.Errorf("expected a string value but got something else")
}
return reflect.ValueOf(*value.String), nil
},
}
var _ functionCaller = (*functionStorer)(nil)