-
Notifications
You must be signed in to change notification settings - Fork 43
/
starlarkwrapper.go
147 lines (127 loc) · 4.57 KB
/
starlarkwrapper.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
package cmd
import (
"fmt"
"github.com/k14s/starlark-go/starlark"
nethttp "github.com/pcj/starlark-go-nethttp"
starlarkcore "go.starlark.net/starlark"
)
type Module struct {
netMod *nethttp.Module
}
// toYttStarlark converts the nethttp module to a ytt-starlark compatible one.
// If we get more of these sorts of conversions, a more reusable pattern will probably
// emerge.
func toStarlarkModule(input *nethttp.Module) *Module {
return &Module{input}
}
func (mod *Module) String() string { return "<module http>" }
func (mod *Module) Type() string { return "module" }
func (mod *Module) Freeze() { mod.netMod.Freeze() }
func (mod *Module) Truth() starlark.Bool { return starlark.True }
func (mod *Module) Hash() (uint32, error) {
return 0, fmt.Errorf("unhashable type: %s", mod.Type())
}
func (mod *Module) Attr(name string) (starlark.Value, error) {
val, err := mod.netMod.Attr(name)
if coreCallable, isCallable := val.(starlarkcore.Callable); isCallable {
return &yttStarlarkCallableWrapper{coreCallable}, err
}
return &yttStarlarkValueWrapper{val}, err
}
func (mod *Module) AttrNames() []string {
return mod.netMod.AttrNames()
}
// yttStarlarkValueWrapper wraps a type of the core starlark type but translates the Bool type so that
// it meets the necessary interfaces for the ytt-fork of starlark.
type yttStarlarkValueWrapper struct {
v starlarkcore.Value
}
func (wrapper *yttStarlarkValueWrapper) String() string { return wrapper.v.String() }
func (wrapper *yttStarlarkValueWrapper) Type() string { return wrapper.v.Type() }
func (wrapper *yttStarlarkValueWrapper) Freeze() { wrapper.v.Freeze() }
func (wrapper *yttStarlarkValueWrapper) Truth() starlark.Bool {
if wrapper.v.Truth() == starlarkcore.True {
return starlark.True
}
return starlark.False
}
func (wrapper *yttStarlarkValueWrapper) Hash() (uint32, error) {
return wrapper.v.Hash()
}
// yttStarlarkValueWrapper wraps a type of the core starlark type but translates the Bool type so that
// it meets the necessary interfaces for the ytt-fork of starlark.
type yttStarlarkValueWrapperToCore struct {
v starlark.Value
}
func (wrapper *yttStarlarkValueWrapperToCore) String() string { return wrapper.v.String() }
func (wrapper *yttStarlarkValueWrapperToCore) Type() string { return wrapper.v.Type() }
func (wrapper *yttStarlarkValueWrapperToCore) Freeze() { wrapper.v.Freeze() }
func (wrapper *yttStarlarkValueWrapperToCore) Truth() starlarkcore.Bool {
if wrapper.v.Truth() == starlark.True {
return starlarkcore.True
}
return starlarkcore.False
}
func (wrapper *yttStarlarkValueWrapperToCore) Hash() (uint32, error) {
return wrapper.v.Hash()
}
type yttStarlarkCallableWrapper struct {
v starlarkcore.Callable
}
func (wrapper *yttStarlarkCallableWrapper) Value() string { return wrapper.v.String() }
func (wrapper *yttStarlarkCallableWrapper) Name() string { return wrapper.v.Name() }
func (wrapper *yttStarlarkCallableWrapper) CallInternal(
thread *starlark.Thread, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
coreThread := starlarkcore.Thread{
Name: thread.Name,
Print: func(thread *starlarkcore.Thread, msg string) {
thread.Print(thread, msg)
},
Load: func(thread *starlarkcore.Thread, module string) (starlarkcore.StringDict, error) {
return thread.Load(thread, module)
},
}
v, err := wrapper.v.CallInternal(
&coreThread,
toCoreTuple(args),
toCoreTupleSlice(kwargs),
)
return &yttStarlarkValueWrapper{v}, err
}
func (wrapper *yttStarlarkCallableWrapper) String() string { return wrapper.v.String() }
func (wrapper *yttStarlarkCallableWrapper) Type() string { return wrapper.v.Type() }
func (wrapper *yttStarlarkCallableWrapper) Freeze() { wrapper.v.Freeze() }
func (wrapper *yttStarlarkCallableWrapper) Truth() starlark.Bool {
if wrapper.v.Truth() == starlarkcore.True {
return starlark.True
}
return starlark.False
}
func (wrapper *yttStarlarkCallableWrapper) Hash() (uint32, error) {
return wrapper.v.Hash()
}
func toCoreTuple(input starlark.Tuple) starlarkcore.Tuple {
result := []starlarkcore.Value{}
for _, v := range input {
switch t := v.(type) {
case starlark.String:
result = append(result, starlarkcore.String(t))
default:
result = append(result, &yttStarlarkValueWrapperToCore{v})
}
}
return result
}
func toCoreTupleSlice(input []starlark.Tuple) []starlarkcore.Tuple {
result := []starlarkcore.Tuple{}
for _, v := range input {
result = append(result, toCoreTuple(v))
}
return result
}
type yttStarlarkThreadWrapper struct {
v starlarkcore.Thread
}
type yttStarlarkTupleWrapper struct {
v starlarkcore.Tuple
}