From e3caf363e7f3e15c9aabc95770cf8d8423f5f513 Mon Sep 17 00:00:00 2001 From: Daniel Banck Date: Wed, 18 Jan 2023 12:15:44 +0100 Subject: [PATCH] Add static function signatures for can and try --- internal/command/jsonfunction/function.go | 58 +++++++++++++++++++---- 1 file changed, 49 insertions(+), 9 deletions(-) diff --git a/internal/command/jsonfunction/function.go b/internal/command/jsonfunction/function.go index 138b4859d49f..99a78c92cf14 100644 --- a/internal/command/jsonfunction/function.go +++ b/internal/command/jsonfunction/function.go @@ -51,16 +51,22 @@ func Marshal(f map[string]function.Function) ([]byte, tfdiags.Diagnostics) { var diags tfdiags.Diagnostics signatures := newFunctions() - for k, v := range f { - signature, err := marshalFunction(v) - if err != nil { - diags = diags.Append(tfdiags.Sourceless( - tfdiags.Error, - fmt.Sprintf("Failed to serialize function %q", k), - err.Error(), - )) + for name, v := range f { + if name == "can" { + signatures.Signatures[name] = marshalCan(v) + } else if name == "try" { + signatures.Signatures[name] = marshalTry(v) + } else { + signature, err := marshalFunction(v) + if err != nil { + diags = diags.Append(tfdiags.Sourceless( + tfdiags.Error, + fmt.Sprintf("Failed to serialize function %q", name), + err.Error(), + )) + } + signatures.Signatures[name] = signature } - signatures.Signatures[k] = signature } if diags.HasErrors() { @@ -103,3 +109,37 @@ func marshalFunction(f function.Function) (*FunctionSignature, error) { VariadicParameter: vp, }, nil } + +// marshalTry returns a static function signature for the try function. +// We need this exception because the function implementation uses capsule +// types that we can't marshal. +func marshalTry(try function.Function) *FunctionSignature { + return &FunctionSignature{ + Description: try.Description(), + ReturnType: cty.DynamicPseudoType, + VariadicParameter: ¶meter{ + Name: try.VarParam().Name, + Description: try.VarParam().Description, + IsNullable: try.VarParam().AllowNull, + Type: cty.DynamicPseudoType, + }, + } +} + +// marshalCan returns a static function signature for the can function. +// We need this exception because the function implementation uses capsule +// types that we can't marshal. +func marshalCan(can function.Function) *FunctionSignature { + return &FunctionSignature{ + Description: can.Description(), + ReturnType: cty.Bool, + Parameters: []*parameter{ + { + Name: can.Params()[0].Name, + Description: can.Params()[0].Description, + IsNullable: can.Params()[0].AllowNull, + Type: cty.DynamicPseudoType, + }, + }, + } +}