Repository navigation
Expand file tree
/
Copy pathwrapper.go
More file actions
305 lines (269 loc) · 13.2 KB
/
Copy pathwrapper.go
File metadata and controls
305 lines (269 loc) · 13.2 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
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
package function
import (
"context"
"encoding/json"
"fmt"
"reflect"
)
// Wrapper is the main interface for wrapped functions, providing multiple calling conventions.
// It combines Description (function metadata), CallWrapper (direct calls),
// CallWithStringsWrapper (string argument parsing), CallWithNamedStringsWrapper (named arguments),
// and CallWithJSONWrapper (JSON argument parsing).
//
// Wrapper implementations can be created either through reflection using ReflectWrapper,
// or through code generation using gen-func-wrappers for better performance.
type Wrapper interface {
Description
CallWrapper
CallWithStringsWrapper
CallWithNamedStringsWrapper
CallWithJSONWrapper
}
// WrapperTODO is a placeholder function used during development with code generation.
// It should be replaced by running gen-func-wrappers, which generates optimized
// wrapper code without reflection overhead.
//
// Usage:
//
// var MyFuncWrapper = function.WrapperTODO(MyFunc)
//
// Then add the code generator as tool to your module once
// and run it:
//
// go get -tool github.com/domonda/go-function/cmd/gen-func-wrappers@latest
// go tool gen-func-wrappers ./...
//
// This will replace WrapperTODO calls with generated wrapper implementations.
// The function panics at runtime to ensure you don't forget to run the code generator.
func WrapperTODO(function any) Wrapper {
if reflect.ValueOf(function).Kind() != reflect.Func {
panic("function.WrapperTODO must be used with a function as argument, then run gen-func-wrappers to replace it with generated code")
}
panic("function.WrapperTODO: run gen-func-wrappers")
}
// CallWrapper provides the ability to call a function with a slice of any values.
// The context is passed as the first argument if the function accepts context.Context,
// so args don't include a context.Context argument.
// The arg for a variadic last argument is a slice of its element type.
// Missing or untyped nil args are passed as zero values.
// ReflectWrapper, VoidFuncWrapper, ErrorFuncWrapper, and generated wrappers
// return an error for too many args or args not assignable to their argument type.
type CallWrapper interface {
Call(ctx context.Context, args []any) (results []any, err error)
}
// CallWrapperTODO is a placeholder for code generation. See WrapperTODO for details.
func CallWrapperTODO(function any) CallWrapper {
if reflect.ValueOf(function).Kind() != reflect.Func {
panic("function.CallWrapperTODO must be used with a function as argument, then run gen-func-wrappers to replace it with generated code")
}
panic("function.CallWrapperTODO: run gen-func-wrappers")
}
// CallWithStringsWrapper provides the ability to call a function with string arguments
// that are automatically converted to the required types.
// String conversion supports basic types, time values, slices, structs (as JSON), and more.
// See ScanString for conversion details.
//
// Like in a Go call, every string from the position of a variadic last argument on
// is one element of the variadic slice, also a single string, so it is never parsed
// as slice literal. Elements are converted to the element type,
// elements of type any get the unconverted string.
// No strings at the variadic position result in no variadic elements.
// Missing strings for other arguments are passed as zero values.
// ReflectWrapper, VoidFuncWrapper, ErrorFuncWrapper, and generated wrappers
// return an error for too many strings for a function without variadic argument.
//
// Security: the strings become argument values as they are, so don't pass
// untrusted input for arguments of types that name files or other resources,
// like fs.File of github.com/ungerik/go-fs which is a path on the server.
type CallWithStringsWrapper interface {
CallWithStrings(ctx context.Context, args ...string) (results []any, err error)
}
// CallWithStringsWrapperTODO is a placeholder for code generation. See WrapperTODO for details.
func CallWithStringsWrapperTODO(function any) CallWithStringsWrapper {
if reflect.ValueOf(function).Kind() != reflect.Func {
panic("function.CallWithStringsWrapperTODO must be used with a function as argument, then run gen-func-wrappers to replace it with generated code")
}
panic("function.CallWithStringsWrapperTODO: run gen-func-wrappers")
}
// CallWithNamedStringsWrapper provides the ability to call a function with named string arguments.
// The argument names must match the function's parameter names.
// String values are automatically converted to the required types.
// Missing arguments will use zero values.
// The single string of a variadic last argument is parsed as slice literal like "[1,2]".
//
// Security: the strings become argument values as they are, so don't pass
// untrusted input for arguments of types that name files or other resources,
// like fs.File of github.com/ungerik/go-fs which is a path on the server.
type CallWithNamedStringsWrapper interface {
CallWithNamedStrings(ctx context.Context, args map[string]string) (results []any, err error)
}
// CallWithNamedStringsWrapperTODO is a placeholder for code generation. See WrapperTODO for details.
func CallWithNamedStringsWrapperTODO(function any) CallWithNamedStringsWrapper {
if reflect.ValueOf(function).Kind() != reflect.Func {
panic("function.CallWithNamedStringsWrapperTODO must be used with a function as argument, then run gen-func-wrappers to replace it with generated code")
}
panic("function.CallWithNamedStringsWrapperTODO: run gen-func-wrappers")
}
// CallWithJSONWrapper provides the ability to call a function with JSON-encoded arguments.
// The JSON must be an object with the argument names as keys,
// a JSON array of arguments is not supported.
// ReflectWrapper, VoidFuncWrapper, ErrorFuncWrapper, and generated wrappers
// return an error for JSON that is no object, except null,
// which they handle like an empty object.
// Arguments are unmarshaled using json.Unmarshal into the required types,
// except error arguments, which are JSON strings where an empty string
// or null is no error.
//
// Security: the JSON values become argument values as they are, so don't pass
// untrusted input for arguments of types that name files or other resources,
// like fs.File of github.com/ungerik/go-fs which is a path on the server.
type CallWithJSONWrapper interface {
CallWithJSON(ctx context.Context, argsJSON []byte) (results []any, err error)
}
// CallWithJSONWrapperTODO is a placeholder for code generation. See WrapperTODO for details.
func CallWithJSONWrapperTODO(function any) CallWithJSONWrapper {
if reflect.ValueOf(function).Kind() != reflect.Func {
panic("function.CallWithJSONWrapperTODO must be used with a function as argument, then run gen-func-wrappers to replace it with generated code")
}
panic("function.CallWithJSONWrapperTODO: run gen-func-wrappers")
}
// Compile-time assertions that the *Func types implement their respective interfaces.
var (
_ CallWrapper = CallWrapperFunc(nil)
_ CallWithStringsWrapper = CallWithStringsWrapperFunc(nil)
_ CallWithNamedStringsWrapper = CallWithNamedStringsWrapperFunc(nil)
_ CallWithJSONWrapper = CallWithJSONWrapperFunc(nil)
)
// CallWrapperFunc is a function type that implements CallWrapper.
// It allows any function with the matching signature to be used as a CallWrapper.
type CallWrapperFunc func(ctx context.Context, args []any) (results []any, err error)
func (f CallWrapperFunc) Call(ctx context.Context, args []any) (results []any, err error) {
return f(ctx, args)
}
// CallWithStringsWrapperFunc is a function type that implements CallWithStringsWrapper.
// It allows any function with the matching signature to be used as a CallWithStringsWrapper.
type CallWithStringsWrapperFunc func(ctx context.Context, args ...string) (results []any, err error)
func (f CallWithStringsWrapperFunc) CallWithStrings(ctx context.Context, args ...string) (results []any, err error) {
return f(ctx, args...)
}
// CallWithNamedStringsWrapperFunc is a function type that implements CallWithNamedStringsWrapper.
// It allows any function with the matching signature to be used as a CallWithNamedStringsWrapper.
type CallWithNamedStringsWrapperFunc func(ctx context.Context, args map[string]string) (results []any, err error)
func (f CallWithNamedStringsWrapperFunc) CallWithNamedStrings(ctx context.Context, args map[string]string) (results []any, err error) {
return f(ctx, args)
}
// CallWithJSONWrapperFunc is a function type that implements CallWithJSONWrapper.
// It allows any function with the matching signature to be used as a CallWithJSONWrapper.
type CallWithJSONWrapperFunc func(ctx context.Context, argsJSON []byte) (results []any, err error)
func (f CallWithJSONWrapperFunc) CallWithJSON(ctx context.Context, argsJSON []byte) (results []any, err error) {
return f(ctx, argsJSON)
}
// VoidFuncWrapper is a Wrapper for a function without arguments and without results.
// It implements all wrapper interfaces and can be used for simple callback-style functions.
// Like ReflectWrapper, its Call methods return an error for passed arguments
// or strings and for JSON that is no object, and ignore named strings
// and the values of a JSON object.
//
// Example:
//
// var PrintHello VoidFuncWrapper = func() { fmt.Println("Hello") }
// PrintHello.Call(ctx, nil) // Prints "Hello"
type VoidFuncWrapper func()
func (VoidFuncWrapper) String() string { return "func()" }
func (VoidFuncWrapper) Name() string { return "func()" }
func (VoidFuncWrapper) NumArgs() int { return 0 }
func (VoidFuncWrapper) ContextArg() bool { return false }
func (VoidFuncWrapper) Variadic() bool { return false }
func (VoidFuncWrapper) NumResults() int { return 0 }
func (VoidFuncWrapper) ErrorResult() bool { return false }
func (VoidFuncWrapper) ArgNames() []string { return nil }
func (VoidFuncWrapper) ArgDescriptions() []string { return nil }
func (VoidFuncWrapper) ArgTypes() []reflect.Type { return nil }
func (VoidFuncWrapper) ResultTypes() []reflect.Type { return nil }
func (f VoidFuncWrapper) Call(_ context.Context, args []any) ([]any, error) {
if len(args) > 0 {
return nil, noArgsError(f, len(args))
}
f()
return nil, nil
}
func (f VoidFuncWrapper) CallWithStrings(_ context.Context, strs ...string) ([]any, error) {
if len(strs) > 0 {
return nil, noArgsError(f, len(strs))
}
f()
return nil, nil
}
func (f VoidFuncWrapper) CallWithNamedStrings(context.Context, map[string]string) ([]any, error) {
f()
return nil, nil
}
func (f VoidFuncWrapper) CallWithJSON(_ context.Context, argsJSON []byte) (results []any, err error) {
err = checkArgsJSONObject(f, argsJSON)
if err != nil {
return nil, err
}
f()
return nil, nil
}
// ErrorFuncWrapper is a Wrapper for a function without arguments that returns only an error.
// It implements all wrapper interfaces and is useful for validation or initialization functions.
// Like ReflectWrapper, its Call methods return an error for passed arguments
// or strings and for JSON that is no object, and ignore named strings
// and the values of a JSON object.
//
// Example:
//
// var ValidateConfig ErrorFuncWrapper = func() error {
// if config == nil { return errors.New("config is nil") }
// return nil
// }
// _, err := ValidateConfig.Call(ctx, nil)
type ErrorFuncWrapper func() error
func (ErrorFuncWrapper) String() string { return "func() error" }
func (ErrorFuncWrapper) Name() string { return "func() error" }
func (ErrorFuncWrapper) NumArgs() int { return 0 }
func (ErrorFuncWrapper) ContextArg() bool { return false }
func (ErrorFuncWrapper) Variadic() bool { return false }
func (ErrorFuncWrapper) NumResults() int { return 1 }
func (ErrorFuncWrapper) ErrorResult() bool { return true }
func (ErrorFuncWrapper) ArgNames() []string { return nil }
func (ErrorFuncWrapper) ArgDescriptions() []string { return nil }
func (ErrorFuncWrapper) ArgTypes() []reflect.Type { return nil }
func (ErrorFuncWrapper) ResultTypes() []reflect.Type { return []reflect.Type{typeOfError} }
func (f ErrorFuncWrapper) Call(_ context.Context, args []any) ([]any, error) {
if len(args) > 0 {
return nil, noArgsError(f, len(args))
}
return nil, f()
}
func (f ErrorFuncWrapper) CallWithStrings(_ context.Context, strs ...string) ([]any, error) {
if len(strs) > 0 {
return nil, noArgsError(f, len(strs))
}
return nil, f()
}
func (f ErrorFuncWrapper) CallWithNamedStrings(context.Context, map[string]string) ([]any, error) {
return nil, f()
}
func (f ErrorFuncWrapper) CallWithJSON(_ context.Context, argsJSON []byte) (results []any, err error) {
err = checkArgsJSONObject(f, argsJSON)
if err != nil {
return nil, err
}
return nil, f()
}
// noArgsError returns the error of a wrapper of a function without arguments
// for numArgs passed arguments, like ReflectWrapper.
func noArgsError(f fmt.Stringer, numArgs int) error {
return fmt.Errorf("%d arguments passed, but function %s takes 0", numArgs, f)
}
// checkArgsJSONObject returns an ErrParseArgsJSON if argsJSON is no JSON object
// or null, like the CallWithJSON method of ReflectWrapper.
func checkArgsJSONObject(f fmt.Stringer, argsJSON []byte) error {
err := json.Unmarshal(argsJSON, new(map[string]json.RawMessage))
if err != nil {
return NewErrParseArgsJSON(err, f)
}
return nil
}