Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,12 @@ func Unmarshal(x starlark.Value) (val interface{}, err error) {
}
val = _var
} else {
err = fmt.Errorf("constructor object from *starlarkstruct.Struct not supported Marshaler to starlark object: %s", v.Constructor().Type())
dict := new(starlark.Dict)
for _, name := range v.AttrNames() {
val, _ := v.Attr(name)
dict.SetKey(starlark.String(name), val)
}
val, err = Unmarshal(dict)
}
default:
fmt.Println("errbadtype:", x.Type())
Expand Down
24 changes: 24 additions & 0 deletions util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,22 @@ func TestUnmarshal(t *testing.T) {
strDictCT.SetKey(starlark.String("foo"), starlark.MakeInt(42))
strDictCT.SetKey(starlark.String("bar"), ct)

strDict2 := make(starlark.StringDict)
strDict2["int"] = starlark.MakeInt(42)
strDict2["ct"] = ct
strDict2["int_dict"] = intDict
strDict2["dict_with_ct"] = strDictCT

struct1 := starlarkstruct.FromStringDict(starlarkstruct.Default, starlark.StringDict{
"int": starlark.MakeInt(42),
"ct": ct,
"int_dict": intDict,
"dict_with_ct": strDictCT,
})
struct2 := starlarkstruct.FromStringDict(starlarkstruct.Default, starlark.StringDict{
"struct": struct1,
})

cases := []struct {
in starlark.Value
want interface{}
Expand Down Expand Up @@ -139,6 +155,14 @@ func TestUnmarshal(t *testing.T) {
{strDictCT, map[string]interface{}{"foo": 42, "bar": &customType{42}}, ""},
{starlark.NewList([]starlark.Value{starlark.MakeInt(42), ct}), []interface{}{42, &customType{42}}, ""},
{starlark.Tuple{starlark.String("foo"), starlark.MakeInt(42)}, []interface{}{"foo", 42}, ""},
{struct2, map[string]interface{}{
"struct": map[string]interface{}{
"int": 42,
"ct": &customType{42},
"int_dict": map[interface{}]interface{}{42 * 2: 42},
"dict_with_ct": map[string]interface{}{"foo": 42, "bar": &customType{42}},
},
}, ""},
}

for i, c := range cases {
Expand Down