2017-10-03 01:07:39 +02:00
|
|
|
package configschema
|
|
|
|
|
|
|
|
import (
|
2020-10-13 21:22:37 +02:00
|
|
|
"runtime"
|
|
|
|
"sync"
|
|
|
|
"unsafe"
|
|
|
|
|
2019-09-10 00:58:44 +02:00
|
|
|
"github.com/hashicorp/hcl/v2/hcldec"
|
2021-02-05 19:34:55 +01:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
2017-10-03 01:07:39 +02:00
|
|
|
)
|
|
|
|
|
2017-10-04 02:16:04 +02:00
|
|
|
var mapLabelNames = []string{"key"}
|
|
|
|
|
2020-10-13 21:22:37 +02:00
|
|
|
// specCache is a global cache of all the generated hcldec.Spec values for
|
|
|
|
// Blocks. This cache is used by the Block.DecoderSpec method to memoize calls
|
|
|
|
// and prevent unnecessary regeneration of the spec, especially when they are
|
|
|
|
// large and deeply nested.
|
|
|
|
// Caching these externally rather than within the struct is required because
|
|
|
|
// Blocks are used by value and copied when working with NestedBlocks, and the
|
|
|
|
// copying of the value prevents any safe synchronisation of the struct itself.
|
2020-10-14 18:28:06 +02:00
|
|
|
//
|
|
|
|
// While we are using the *Block pointer as the cache key, and the Block
|
|
|
|
// contents are mutable, once a Block is created it is treated as immutable for
|
|
|
|
// the duration of its life. Because a Block is a representation of a logical
|
|
|
|
// schema, which cannot change while it's being used, any modifications to the
|
|
|
|
// schema during execution would be an error.
|
2020-10-13 21:22:37 +02:00
|
|
|
type specCache struct {
|
|
|
|
sync.Mutex
|
|
|
|
specs map[uintptr]hcldec.Spec
|
|
|
|
}
|
|
|
|
|
|
|
|
var decoderSpecCache = specCache{
|
|
|
|
specs: map[uintptr]hcldec.Spec{},
|
|
|
|
}
|
|
|
|
|
2020-10-14 18:28:06 +02:00
|
|
|
// get returns the Spec associated with eth given Block, or nil if non is
|
2020-10-13 21:22:37 +02:00
|
|
|
// found.
|
|
|
|
func (s *specCache) get(b *Block) hcldec.Spec {
|
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
k := uintptr(unsafe.Pointer(b))
|
|
|
|
return s.specs[k]
|
|
|
|
}
|
|
|
|
|
|
|
|
// set stores the given Spec as being the result of b.DecoderSpec().
|
|
|
|
func (s *specCache) set(b *Block, spec hcldec.Spec) {
|
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
|
|
|
|
// the uintptr value gets us a unique identifier for each block, without
|
|
|
|
// tying this to the block value itself.
|
|
|
|
k := uintptr(unsafe.Pointer(b))
|
|
|
|
if _, ok := s.specs[k]; ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s.specs[k] = spec
|
|
|
|
|
|
|
|
// This must use a finalizer tied to the Block, otherwise we'll continue to
|
|
|
|
// build up Spec values as the Blocks are recycled.
|
|
|
|
runtime.SetFinalizer(b, s.delete)
|
|
|
|
}
|
|
|
|
|
|
|
|
// delete removes the spec associated with the given Block.
|
|
|
|
func (s *specCache) delete(b *Block) {
|
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
|
|
|
|
k := uintptr(unsafe.Pointer(b))
|
|
|
|
delete(s.specs, k)
|
|
|
|
}
|
|
|
|
|
2017-10-04 02:16:04 +02:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2020-10-13 21:22:37 +02:00
|
|
|
if spec := decoderSpecCache.get(b); spec != nil {
|
|
|
|
return spec
|
|
|
|
}
|
|
|
|
|
2017-10-04 02:16:04 +02:00
|
|
|
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()
|
|
|
|
|
2019-07-26 23:43:11 +02:00
|
|
|
// We can only validate 0 or 1 for MinItems, because a dynamic block
|
|
|
|
// may satisfy any number of min items while only having a single
|
2019-08-20 15:49:41 +02:00
|
|
|
// block in the config. We cannot validate MaxItems because a
|
|
|
|
// configuration may have any number of dynamic blocks
|
2019-07-26 23:43:11 +02:00
|
|
|
minItems := 0
|
|
|
|
if blockS.MinItems > 1 {
|
|
|
|
minItems = 1
|
|
|
|
}
|
|
|
|
|
2017-10-04 02:16:04 +02:00
|
|
|
switch blockS.Nesting {
|
2019-04-09 00:32:53 +02:00
|
|
|
case NestingSingle, NestingGroup:
|
2017-10-04 02:16:04 +02:00
|
|
|
ret[name] = &hcldec.BlockSpec{
|
|
|
|
TypeName: name,
|
|
|
|
Nested: childSpec,
|
2019-08-20 15:49:41 +02:00
|
|
|
Required: blockS.MinItems == 1,
|
2017-10-04 02:16:04 +02:00
|
|
|
}
|
2019-04-09 00:32:53 +02:00
|
|
|
if blockS.Nesting == NestingGroup {
|
|
|
|
ret[name] = &hcldec.DefaultSpec{
|
|
|
|
Primary: ret[name],
|
|
|
|
Default: &hcldec.LiteralSpec{
|
|
|
|
Value: blockS.EmptyValue(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2017-10-04 02:16:04 +02:00
|
|
|
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,
|
2019-07-26 23:43:11 +02:00
|
|
|
MinItems: minItems,
|
2018-08-23 00:45:34 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ret[name] = &hcldec.BlockListSpec{
|
|
|
|
TypeName: name,
|
|
|
|
Nested: childSpec,
|
2019-07-26 23:43:11 +02:00
|
|
|
MinItems: minItems,
|
2018-08-23 00:45:34 +02:00
|
|
|
}
|
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,
|
2019-07-26 23:43:11 +02:00
|
|
|
MinItems: minItems,
|
2017-10-04 02:16:04 +02:00
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-13 21:22:37 +02:00
|
|
|
decoderSpecCache.set(b, ret)
|
2017-10-04 02:16:04 +02:00
|
|
|
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 {
|
2021-02-05 19:34:55 +01:00
|
|
|
ret := &hcldec.AttrSpec{Name: name}
|
|
|
|
if a == nil {
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
if a.NestedType != nil {
|
|
|
|
// FIXME: a panic() is a bad UX. Fix this, probably by extending
|
|
|
|
// InternalValidate() to check Attribute schemas as well and calling it
|
|
|
|
// when we get the schema from the provider in Context().
|
|
|
|
if a.Type != cty.NilType {
|
|
|
|
panic("Invalid attribute schema: NestedType and Type cannot both be set. This is a bug in the provider.")
|
|
|
|
}
|
|
|
|
|
|
|
|
ty := a.NestedType.ImpliedType()
|
2021-02-12 19:34:25 +01:00
|
|
|
ret.Type = ty
|
|
|
|
ret.Required = a.Required || a.NestedType.MinItems > 0
|
2021-02-05 19:34:55 +01:00
|
|
|
return ret
|
2019-02-23 01:52:11 +01:00
|
|
|
}
|
2021-02-05 19:34:55 +01:00
|
|
|
|
|
|
|
ret.Type = a.Type
|
|
|
|
ret.Required = a.Required
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2021-02-12 19:34:25 +01:00
|
|
|
// listOptionalAttrsFromObject is a helper function which does *not* recurse
|
|
|
|
// into NestedType Attributes, because the optional types for each of those will
|
|
|
|
// belong to their own cty.Object definitions. It is used in other functions
|
|
|
|
// which themselves handle that recursion.
|
|
|
|
func listOptionalAttrsFromObject(o *Object) []string {
|
|
|
|
var ret []string
|
2021-02-05 19:34:55 +01:00
|
|
|
for name, attr := range o.Attributes {
|
|
|
|
if attr.Optional == true {
|
2021-02-12 19:34:25 +01:00
|
|
|
ret = append(ret, name)
|
2021-02-05 19:34:55 +01:00
|
|
|
}
|
|
|
|
}
|
2021-02-12 19:34:25 +01:00
|
|
|
return ret
|
2019-02-23 01:52:11 +01:00
|
|
|
}
|