Skip to content

Commit

Permalink
Support persisting empty fields (#3362)
Browse files Browse the repository at this point in the history
  • Loading branch information
mgyucht authored Mar 15, 2024
1 parent 11bceaa commit ce43ce3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
3 changes: 0 additions & 3 deletions common/reflect_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -613,9 +613,6 @@ func collectionToMaps(v any, s *schema.Schema, aliases map[string]map[string]str
if err != nil {
return nil, err
}
if len(data) == 0 {
continue
}
resultList = append(resultList, data)
}
return resultList, nil
Expand Down
38 changes: 38 additions & 0 deletions common/reflect_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,44 @@ func TestDataToStructPointerWithResourceProviderStruct(t *testing.T) {
DataToStructPointer(d, s, &dummyCopy)
}

func TestStructToData_EmptyField(t *testing.T) {
type EmptyField struct{}
type Container struct {
EmptyField *EmptyField `json:"empty_field,omitempty"`
}
s := StructToSchema(Container{}, nil)
assert.NotNil(t, s)

dummy := Container{
EmptyField: &EmptyField{},
}

d := schema.TestResourceDataRaw(t, s, map[string]any{})
d.MarkNewResource()
err := StructToData(dummy, s, d)
assert.NoError(t, err)
assert.Equal(t, 1, d.Get("empty_field.#"))
}

func TestStructToData_EmptyFieldNil(t *testing.T) {
type EmptyField struct{}
type Container struct {
EmptyField *EmptyField `json:"empty_field,omitempty"`
}
s := StructToSchema(Container{}, nil)
assert.NotNil(t, s)

dummy := Container{
EmptyField: nil,
}

d := schema.TestResourceDataRaw(t, s, map[string]any{})
d.MarkNewResource()
err := StructToData(dummy, s, d)
assert.NoError(t, err)
assert.Equal(t, 0, d.Get("empty_field.#"))
}

func TestStructToData(t *testing.T) {
s := StructToSchema(Dummy{}, func(s map[string]*schema.Schema) map[string]*schema.Schema {
return s
Expand Down

0 comments on commit ce43ce3

Please sign in to comment.