kubeform.dev/terraform-backend-sdk@v0.0.0-20220310143633-45f07fe731c5/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 } 48 for _, blockS := range b.BlockTypes { 49 if blockS.ContainsSensitive() { 50 return true 51 } 52 } 53 return false 54 } 55 56 // ImpliedType returns the cty.Type that would result from decoding a 57 // NestedType Attribute using the receiving block schema. 58 // 59 // ImpliedType always returns a result, even if the given schema is 60 // inconsistent. Code that creates configschema.Object objects should be tested 61 // using the InternalValidate method to detect any inconsistencies that would 62 // cause this method to fall back on defaults and assumptions. 63 func (o *Object) ImpliedType() cty.Type { 64 return o.specType().WithoutOptionalAttributesDeep() 65 } 66 67 // specType returns the cty.Type used for decoding a NestedType Attribute using 68 // the receiving block schema. 69 func (o *Object) specType() cty.Type { 70 if o == nil { 71 return cty.EmptyObject 72 } 73 74 attrTys := make(map[string]cty.Type, len(o.Attributes)) 75 for name, attrS := range o.Attributes { 76 if attrS.NestedType != nil { 77 attrTys[name] = attrS.NestedType.specType() 78 } else { 79 attrTys[name] = attrS.Type 80 } 81 } 82 optAttrs := listOptionalAttrsFromObject(o) 83 84 var ret cty.Type 85 if len(optAttrs) > 0 { 86 ret = cty.ObjectWithOptionalAttrs(attrTys, optAttrs) 87 } else { 88 ret = cty.Object(attrTys) 89 } 90 switch o.Nesting { 91 case NestingSingle: 92 return ret 93 case NestingList: 94 return cty.List(ret) 95 case NestingMap: 96 return cty.Map(ret) 97 case NestingSet: 98 return cty.Set(ret) 99 default: // Should never happen 100 return cty.EmptyObject 101 } 102 } 103 104 // ContainsSensitive returns true if any of the attributes of the receiving 105 // Object are marked as sensitive. 106 func (o *Object) ContainsSensitive() bool { 107 for _, attrS := range o.Attributes { 108 if attrS.Sensitive { 109 return true 110 } 111 if attrS.NestedType != nil { 112 return attrS.NestedType.ContainsSensitive() 113 } 114 } 115 return false 116 }