[#1365] fixed Record.MergeExpand panic
This commit is contained in:
parent
f91f009fce
commit
4abc8ae021
|
@ -139,6 +139,10 @@ func (m *Record) SetExpand(expand map[string]any) {
|
||||||
// then both old and new records will be merged into a new slice (aka. a :merge: [b,c] => [a,b,c]).
|
// then both old and new records will be merged into a new slice (aka. a :merge: [b,c] => [a,b,c]).
|
||||||
// Otherwise the "old" expanded record will be replace with the "new" one (aka. a :merge: aNew => aNew).
|
// Otherwise the "old" expanded record will be replace with the "new" one (aka. a :merge: aNew => aNew).
|
||||||
func (m *Record) MergeExpand(expand map[string]any) {
|
func (m *Record) MergeExpand(expand map[string]any) {
|
||||||
|
if m.expand == nil && len(expand) > 0 {
|
||||||
|
m.expand = make(map[string]any)
|
||||||
|
}
|
||||||
|
|
||||||
for key, new := range expand {
|
for key, new := range expand {
|
||||||
old, ok := m.expand[key]
|
old, ok := m.expand[key]
|
||||||
if !ok {
|
if !ok {
|
||||||
|
|
|
@ -375,7 +375,7 @@ func TestRecordSetAndGetExpand(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRecordMergeExpandDirect(t *testing.T) {
|
func TestRecordMergeExpand(t *testing.T) {
|
||||||
collection := &models.Collection{}
|
collection := &models.Collection{}
|
||||||
m := models.NewRecord(collection)
|
m := models.NewRecord(collection)
|
||||||
m.Id = "m"
|
m.Id = "m"
|
||||||
|
@ -467,6 +467,47 @@ func TestRecordMergeExpandDirect(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRecordMergeExpandNilCheck(t *testing.T) {
|
||||||
|
collection := &models.Collection{}
|
||||||
|
|
||||||
|
scenarios := []struct {
|
||||||
|
name string
|
||||||
|
expand map[string]any
|
||||||
|
expected string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
"nil expand",
|
||||||
|
nil,
|
||||||
|
`{"collectionId":"","collectionName":"","created":"","id":"","updated":""}`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"empty expand",
|
||||||
|
map[string]any{},
|
||||||
|
`{"collectionId":"","collectionName":"","created":"","id":"","updated":""}`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"non-empty expand",
|
||||||
|
map[string]any{"test": models.NewRecord(collection)},
|
||||||
|
`{"collectionId":"","collectionName":"","created":"","expand":{"test":{"collectionId":"","collectionName":"","created":"","id":"","updated":""}},"id":"","updated":""}`,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, s := range scenarios {
|
||||||
|
m := models.NewRecord(collection)
|
||||||
|
m.MergeExpand(s.expand)
|
||||||
|
|
||||||
|
raw, err := json.Marshal(m)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
rawStr := string(raw)
|
||||||
|
|
||||||
|
if rawStr != s.expected {
|
||||||
|
t.Fatalf("[%s] Expected \n%v, \ngot \n%v", s.name, s.expected, rawStr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestRecordSchemaData(t *testing.T) {
|
func TestRecordSchemaData(t *testing.T) {
|
||||||
collection := &models.Collection{
|
collection := &models.Collection{
|
||||||
Type: models.CollectionTypeAuth,
|
Type: models.CollectionTypeAuth,
|
||||||
|
|
Loading…
Reference in New Issue