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

config generation: stop editing the original schema when filtering #35484

Merged
merged 2 commits into from
Jul 23, 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
71 changes: 71 additions & 0 deletions internal/configs/configschema/copy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1

package configschema

// DeepCopy returns a deep copy of the schema.
func (b *Block) DeepCopy() *Block {
block := &Block{
Description: b.Description,
DescriptionKind: b.DescriptionKind,
Deprecated: b.Deprecated,
}

if b.Attributes != nil {
block.Attributes = make(map[string]*Attribute, len(b.Attributes))
}
for name, a := range b.Attributes {
block.Attributes[name] = a.DeepCopy()
}

if b.BlockTypes != nil {
block.BlockTypes = make(map[string]*NestedBlock, len(b.BlockTypes))
}
for name, bt := range b.BlockTypes {
inner := bt.Block.DeepCopy()
block.BlockTypes[name] = &NestedBlock{
Block: *inner,
Nesting: bt.Nesting,
MinItems: bt.MinItems,
MaxItems: bt.MaxItems,
}
}

return block
}

// DeepCopy returns a deep copy of the schema.
func (a *Attribute) DeepCopy() *Attribute {
attr := &Attribute{
Type: a.Type,
Description: a.Description,
DescriptionKind: a.DescriptionKind,
Deprecated: a.Deprecated,
Required: a.Required,
Computed: a.Computed,
Optional: a.Optional,
Sensitive: a.Sensitive,

// NestedType is not copied here because it will be copied
// separately if it is set.
NestedType: nil,
}
if a.NestedType != nil {
attr.NestedType = a.NestedType.DeepCopy()
}
return attr
}

// DeepCopy returns a deep copy of the schema.
func (o *Object) DeepCopy() *Object {
object := &Object{
Nesting: o.Nesting,
}
if o.Attributes != nil {
object.Attributes = make(map[string]*Attribute, len(o.Attributes))
for name, a := range o.Attributes {
object.Attributes[name] = a.DeepCopy()
}
}
return object
}
10 changes: 6 additions & 4 deletions internal/configs/configschema/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,11 @@ func (b *Block) filter(path cty.Path, filterAttribute FilterT[*Attribute], filte
for name, attrS := range b.Attributes {
path := path.GetAttr(name)
if filterAttribute == nil || !filterAttribute(path, attrS) {
ret.Attributes[name] = attrS
attr := *attrS
if attrS.NestedType != nil {
ret.Attributes[name].NestedType = filterNestedType(attrS.NestedType, path, filterAttribute)
attr.NestedType = filterNestedType(attrS.NestedType, path, filterAttribute)
}
ret.Attributes[name] = &attr
}
}

Expand Down Expand Up @@ -95,10 +96,11 @@ func filterNestedType(obj *Object, path cty.Path, filterAttribute FilterT[*Attri
for name, attrS := range obj.Attributes {
path := path.GetAttr(name)
if filterAttribute == nil || !filterAttribute(path, attrS) {
ret.Attributes[name] = attrS
attr := *attrS
if attrS.NestedType != nil {
ret.Attributes[name].NestedType = filterNestedType(attrS.NestedType, path, filterAttribute)
attr.NestedType = filterNestedType(attrS.NestedType, path, filterAttribute)
}
ret.Attributes[name] = &attr
}
}

Expand Down
9 changes: 8 additions & 1 deletion internal/configs/configschema/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,16 @@ func TestFilter(t *testing.T) {

for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
original := tc.schema.DeepCopy()

got := tc.schema.Filter(tc.filterAttribute, tc.filterBlock)
if !cmp.Equal(got, tc.want, cmp.Comparer(cty.Type.Equals), cmpopts.EquateEmpty()) {
t.Fatal(cmp.Diff(got, tc.want, cmp.Comparer(cty.Type.Equals), cmpopts.EquateEmpty()))
t.Error(cmp.Diff(got, tc.want, cmp.Comparer(cty.Type.Equals), cmpopts.EquateEmpty()))
}

// We shouldn't have edited the original schema.
if !cmp.Equal(tc.schema, original, cmp.Comparer(cty.Type.Equals), cmpopts.EquateEmpty()) {
t.Errorf("the original schema was edited when it shouldn't have been: %s", cmp.Diff(tc.schema, original, cmp.Comparer(cty.Type.Equals), cmpopts.EquateEmpty()))
}
})
}
Expand Down
Loading