Skip to content

Latest commit

 

History

History
115 lines (78 loc) · 6.33 KB

File metadata and controls

115 lines (78 loc) · 6.33 KB
page_title description
Plugin Development - Framework: Number Type
Learn the float64 value type in the provider development framework.

Number Type

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

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

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:

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 value
if data.ExampleAttribute.IsNull() {
    // ...
}

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

// myNumber now contains a Go *big.Float with the known value
myNumber := data.ExampleAttribute.ValueBigFloat()

Setting Values

Call one of the following to create a types.Number 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:

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.