vendor: update github.com/zclconf/go-cty to v1.14.4

Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax
2024-04-09 09:41:03 +02:00
parent 744c055560
commit ea3338c3f3
8 changed files with 45 additions and 29 deletions

View File

@ -40,6 +40,11 @@ func dynamicPassthrough(in cty.Value, path cty.Path) (cty.Value, error) {
// perspective, and will panic if it finds that they are not. For example if
// in is an object and out is a map, this function will still attempt to iterate
// through both as if they were the same.
// While the outermost in and out types may be compatible from a Convert
// perspective, inner types may not match when converting between maps and
// objects with optional attributes when the optional attributes don't match
// the map element type. Therefor in the case of a non-primitive type mismatch,
// we have to assume conversion was possible and pass the out type through.
func dynamicReplace(in, out cty.Type) cty.Type {
if in == cty.DynamicPseudoType || in == cty.NilType {
// Short circuit this case, there's no point worrying about this if in
@ -56,11 +61,9 @@ func dynamicReplace(in, out cty.Type) cty.Type {
// return it unchanged.
return out
case out.IsMapType():
var elemType cty.Type
// Maps are compatible with other maps or objects.
if in.IsMapType() {
elemType = dynamicReplace(in.ElementType(), out.ElementType())
return cty.Map(dynamicReplace(in.ElementType(), out.ElementType()))
}
if in.IsObjectType() {
@ -69,10 +72,10 @@ func dynamicReplace(in, out cty.Type) cty.Type {
types = append(types, t)
}
unifiedType, _ := unify(types, true)
elemType = dynamicReplace(unifiedType, out.ElementType())
return cty.Map(dynamicReplace(unifiedType, out.ElementType()))
}
return cty.Map(elemType)
return out
case out.IsObjectType():
// Objects are compatible with other objects and maps.
outTypes := map[string]cty.Type{}
@ -97,33 +100,29 @@ func dynamicReplace(in, out cty.Type) cty.Type {
return cty.Object(outTypes)
case out.IsSetType():
var elemType cty.Type
// Sets are compatible with other sets, lists, tuples.
if in.IsSetType() || in.IsListType() {
elemType = dynamicReplace(in.ElementType(), out.ElementType())
return cty.Set(dynamicReplace(in.ElementType(), out.ElementType()))
}
if in.IsTupleType() {
unifiedType, _ := unify(in.TupleElementTypes(), true)
elemType = dynamicReplace(unifiedType, out.ElementType())
return cty.Set(dynamicReplace(unifiedType, out.ElementType()))
}
return cty.Set(elemType)
return out
case out.IsListType():
var elemType cty.Type
// Lists are compatible with other lists, sets, and tuples.
if in.IsSetType() || in.IsListType() {
elemType = dynamicReplace(in.ElementType(), out.ElementType())
return cty.List(dynamicReplace(in.ElementType(), out.ElementType()))
}
if in.IsTupleType() {
unifiedType, _ := unify(in.TupleElementTypes(), true)
elemType = dynamicReplace(unifiedType, out.ElementType())
return cty.List(dynamicReplace(unifiedType, out.ElementType()))
}
return cty.List(elemType)
return out
case out.IsTupleType():
// Tuples are only compatible with other tuples
var types []cty.Type

View File

@ -1506,8 +1506,8 @@ func Keys(inputMap cty.Value) (cty.Value, error) {
}
// Lookup performs a dynamic lookup into a map.
// There are two required arguments, map and key, plus an optional default,
// which is a value to return if no key is found in map.
// There are three required arguments, inputMap and key, plus a defaultValue,
// which is a value to return if the given key is not found in the inputMap.
func Lookup(inputMap, key, defaultValue cty.Value) (cty.Value, error) {
return LookupFunc.Call([]cty.Value{inputMap, key, defaultValue})
}

View File

@ -30,8 +30,9 @@ func MakeToFunc(wantTy cty.Type) function.Function {
// messages to be more appropriate for an explicit type
// conversion, whereas the cty function system produces
// messages aimed at _implicit_ type conversions.
Type: cty.DynamicPseudoType,
AllowNull: true,
Type: cty.DynamicPseudoType,
AllowNull: true,
AllowDynamicType: true,
},
},
Type: func(args []cty.Value) (cty.Type, error) {

View File

@ -12,6 +12,9 @@ func marshal(val cty.Value, t cty.Type, path cty.Path, b *bytes.Buffer) error {
if val.IsMarked() {
return path.NewErrorf("value has marks, so it cannot be serialized as JSON")
}
if !val.IsKnown() {
return path.NewErrorf("value is not known")
}
// If we're going to decode as DynamicPseudoType then we need to save
// dynamic type information to recover the real type.
@ -24,10 +27,6 @@ func marshal(val cty.Value, t cty.Type, path cty.Path, b *bytes.Buffer) error {
return nil
}
if !val.IsKnown() {
return path.NewErrorf("value is not known")
}
// The caller should've guaranteed that the given val is conformant with
// the given type t, so we'll proceed under that assumption here.
@ -185,7 +184,10 @@ func marshalDynamic(val cty.Value, path cty.Path, b *bytes.Buffer) error {
return path.NewErrorf("failed to serialize type: %s", err)
}
b.WriteString(`{"value":`)
marshal(val, val.Type(), path, b)
err = marshal(val, val.Type(), path, b)
if err != nil {
return path.NewErrorf("failed to serialize value: %s", err)
}
b.WriteString(`,"type":`)
b.Write(typeJSON)
b.WriteRune('}')

View File

@ -1,6 +1,8 @@
package cty
import "math/big"
import (
"math/big"
)
// primitiveType is the hidden implementation of the various primitive types
// that are exposed as variables in this package.
@ -77,6 +79,18 @@ func rawNumberEqual(a, b *big.Float) bool {
case a.Sign() != b.Sign():
return false
default:
// First check if these are integers, and compare them directly. Floats
// need a more nuanced approach.
aInt, aAcc := a.Int(nil)
bInt, bAcc := b.Int(nil)
if aAcc != bAcc {
// only one is an exact integer value, so they can't be equal
return false
}
if aAcc == big.Exact {
return aInt.Cmp(bInt) == 0
}
// This format and precision matches that used by cty/json.Marshal,
// and thus achieves our definition of "two numbers are equal if
// we'd use the same JSON serialization for both of them".