2017-10-03 01:07:39 +02:00
|
|
|
package configschema
|
|
|
|
|
|
|
|
import (
|
2019-09-10 00:58:44 +02:00
|
|
|
"github.com/hashicorp/hcl/v2/hcldec"
|
2017-10-03 01:07:39 +02:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ImpliedType returns the cty.Type that would result from decoding a
|
|
|
|
// configuration block using the receiving block schema.
|
|
|
|
//
|
|
|
|
// ImpliedType always returns a result, even if the given schema is
|
|
|
|
// inconsistent. Code that creates configschema.Block objects should be
|
|
|
|
// tested using the InternalValidate method to detect any inconsistencies
|
|
|
|
// that would cause this method to fall back on defaults and assumptions.
|
|
|
|
func (b *Block) ImpliedType() cty.Type {
|
2017-10-03 03:48:40 +02:00
|
|
|
if b == nil {
|
|
|
|
return cty.EmptyObject
|
|
|
|
}
|
|
|
|
|
2017-10-04 01:38:29 +02:00
|
|
|
return hcldec.ImpliedType(b.DecoderSpec())
|
2017-10-03 01:07:39 +02:00
|
|
|
}
|
2019-02-11 23:18:58 +01:00
|
|
|
|
|
|
|
// ContainsSensitive returns true if any of the attributes of the receiving
|
|
|
|
// block or any of its descendent blocks are marked as sensitive.
|
|
|
|
//
|
|
|
|
// Blocks themselves cannot be sensitive as a whole -- sensitivity is a
|
|
|
|
// per-attribute idea -- but sometimes we want to include a whole object
|
|
|
|
// decoded from a block in some UI output, and that is safe to do only if
|
|
|
|
// none of the contained attributes are sensitive.
|
|
|
|
func (b *Block) ContainsSensitive() bool {
|
|
|
|
for _, attrS := range b.Attributes {
|
|
|
|
if attrS.Sensitive {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, blockS := range b.BlockTypes {
|
|
|
|
if blockS.ContainsSensitive() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2021-02-05 19:34:55 +01:00
|
|
|
|
|
|
|
// ImpliedType returns the cty.Type that would result from decoding a NestedType
|
|
|
|
// Attribute using the receiving block schema.
|
|
|
|
//
|
|
|
|
// ImpliedType always returns a result, even if the given schema is
|
|
|
|
// inconsistent. Code that creates configschema.Object objects should be tested
|
|
|
|
// using the InternalValidate method to detect any inconsistencies that would
|
|
|
|
// cause this method to fall back on defaults and assumptions.
|
|
|
|
func (o *Object) ImpliedType() cty.Type {
|
|
|
|
if o == nil {
|
|
|
|
return cty.EmptyObject
|
|
|
|
}
|
|
|
|
|
|
|
|
attrTys := make(map[string]cty.Type, len(o.Attributes))
|
|
|
|
for name, attrS := range o.Attributes {
|
2021-02-12 19:34:25 +01:00
|
|
|
if attrS.NestedType != nil {
|
|
|
|
attrTys[name] = attrS.NestedType.ImpliedType()
|
|
|
|
} else {
|
|
|
|
attrTys[name] = attrS.Type
|
|
|
|
}
|
|
|
|
}
|
|
|
|
optAttrs := listOptionalAttrsFromObject(o)
|
2021-02-18 14:48:52 +01:00
|
|
|
|
|
|
|
var ret cty.Type
|
2021-02-12 19:34:25 +01:00
|
|
|
if len(optAttrs) > 0 {
|
2021-02-18 14:48:52 +01:00
|
|
|
ret = cty.ObjectWithOptionalAttrs(attrTys, optAttrs)
|
|
|
|
} else {
|
|
|
|
ret = cty.Object(attrTys)
|
2021-02-05 19:34:55 +01:00
|
|
|
}
|
2021-02-12 19:34:25 +01:00
|
|
|
switch o.Nesting {
|
|
|
|
case NestingSingle:
|
2021-02-18 14:48:52 +01:00
|
|
|
return ret
|
2021-02-12 19:34:25 +01:00
|
|
|
case NestingList:
|
2021-02-18 14:48:52 +01:00
|
|
|
return cty.List(ret)
|
2021-02-12 19:34:25 +01:00
|
|
|
case NestingMap:
|
2021-02-18 14:48:52 +01:00
|
|
|
return cty.Map(ret)
|
2021-02-12 19:34:25 +01:00
|
|
|
case NestingSet:
|
2021-02-18 14:48:52 +01:00
|
|
|
return cty.Set(ret)
|
2021-02-12 19:34:25 +01:00
|
|
|
default: // Should never happen
|
|
|
|
panic("invalid Nesting")
|
|
|
|
}
|
2021-02-05 19:34:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ContainsSensitive returns true if any of the attributes of the receiving
|
|
|
|
// Object are marked as sensitive.
|
|
|
|
func (o *Object) ContainsSensitive() bool {
|
|
|
|
for _, attrS := range o.Attributes {
|
|
|
|
if attrS.Sensitive {
|
|
|
|
return true
|
|
|
|
}
|
2021-02-11 16:32:31 +01:00
|
|
|
if attrS.NestedType != nil {
|
|
|
|
return attrS.NestedType.ContainsSensitive()
|
|
|
|
}
|
2021-02-05 19:34:55 +01:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|