Skip to content

Commit

Permalink
fix(utils): fixes #162
Browse files Browse the repository at this point in the history
  • Loading branch information
bl4ko committed May 13, 2024
1 parent a7afb50 commit 57f5ff1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
7 changes: 7 additions & 0 deletions internal/utils/diff_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,13 @@ func addSliceDiff(newSlice reflect.Value, existingSlice reflect.Value, jsonTag s
if err != nil {
return fmt.Errorf("error converting slice to comparable slice: %s", err)
}

// If second slice is empty or not valid set new
if !existingSlice.IsValid() || existingSlice.Len() == 0 {
diffMap[jsonTag] = newSlice.Interface()
return nil
}

existingSlice, err = convertSliceToComparableSlice(existingSlice)
if err != nil {
return fmt.Errorf("error converting slice to comparable slice: %s", err)
Expand Down
24 changes: 24 additions & 0 deletions internal/utils/diff_map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,30 @@ func Test_addSliceDiff(t *testing.T) {
wantErr: false,
wantDiffMap: map[string]interface{}{"test": []int{1, 2}},
},
{
name: "Has priority. Existing slice is nil",
args: args{
newSlice: reflect.ValueOf([]testStructWithIntIDAttribute{{ID: 1}, {ID: 2}}),
existingSlice: reflect.ValueOf(nil),
jsonTag: "test",
hasPriority: true,
diffMap: map[string]interface{}{},
},
wantErr: false,
wantDiffMap: map[string]interface{}{"test": []int{1, 2}},
},
{
name: "Has priority. Same Existing slice is empty",
args: args{
newSlice: reflect.ValueOf([]testStructWithIntIDAttribute{{ID: 1}, {ID: 2}}),
existingSlice: reflect.ValueOf([]testStructWithIntIDAttribute{}),
jsonTag: "test",
hasPriority: true,
diffMap: map[string]interface{}{},
},
wantErr: false,
wantDiffMap: map[string]interface{}{"test": []int{1, 2}},
},
{
name: "Has priority. Same length and the same. Slice with structs with ID attributes.",
args: args{
Expand Down

0 comments on commit 57f5ff1

Please sign in to comment.