Terraform
String function parameters
String function parameters expect a collection of UTF-8 encoded bytes from a practitioner configuration. Values are accessible in function logic by the Go built-in string type, Go built-in *string type, or the framework string type.
In this Terraform configuration example, a string parameter is set to the value "hello world":
provider::example::example("hello world")
Function Definition
Use the function.StringParameter type in the function definition to accept a string value.
In this example, a function definition includes a first position string parameter:
func (f ExampleFunction) Definition(ctx context.Context, req function.DefinitionRequest, resp *function.DefinitionResponse) {
resp.Definition = function.Definition{
// ... other Definition fields ...
Parameters: []function.Parameter{
function.StringParameter{
Name: "string_param",
// ... potentially other StringParameter fields ...
},
},
}
}
If the string value should be the element type of a collection parameter type, set the ElementType field according to the framework string type. Refer to the collection parameter type documentation for additional details.
If the string value should be a value type of an object parameter type, set the AttributeTypes map value according to the framework string type. Refer to the object parameter type documentation for additional details.
Allow Null Values
By default, Terraform will not pass null values to the function logic. Use the AllowNullValue field to explicitly allow null values, if there is a meaningful distinction that should occur in function logic. Enabling AllowNullValue requires using a Go pointer type or framework string type when reading argument data.
Allow Unknown Values
By default, Terraform will not pass unknown values to the function logic. Use the AllowUnknownValues field to explicitly allow unknown values, if there is a meaningful distinction that should occur in function logic. Enabling AllowUnknownValues requires using a framework string type when reading argument data.
Custom Types
You may want to build your own data value and type implementations to allow your provider to combine validation and other behaviors into a reusable bundle. This helps avoid duplication and ensures consistency. These implementations use the CustomType field in the parameter type.
Refer to Custom Types for further details on creating provider-defined types and values.
Documentation
Refer to function documentation for information about the Name, Description, and MarkdownDescription fields available.
Reading Argument Data
The function implementation documentation covers the general methods for reading function argument data in function logic.
When retrieving the argument value for this parameter:
- If
CustomTypeis set, use its associated value type. - If
AllowUnknownValuesis enabled, you must use the framework string type. - If
AllowNullValueis enabled, you must use the Go built-in*stringtype or framework string type. - Otherwise, use the Go built-in
stringtype, Go built-in*stringtype, or framework string type.
In this example, a function defines a single string parameter and accesses its argument value:
func (f ExampleFunction) Definition(ctx context.Context, req function.DefinitionRequest, resp *function.DefinitionResponse) {
resp.Definition = function.Definition{
// ... other Definition fields ...
Parameters: []function.Parameter{
function.StringParameter{
Name: "string_param",
// ... potentially other StringParameter fields ...
},
},
}
}
func (f ExampleFunction) Run(ctx context.Context, req function.RunRequest, resp *function.RunResponse) {
var stringArg string
// var stringArg *string // e.g. with AllowNullValue, where Go nil equals Terraform null
// var stringArg types.String // e.g. with AllowUnknownValues or AllowNullValue
resp.Error = function.ConcatFuncErrors(resp.Error, req.Arguments.Get(ctx, &stringArg))
// stringArg is now populated
// ... other logic ...
}