govendor fetch github.com/zclconf/go-cty/...
This new version supports a conversion from object types to map types, which is important for Terraform because HCL2 { ... } syntax produces objects but lots of Terraform attributes require maps.
This commit is contained in:
parent
c007e3a7da
commit
26861dd7aa
|
@ -76,6 +76,10 @@ func getConversionKnown(in cty.Type, out cty.Type, unsafe bool) conversion {
|
|||
outEty := out.ElementType()
|
||||
return conversionTupleToList(in, outEty, unsafe)
|
||||
|
||||
case out.IsMapType() && in.IsObjectType():
|
||||
outEty := out.ElementType()
|
||||
return conversionObjectToMap(in, outEty, unsafe)
|
||||
|
||||
default:
|
||||
return nil
|
||||
|
||||
|
|
|
@ -113,3 +113,74 @@ func conversionTupleToList(tupleType cty.Type, listEty cty.Type, unsafe bool) co
|
|||
return cty.ListVal(elems), nil
|
||||
}
|
||||
}
|
||||
|
||||
// conversionObjectToMap returns a conversion that will take a value of the
|
||||
// given object type and return a map of the given element type.
|
||||
//
|
||||
// Will panic if the given objectType isn't actually an object type.
|
||||
func conversionObjectToMap(objectType cty.Type, mapEty cty.Type, unsafe bool) conversion {
|
||||
objectAtys := objectType.AttributeTypes()
|
||||
|
||||
if len(objectAtys) == 0 {
|
||||
// Empty object short-circuit
|
||||
return func(val cty.Value, path cty.Path) (cty.Value, error) {
|
||||
return cty.MapValEmpty(mapEty), nil
|
||||
}
|
||||
}
|
||||
|
||||
if mapEty == cty.DynamicPseudoType {
|
||||
// This is a special case where the caller wants us to find
|
||||
// a suitable single type that all elements can convert to, if
|
||||
// possible.
|
||||
objectAtysList := make([]cty.Type, 0, len(objectAtys))
|
||||
for _, aty := range objectAtys {
|
||||
objectAtysList = append(objectAtysList, aty)
|
||||
}
|
||||
mapEty, _ = unify(objectAtysList, unsafe)
|
||||
if mapEty == cty.NilType {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
elemConvs := make(map[string]conversion, len(objectAtys))
|
||||
for name, objectAty := range objectAtys {
|
||||
if objectAty.Equals(mapEty) {
|
||||
// no conversion required
|
||||
continue
|
||||
}
|
||||
|
||||
elemConvs[name] = getConversion(objectAty, mapEty, unsafe)
|
||||
if elemConvs[name] == nil {
|
||||
// If any of our element conversions are impossible, then the our
|
||||
// whole conversion is impossible.
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// If we fall out here then a conversion is possible, using the
|
||||
// element conversions in elemConvs
|
||||
return func(val cty.Value, path cty.Path) (cty.Value, error) {
|
||||
elems := make(map[string]cty.Value, len(elemConvs))
|
||||
path = append(path, nil)
|
||||
it := val.ElementIterator()
|
||||
for it.Next() {
|
||||
name, val := it.Element()
|
||||
var err error
|
||||
|
||||
path[len(path)-1] = cty.IndexStep{
|
||||
Key: name,
|
||||
}
|
||||
|
||||
conv := elemConvs[name.AsString()]
|
||||
if conv != nil {
|
||||
val, err = conv(val, path)
|
||||
if err != nil {
|
||||
return cty.NilVal, err
|
||||
}
|
||||
}
|
||||
elems[name.AsString()] = val
|
||||
}
|
||||
|
||||
return cty.MapVal(elems), nil
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2035,44 +2035,44 @@
|
|||
{
|
||||
"checksumSHA1": "R9ayYqxeUsPcIbs6KXCVwDIdf6M=",
|
||||
"path": "github.com/zclconf/go-cty/cty",
|
||||
"revision": "782e9a3a01c10a244d7440ee6590443d761deb74",
|
||||
"revisionTime": "2017-07-26T01:06:00Z"
|
||||
"revision": "8bf222d6d03b7b336d013978f3acbfd877da428f",
|
||||
"revisionTime": "2017-10-13T21:58:09Z"
|
||||
},
|
||||
{
|
||||
"checksumSHA1": "eNB2+F8P+0eldrN50x4YioU9jIs=",
|
||||
"checksumSHA1": "IjvfMUZ9S1L1NM0haXwMfKzkyvM=",
|
||||
"path": "github.com/zclconf/go-cty/cty/convert",
|
||||
"revision": "782e9a3a01c10a244d7440ee6590443d761deb74",
|
||||
"revisionTime": "2017-07-26T01:06:00Z"
|
||||
"revision": "8bf222d6d03b7b336d013978f3acbfd877da428f",
|
||||
"revisionTime": "2017-10-13T21:58:09Z"
|
||||
},
|
||||
{
|
||||
"checksumSHA1": "TU21yqpRZdbEbH8pp4I5YsQa00E=",
|
||||
"path": "github.com/zclconf/go-cty/cty/function",
|
||||
"revision": "782e9a3a01c10a244d7440ee6590443d761deb74",
|
||||
"revisionTime": "2017-07-26T01:06:00Z"
|
||||
"revision": "8bf222d6d03b7b336d013978f3acbfd877da428f",
|
||||
"revisionTime": "2017-10-13T21:58:09Z"
|
||||
},
|
||||
{
|
||||
"checksumSHA1": "Ke4kpRBTSophcLSCrusR8XxSC0Y=",
|
||||
"path": "github.com/zclconf/go-cty/cty/function/stdlib",
|
||||
"revision": "782e9a3a01c10a244d7440ee6590443d761deb74",
|
||||
"revisionTime": "2017-07-26T01:06:00Z"
|
||||
"revision": "8bf222d6d03b7b336d013978f3acbfd877da428f",
|
||||
"revisionTime": "2017-10-13T21:58:09Z"
|
||||
},
|
||||
{
|
||||
"checksumSHA1": "tmCzwfNXOEB1sSO7TKVzilb2vjA=",
|
||||
"path": "github.com/zclconf/go-cty/cty/gocty",
|
||||
"revision": "782e9a3a01c10a244d7440ee6590443d761deb74",
|
||||
"revisionTime": "2017-07-26T01:06:00Z"
|
||||
"revision": "8bf222d6d03b7b336d013978f3acbfd877da428f",
|
||||
"revisionTime": "2017-10-13T21:58:09Z"
|
||||
},
|
||||
{
|
||||
"checksumSHA1": "1ApmO+Q33+Oem/3f6BU6sztJWNc=",
|
||||
"path": "github.com/zclconf/go-cty/cty/json",
|
||||
"revision": "782e9a3a01c10a244d7440ee6590443d761deb74",
|
||||
"revisionTime": "2017-07-26T01:06:00Z"
|
||||
"revision": "8bf222d6d03b7b336d013978f3acbfd877da428f",
|
||||
"revisionTime": "2017-10-13T21:58:09Z"
|
||||
},
|
||||
{
|
||||
"checksumSHA1": "gH4rRyzIQknMIXAJfpvC04KTsME=",
|
||||
"path": "github.com/zclconf/go-cty/cty/set",
|
||||
"revision": "782e9a3a01c10a244d7440ee6590443d761deb74",
|
||||
"revisionTime": "2017-07-26T01:06:00Z"
|
||||
"revision": "8bf222d6d03b7b336d013978f3acbfd877da428f",
|
||||
"revisionTime": "2017-10-13T21:58:09Z"
|
||||
},
|
||||
{
|
||||
"checksumSHA1": "vE43s37+4CJ2CDU6TlOUOYE0K9c=",
|
||||
|
|
Loading…
Reference in New Issue