Skip to content

Commit

Permalink
Make slice mapping consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
orlade-anz committed Apr 28, 2021
1 parent c4631ed commit e697857
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
2 changes: 0 additions & 2 deletions rel/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
26 changes: 17 additions & 9 deletions rel/value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,34 +41,38 @@ 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
str string
// 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)
Expand All @@ -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),
))),
))
Expand Down

0 comments on commit e697857

Please sign in to comment.