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/handling-data/types/dynamic below:

Dynamic types | Terraform | HashiCorp Developer

Tip

Static types should always be preferred over dynamic types, when possible.

Developers dealing with dynamic data will need to have extensive knowledge of the Terraform type system to properly handle all potential practitioner configuration scenarios.

Refer to Dynamic Data - Considerations for more information.

Dynamic is a container type that can have an underlying value of any type.

By default, dynamic values from schema (configuration, plan, and state) data are represented in the framework by types.DynamicType and its associated value storage type of types.Dynamic. These types fully support Terraform's type system concepts that cannot be represented in Go built-in types. Framework types can be extended by provider code or shared libraries to provide specific use case functionality.

Use one of the following attribute types to directly add a dynamic value to a schema or nested attribute type:

Dynamic values are not supported as the element type of a collection type or within collection attribute types.

If the dynamic value should be a value type of an object attribute type, set the AttrTypes map value to types.DynamicType or the appropriate custom type.

Tip

Review the attribute documentation to understand how schema-based data gets mapped into accessible values, such as a types.Dynamic in this case.

Access types.Dynamic information via the following methods:

In this example, a dynamic value is checked for being null or unknown value first, before accessing its known value:

// Example data model definition
// type ExampleModel struct {
//   ExampleAttribute types.Dynamic `tfsdk:"example_attribute"`
// }
//
// This would be filled in, such as calling: req.Plan.Get(ctx, &data)
var data ExampleModel

// optional logic for handling null value
if data.ExampleAttribute.IsNull() {
    // ...
}

// optional logic for handling unknown value
if data.ExampleAttribute.IsUnknown() {
    // ...
}

// myDynamicVal now contains the underlying value, determined by Terraform at runtime
myDynamicVal := data.ExampleAttribute.UnderlyingValue()
Handling the Underlying Value

If a dynamic value is known, a Go type switch can be used to access the type-specific methods for data handling:

    switch value := data.ExampleAttribute.UnderlyingValue().(type) {
    case types.Bool:
        // Handle boolean value
    case types.Number:
        // Handle float64, int64, and number values
    case types.List:
        // Handle list value
    case types.Map:
        // Handle map value
    case types.Object:
        // Handle object value
    case types.Set:
        // Handle set value
    case types.String:
        // Handle string value
    case types.Tuple:
        // Handle tuple value
    }

Tip

Float64 and Int64 framework types will never appear in the underlying value as both are represented as the Terraform type number.

The type of the underlying value is determined at runtime by Terraform if the value is from configuration. Developers dealing with dynamic data will need to have extensive knowledge of the Terraform type system to properly handle all potential practitioner configuration scenarios.

Refer to the Dynamic Data documentation for more information.

Call one of the following to create a types.Dynamic value:

In this example, a known dynamic value is created, where the underlying value is a known string value:

types.DynamicValue(types.StringValue("hello world!"))

In this example, a known dynamic value is created, where the underlying value is a known object value:

elementTypes := map[string]attr.Type{
    "attr1": types.StringType,
    "attr2": types.Int64Type,
}
elements := map[string]attr.Value{
    "attr1": types.StringValue("value"),
    "attr2": types.Int64Value(123),
}
objectValue, diags := types.ObjectValue(elementTypes, elements)
// ... handle any diagnostics ...

types.DynamicValue(objectValue)

There are no reflection rules defined for creating dynamic values, meaning they must be created using the types implementation.

In this example, a types.Dynamic with a known boolean value is used to set a dynamic attribute value:

diags := resp.State.SetAttribute(ctx, path.Root("example_attribute"), types.DynamicValue(types.BoolValue(true)))

The framework supports extending its base type implementations with custom types. These can adjust expected provider code usage depending on their implementation.


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