From 7fdc2bfb5ebf8bb77b4d69731384e8e3a04f2887 Mon Sep 17 00:00:00 2001 From: Dmytro Lahoza Date: Wed, 30 Jan 2019 17:15:45 +0200 Subject: [PATCH 1/2] Added Value method to get reflect.Value from underlying entities --- convert/interface.go | 5 +++++ convert/map.go | 5 +++++ convert/slice.go | 7 ++++++- convert/struct.go | 5 +++++ 4 files changed, 21 insertions(+), 1 deletion(-) diff --git a/convert/interface.go b/convert/interface.go index 2f541df..a8663da 100644 --- a/convert/interface.go +++ b/convert/interface.go @@ -43,6 +43,11 @@ type GoInterface struct { v reflect.Value } +// Value returns relfect.Value of the underlying interface +func (g *GoInterface) Value() reflect.Value { + return g.v +} + // Attr returns a starlark value that wraps the method or field with the given // name. func (g *GoInterface) Attr(name string) (starlark.Value, error) { diff --git a/convert/map.go b/convert/map.go index 7cadeb3..69d3afb 100644 --- a/convert/map.go +++ b/convert/map.go @@ -32,6 +32,11 @@ func NewGoMap(m interface{}) *GoMap { return &GoMap{v: v} } +// Value returns relfect.Value of the underlying map +func (g *GoMap) Value() reflect.Value { + return g.v +} + // SetKey implements starlark.HasSetKey. func (g *GoMap) SetKey(k, v starlark.Value) (err error) { if g.frozen { diff --git a/convert/slice.go b/convert/slice.go index d5ad810..f89cd45 100644 --- a/convert/slice.go +++ b/convert/slice.go @@ -21,7 +21,7 @@ type GoSlice struct { frozen bool } -// NewGoMap wraps the given slice in a new GoSlice. This function will panic if m +// NewGoSlice wraps the given slice in a new GoSlice. This function will panic if m // is not a map. func NewGoSlice(slice interface{}) *GoSlice { v := reflect.ValueOf(slice) @@ -31,6 +31,11 @@ func NewGoSlice(slice interface{}) *GoSlice { return &GoSlice{v: v} } +// Value returns relfect.Value of the underlying slice +func (g *GoSlice) Value() reflect.Value { + return g.v +} + // String returns the string representation of the value. // Starlark string values are quoted as if by Python's repr. func (g *GoSlice) String() string { diff --git a/convert/struct.go b/convert/struct.go index 12f398e..f6a46e9 100644 --- a/convert/struct.go +++ b/convert/struct.go @@ -24,6 +24,11 @@ type GoStruct struct { v reflect.Value } +// Value returns relfect.Value of the underlying struct +func (g *GoStruct) Value() reflect.Value { + return g.v +} + // Attr returns a starlark value that wraps the method or field with the given // name. func (g *GoStruct) Attr(name string) (starlark.Value, error) { From cc75178c5236e2a5317865f6a741a0af48ac5663 Mon Sep 17 00:00:00 2001 From: Dmytro Lahoza Date: Thu, 31 Jan 2019 15:20:40 +0200 Subject: [PATCH 2/2] FromDict should convert values too --- convert/conv.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/convert/conv.go b/convert/conv.go index a6f41c3..5961d56 100644 --- a/convert/conv.go +++ b/convert/conv.go @@ -200,7 +200,7 @@ func FromDict(m *starlark.Dict) map[interface{}]interface{} { key := FromValue(k) // should never be not found or unhashable, so ignore err and found. val, _, _ := m.Get(k) - ret[key] = val + ret[key] = FromValue(val) } return ret }