Terraform
Object return values
Object function return expects a single structure mapping explicit attribute names to type definitions from function logic. Set values in function logic with a Go structure type annotated with tfsdk field tags or the framework map type.
Function Definition
Use the function.ObjectReturn type in the function definition.
The AttributeTypes field must be defined, which represents a mapping of attribute names to framework value types. An attribute type may itself contain further collection or object types, if necessary.
In this example, a function definition includes an object return:
func (f ExampleFunction) Definition(ctx context.Context, req function.DefinitionRequest, resp *function.DefinitionResponse) {
resp.Definition = function.Definition{
// ... other Definition fields ...
Return: function.ObjectReturn{
AttributeTypes: map[string]attr.Type{
"attr1": types.StringType,
"attr2": types.Int64Type,
},
// ... potentially other ObjectReturn fields ...
},
}
}
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 return type.
Refer to Custom Types for further details on creating provider-defined types and values.
Documentation
Return documentation is expected in the top-level function documentation. Refer to function documentation for information about the Summary, Description, and MarkdownDescription fields available.
Setting Return Data
The function implementation documentation covers the general methods for setting function return data in function logic.
When setting the value for this return:
- If
CustomTypeis set, use its associated value type. - Otherwise, use a Go structure type annotated with
tfsdkfield tags or framework map type.
In this example, a function defines a map of string return and sets its value:
func (f ExampleFunction) Definition(ctx context.Context, req function.DefinitionRequest, resp *function.DefinitionResponse) {
resp.Definition = function.Definition{
// ... other Definition fields ...
Return: function.ObjectReturn{
AttributeTypes: map[string]attr.Type{
"attr1": types.StringType,
"attr2": types.Int64Type,
},
},
}
}
func (f ExampleFunction) Run(ctx context.Context, req function.RunRequest, resp *function.RunResponse) {
// ... other logic ...
// hardcoded structure type and value for example brevity
result := struct{
Attr1 string `tfsdk:"attr1"`
Attr2 int64 `tfsdk:"attr2"`
}{
Attr1: "value1",
Attr2: 123,
}
resp.Error = function.ConcatFuncErrors(resp.Error, resp.Result.Set(ctx, &result))
}