github.com/opentofu/opentofu@v1.7.1/internal/plugin/convert/function.go (about) 1 package convert 2 3 import ( 4 "encoding/json" 5 "fmt" 6 7 "github.com/opentofu/opentofu/internal/providers" 8 "github.com/opentofu/opentofu/internal/tfplugin5" 9 "github.com/zclconf/go-cty/cty" 10 ) 11 12 func ProtoToCtyType(in []byte) cty.Type { 13 var out cty.Type 14 if err := json.Unmarshal(in, &out); err != nil { 15 panic(err) 16 } 17 return out 18 } 19 20 func ProtoToTextFormatting(proto tfplugin5.StringKind) providers.TextFormatting { 21 switch proto { 22 case tfplugin5.StringKind_PLAIN: 23 return providers.TextFormattingPlain 24 case tfplugin5.StringKind_MARKDOWN: 25 return providers.TextFormattingMarkdown 26 default: 27 panic(fmt.Sprintf("Invalid text tfplugin5.StringKind %v", proto)) 28 } 29 } 30 31 func ProtoToFunctionParameterSpec(proto *tfplugin5.Function_Parameter) providers.FunctionParameterSpec { 32 return providers.FunctionParameterSpec{ 33 Name: proto.Name, 34 Type: ProtoToCtyType(proto.Type), 35 AllowNullValue: proto.AllowNullValue, 36 AllowUnknownValues: proto.AllowUnknownValues, 37 Description: proto.Description, 38 DescriptionFormat: ProtoToTextFormatting(proto.DescriptionKind), 39 } 40 } 41 42 func ProtoToFunctionSpec(proto *tfplugin5.Function) providers.FunctionSpec { 43 params := make([]providers.FunctionParameterSpec, len(proto.Parameters)) 44 for i, param := range proto.Parameters { 45 params[i] = ProtoToFunctionParameterSpec(param) 46 } 47 48 var varParam *providers.FunctionParameterSpec 49 if proto.VariadicParameter != nil { 50 param := ProtoToFunctionParameterSpec(proto.VariadicParameter) 51 varParam = ¶m 52 } 53 54 return providers.FunctionSpec{ 55 Parameters: params, 56 VariadicParameter: varParam, 57 Return: ProtoToCtyType(proto.Return.Type), 58 Summary: proto.Summary, 59 Description: proto.Description, 60 DescriptionFormat: ProtoToTextFormatting(proto.DescriptionKind), 61 DeprecationMessage: proto.DeprecationMessage, 62 } 63 }