Number Type
Tip
Use Float64 Type for 64-bit floating point numbers. Use Int64 Type for 64-bit integer numbers.
Number types store an arbitrary precision (generally more than 64-bit, up to 512-bit) number.
By default, number from schema (configuration, plan, and state) data are represented in the framework by types.NumberType
and its associated value storage type of types.Number
. These types fully support Terraform's type system concepts that cannot be represented in Go built-in types, such as *big.Float
. 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 number value to a schema or nested attribute type:
Schema Type | Attribute Type |
---|---|
Data Source | schema.NumberAttribute |
Provider | schema.NumberAttribute |
Resource | schema.NumberAttribute |
Ephemeral Resource | schema.NumberAttribute |
If the number value should be the element type of a collection attribute type, set the ElemType
field to types.NumberType
or the appropriate custom type.
If the number value should be a value type of an object attribute type, set the AttrTypes
map value to types.NumberType
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.Number
in this case.
Access types.Number
information via the following methods:
(types.Number).IsNull() bool
: Returns true if the number is null.(types.Number).IsUnknown() bool
: Returns true if the number is unknown.(types.Number).ValueBigFloat() *big.Float
: Returns the known number, or the equivalent of0.0
if null or unknown.
In this example, a number value is checked for being null or unknown value first, before accessing its known value:
// Example data model definition// type ExampleModel struct {// ExampleAttribute types.Number `tfsdk:"example_attribute"`// }//// This would be filled in, such as calling: req.Plan.Get(ctx, &data)var data ExampleModel // optional logic for handling null valueif data.ExampleAttribute.IsNull() { // ...} // optional logic for handling unknown valueif data.ExampleAttribute.IsUnknown() { // ...} // myNumber now contains a Go *big.Float with the known valuemyNumber := data.ExampleAttribute.ValueBigFloat()
Setting Values
Call one of the following to create a types.Number
value:
types.NumberNull()
: A null float64 value.types.NumberUnknown()
: An unknown float64 value.types.NumberValue(*big.Float)
: A known value.
In this example, a known number value is created:
types.NumberValue(big.NewFloat(1.23))
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
In this example, a *big.Float
is directly used to set a number attribute value:
diags := resp.State.SetAttribute(ctx, path.Root("example_attribute"), big.NewFloat(1.23))
In this example, a types.List
of types.Number
is created from a []*big.Float
:
listValue, diags := types.ListValueFrom(ctx, types.NumberType, []*big.Float{big.NewFloat(1.2), big.NewFloat(2.4)})
Extending
The framework supports extending its base type implementations with custom types. These can adjust expected provider code usage depending on their implementation.