Debian Codegraph @

cty/convert/conversion_dynamic.go

raw symbols SCIP
1 package convertscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/
2
3 import (
4 "github.com/zclconf/go-cty/ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/"
5 )
6
7 // dynamicFixup deals with just-in-time conversions of values that were
8 // input-typed as cty.DynamicPseudoType during analysis, ensuring that
9 // we end up with the desired output type once the value is known, or
10 // failing with an error if that is not possible.
11 //
12 // This is in the spirit of the cty philosophy of optimistically assuming that
13 // DynamicPseudoType values will become the intended value eventually, and
14 // dealing with any inconsistencies during final evaluation.
15 func dynamicFixupscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/dynamicFixup().(wantType ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.Typescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#) conversionscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/conversion# {
16 return func(in ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.Valuescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Value#, path ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.Pathscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Path#) (ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.Valuescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Value#, error) {
17 ret, err := Convertscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/Convert().(in, wantType)
18 if err != nil {
19 // Re-wrap this error so that the returned path is relative
20 // to the caller's original value, rather than relative to our
21 // conversion value here.
22 return ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.NilValscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/NilVal., path.NewErrorscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Path#NewError().(err)
23 }
24 return ret, nil
25 }
26 }
27
28 // dynamicPassthrough is an identity conversion that is used when the
29 // target type is DynamicPseudoType, indicating that the caller doesn't care
30 // which type is returned.
31 func dynamicPassthroughscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/dynamicPassthrough().(in ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.Valuescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Value#, path ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.Pathscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Path#) (ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.Valuescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Value#, error) {
32 return in, nil
33 }
34
35 // dynamicReplace aims to return the out type unchanged, but if it finds a
36 // dynamic type either directly or in any descendent elements it replaces them
37 // with the equivalent type from in.
38 //
39 // This function assumes that in and out are compatible from a Convert
40 // perspective, and will panic if it finds that they are not. For example if
41 // in is an object and out is a map, this function will still attempt to iterate
42 // through both as if they were the same.
43 func dynamicReplacescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/dynamicReplace().(in, out ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.Typescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#) ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.Typescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type# {
44 if in == ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.DynamicPseudoTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/DynamicPseudoType. || in == ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.NilTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/NilType. {
45 // Short circuit this case, there's no point worrying about this if in
46 // is a dynamic type or a nil type. Out is the best we can do.
47 return out
48 }
49
50 switch {
51 case out == ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.DynamicPseudoTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/DynamicPseudoType.:
52 // So replace out with in.
53 return in
54 case out.IsPrimitiveTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsPrimitiveType().(), out.IsCapsuleTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsCapsuleType().():
55 // out is not dynamic and it doesn't contain descendent elements so just
56 // return it unchanged.
57 return out
58 case out.IsMapTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsMapType().():
59 var elemType ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.Typescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#
60
61 // Maps are compatible with other maps or objects.
62 if in.IsMapTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsMapType().() {
63 elemType = dynamicReplacescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/dynamicReplace().(in.ElementTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#ElementType().(), out.ElementTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#ElementType().())
64 }
65
66 if in.IsObjectTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsObjectType().() {
67 var types []ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.Typescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#
68 for _, t := range in.AttributeTypesscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#AttributeTypes().() {
69 types = append(types, t)
70 }
71 unifiedType, _ := unifyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/unify().(types, true)
72 elemType = dynamicReplacescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/dynamicReplace().(unifiedType, out.ElementTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#ElementType().())
73 }
74
75 return ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.Mapscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Map().(elemType)
76 case out.IsObjectTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsObjectType().():
77 // Objects are compatible with other objects and maps.
78 outTypes := map[string]ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.Typescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#{}
79 if in.IsMapTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsMapType().() {
80 for attr, attrType := range out.AttributeTypesscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#AttributeTypes().() {
81 outTypes[attr] = dynamicReplacescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/dynamicReplace().(in.ElementTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#ElementType().(), attrType)
82 }
83 }
84
85 if in.IsObjectTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsObjectType().() {
86 for attr, attrType := range out.AttributeTypesscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#AttributeTypes().() {
87 if !in.HasAttributescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#HasAttribute().(attr) {
88 // If in does not have this attribute, then it is an
89 // optional attribute and there is nothing we can do except
90 // to return the type from out even if it is dynamic.
91 outTypes[attr] = attrType
92 continue
93 }
94 outTypes[attr] = dynamicReplacescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/dynamicReplace().(in.AttributeTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#AttributeType().(attr), attrType)
95 }
96 }
97
98 return ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.Objectscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Object().(outTypes)
99 case out.IsSetTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsSetType().():
100 var elemType ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.Typescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#
101
102 // Sets are compatible with other sets, lists, tuples.
103 if in.IsSetTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsSetType().() || in.IsListTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsListType().() {
104 elemType = dynamicReplacescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/dynamicReplace().(in.ElementTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#ElementType().(), out.ElementTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#ElementType().())
105 }
106
107 if in.IsTupleTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsTupleType().() {
108 unifiedType, _ := unifyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/unify().(in.TupleElementTypesscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#TupleElementTypes().(), true)
109 elemType = dynamicReplacescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/dynamicReplace().(unifiedType, out.ElementTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#ElementType().())
110 }
111
112 return ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.Setscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Set().(elemType)
113 case out.IsListTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsListType().():
114 var elemType ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.Typescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#
115
116 // Lists are compatible with other lists, sets, and tuples.
117 if in.IsSetTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsSetType().() || in.IsListTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsListType().() {
118 elemType = dynamicReplacescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/dynamicReplace().(in.ElementTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#ElementType().(), out.ElementTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#ElementType().())
119 }
120
121 if in.IsTupleTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsTupleType().() {
122 unifiedType, _ := unifyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/unify().(in.TupleElementTypesscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#TupleElementTypes().(), true)
123 elemType = dynamicReplacescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/dynamicReplace().(unifiedType, out.ElementTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#ElementType().())
124 }
125
126 return ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.Listscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/List().(elemType)
127 case out.IsTupleTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#IsTupleType().():
128 // Tuples are only compatible with other tuples
129 var types []ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.Typescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#
130 for ix := 0; ix < len(out.TupleElementTypesscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#TupleElementTypes().()); ix++ {
131 types = append(types, dynamicReplacescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty/convert`/dynamicReplace().(in.TupleElementTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#TupleElementType().(ix), out.TupleElementTypescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#TupleElementType().(ix)))
132 }
133 return ctyscip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/.Tuplescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Tuple().(types)
134 default:
135 panic("unrecognized type " + out.FriendlyNamescip-go gomod github.com/zclconf/go-cty . `github.com/zclconf/go-cty/cty`/Type#FriendlyName().())
136 }
137 }