github.com/iaas-resource-provision/iaas-rpc@v1.0.7-0.20211021023331-ed21f798c408/internal/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 // ImpliedType always returns a result, even if the given schema is 12 // inconsistent. Code that creates configschema.Block objects should be 13 // tested using the InternalValidate method to detect any inconsistencies 14 // that would cause this method to fall back on defaults and assumptions. 15 func (b *Block) ImpliedType() cty.Type { 16 if b == nil { 17 return cty.EmptyObject 18 } 19 20 return hcldec.ImpliedType(b.DecoderSpec()) 21 } 22 23 // ContainsSensitive returns true if any of the attributes of the receiving 24 // block or any of its descendent blocks are marked as sensitive. 25 // 26 // Blocks themselves cannot be sensitive as a whole -- sensitivity is a 27 // per-attribute idea -- but sometimes we want to include a whole object 28 // decoded from a block in some UI output, and that is safe to do only if 29 // none of the contained attributes are sensitive. 30 func (b *Block) ContainsSensitive() bool { 31 for _, attrS := range b.Attributes { 32 if attrS.Sensitive { 33 return true 34 } 35 } 36 for _, blockS := range b.BlockTypes { 37 if blockS.ContainsSensitive() { 38 return true 39 } 40 } 41 return false 42 } 43 44 // ImpliedType returns the cty.Type that would result from decoding a NestedType 45 // Attribute using the receiving block schema. 46 // 47 // ImpliedType always returns a result, even if the given schema is 48 // inconsistent. Code that creates configschema.Object objects should be tested 49 // using the InternalValidate method to detect any inconsistencies that would 50 // cause this method to fall back on defaults and assumptions. 51 func (o *Object) ImpliedType() cty.Type { 52 if o == nil { 53 return cty.EmptyObject 54 } 55 56 attrTys := make(map[string]cty.Type, len(o.Attributes)) 57 for name, attrS := range o.Attributes { 58 if attrS.NestedType != nil { 59 attrTys[name] = attrS.NestedType.ImpliedType() 60 } else { 61 attrTys[name] = attrS.Type 62 } 63 } 64 optAttrs := listOptionalAttrsFromObject(o) 65 66 var ret cty.Type 67 if len(optAttrs) > 0 { 68 ret = cty.ObjectWithOptionalAttrs(attrTys, optAttrs) 69 } else { 70 ret = cty.Object(attrTys) 71 } 72 switch o.Nesting { 73 case NestingSingle: 74 return ret 75 case NestingList: 76 return cty.List(ret) 77 case NestingMap: 78 return cty.Map(ret) 79 case NestingSet: 80 return cty.Set(ret) 81 default: // Should never happen 82 return cty.EmptyObject 83 } 84 } 85 86 // ContainsSensitive returns true if any of the attributes of the receiving 87 // Object are marked as sensitive. 88 func (o *Object) ContainsSensitive() bool { 89 for _, attrS := range o.Attributes { 90 if attrS.Sensitive { 91 return true 92 } 93 if attrS.NestedType != nil { 94 return attrS.NestedType.ContainsSensitive() 95 } 96 } 97 return false 98 }