Skip to content

Commit

Permalink
Fix empty slice assigned as nil
Browse files Browse the repository at this point in the history
If tf schema is updated to empty, the sdk obj should be updated to empty
slice instead of nil slice, so that it will be in the PATCH call to
remove and clear the list on NSX

Signed-off-by: Shawn Wang <[email protected]>
  • Loading branch information
wsquan171 committed May 31, 2024
1 parent 81873ca commit 4d81c34
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
4 changes: 0 additions & 4 deletions nsxt/metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,6 @@ func SchemaToStruct(elem reflect.Value, d *schema.ResourceData, metadata map[str
}
}

if len(itemList) == 0 {
continue
}

// List of string, bool, int
if childElem, ok := item.Schema.Elem.(*ExtendedSchema); ok {
sliceElem := elem.FieldByName(item.Metadata.SdkFieldName)
Expand Down
29 changes: 29 additions & 0 deletions nsxt/metadata/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -685,3 +685,32 @@ func TestSchemaToStruct(t *testing.T) {
}, obj.DeepNestedStruct)
})
}

func TestSchemaToStructEmptySlice(t *testing.T) {
d := schema.TestResourceDataRaw(
t, testSchema, map[string]interface{}{
"bool_list": []interface{}{},
"int_set": []interface{}{},
"struct_list": []interface{}{},
})

obj := testDeepNestedStruct{}
elem := reflect.ValueOf(&obj).Elem()
testSch := mixedStructSchema().Elem.(*ExtendedResource).Schema
SchemaToStruct(elem, d, testSch, "", nil)

t.Run("Empty bool list", func(t *testing.T) {
assert.NotNil(t, obj.BoolList)
assert.Equal(t, 0, len(obj.BoolList))
})

t.Run("Empty int set", func(t *testing.T) {
assert.NotNil(t, obj.IntSet)
assert.Equal(t, 0, len(obj.IntSet))
})

t.Run("Struct list", func(t *testing.T) {
assert.NotNil(t, obj.StructList)
assert.Equal(t, 0, len(obj.StructList))
})
}

0 comments on commit 4d81c34

Please sign in to comment.