-
Notifications
You must be signed in to change notification settings - Fork 2
/
unpack.go
165 lines (135 loc) · 4.22 KB
/
unpack.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
package sdkmodule
import (
"fmt"
"maps"
"reflect"
"strings"
"go.autokitteh.dev/autokitteh/internal/kittehs"
"go.autokitteh.dev/autokitteh/sdk/sdktypes"
)
// UnpackArgs unpacks the positional and keyword arguments into the supplied parameter
// variables. pairs is an alternating list of names and pointers to variables.
//
// If the parameter name ends with "?", it is optional.
// If the parameter name ends with "=", it must be supplied in kwargs.
// If the parameter name ends with "?=" or "?="", it must be supplied in kwargs but is optional.
// If the parameter name ends with "=", it must be supplied in kwargs.
// If the parameter name starts with "**", the destination will accept all kwargs as a dict.
// If the parameter name starts with "*", the destination will aceppt all args as a list.
//
// A nameless parameter can also be specified. That parameter must be a pointer to a struct.
// The function will use the member names of the struct as the parameter names. If the fields
// are tagged with `json:"..."`, the tag will be used as the parameter name. If the tag is
// "-", the field will be ignored. If the tag modifier is "omitempty", the field will be optional.
//
// Example:
//
// func SomeFunc(ctx context.Context, args []sdktypes.Value, kwargs map[string]sdktypes.Value) (sdktypes.Value, error) {
// var (
// x int
// y string
// args []int
// )
//
// var st struct { Z int `json:"z"`}
//
// if err := UnpackArgs(args, kwargs, "x", &x, &st, "y=", &y, "*args", &args); err != nil {
// return err
// }
//
// ...
// }
//
// (this function is heavily inspired by https://pkg.go.dev/go.starlark.net/starlark#UnpackArgs,
// it essentially does the same thing with some bells and whistles, but on autokitteh level)
func UnpackArgs(args []sdktypes.Value, kwargs map[string]sdktypes.Value, dsts ...any) error {
kwargs = maps.Clone(kwargs)
var flattened []any
for i := 0; i < len(dsts); i++ {
if _, ok := dsts[i].(string); ok {
i++
continue
}
t := reflect.TypeOf(dsts[i])
if t.Kind() != reflect.Ptr {
return fmt.Errorf("dst %d must be name or a pointer to a struct", i)
}
tt := t.Elem()
if tt.Kind() != reflect.Struct {
return fmt.Errorf("dst %d must be a pointer to a struct", i)
}
for j := 0; j < tt.NumField(); j++ {
ttf := tt.Field(j)
name := ttf.Name
optional := ttf.Type.Kind() == reflect.Ptr
if j := ttf.Tag.Get("json"); j != "" {
if j == "-" {
continue
}
jname, rest, _ := strings.Cut(j, ",")
if rest == "omitempty" {
optional = true
}
if jname != "" {
name = jname
}
}
if len(name) == 0 {
continue
}
name += "="
if optional {
name += "?"
}
flattened = append(flattened, name, reflect.ValueOf(dsts[i]).Elem().Field(j).Addr().Interface())
}
dsts = append(dsts[:i], dsts[i+1:]...)
}
dsts = append(dsts, flattened...)
for i := 0; i+1 < len(dsts); i += 2 {
nameitf, dst := dsts[i], dsts[i+1]
name, ok := nameitf.(string)
if !ok {
return fmt.Errorf("dst pair %d name must be a string", i/2)
}
if strings.HasPrefix(name, "**") {
if err := sdktypes.DefaultValueWrapper.UnwrapInto(dst, sdktypes.NewDictValueFromStringMap(kwargs)); err != nil {
return fmt.Errorf("dst %q: %w", name, err)
}
kwargs = nil
continue
} else if strings.HasPrefix(name, "*") {
if err := sdktypes.DefaultValueWrapper.UnwrapInto(dst, kittehs.Must1(sdktypes.NewListValue(args))); err != nil {
return fmt.Errorf("dst %q: %w", name, err)
}
args = nil
continue
}
optional := strings.ContainsRune(name, '?')
mustkw := strings.ContainsRune(name, '=')
name = strings.TrimRight(name, "?=")
v, found := kwargs[name]
if found {
delete(kwargs, name)
} else {
if len(args) > 0 && !mustkw {
v, args = args[0], args[1:]
} else {
if !optional {
return fmt.Errorf("required parameter %q not specified", name)
}
continue
}
}
if err := sdktypes.DefaultValueWrapper.UnwrapInto(dst, v); err != nil {
return fmt.Errorf("dst %q: %w", name, err)
}
}
if len(args) > 0 {
return fmt.Errorf("not all positional arguments consumed: %v", args)
}
if len(kwargs) > 0 {
return fmt.Errorf("not all keyword arguments consumed: %v", kwargs)
}
return nil
}