zcldec: BlockSpec decode implementation
This commit is contained in:
parent
e100bf4723
commit
d3703888b6
@ -55,6 +55,76 @@ func TestDecode(t *testing.T) {
|
||||
cty.NumberIntVal(1),
|
||||
0,
|
||||
},
|
||||
{
|
||||
``,
|
||||
&AttrSpec{
|
||||
Name: "a",
|
||||
Type: cty.Number,
|
||||
Required: true,
|
||||
},
|
||||
nil,
|
||||
cty.DynamicVal,
|
||||
1, // attribute "a" is required
|
||||
},
|
||||
|
||||
{
|
||||
`
|
||||
b {
|
||||
}
|
||||
`,
|
||||
&BlockSpec{
|
||||
TypeName: "b",
|
||||
Nested: ObjectSpec{},
|
||||
},
|
||||
nil,
|
||||
cty.EmptyObjectVal,
|
||||
0,
|
||||
},
|
||||
{
|
||||
``,
|
||||
&BlockSpec{
|
||||
TypeName: "b",
|
||||
Nested: ObjectSpec{},
|
||||
},
|
||||
nil,
|
||||
cty.NullVal(cty.DynamicPseudoType),
|
||||
0,
|
||||
},
|
||||
{
|
||||
`a {}`,
|
||||
&BlockSpec{
|
||||
TypeName: "b",
|
||||
Nested: ObjectSpec{},
|
||||
},
|
||||
nil,
|
||||
cty.NullVal(cty.DynamicPseudoType),
|
||||
1, // blocks of type "a" are not supported
|
||||
},
|
||||
{
|
||||
``,
|
||||
&BlockSpec{
|
||||
TypeName: "b",
|
||||
Nested: ObjectSpec{},
|
||||
Required: true,
|
||||
},
|
||||
nil,
|
||||
cty.NullVal(cty.DynamicPseudoType),
|
||||
1, // a block of type "b" is required
|
||||
},
|
||||
{
|
||||
`
|
||||
b {}
|
||||
b {}
|
||||
`,
|
||||
&BlockSpec{
|
||||
TypeName: "b",
|
||||
Nested: ObjectSpec{},
|
||||
Required: true,
|
||||
},
|
||||
nil,
|
||||
cty.EmptyObjectVal,
|
||||
1, // only one "b" block is allowed
|
||||
},
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
|
@ -184,6 +184,15 @@ func (s *BlockSpec) visitSameBodyChildren(cb visitFunc) {
|
||||
// leaf node ("Nested" does not use the same body)
|
||||
}
|
||||
|
||||
// blockSpec implementation
|
||||
func (s *BlockSpec) blockHeaderSchemata() []zcl.BlockHeaderSchema {
|
||||
return []zcl.BlockHeaderSchema{
|
||||
{
|
||||
Type: s.TypeName,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (s *BlockSpec) decode(content *zcl.BodyContent, block *zcl.Block, ctx *zcl.EvalContext) (cty.Value, zcl.Diagnostics) {
|
||||
var diags zcl.Diagnostics
|
||||
|
||||
@ -223,6 +232,9 @@ func (s *BlockSpec) decode(content *zcl.BodyContent, block *zcl.Block, ctx *zcl.
|
||||
return cty.NullVal(cty.DynamicPseudoType), diags
|
||||
}
|
||||
|
||||
if s.Nested == nil {
|
||||
panic("BlockSpec with no Nested Spec")
|
||||
}
|
||||
val, _, childDiags := decode(childBlock.Body, childBlock, ctx, s.Nested, false)
|
||||
diags = append(diags, childDiags...)
|
||||
return val, diags
|
||||
|
Loading…
Reference in New Issue
Block a user