Terraform
Int32 Type
Tip
Use Float32 Type for 32-bit floating point numbers. Use Number Attribute for arbitrary precision numbers.
Int32 types store a 32-bit integer number.
By default, int32 from schema (configuration, plan, and state) data are represented in the framework by types.Int32Type
and its associated value storage type of types.Int32
. These types fully support Terraform's type system concepts that cannot be represented in Go built-in types, such as *int32
. 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 int32 value to a schema or nested attribute type:
Schema Type | Attribute Type |
---|---|
Data Source | schema.Int32Attribute |
Provider | schema.Int32Attribute |
Resource | schema.Int32Attribute |
Ephemeral Resource | schema.Int32Attribute |
If the int32 value should be the element type of a collection attribute type, set the ElemType
field to types.Int32Type
or the appropriate custom type.
If the int32 value should be a value type of an object attribute type, set the AttrTypes
map value to types.Int32Type
or the appropriate custom type.
Accessing Values
Tip
Review the attribute documentation to understand how schema-based data gets mapped into accessible values, such as a types.Int32
in this case.
Access types.Int32
information via the following methods:
(types.Int32).IsNull() bool
: Returns true if the int32 is null.(types.Int32).IsUnknown() bool
: Returns true if the int32 is unknown.(types.Int32).ValueInt32() int32
: Returns the known int32, or0
if null or unknown.(types.Int32).ValueInt32Pointer() *int32
: Returns a int32 pointer to a known value,nil
if null, or a pointer to0
if unknown.
In this example, a int32 value is checked for being null or unknown value first, before accessing its known value:
// Example data model definition
// type ExampleModel struct {
// ExampleAttribute types.Int32 `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() {
// ...
}
// myInt32 now contains a Go int32 with the known value
myInt32 := data.ExampleAttribute.ValueInt32()
Setting Values
Call one of the following to create a types.Int32
value:
types.Int32Null()
: A null int32 value.types.Int32Unknown()
: An unknown int32 value.types.Int32Value(int32)
: A known value.types.Int32PointerValue(*int32)
: A known value.
In this example, a known int32 value is created:
types.Int32Value(123)
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()
Numbers can be automatically converted from the following Go types, pointers to these types, or any aliases of these types, such type MyNumber int
:
int
,int8
,int16
,int32
,int64
uint
,uint8
,uint16
,uint32
,uint64
float32
,float64
*big.Int
,*big.Float
An error will be returned if the value of the number cannot be stored in the numeric type supplied because of an overflow or other loss of precision.
In this example, a int32
is directly used to set a int32 attribute value:
diags := resp.State.SetAttribute(ctx, path.Root("example_attribute"), 123)
In this example, a types.List
of types.Int32
is created from a []int32
:
listValue, diags := types.ListValueFrom(ctx, types.Int32Type, []int32{123, 456})
Extending
The framework supports extending its base type implementations with custom types. These can adjust expected provider code usage depending on their implementation.