mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
handle nested types in StaticValidateTraversal
The StaticValidateTraversal code was not taking into account nested structural types. Rather than create more special cases for checking Type vs NestedType, we move the ImpliedType method up to the Attribute to ensure both are used to generate the final type spec.
This commit is contained in:
parent
a857084dfe
commit
43100fbe39
@ -26,10 +26,7 @@ func (b *Block) EmptyValue() cty.Value {
|
||||
// the value that would be returned if there were no definition of the attribute
|
||||
// at all, ignoring any required constraint.
|
||||
func (a *Attribute) EmptyValue() cty.Value {
|
||||
if a.NestedType != nil {
|
||||
return cty.NullVal(a.NestedType.ImpliedType())
|
||||
}
|
||||
return cty.NullVal(a.Type)
|
||||
return cty.NullVal(a.ImpliedType())
|
||||
}
|
||||
|
||||
// EmptyValue returns the "empty value" for when there are zero nested blocks
|
||||
|
@ -56,6 +56,20 @@ func (b *Block) ContainsSensitive() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// ImpliedType returns the cty.Type that would result from decoding a Block's
|
||||
// ImpliedType and getting the resulting AttributeType.
|
||||
//
|
||||
// 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 (a *Attribute) ImpliedType() cty.Type {
|
||||
if a.NestedType != nil {
|
||||
return a.NestedType.specType().WithoutOptionalAttributesDeep()
|
||||
}
|
||||
return a.Type
|
||||
}
|
||||
|
||||
// ImpliedType returns the cty.Type that would result from decoding a
|
||||
// NestedType Attribute using the receiving block schema.
|
||||
//
|
||||
|
@ -135,7 +135,7 @@ func (a *Attribute) internalValidate(name, prefix string) error {
|
||||
// no validations to perform
|
||||
case NestingList, NestingSet:
|
||||
if a.NestedType.Nesting == NestingSet {
|
||||
ety := a.NestedType.ImpliedType()
|
||||
ety := a.ImpliedType()
|
||||
if ety.HasDynamicTypes() {
|
||||
// This is not permitted because the HCL (cty) set implementation
|
||||
// needs to know the exact type of set elements in order to
|
||||
|
@ -75,10 +75,10 @@ func (b *Block) StaticValidateTraversal(traversal hcl.Traversal) tfdiags.Diagnos
|
||||
|
||||
if attrS, exists := b.Attributes[name]; exists {
|
||||
// For attribute validation we will just apply the rest of the
|
||||
// traversal to an unknown value of the attribute type and pass
|
||||
// through HCL's own errors, since we don't want to replicate all of
|
||||
// HCL's type checking rules here.
|
||||
val := cty.UnknownVal(attrS.Type)
|
||||
// traversal to an unknown value of the attribute type and pass through
|
||||
// HCL's own errors, since we don't want to replicate all of HCL's type
|
||||
// checking rules here.
|
||||
val := cty.UnknownVal(attrS.ImpliedType())
|
||||
_, hclDiags := after.TraverseRel(val)
|
||||
diags = diags.Append(hclDiags)
|
||||
return diags
|
||||
|
@ -13,6 +13,42 @@ func TestStaticValidateTraversal(t *testing.T) {
|
||||
"str": {Type: cty.String, Optional: true},
|
||||
"list": {Type: cty.List(cty.String), Optional: true},
|
||||
"dyn": {Type: cty.DynamicPseudoType, Optional: true},
|
||||
"nested_single": {
|
||||
Optional: true,
|
||||
NestedType: &Object{
|
||||
Nesting: NestingSingle,
|
||||
Attributes: map[string]*Attribute{
|
||||
"optional": {Type: cty.String, Optional: true},
|
||||
},
|
||||
},
|
||||
},
|
||||
"nested_list": {
|
||||
Optional: true,
|
||||
NestedType: &Object{
|
||||
Nesting: NestingList,
|
||||
Attributes: map[string]*Attribute{
|
||||
"optional": {Type: cty.String, Optional: true},
|
||||
},
|
||||
},
|
||||
},
|
||||
"nested_set": {
|
||||
Optional: true,
|
||||
NestedType: &Object{
|
||||
Nesting: NestingSet,
|
||||
Attributes: map[string]*Attribute{
|
||||
"optional": {Type: cty.String, Optional: true},
|
||||
},
|
||||
},
|
||||
},
|
||||
"nested_map": {
|
||||
Optional: true,
|
||||
NestedType: &Object{
|
||||
Nesting: NestingMap,
|
||||
Attributes: map[string]*Attribute{
|
||||
"optional": {Type: cty.String, Optional: true},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
schema := &Block{
|
||||
Attributes: attrs,
|
||||
@ -168,6 +204,22 @@ func TestStaticValidateTraversal(t *testing.T) {
|
||||
`obj.map_block.anything.nonexist`,
|
||||
`Unsupported attribute: This object has no argument, nested block, or exported attribute named "nonexist".`,
|
||||
},
|
||||
{
|
||||
`obj.nested_single.optional`,
|
||||
``,
|
||||
},
|
||||
{
|
||||
`obj.nested_list[0].optional`,
|
||||
``,
|
||||
},
|
||||
{
|
||||
`obj.nested_set[0].optional`,
|
||||
`Invalid index: Elements of a set are identified only by their value and don't have any separate index or key to select with, so it's only possible to perform operations across all elements of the set.`,
|
||||
},
|
||||
{
|
||||
`obj.nested_map["key"].optional`,
|
||||
``,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
|
Loading…
Reference in New Issue
Block a user