zcldec: BlockSpec decode implementation

This commit is contained in:
Martin Atkins 2017-06-04 07:43:13 -07:00
parent e100bf4723
commit d3703888b6
2 changed files with 82 additions and 0 deletions

View File

@ -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 {

View File

@ -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