MarshalProviderFunctions

pull/34450/head
James Bardin 2 years ago
parent c7f052e14d
commit 8c41a1e036

@ -7,6 +7,7 @@ import (
"encoding/json"
"fmt"
"github.com/hashicorp/terraform/internal/providers"
"github.com/hashicorp/terraform/internal/tfdiags"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/function"
@ -50,6 +51,24 @@ func newFunctions() *functions {
}
}
func MarshalProviderFunctions(f map[string]providers.FunctionDecl) ([]byte, error) {
if len(f) == 0 {
return nil, nil
}
signatures := newFunctions()
for name, v := range f {
signatures.Signatures[name] = marshalProviderFunction(v)
}
ret, err := json.Marshal(signatures)
if err != nil {
return nil, fmt.Errorf("Failed to serialize provider functions: %w", err)
}
return ret, nil
}
func Marshal(f map[string]function.Function) ([]byte, tfdiags.Diagnostics) {
var diags tfdiags.Diagnostics
signatures := newFunctions()
@ -113,6 +132,25 @@ func marshalFunction(f function.Function) (*FunctionSignature, error) {
}, nil
}
func marshalProviderFunction(f providers.FunctionDecl) *FunctionSignature {
var vp *parameter
if f.VariadicParameter != nil {
vp = marshalProviderParameter(*f.VariadicParameter)
}
var p []*parameter
if len(f.Parameters) > 0 {
p = marshalProviderParameters(f.Parameters)
}
return &FunctionSignature{
Description: f.Description,
ReturnType: f.ReturnType,
Parameters: p,
VariadicParameter: vp,
}
}
// 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.

@ -4,6 +4,7 @@
package jsonfunction
import (
"github.com/hashicorp/terraform/internal/providers"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/function"
)
@ -44,3 +45,20 @@ func marshalParameters(parameters []function.Parameter) []*parameter {
}
return ret
}
func marshalProviderParameter(p providers.FunctionParam) *parameter {
return &parameter{
Name: p.Name,
Description: p.Description,
IsNullable: p.AllowNullValue,
Type: p.Type,
}
}
func marshalProviderParameters(parameters []providers.FunctionParam) []*parameter {
ret := make([]*parameter, len(parameters))
for k, p := range parameters {
ret[k] = marshalProviderParameter(p)
}
return ret
}

Loading…
Cancel
Save