A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://developer.hashicorp.com/terraform/plugin/framework/functions/returns/object below:

Object return values | Terraform

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.

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.

The function implementation documentation covers the general methods for setting function return data in function logic.

When setting the value for this return:

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))
}

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4