Skip to content

Commit c346c16

Browse files
authored
Merge pull request #37494 from hashicorp/jbardin/genconfig-refactor
refactor config generation
2 parents 2463f86 + ee6588a commit c346c16

File tree

5 files changed

+258
-164
lines changed

5 files changed

+258
-164
lines changed

internal/configs/configschema/path.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,23 @@ func (o *Object) AttributeByPath(path cty.Path) *Attribute {
5656
}
5757
return nil
5858
}
59+
60+
// BlockByPath looks up the Block schema which corresponds to the given
61+
// cty.Path. A nil value is returned if the given path does not correspond to a
62+
// specific attribute.
63+
func (b *Block) BlockByPath(path cty.Path) *Block {
64+
for i, step := range path {
65+
switch step := step.(type) {
66+
case cty.GetAttrStep:
67+
if blockType := b.BlockTypes[step.Name]; blockType != nil {
68+
if len(blockType.Block.BlockTypes) > 0 && i < len(path)-1 {
69+
return blockType.Block.BlockByPath(path[i+1:])
70+
} else if i < len(path)-1 {
71+
return nil
72+
}
73+
return &blockType.Block
74+
}
75+
}
76+
}
77+
return nil
78+
}

internal/configs/configschema/path_test.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package configschema
55

66
import (
7+
"fmt"
78
"testing"
89

910
"github.com/zclconf/go-cty/cty"
@@ -256,5 +257,88 @@ func TestObject_AttributeByPath(t *testing.T) {
256257
}
257258
})
258259
}
260+
}
261+
262+
func TestBlockByPath(t *testing.T) {
263+
schema := &Block{
264+
BlockTypes: map[string]*NestedBlock{
265+
"b1": {
266+
Nesting: NestingList,
267+
Block: Block{
268+
Attributes: map[string]*Attribute{
269+
"a3": {Description: "a3"},
270+
"a4": {Description: "a4"},
271+
},
272+
BlockTypes: map[string]*NestedBlock{
273+
"b2": {
274+
Nesting: NestingMap,
275+
Block: Block{
276+
Attributes: map[string]*Attribute{
277+
"a5": {Description: "a5"},
278+
"a6": {Description: "a6"},
279+
},
280+
},
281+
},
282+
},
283+
},
284+
},
285+
"b3": {
286+
Nesting: NestingMap,
287+
Block: Block{
288+
Attributes: map[string]*Attribute{
289+
"a7": {Description: "a7"},
290+
"a8": {Description: "a8"},
291+
},
292+
BlockTypes: map[string]*NestedBlock{
293+
"b4": {
294+
Nesting: NestingSet,
295+
Block: Block{
296+
Attributes: map[string]*Attribute{
297+
"a9": {Description: "a9"},
298+
"a10": {Description: "a10"},
299+
},
300+
},
301+
},
302+
},
303+
},
304+
},
305+
},
306+
}
259307

308+
for i, tc := range []struct {
309+
path cty.Path
310+
exists bool
311+
}{
312+
{
313+
cty.GetAttrPath("b1").IndexInt(1).GetAttr("b2"),
314+
true,
315+
},
316+
{
317+
cty.GetAttrPath("b1"),
318+
true,
319+
},
320+
{
321+
cty.GetAttrPath("b2"),
322+
false,
323+
},
324+
{
325+
cty.GetAttrPath("b3").IndexString("foo").GetAttr("b2"),
326+
false,
327+
},
328+
{
329+
cty.GetAttrPath("b3").IndexString("foo").GetAttr("b4"),
330+
true,
331+
},
332+
} {
333+
t.Run(fmt.Sprint(i), func(t *testing.T) {
334+
block := schema.BlockByPath(tc.path)
335+
if !tc.exists && block == nil {
336+
return
337+
}
338+
339+
if block == nil {
340+
t.Fatalf("missing block from path %#v\n", tc.path)
341+
}
342+
})
343+
}
260344
}

0 commit comments

Comments
 (0)