vendor: github.com/zclconf/go-cty v1.16.0

Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax
2025-01-13 18:00:34 +01:00
parent 26026810fe
commit 66ecb53fa7
8 changed files with 38 additions and 28 deletions

View File

@ -251,15 +251,25 @@ func (f Function) Call(args []cty.Value) (val cty.Value, err error) {
if err != nil {
return cty.NilVal, err
}
var resultMarks []cty.ValueMarks
// If we are returning an unknown early due to some unknown in the
// arguments, we first need to complete the iteration over all the args
// to ensure we collect as many marks as possible for resultMarks.
returnUnknown := false
if dynTypeArgs {
// returnTypeForValues sets this if any argument was inexactly typed
// and the corresponding parameter did not indicate it could deal with
// that. In that case we also avoid calling the implementation function
// because it will also typically not be ready to deal with that case.
return cty.UnknownVal(expectedType), nil
returnUnknown = true
}
if refineResult := f.spec.RefineResult; refineResult != nil {
// If returnUnknown is set already, it means we don't have a refinement
// because of dynTypeArgs, but we may still need to collect marks from the
// rest of the arguments.
if refineResult := f.spec.RefineResult; refineResult != nil && !returnUnknown {
// If this function has a refinement callback then we'll refine
// our result value in the same way regardless of how we return.
// It's the function author's responsibility to ensure that the
@ -280,15 +290,10 @@ func (f Function) Call(args []cty.Value) (val cty.Value, err error) {
// values and marked values.
posArgs := args[:len(f.spec.Params)]
varArgs := args[len(f.spec.Params):]
var resultMarks []cty.ValueMarks
for i, spec := range f.spec.Params {
val := posArgs[i]
if !val.IsKnown() && !spec.AllowUnknown {
return cty.UnknownVal(expectedType), nil
}
if !spec.AllowMarked {
unwrappedVal, marks := val.UnmarkDeep()
if len(marks) > 0 {
@ -305,14 +310,15 @@ func (f Function) Call(args []cty.Value) (val cty.Value, err error) {
args = newArgs
}
}
if !val.IsKnown() && !spec.AllowUnknown {
returnUnknown = true
}
}
if f.spec.VarParam != nil {
spec := f.spec.VarParam
for i, val := range varArgs {
if !val.IsKnown() && !spec.AllowUnknown {
return cty.UnknownVal(expectedType), nil
}
if !spec.AllowMarked {
unwrappedVal, marks := val.UnmarkDeep()
if len(marks) > 0 {
@ -323,9 +329,16 @@ func (f Function) Call(args []cty.Value) (val cty.Value, err error) {
args = newArgs
}
}
if !val.IsKnown() && !spec.AllowUnknown {
returnUnknown = true
}
}
}
if returnUnknown {
return cty.UnknownVal(expectedType).WithMarks(resultMarks...), nil
}
var retVal cty.Value
{
// Intercept any panics from the function and return them as normal errors,

View File

@ -147,12 +147,6 @@ var ElementFunc = function.New(&function.Spec{
},
Type: func(args []cty.Value) (cty.Type, error) {
list := args[0]
index := args[1]
if index.IsKnown() {
if index.LessThan(cty.NumberIntVal(0)).True() {
return cty.DynamicPseudoType, fmt.Errorf("cannot use element function with a negative index")
}
}
listTy := list.Type()
switch {
@ -189,10 +183,6 @@ var ElementFunc = function.New(&function.Spec{
return cty.DynamicVal, fmt.Errorf("invalid index: %s", err)
}
if args[1].LessThan(cty.NumberIntVal(0)).True() {
return cty.DynamicVal, fmt.Errorf("cannot use element function with a negative index")
}
input, marks := args[0].Unmark()
if !input.IsKnown() {
return cty.UnknownVal(retType), nil
@ -203,6 +193,9 @@ var ElementFunc = function.New(&function.Spec{
return cty.DynamicVal, errors.New("cannot use element function with an empty list")
}
index = index % l
if index < 0 {
index += l
}
// We did all the necessary type checks in the type function above,
// so this is guaranteed not to fail.