From e6978578c0e8ca94af9b650cf96eb182cc092543 Mon Sep 17 00:00:00 2001 From: Oliver Lade Date: Wed, 28 Apr 2021 13:14:45 +1000 Subject: [PATCH] Make slice mapping consistent --- rel/value.go | 2 -- rel/value_test.go | 26 +++++++++++++++++--------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/rel/value.go b/rel/value.go index 927e38c6..d63566b9 100644 --- a/rel/value.go +++ b/rel/value.go @@ -215,8 +215,6 @@ func NewValue(v interface{}) (Value, error) { return NewBytes(x), nil case map[string]interface{}: return NewTupleFromMap(x) - case []interface{}: - return NewSetFrom(x...) default: // Fall back on reflection for custom types. return reflectNewValue(reflect.ValueOf(x)) diff --git a/rel/value_test.go b/rel/value_test.go index 2e6f4d0a..a7e7a71c 100644 --- a/rel/value_test.go +++ b/rel/value_test.go @@ -41,7 +41,7 @@ func TestSetCall(t *testing.T) { } //nolint:structcheck -func TestReflectNewValue(t *testing.T) { +func TestNewValue(t *testing.T) { // Structs are serialized to tuples. type Foo struct { num int @@ -49,26 +49,30 @@ func TestReflectNewValue(t *testing.T) { // Slices without the ordered tag are serialized to arrays. arr []int // Slices with the unordered tag are serialized to sets. - set []int `unordered:"true"` + set []int `unordered:"true"` + iset []interface{} `unordered:"true"` none *Foo // All struct field names are serialized to start lowercase. CASE int children []*Foo // Non-string maps are serialized to dictionaries. - mixedMap map[interface{}]interface{} + mixedMap map[interface{}]interface{} + stringMap map[string]interface{} } input := []*Foo{{ - num: 1, - str: "a", - arr: []int{2, 1}, - set: []int{2, 1}, + num: 1, + str: "a", + arr: []int{2, 1}, + set: []int{2, 1}, + iset: []interface{}{3}, // Nil values are serialized to empty sets (None). none: nil, CASE: 0, // Unset fields of structs are serialized with default empty values. - children: []*Foo{{num: 2}}, - mixedMap: map[interface{}]interface{}{1: 2, "k": nil}, + children: []*Foo{{num: 2}}, + mixedMap: map[interface{}]interface{}{1: 2, "k": nil}, + stringMap: map[string]interface{}{"a": 1}, }} actual, err := NewValue(input) @@ -79,20 +83,24 @@ func TestReflectNewValue(t *testing.T) { NewStringAttr("str", []rune("a")), NewAttr("arr", NewArray(NewNumber(2), NewNumber(1))), NewAttr("set", MustNewSet(NewNumber(1), NewNumber(2))), + NewAttr("iset", MustNewSet(NewNumber(3))), NewAttr("none", None), NewAttr("cASE", NewNumber(0)), NewAttr("mixedMap", MustNewDict(false, NewDictEntryTuple(NewNumber(1), NewNumber(2)), NewDictEntryTuple(NewString([]rune("k")), None), )), + NewAttr("stringMap", NewTuple(NewIntAttr("a", 1))), NewAttr("children", NewArray(NewTuple( NewAttr("num", NewNumber(2)), NewAttr("str", None), NewAttr("arr", None), NewAttr("set", None), + NewAttr("iset", None), NewAttr("none", None), NewAttr("cASE", NewNumber(0)), NewAttr("mixedMap", None), + NewAttr("stringMap", NewTuple()), NewAttr("children", None), ))), ))