2017-10-03 01:07:39 +02:00
|
|
|
package configschema
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/hashicorp/hcl2/hcldec"
|
|
|
|
)
|
|
|
|
|
2017-10-04 02:16:04 +02:00
|
|
|
var mapLabelNames = []string{"key"}
|
|
|
|
|
|
|
|
// DecoderSpec returns a hcldec.Spec that can be used to decode a HCL Body
|
|
|
|
// using the facilities in the hcldec package.
|
2017-10-03 01:07:39 +02:00
|
|
|
//
|
|
|
|
// The returned specification is guaranteed to return a value of the same type
|
2018-07-20 18:40:22 +02:00
|
|
|
// returned by method ImpliedType, but it may contain null values if any of the
|
|
|
|
// block attributes are defined as optional and/or computed respectively.
|
2017-10-03 01:07:39 +02:00
|
|
|
func (b *Block) DecoderSpec() hcldec.Spec {
|
2017-10-04 02:16:04 +02:00
|
|
|
ret := hcldec.ObjectSpec{}
|
|
|
|
if b == nil {
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, attrS := range b.Attributes {
|
2019-02-23 01:52:11 +01:00
|
|
|
ret[name] = attrS.decoderSpec(name)
|
2017-10-04 02:16:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for name, blockS := range b.BlockTypes {
|
|
|
|
if _, exists := ret[name]; exists {
|
|
|
|
// This indicates an invalid schema, since it's not valid to
|
|
|
|
// define both an attribute and a block type of the same name.
|
|
|
|
// However, we don't raise this here since it's checked by
|
|
|
|
// InternalValidate.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
childSpec := blockS.Block.DecoderSpec()
|
|
|
|
|
|
|
|
switch blockS.Nesting {
|
|
|
|
case NestingSingle:
|
|
|
|
ret[name] = &hcldec.BlockSpec{
|
|
|
|
TypeName: name,
|
|
|
|
Nested: childSpec,
|
|
|
|
Required: blockS.MinItems == 1 && blockS.MaxItems >= 1,
|
|
|
|
}
|
|
|
|
case NestingList:
|
2018-08-23 00:45:34 +02:00
|
|
|
// We prefer to use a list where possible, since it makes our
|
|
|
|
// implied type more complete, but if there are any
|
|
|
|
// dynamically-typed attributes inside we must use a tuple
|
|
|
|
// instead, at the expense of our type then not being predictable.
|
|
|
|
if blockS.Block.ImpliedType().HasDynamicTypes() {
|
|
|
|
ret[name] = &hcldec.BlockTupleSpec{
|
|
|
|
TypeName: name,
|
|
|
|
Nested: childSpec,
|
|
|
|
MinItems: blockS.MinItems,
|
|
|
|
MaxItems: blockS.MaxItems,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ret[name] = &hcldec.BlockListSpec{
|
|
|
|
TypeName: name,
|
|
|
|
Nested: childSpec,
|
|
|
|
MinItems: blockS.MinItems,
|
|
|
|
MaxItems: blockS.MaxItems,
|
|
|
|
}
|
2017-10-04 02:16:04 +02:00
|
|
|
}
|
|
|
|
case NestingSet:
|
2018-08-23 00:45:34 +02:00
|
|
|
// We forbid dynamically-typed attributes inside NestingSet in
|
|
|
|
// InternalValidate, so we don't do anything special to handle
|
|
|
|
// that here. (There is no set analog to tuple and object types,
|
|
|
|
// because cty's set implementation depends on knowing the static
|
|
|
|
// type in order to properly compute its internal hashes.)
|
2017-10-04 02:16:04 +02:00
|
|
|
ret[name] = &hcldec.BlockSetSpec{
|
|
|
|
TypeName: name,
|
|
|
|
Nested: childSpec,
|
|
|
|
MinItems: blockS.MinItems,
|
|
|
|
MaxItems: blockS.MaxItems,
|
|
|
|
}
|
|
|
|
case NestingMap:
|
2018-08-23 00:45:34 +02:00
|
|
|
// We prefer to use a list where possible, since it makes our
|
|
|
|
// implied type more complete, but if there are any
|
|
|
|
// dynamically-typed attributes inside we must use a tuple
|
|
|
|
// instead, at the expense of our type then not being predictable.
|
|
|
|
if blockS.Block.ImpliedType().HasDynamicTypes() {
|
|
|
|
ret[name] = &hcldec.BlockObjectSpec{
|
|
|
|
TypeName: name,
|
|
|
|
Nested: childSpec,
|
|
|
|
LabelNames: mapLabelNames,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ret[name] = &hcldec.BlockMapSpec{
|
|
|
|
TypeName: name,
|
|
|
|
Nested: childSpec,
|
|
|
|
LabelNames: mapLabelNames,
|
|
|
|
}
|
2017-10-04 02:16:04 +02:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
// Invalid nesting type is just ignored. It's checked by
|
|
|
|
// InternalValidate.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
2017-10-03 01:07:39 +02:00
|
|
|
}
|
2019-02-23 01:52:11 +01:00
|
|
|
|
|
|
|
func (a *Attribute) decoderSpec(name string) hcldec.Spec {
|
|
|
|
return &hcldec.AttrSpec{
|
|
|
|
Name: name,
|
|
|
|
Type: a.Type,
|
|
|
|
Required: a.Required,
|
|
|
|
}
|
|
|
|
}
|