-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrapper.go
140 lines (112 loc) · 4.97 KB
/
wrapper.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
package function
import (
"context"
"reflect"
)
type Wrapper interface {
Description
CallWrapper
CallWithStringsWrapper
CallWithNamedStringsWrapper
CallWithJSONWrapper
}
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 to replace it with generated code")
}
panic("function.WrapperTODO: run gen-func-wrappers")
}
type CallWrapper interface {
Call(ctx context.Context, args []any) (results []any, err error)
}
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 to replace it with generated code")
}
panic("function.CallWrapperTODO: run gen-func-wrappers")
}
type CallWithStringsWrapper interface {
CallWithStrings(ctx context.Context, args ...string) (results []any, err error)
}
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")
}
type CallWithNamedStringsWrapper interface {
CallWithNamedStrings(ctx context.Context, args map[string]string) (results []any, err error)
}
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")
}
type CallWithJSONWrapper interface {
CallWithJSON(ctx context.Context, argsJSON []byte) (results []any, err error)
}
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")
}
// Implementations of the call interfaces as higher order functions
var (
_ CallWrapper = CallWrapperFunc(nil)
_ CallWithStringsWrapper = CallWithStringsWrapperFunc(nil)
_ CallWithNamedStringsWrapper = CallWithNamedStringsWrapperFunc(nil)
_ CallWithJSONWrapper = CallWithJSONWrapperFunc(nil)
)
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)
}
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...)
}
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)
}
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)
}
// NoArgNoResultWrapper returns a Wrapper for a function call
// without arguments and without results.
func NoArgNoResultWrapper(name string, call func()) Wrapper {
return noArgNoResultWrapper{name, call}
}
type noArgNoResultWrapper struct {
name string
call func()
}
func (f noArgNoResultWrapper) String() string { return f.name }
func (f noArgNoResultWrapper) Name() string { return f.name }
func (noArgNoResultWrapper) NumArgs() int { return 0 }
func (noArgNoResultWrapper) ContextArg() bool { return false }
func (noArgNoResultWrapper) NumResults() int { return 0 }
func (noArgNoResultWrapper) ErrorResult() bool { return false }
func (noArgNoResultWrapper) ArgNames() []string { return nil }
func (noArgNoResultWrapper) ArgDescriptions() []string { return nil }
func (noArgNoResultWrapper) ArgTypes() []reflect.Type { return nil }
func (noArgNoResultWrapper) ResultTypes() []reflect.Type { return nil }
func (f noArgNoResultWrapper) Call(context.Context, []any) ([]any, error) {
f.call()
return nil, nil
}
func (f noArgNoResultWrapper) CallWithStrings(context.Context, ...string) ([]any, error) {
f.call()
return nil, nil
}
func (f noArgNoResultWrapper) CallWithNamedStrings(context.Context, map[string]string) ([]any, error) {
f.call()
return nil, nil
}
func (f noArgNoResultWrapper) CallWithJSON(context.Context, []byte) (results []any, err error) {
f.call()
return nil, nil
}