Terraform
- Plugin Framework
- v1.16.x (latest)
- No versions of this document exist before v1.15.x. Click below to redirect to the version homepage.
- v1.15.x
- v1.14.x
- v1.13.x
- v1.12.x
- v1.11.x
- v1.10.x
- v1.9.x
- v1.8.x
- v1.7.x
- v1.6.x
- v1.5.x
- v1.3.x
- v1.2.x
- v1.1.x
- v1.0.x
- v0.17.x
- v0.16.x
- v0.15.x
- v0.14.x
- v0.13.x
- v0.12.x
- v0.11.x
- v0.10.x
- v0.9.x
- v0.8.x
- v0.7.x
List Type
List types store an ordered collection of single element type.
By default, lists from schema (configuration, plan, and state) data are represented in the framework by types.ListType and its associated value storage type of types.List. These types fully support Terraform's type system concepts that cannot be represented in Go built-in types, such as a slice. Framework types can be extended by provider code or shared libraries to provide specific use case functionality.
Schema Definitions
Use one of the following attribute types to directly add a list of a single element type to a schema or nested attribute type:
| Schema Type | Attribute Type |
|---|---|
| Data Source | schema.ListAttribute |
| Provider | schema.ListAttribute |
| Resource | schema.ListAttribute |
Use one of the following attribute types to directly add a list of a nested attributes to a schema or nested attribute type:
If the list value should be the element type of another collection attribute type, set the ElementType field to types.ListType{ElemType: /* ... */} or the appropriate custom type.
If the list value should be a value type of an object attribute type, set the AttributeTypes map value to types.ListType{ElemType: /* ... */} or the appropriate custom type.
Accessing Values
Access types.List information via the following methods:
(types.List).IsNull() bool: Returnstrueif the list is null.(types.List).IsUnknown() bool: Returnstrueif the list is unknown. Returnsfalseif the number of elements is known, any of which may be unknown.(types.List).Elements() []attr.Value: Returns the known[]attr.Valuevalue, ornilif null or unknown.(types.List).ElementsAs(context.Context, any, bool) diag.Diagnostics: Converts the known values into the given Go type, if possible. It is recommended to use a slice of framework types to account for elements which may be unknown.
In this example, a list of strings value is checked for being null or unknown value first, before accessing its known value elements as a []types.String:
// Example data model definition
// type ExampleModel struct {
// ExampleAttribute types.List `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() {
// ...
}
elements := make([]types.String, 0, len(data.ExampleAttribute.Elements()))
diags := data.ExampleAttribute.ElementsAs(ctx, &elements, false)
Setting Values
Call one of the following to create a types.List value:
types.ListNull(attr.Type) types.List: A null list value with the given element type.types.ListUnknown(attr.Type) types.List: An unknown list value with the given element type.types.ListValue(attr.Type, []attr.Value) (types.List, diag.Diagnostics): A known value with the given element type and values.types.ListValueFrom(context.Context, attr.Type, any) (types.List, diag.Diagnostics): A known value with the given element type and values. This can convert the source data from standard Go types into framework types as noted in the documentation for each element type, such as giving[]*stringfor atypes.Listoftypes.String.types.ListValueMust(map[string]attr.Type, map[string]attr.Value) types.List: A known value with the given element type and values. Any diagnostics are converted to a runtime panic. This is recommended only for testing or exhaustively tested logic.
In this example, a known list value is created from framework types:
elements := []attr.Value{types.StringValue("one"), types.StringValue("two")}
listValue, diags := types.ListValue(types.StringType, elements)
Otherwise, for certain framework functionality that does not require types implementations directly, such as:
(tfsdk.State).SetAttribute()types.ListValueFrom()types.MapValueFrom()types.ObjectValueFrom()types.SetValueFrom()
A Go built-in slice type ([]T) or type alias of a slice type such as type MyListType []T can be used instead.
In this example, a []string is directly used to set a list attribute value:
elements := []string{"one", "two"}
diags := resp.State.SetAttribute(ctx, path.Root("example_attribute"), elements)
In this example, a types.List of types.String is created from a []string:
elements := []string{"one", "two"}
listValue, diags := types.ListValueFrom(ctx, types.StringType, elements)
Extending
The framework supports extending its base type implementations with custom types. These can adjust expected provider code usage depending on their implementation.