Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support persisting empty fields in StructToData #3362

Merged
merged 1 commit into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading