Skip to content

Commit

Permalink
starbind: fix Starlark slice unmarshaling (#3454)
Browse files Browse the repository at this point in the history
The unmarshaling code for slices wasn't actually setting the
destination. This patch fixes it.
  • Loading branch information
andreimatei committed Aug 7, 2023
1 parent b5c9edc commit ae67a45
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
3 changes: 2 additions & 1 deletion pkg/terminal/starbind/conv.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 29 additions & 0 deletions pkg/terminal/starbind/conv_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit ae67a45

Please sign in to comment.