starbind: fix Starlark slice unmarshaling (#3454)

The unmarshaling code for slices wasn't actually setting the
destination. This patch fixes it.
This commit is contained in:
Andrei Matei 2023-08-07 15:11:05 -04:00 committed by GitHub
parent b5c9edccff
commit ae67a45a1c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 1 deletions

@ -664,8 +664,9 @@ func unmarshalStarlarkValueIntl(val starlark.Value, dst reflect.Value, path stri
if dst.Kind() != reflect.Slice {
return converr()
}
dst.Set(reflect.MakeSlice(dst.Type(), val.Len(), val.Len()))
for i := 0; i < val.Len(); i++ {
cur := reflect.New(dst.Type().Elem())
cur := dst.Index(i).Addr()
err := unmarshalStarlarkValueIntl(val.Index(i), cur, path)
if err != nil {
return err

@ -0,0 +1,29 @@
package starbind
import (
"go.starlark.net/starlark"
"testing"
)
func TestConv(t *testing.T) {
script := `
# A list global that we'll unmarhsal into a slice.
x = [1,2]
`
globals, err := starlark.ExecFile(&starlark.Thread{}, "test.star", script, nil)
starlarkVal, ok := globals["x"]
if !ok {
t.Fatal("missing global 'x'")
}
if err != nil {
t.Fatal(err)
}
var x []int
err = unmarshalStarlarkValue(starlarkVal, &x, "x")
if err != nil {
t.Fatal(err)
}
if len(x) != 2 || x[0] != 1 || x[1] != 2 {
t.Fatalf("expected [1 2], got: %v", x)
}
}