-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgo2star.go
343 lines (303 loc) · 8.87 KB
/
go2star.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
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package startype
import (
"fmt"
"reflect"
"go.starlark.net/starlark"
"go.starlark.net/starlarkstruct"
)
// GoValue represents an inherent Go value which can be
// converted to a Starlark value/type
type GoValue struct {
val interface{}
}
// Go wraps a Go value into GoValue so that it can be converted to
// a Starlark value.
func Go(val interface{}) *GoValue {
return &GoValue{val: val}
}
// Value returns the original Go value as an interface{}
func (v *GoValue) Value() interface{} {
return v.val
}
// Starlark translates Go value to a starlark.Value value
// using the following type mapping:
//
// bool -- starlark.Bool
// int{8,16,32,64} -- starlark.Int
// uint{8,16,32,64} -- starlark.Int
// float{32,64} -- starlark.Float
// string -- starlark.String
// []T, [n]T -- starlark.Tuple
// map[K]T -- *starlark.Dict
//
// The specified Starlark value must be provided as
// a pointer to the target Starlark type.
//
// Example:
//
// num := 64
// var starInt starlark.Int
// Go(num).Starlark(&starInt)
//
// For starlark.List and starlark.Set refer to their
// respective namesake methods.
func (v *GoValue) Starlark(starval interface{}) error {
return goToStarlark(v.val, starval)
}
// StarlarkList converts a slice of Go values to a starlark.Tuple,
// then converts that tuple into a starlark.List
func (v *GoValue) StarlarkList(starval interface{}) error {
var tuple starlark.Tuple
if err := v.Starlark(&tuple); err != nil {
return err
}
switch val := starval.(type) {
case *starlark.Value:
*val = starlark.NewList(tuple)
case *starlark.List:
*val = *starlark.NewList(tuple)
case **starlark.List:
listVal := *starlark.NewList(tuple)
*val = &listVal
default:
return fmt.Errorf("target type %T: must be *starlark.List or *starlark.Value", starval)
}
return nil
}
// StarlarkSet converts a slice of Go values to a starlark.Tuple,
// then converts that tuple into a starlark.Set
func (v *GoValue) StarlarkSet(starval interface{}) error {
var tuple starlark.Tuple
if err := v.Starlark(&tuple); err != nil {
return err
}
starSet := starlark.NewSet(len(tuple))
for _, val := range tuple {
if err := starSet.Insert(val); err != nil {
continue
}
}
switch val := starval.(type) {
case *starlark.Value:
*val = starSet
case *starlark.Set:
*val = *starSet //nolint:govet
case **starlark.Set:
*val = starSet
default:
return fmt.Errorf("target type %T: must be *starlark.List or *starlark.Value", starval)
}
return nil
}
// GoStructToStringDict is a helper func that converts a Go struct type to
// starlark.StringDict.
func GoStructToStringDict(gostruct interface{}) (starlark.StringDict, error) {
goval := reflect.ValueOf(gostruct)
gotype := goval.Type()
if gotype.Kind() != reflect.Struct {
return nil, fmt.Errorf("source type must be a struct")
}
return goStructToStringDict(goval)
}
// goToStarlark translates Go value to a starlark.Value value
// using the following type mapping:
//
// bool -- starlark.Bool
// int{8,16,32,64} -- starlark.Int
// uint{8,16,32,64} -- starlark.Int
// float{32,64} -- starlark.Float
// string -- starlark.String
// []T, [n]T -- starlark.Tuple
// map[K]T -- *starlark.Dict
func goToStarlark(gov interface{}, starval interface{}) error {
goval := reflect.ValueOf(gov)
if !goval.IsValid() || goval.IsZero() {
return nil
}
gotype := goval.Type()
switch gotype.Kind() {
case reflect.Bool:
switch val := starval.(type) {
case *starlark.Value:
*val = starlark.Bool(goval.Bool())
case *starlark.Bool:
*val = starlark.Bool(goval.Bool())
case **starlark.Bool:
boolVal := starlark.Bool(goval.Bool())
*val = &boolVal
default:
return fmt.Errorf("target type (%T): must be *starlark.Bool, *starlark.Value", starval)
}
return nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
switch val := starval.(type) {
case *starlark.Value:
*val = starlark.MakeInt64(goval.Int())
case *starlark.Int:
*val = starlark.MakeInt64(goval.Int())
case **starlark.Int:
intVal := starlark.MakeInt64(goval.Int())
*val = &intVal
default:
return fmt.Errorf("target type (%T): must be *starlark.Int or *starlark.Value", starval)
}
return nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
switch val := starval.(type) {
case *starlark.Value:
*val = starlark.MakeUint64(goval.Uint())
case *starlark.Int:
*val = starlark.MakeUint64(goval.Uint())
case **starlark.Int:
uintVal := starlark.MakeUint64(goval.Uint())
*val = &uintVal
default:
return fmt.Errorf("target type %T: must be *starlark.Int or *starlark.Value", starval)
}
return nil
case reflect.Float32, reflect.Float64:
switch val := starval.(type) {
case *starlark.Value:
*val = starlark.Float(goval.Float())
case *starlark.Float:
*val = starlark.Float(goval.Float())
case **starlark.Float:
floatVal := starlark.Float(goval.Float())
*val = &floatVal
default:
return fmt.Errorf("target type %T: must be *starlark.Float or *starlark.Value", starval)
}
return nil
case reflect.String:
switch val := starval.(type) {
case *starlark.Value:
*val = starlark.String(goval.String())
case *starlark.String:
*val = starlark.String(goval.String())
case **starlark.String:
strVal := starlark.String(goval.String())
*val = &strVal
default:
return fmt.Errorf("target type %T: must be *starlark.String or *starlark.Value", starval)
}
return nil
case reflect.Slice, reflect.Array:
result, err := makeTuple(goval)
if err != nil {
return err
}
switch val := starval.(type) {
case *starlark.Value:
*val = starlark.Tuple(result)
case *starlark.Tuple:
*val = result
case **starlark.Tuple:
tupVal := starlark.Tuple(result)
*val = &tupVal
default:
return fmt.Errorf("target type %T: must be *starlark.Tuple or *starlark.Value", starval)
}
return nil
case reflect.Map:
dict, err := goMapToDict(goval)
if err != nil {
return err
}
switch dictVal := starval.(type) {
case *starlark.Dict:
*dictVal = *dict //nolint:govet
case **starlark.Dict:
*dictVal = dict
case *starlark.Value:
*dictVal = dict
default:
return fmt.Errorf("want target type *starlark.Dict, got: %T", dictVal)
}
return nil
case reflect.Struct:
dict, err := goStructToStringDict(goval)
if err != nil {
return err
}
switch val := starval.(type) {
case *starlark.Value:
result := starlarkstruct.FromStringDict(starlark.String(gotype.Name()), dict)
*val = result
case *starlarkstruct.Struct:
result := starlarkstruct.FromStringDict(starlark.String(gotype.Name()), dict)
*val = *result
case **starlarkstruct.Struct:
result := starlarkstruct.FromStringDict(starlark.String(gotype.Name()), dict)
*val = result
case *starlark.StringDict:
*val = dict
case **starlark.StringDict:
*val = &dict
default:
return fmt.Errorf("target type %T: must be *starlarkstruct.Struct or *starlark.Value", starval)
}
return nil
case reflect.Pointer:
goElem := goval.Elem()
if !goElem.IsValid() {
return nil
}
return goToStarlark(goElem.Interface(), starval)
default:
return fmt.Errorf("unable to convert Go type %T to Starlark type", gov)
}
}
func makeTuple(sliceVal reflect.Value) ([]starlark.Value, error) {
tuple := make([]starlark.Value, sliceVal.Len())
for i := 0; i < sliceVal.Len(); i++ {
var elem starlark.Value
if err := goToStarlark(sliceVal.Index(i).Interface(), &elem); err != nil {
return nil, err
}
tuple[i] = elem
}
return tuple, nil
}
func goMapToDict(mapVal reflect.Value) (*starlark.Dict, error) {
iter := mapVal.MapRange()
dict := starlark.NewDict(mapVal.Len())
for iter.Next() {
// convert key
var key starlark.Value
if err := goToStarlark(iter.Key().Interface(), &key); err != nil {
return nil, fmt.Errorf("GoToStarlrk: failed map key conversion: %s", err)
}
// convert value
var val starlark.Value
if err := goToStarlark(iter.Value().Interface(), &val); err != nil {
return nil, fmt.Errorf("GoToStarlark: failed map value conversion: %s", err)
}
if err := dict.SetKey(key, val); err != nil {
return nil, fmt.Errorf("GoToStarlark: failed to set map value with key: %s", key)
}
}
return dict, nil
}
func goStructToStringDict(goval reflect.Value) (starlark.StringDict, error) {
gotype := goval.Type()
stringDict := make(starlark.StringDict)
for i := 0; i < goval.NumField(); i++ {
field := gotype.Field(i)
// only grab exported field to avoid panic
if !field.IsExported() {
continue
}
fname := field.Name
// get starlarkstruct field name from tag (if any)
name, _ := field.Tag.Lookup("name")
if name != "" {
fname = name
}
var fval starlark.Value
if err := goToStarlark(goval.Field(i).Interface(), &fval); err != nil {
return nil, fmt.Errorf("GoToStarlark: failed struct field conversion: %s", err)
}
stringDict[fname] = fval
}
return stringDict, nil
}