diff options
Diffstat (limited to 'vendor/github.com/zclconf/go-cty/cty/convert/conversion_collection.go')
-rw-r--r-- | vendor/github.com/zclconf/go-cty/cty/convert/conversion_collection.go | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/vendor/github.com/zclconf/go-cty/cty/convert/conversion_collection.go b/vendor/github.com/zclconf/go-cty/cty/convert/conversion_collection.go index eace85d..3039ba2 100644 --- a/vendor/github.com/zclconf/go-cty/cty/convert/conversion_collection.go +++ b/vendor/github.com/zclconf/go-cty/cty/convert/conversion_collection.go | |||
@@ -84,6 +84,120 @@ func conversionCollectionToSet(ety cty.Type, conv conversion) conversion { | |||
84 | } | 84 | } |
85 | } | 85 | } |
86 | 86 | ||
87 | // conversionCollectionToMap returns a conversion that will apply the given | ||
88 | // conversion to all of the elements of a collection (something that supports | ||
89 | // ForEachElement and LengthInt) and then returns the result as a map. | ||
90 | // | ||
91 | // "conv" can be nil if the elements are expected to already be of the | ||
92 | // correct type and just need to be re-wrapped into a map. | ||
93 | func conversionCollectionToMap(ety cty.Type, conv conversion) conversion { | ||
94 | return func(val cty.Value, path cty.Path) (cty.Value, error) { | ||
95 | elems := make(map[string]cty.Value, 0) | ||
96 | path = append(path, nil) | ||
97 | it := val.ElementIterator() | ||
98 | for it.Next() { | ||
99 | key, val := it.Element() | ||
100 | var err error | ||
101 | |||
102 | path[len(path)-1] = cty.IndexStep{ | ||
103 | Key: key, | ||
104 | } | ||
105 | |||
106 | keyStr, err := Convert(key, cty.String) | ||
107 | if err != nil { | ||
108 | // Should never happen, because keys can only be numbers or | ||
109 | // strings and both can convert to string. | ||
110 | return cty.DynamicVal, path.NewErrorf("cannot convert key type %s to string for map", key.Type().FriendlyName()) | ||
111 | } | ||
112 | |||
113 | if conv != nil { | ||
114 | val, err = conv(val, path) | ||
115 | if err != nil { | ||
116 | return cty.NilVal, err | ||
117 | } | ||
118 | } | ||
119 | |||
120 | elems[keyStr.AsString()] = val | ||
121 | } | ||
122 | |||
123 | if len(elems) == 0 { | ||
124 | return cty.MapValEmpty(ety), nil | ||
125 | } | ||
126 | |||
127 | return cty.MapVal(elems), nil | ||
128 | } | ||
129 | } | ||
130 | |||
131 | // conversionTupleToSet returns a conversion that will take a value of the | ||
132 | // given tuple type and return a set of the given element type. | ||
133 | // | ||
134 | // Will panic if the given tupleType isn't actually a tuple type. | ||
135 | func conversionTupleToSet(tupleType cty.Type, listEty cty.Type, unsafe bool) conversion { | ||
136 | tupleEtys := tupleType.TupleElementTypes() | ||
137 | |||
138 | if len(tupleEtys) == 0 { | ||
139 | // Empty tuple short-circuit | ||
140 | return func(val cty.Value, path cty.Path) (cty.Value, error) { | ||
141 | return cty.SetValEmpty(listEty), nil | ||
142 | } | ||
143 | } | ||
144 | |||
145 | if listEty == cty.DynamicPseudoType { | ||
146 | // This is a special case where the caller wants us to find | ||
147 | // a suitable single type that all elements can convert to, if | ||
148 | // possible. | ||
149 | listEty, _ = unify(tupleEtys, unsafe) | ||
150 | if listEty == cty.NilType { | ||
151 | return nil | ||
152 | } | ||
153 | } | ||
154 | |||
155 | elemConvs := make([]conversion, len(tupleEtys)) | ||
156 | for i, tupleEty := range tupleEtys { | ||
157 | if tupleEty.Equals(listEty) { | ||
158 | // no conversion required | ||
159 | continue | ||
160 | } | ||
161 | |||
162 | elemConvs[i] = getConversion(tupleEty, listEty, unsafe) | ||
163 | if elemConvs[i] == nil { | ||
164 | // If any of our element conversions are impossible, then the our | ||
165 | // whole conversion is impossible. | ||
166 | return nil | ||
167 | } | ||
168 | } | ||
169 | |||
170 | // If we fall out here then a conversion is possible, using the | ||
171 | // element conversions in elemConvs | ||
172 | return func(val cty.Value, path cty.Path) (cty.Value, error) { | ||
173 | elems := make([]cty.Value, 0, len(elemConvs)) | ||
174 | path = append(path, nil) | ||
175 | i := int64(0) | ||
176 | it := val.ElementIterator() | ||
177 | for it.Next() { | ||
178 | _, val := it.Element() | ||
179 | var err error | ||
180 | |||
181 | path[len(path)-1] = cty.IndexStep{ | ||
182 | Key: cty.NumberIntVal(i), | ||
183 | } | ||
184 | |||
185 | conv := elemConvs[i] | ||
186 | if conv != nil { | ||
187 | val, err = conv(val, path) | ||
188 | if err != nil { | ||
189 | return cty.NilVal, err | ||
190 | } | ||
191 | } | ||
192 | elems = append(elems, val) | ||
193 | |||
194 | i++ | ||
195 | } | ||
196 | |||
197 | return cty.SetVal(elems), nil | ||
198 | } | ||
199 | } | ||
200 | |||
87 | // conversionTupleToList returns a conversion that will take a value of the | 201 | // conversionTupleToList returns a conversion that will take a value of the |
88 | // given tuple type and return a list of the given element type. | 202 | // given tuple type and return a list of the given element type. |
89 | // | 203 | // |