mirror of
https://gitea.com/Lydanne/buildx.git
synced 2025-07-10 05:27:07 +08:00
Extend hcl2 support with more functions
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
95
vendor/github.com/zclconf/go-cty/cty/convert/conversion_collection.go
generated
vendored
95
vendor/github.com/zclconf/go-cty/cty/convert/conversion_collection.go
generated
vendored
@ -13,6 +13,14 @@ import (
|
||||
// if we're converting from a set into a list of the same element type.)
|
||||
func conversionCollectionToList(ety cty.Type, conv conversion) conversion {
|
||||
return func(val cty.Value, path cty.Path) (cty.Value, error) {
|
||||
if !val.Length().IsKnown() {
|
||||
// If the input collection has an unknown length (which is true
|
||||
// for a set containing unknown values) then our result must be
|
||||
// an unknown list, because we can't predict how many elements
|
||||
// the resulting list should have.
|
||||
return cty.UnknownVal(cty.List(val.Type().ElementType())), nil
|
||||
}
|
||||
|
||||
elems := make([]cty.Value, 0, val.LengthInt())
|
||||
i := int64(0)
|
||||
elemPath := append(path.Copy(), nil)
|
||||
@ -156,34 +164,45 @@ func conversionCollectionToMap(ety cty.Type, conv conversion) conversion {
|
||||
// given tuple type and return a set of the given element type.
|
||||
//
|
||||
// Will panic if the given tupleType isn't actually a tuple type.
|
||||
func conversionTupleToSet(tupleType cty.Type, listEty cty.Type, unsafe bool) conversion {
|
||||
func conversionTupleToSet(tupleType cty.Type, setEty cty.Type, unsafe bool) conversion {
|
||||
tupleEtys := tupleType.TupleElementTypes()
|
||||
|
||||
if len(tupleEtys) == 0 {
|
||||
// Empty tuple short-circuit
|
||||
return func(val cty.Value, path cty.Path) (cty.Value, error) {
|
||||
return cty.SetValEmpty(listEty), nil
|
||||
return cty.SetValEmpty(setEty), nil
|
||||
}
|
||||
}
|
||||
|
||||
if listEty == cty.DynamicPseudoType {
|
||||
if setEty == 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.
|
||||
listEty, _ = unify(tupleEtys, unsafe)
|
||||
if listEty == cty.NilType {
|
||||
setEty, _ = unify(tupleEtys, unsafe)
|
||||
if setEty == cty.NilType {
|
||||
return nil
|
||||
}
|
||||
|
||||
// If the set element type after unification is still the dynamic
|
||||
// type, the only way this can result in a valid set is if all values
|
||||
// are of dynamic type
|
||||
if setEty == cty.DynamicPseudoType {
|
||||
for _, tupleEty := range tupleEtys {
|
||||
if !tupleEty.Equals(cty.DynamicPseudoType) {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
elemConvs := make([]conversion, len(tupleEtys))
|
||||
for i, tupleEty := range tupleEtys {
|
||||
if tupleEty.Equals(listEty) {
|
||||
if tupleEty.Equals(setEty) {
|
||||
// no conversion required
|
||||
continue
|
||||
}
|
||||
|
||||
elemConvs[i] = getConversion(tupleEty, listEty, unsafe)
|
||||
elemConvs[i] = getConversion(tupleEty, setEty, unsafe)
|
||||
if elemConvs[i] == nil {
|
||||
// If any of our element conversions are impossible, then the our
|
||||
// whole conversion is impossible.
|
||||
@ -244,6 +263,17 @@ func conversionTupleToList(tupleType cty.Type, listEty cty.Type, unsafe bool) co
|
||||
if listEty == cty.NilType {
|
||||
return nil
|
||||
}
|
||||
|
||||
// If the list element type after unification is still the dynamic
|
||||
// type, the only way this can result in a valid list is if all values
|
||||
// are of dynamic type
|
||||
if listEty == cty.DynamicPseudoType {
|
||||
for _, tupleEty := range tupleEtys {
|
||||
if !tupleEty.Equals(cty.DynamicPseudoType) {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
elemConvs := make([]conversion, len(tupleEtys))
|
||||
@ -265,6 +295,7 @@ func conversionTupleToList(tupleType cty.Type, listEty cty.Type, unsafe bool) co
|
||||
// element conversions in elemConvs
|
||||
return func(val cty.Value, path cty.Path) (cty.Value, error) {
|
||||
elems := make([]cty.Value, 0, len(elemConvs))
|
||||
elemTys := make([]cty.Type, 0, len(elems))
|
||||
elemPath := append(path.Copy(), nil)
|
||||
i := int64(0)
|
||||
it := val.ElementIterator()
|
||||
@ -284,10 +315,15 @@ func conversionTupleToList(tupleType cty.Type, listEty cty.Type, unsafe bool) co
|
||||
}
|
||||
}
|
||||
elems = append(elems, val)
|
||||
elemTys = append(elemTys, val.Type())
|
||||
|
||||
i++
|
||||
}
|
||||
|
||||
elems, err := conversionUnifyListElements(elems, elemPath, unsafe)
|
||||
if err != nil {
|
||||
return cty.NilVal, err
|
||||
}
|
||||
return cty.ListVal(elems), nil
|
||||
}
|
||||
}
|
||||
@ -430,6 +466,16 @@ func conversionMapToObject(mapType cty.Type, objType cty.Type, unsafe bool) conv
|
||||
elems[name.AsString()] = val
|
||||
}
|
||||
|
||||
for name, aty := range objectAtys {
|
||||
if _, exists := elems[name]; !exists {
|
||||
if optional := objType.AttributeOptional(name); optional {
|
||||
elems[name] = cty.NullVal(aty)
|
||||
} else {
|
||||
return cty.NilVal, path.NewErrorf("map has no element for required attribute %q", name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return cty.ObjectVal(elems), nil
|
||||
}
|
||||
}
|
||||
@ -441,6 +487,7 @@ func conversionUnifyCollectionElements(elems map[string]cty.Value, path cty.Path
|
||||
}
|
||||
unifiedType, _ := unify(elemTypes, unsafe)
|
||||
if unifiedType == cty.NilType {
|
||||
return nil, path.NewErrorf("collection elements cannot be unified")
|
||||
}
|
||||
|
||||
unifiedElems := make(map[string]cty.Value)
|
||||
@ -486,3 +533,37 @@ func conversionCheckMapElementTypes(elems map[string]cty.Value, path cty.Path) e
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func conversionUnifyListElements(elems []cty.Value, path cty.Path, unsafe bool) ([]cty.Value, error) {
|
||||
elemTypes := make([]cty.Type, len(elems))
|
||||
for i, elem := range elems {
|
||||
elemTypes[i] = elem.Type()
|
||||
}
|
||||
unifiedType, _ := unify(elemTypes, unsafe)
|
||||
if unifiedType == cty.NilType {
|
||||
return nil, path.NewErrorf("collection elements cannot be unified")
|
||||
}
|
||||
|
||||
ret := make([]cty.Value, len(elems))
|
||||
elemPath := append(path.Copy(), nil)
|
||||
|
||||
for i, elem := range elems {
|
||||
if elem.Type().Equals(unifiedType) {
|
||||
ret[i] = elem
|
||||
continue
|
||||
}
|
||||
conv := getConversion(elem.Type(), unifiedType, unsafe)
|
||||
if conv == nil {
|
||||
}
|
||||
elemPath[len(elemPath)-1] = cty.IndexStep{
|
||||
Key: cty.NumberIntVal(int64(i)),
|
||||
}
|
||||
val, err := conv(elem, elemPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ret[i] = val
|
||||
}
|
||||
|
||||
return ret, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user