-
Notifications
You must be signed in to change notification settings - Fork 136
/
struct.go
87 lines (66 loc) · 2.71 KB
/
struct.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
package yttlibrary
import (
"fmt"
"github.com/k14s/ytt/pkg/template/core"
"go.starlark.net/starlark"
"go.starlark.net/starlarkstruct"
)
var (
StructAPI = starlark.StringDict{
"struct": &starlarkstruct.Module{
Name: "struct",
Members: starlark.StringDict{
"make": starlark.NewBuiltin("struct.make", core.ErrWrapper(starlarkstruct.Make)),
"make_and_bind": starlark.NewBuiltin("struct.make_and_bind", core.ErrWrapper(structModule{}.MakeAndBind)),
"bind": starlark.NewBuiltin("struct.bind", core.ErrWrapper(structModule{}.Bind)),
"encode": starlark.NewBuiltin("struct.encode", core.ErrWrapper(structModule{}.Encode)),
"decode": starlark.NewBuiltin("struct.decode", core.ErrWrapper(structModule{}.Decode)),
},
},
}
)
type structModule struct{}
func (b structModule) MakeAndBind(thread *starlark.Thread, f *starlark.Builtin,
bindArgs starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
if bindArgs.Len() != 1 {
return starlark.None, fmt.Errorf("expected at exactly one argument")
}
for i, kwarg := range kwargs {
if _, ok := kwarg[1].(starlark.Callable); ok {
boundFunc, err := b.Bind(thread, nil, starlark.Tuple{kwarg[1], bindArgs.Index(0)}, nil)
if err != nil {
return starlark.None, fmt.Errorf("binding %s: %s", kwarg[0], err)
}
kwarg[1] = boundFunc
kwargs[i] = kwarg
}
}
return starlarkstruct.Make(thread, nil, starlark.Tuple{}, kwargs)
}
func (b structModule) Bind(thread *starlark.Thread, f *starlark.Builtin,
bindArgs starlark.Tuple, _ []starlark.Tuple) (starlark.Value, error) {
if bindArgs.Len() < 2 {
return starlark.None, fmt.Errorf("expected at least two arguments")
}
resultFunc := func(thread *starlark.Thread, f *starlark.Builtin,
args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
newArgs := append(starlark.Tuple{}, bindArgs[1:]...)
newArgs = append(newArgs, args...)
return starlark.Call(thread, bindArgs.Index(0), newArgs, kwargs)
}
return starlark.NewBuiltin("struct.bind_result", core.ErrWrapper(resultFunc)), nil
}
func (b structModule) Encode(thread *starlark.Thread, f *starlark.Builtin,
args starlark.Tuple, _ []starlark.Tuple) (starlark.Value, error) {
if args.Len() != 1 {
return starlark.None, fmt.Errorf("expected exactly one argument")
}
return core.NewGoValue(core.NewStarlarkValue(args.Index(0)).AsInterface(), true).AsStarlarkValue(), nil
}
func (b structModule) Decode(thread *starlark.Thread, f *starlark.Builtin,
args starlark.Tuple, _ []starlark.Tuple) (starlark.Value, error) {
if args.Len() != 1 {
return starlark.None, fmt.Errorf("expected exactly one argument")
}
return core.NewGoValue(core.NewStarlarkValue(args.Index(0)).AsInterface(), false).AsStarlarkValue(), nil
}