github.com/cycloidio/terraform@v1.1.10-0.20220513142504-76d5c768dc63/configs/configschema/implied_type.go (about)

     1  package configschema
     2  
     3  import (
     4  	"github.com/hashicorp/hcl/v2/hcldec"
     5  	"github.com/zclconf/go-cty/cty"
     6  )
     7  
     8  // ImpliedType returns the cty.Type that would result from decoding a
     9  // configuration block using the receiving block schema.
    10  //
    11  // The type returned from Block.ImpliedType differs from the type returned by
    12  // hcldec.ImpliedType in that there will be no objects with optional
    13  // attributes, since this value is not to be used for the decoding of
    14  // configuration.
    15  //
    16  // ImpliedType always returns a result, even if the given schema is
    17  // inconsistent. Code that creates configschema.Block objects should be
    18  // tested using the InternalValidate method to detect any inconsistencies
    19  // that would cause this method to fall back on defaults and assumptions.
    20  func (b *Block) ImpliedType() cty.Type {
    21  	return b.specType().WithoutOptionalAttributesDeep()
    22  }
    23  
    24  // specType returns the cty.Type used for decoding a configuration
    25  // block using the receiving block schema. This is the type used internally by
    26  // hcldec to decode configuration.
    27  func (b *Block) specType() cty.Type {
    28  	if b == nil {
    29  		return cty.EmptyObject
    30  	}
    31  
    32  	return hcldec.ImpliedType(b.DecoderSpec())
    33  }
    34  
    35  // ContainsSensitive returns true if any of the attributes of the receiving
    36  // block or any of its descendent blocks are marked as sensitive.
    37  //
    38  // Blocks themselves cannot be sensitive as a whole -- sensitivity is a
    39  // per-attribute idea -- but sometimes we want to include a whole object
    40  // decoded from a block in some UI output, and that is safe to do only if
    41  // none of the contained attributes are sensitive.
    42  func (b *Block) ContainsSensitive() bool {
    43  	for _, attrS := range b.Attributes {
    44  		if attrS.Sensitive {
    45  			return true
    46  		}
    47  		if attrS.NestedType != nil && attrS.NestedType.ContainsSensitive() {
    48  			return true
    49  		}
    50  	}
    51  	for _, blockS := range b.BlockTypes {
    52  		if blockS.ContainsSensitive() {
    53  			return true
    54  		}
    55  	}
    56  	return false
    57  }
    58  
    59  // ImpliedType returns the cty.Type that would result from decoding a
    60  // NestedType Attribute using the receiving block schema.
    61  //
    62  // ImpliedType always returns a result, even if the given schema is
    63  // inconsistent. Code that creates configschema.Object objects should be tested
    64  // using the InternalValidate method to detect any inconsistencies that would
    65  // cause this method to fall back on defaults and assumptions.
    66  func (o *Object) ImpliedType() cty.Type {
    67  	return o.specType().WithoutOptionalAttributesDeep()
    68  }
    69  
    70  // specType returns the cty.Type used for decoding a NestedType Attribute using
    71  // the receiving block schema.
    72  func (o *Object) specType() cty.Type {
    73  	if o == nil {
    74  		return cty.EmptyObject
    75  	}
    76  
    77  	attrTys := make(map[string]cty.Type, len(o.Attributes))
    78  	for name, attrS := range o.Attributes {
    79  		if attrS.NestedType != nil {
    80  			attrTys[name] = attrS.NestedType.specType()
    81  		} else {
    82  			attrTys[name] = attrS.Type
    83  		}
    84  	}
    85  	optAttrs := listOptionalAttrsFromObject(o)
    86  
    87  	var ret cty.Type
    88  	if len(optAttrs) > 0 {
    89  		ret = cty.ObjectWithOptionalAttrs(attrTys, optAttrs)
    90  	} else {
    91  		ret = cty.Object(attrTys)
    92  	}
    93  	switch o.Nesting {
    94  	case NestingSingle:
    95  		return ret
    96  	case NestingList:
    97  		return cty.List(ret)
    98  	case NestingMap:
    99  		return cty.Map(ret)
   100  	case NestingSet:
   101  		return cty.Set(ret)
   102  	default: // Should never happen
   103  		return cty.EmptyObject
   104  	}
   105  }
   106  
   107  // ContainsSensitive returns true if any of the attributes of the receiving
   108  // Object are marked as sensitive.
   109  func (o *Object) ContainsSensitive() bool {
   110  	for _, attrS := range o.Attributes {
   111  		if attrS.Sensitive {
   112  			return true
   113  		}
   114  		if attrS.NestedType != nil && attrS.NestedType.ContainsSensitive() {
   115  			return true
   116  		}
   117  	}
   118  	return false
   119  }