Skip to content

Commit fe788ce

Browse files
author
Liam Cervante
committed
config generation: stop editing the original schema when filtering (#35484)
* config generation: stop editing the original schema when filtering * copywrite headers
1 parent a4e4077 commit fe788ce

File tree

3 files changed

+85
-5
lines changed

3 files changed

+85
-5
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: BUSL-1.1
3+
4+
package configschema
5+
6+
// DeepCopy returns a deep copy of the schema.
7+
func (b *Block) DeepCopy() *Block {
8+
block := &Block{
9+
Description: b.Description,
10+
DescriptionKind: b.DescriptionKind,
11+
Deprecated: b.Deprecated,
12+
}
13+
14+
if b.Attributes != nil {
15+
block.Attributes = make(map[string]*Attribute, len(b.Attributes))
16+
}
17+
for name, a := range b.Attributes {
18+
block.Attributes[name] = a.DeepCopy()
19+
}
20+
21+
if b.BlockTypes != nil {
22+
block.BlockTypes = make(map[string]*NestedBlock, len(b.BlockTypes))
23+
}
24+
for name, bt := range b.BlockTypes {
25+
inner := bt.Block.DeepCopy()
26+
block.BlockTypes[name] = &NestedBlock{
27+
Block: *inner,
28+
Nesting: bt.Nesting,
29+
MinItems: bt.MinItems,
30+
MaxItems: bt.MaxItems,
31+
}
32+
}
33+
34+
return block
35+
}
36+
37+
// DeepCopy returns a deep copy of the schema.
38+
func (a *Attribute) DeepCopy() *Attribute {
39+
attr := &Attribute{
40+
Type: a.Type,
41+
Description: a.Description,
42+
DescriptionKind: a.DescriptionKind,
43+
Deprecated: a.Deprecated,
44+
Required: a.Required,
45+
Computed: a.Computed,
46+
Optional: a.Optional,
47+
Sensitive: a.Sensitive,
48+
49+
// NestedType is not copied here because it will be copied
50+
// separately if it is set.
51+
NestedType: nil,
52+
}
53+
if a.NestedType != nil {
54+
attr.NestedType = a.NestedType.DeepCopy()
55+
}
56+
return attr
57+
}
58+
59+
// DeepCopy returns a deep copy of the schema.
60+
func (o *Object) DeepCopy() *Object {
61+
object := &Object{
62+
Nesting: o.Nesting,
63+
}
64+
if o.Attributes != nil {
65+
object.Attributes = make(map[string]*Attribute, len(o.Attributes))
66+
for name, a := range o.Attributes {
67+
object.Attributes[name] = a.DeepCopy()
68+
}
69+
}
70+
return object
71+
}

internal/configs/configschema/filter.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,11 @@ func (b *Block) filter(path cty.Path, filterAttribute FilterT[*Attribute], filte
5656
for name, attrS := range b.Attributes {
5757
path := path.GetAttr(name)
5858
if filterAttribute == nil || !filterAttribute(path, attrS) {
59-
ret.Attributes[name] = attrS
59+
attr := *attrS
6060
if attrS.NestedType != nil {
61-
ret.Attributes[name].NestedType = filterNestedType(attrS.NestedType, path, filterAttribute)
61+
attr.NestedType = filterNestedType(attrS.NestedType, path, filterAttribute)
6262
}
63+
ret.Attributes[name] = &attr
6364
}
6465
}
6566

@@ -95,10 +96,11 @@ func filterNestedType(obj *Object, path cty.Path, filterAttribute FilterT[*Attri
9596
for name, attrS := range obj.Attributes {
9697
path := path.GetAttr(name)
9798
if filterAttribute == nil || !filterAttribute(path, attrS) {
98-
ret.Attributes[name] = attrS
99+
attr := *attrS
99100
if attrS.NestedType != nil {
100-
ret.Attributes[name].NestedType = filterNestedType(attrS.NestedType, path, filterAttribute)
101+
attr.NestedType = filterNestedType(attrS.NestedType, path, filterAttribute)
101102
}
103+
ret.Attributes[name] = &attr
102104
}
103105
}
104106

internal/configs/configschema/filter_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,9 +278,16 @@ func TestFilter(t *testing.T) {
278278

279279
for name, tc := range testCases {
280280
t.Run(name, func(t *testing.T) {
281+
original := tc.schema.DeepCopy()
282+
281283
got := tc.schema.Filter(tc.filterAttribute, tc.filterBlock)
282284
if !cmp.Equal(got, tc.want, cmp.Comparer(cty.Type.Equals), cmpopts.EquateEmpty()) {
283-
t.Fatal(cmp.Diff(got, tc.want, cmp.Comparer(cty.Type.Equals), cmpopts.EquateEmpty()))
285+
t.Error(cmp.Diff(got, tc.want, cmp.Comparer(cty.Type.Equals), cmpopts.EquateEmpty()))
286+
}
287+
288+
// We shouldn't have edited the original schema.
289+
if !cmp.Equal(tc.schema, original, cmp.Comparer(cty.Type.Equals), cmpopts.EquateEmpty()) {
290+
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()))
284291
}
285292
})
286293
}

0 commit comments

Comments
 (0)