I’m working on a Terraform provider, where I have a resource which contains Float64 attributes.
Within the Schema I’ve specified this attribute;
"latitude": schema.Float64Attribute{
Description: "Latitude of the resource location",
MarkdownDescription: "Latitude of the resource location",
Optional: true,
},
Everything compiles and runs, but the validation fails, when I try to set a value, which is not a “whole” number or a float ending with “.5” So these numbers work (123
, 123.5
), but all other numbers like (123.1
, 123.2
, etc.) fail.
The error I get is:
Float64 Type Validation Error: Value %!s(*big.Float=123.6) cannot be represented as a 64-bit floating point.
Since I’m new to the plugin framework, I’m not sure if I’m missing something or that this might be a bug within the framework. These are the (hashicorp) modules as shown in go.mod
.
github.com/hashicorp/terraform-plugin-framework v1.0.1
github.com/hashicorp/terraform-plugin-go v0.14.3 // indirect
github.com/hashicorp/terraform-plugin-log v0.7.0 // indirect
github.com/hashicorp/terraform-registry-address v0.1.0 // indirect
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734 // indirect
github.com/hashicorp/go-hclog v1.4.0 // indirect
github.com/hashicorp/go-plugin v1.4.8 // indirect
github.com/hashicorp/go-uuid v1.0.3 // indirect
github.com/hashicorp/yamux v0.1.1 // indirect
bflad
January 9, 2023, 5:10pm
2
Hi @rob.maas Thank you for raising this topic. It doesn’t appear you are doing anything wrong here and I have verified that this is indeed a bug in terraform-plugin-framework’s validation handling for Float64 types. Could you raise a bug report so we can get this fixed up?
Thanks so much.
Until this is fixed, you can fallback to the Number type/attribute, which does not have the same validation (since its ultimately applying the same logic as Terraform core’s type system). It does have the slight downside of being a big package - math/big - Go Packages instead of the more convenient float64
Go type, but there is the (*big.Float).Float64()
method which can be used to convert it to float64
.
1 Like
Sorry, I completely missed that 1.1.0 was released already.
Unfortunately I tried using the float option with release 1.1.1, but it keeps coming with the same error.
Output of terraform plan.
Error: Value %!s(*big.Float=51.12) cannot be represented as a 64-bit floating point.
go.mod
require (
github.com/hashicorp/terraform-plugin-framework v1.1.1
github.com/on2itsecurity/go-auxo v1.0.1
)
require (
github.com/golang/protobuf v1.5.2 // indirect
github.com/hashicorp/terraform-plugin-go v0.14.3 // indirect
github.com/hashicorp/terraform-plugin-log v0.7.0 // indirect
github.com/hashicorp/terraform-registry-address v0.1.0 // indirect
github.com/hashicorp/terraform-svchost v0.1.0 // indirect
github.com/kr/pretty v0.3.0 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/vmihailenco/msgpack/v4 v4.3.12 // indirect
github.com/vmihailenco/tagparser v0.1.2 // indirect
google.golang.org/protobuf v1.28.1 // indirect
)
I can see the changes within my local vendor folder, so that seems all ok.
hashicorp:main
← hashicorp:bflad/float64type-string-floats
opened 10:21PM - 09 Jan 23 UTC
Closes #613
Numbers in Terraform's type system can be represented with up to … 512 bits of base 10 precision. When encoding and decoding across the protocol, the type system or MessagePack may switch the representation to be stringified, which is valid and expected.
The prior `Float64Type` type `Validate` method logic was only checking the `big.Accuracy` return from the value, however in certain cases the stringified number was saved with a `big.Below` or `big.Above` accuracy, even though the value itself could be successfully converted into a `float64` type. This change updates the validation logic to follow the `(*big.Float).Float64()` method documentation to check the returned value, along with the `big.Accuracy`.
Previously before logic updates:
```
--- FAIL: TestFloat64TypeValidate (0.00s)
--- FAIL: TestFloat64TypeValidate/positive-string-float (0.00s)
/Users/bflad/src/github.com/hashicorp/terraform-plugin-framework/types/basetypes/float64_test.go:98: unexpected difference: diag.Diagnostics(
- {
- diag.withPath{
- Diagnostic: diag.ErrorDiagnostic{
- detail: "Value %!s(*big.Float=123.2) cann"...,
- summary: "Float64 Type Validation Error",
- },
- path: s"test",
- },
- },
+ nil,
)
--- FAIL: TestFloat64TypeValidate/negative-string-float (0.00s)
/Users/bflad/src/github.com/hashicorp/terraform-plugin-framework/types/basetypes/float64_test.go:98: unexpected difference: diag.Diagnostics(
- {
- diag.withPath{
- Diagnostic: diag.ErrorDiagnostic{
- detail: "Value %!s(*big.Float=-123.2) can"...,
- summary: "Float64 Type Validation Error",
- },
- path: s"test",
- },
- },
+ nil,
)
--- FAIL: TestFloat64TypeValidate/MaxFloat64-above (0.00s)
/Users/bflad/src/github.com/hashicorp/terraform-plugin-framework/types/basetypes/float64_test.go:98: unexpected difference: diag.Diagnostics(
- {
- diag.withPath{
- Diagnostic: diag.ErrorDiagnostic{
- detail: "Value %!s(*big.Float=2.797693135"...,
- summary: "Float64 Type Validation Error",
- },
- path: s"test",
- },
- },
+ nil,
)
--- FAIL: TestFloat64TypeValidate/SmallestNonzeroFloat64-below (0.00s)
/Users/bflad/src/github.com/hashicorp/terraform-plugin-framework/types/basetypes/float64_test.go:98: unexpected difference: diag.Diagnostics(
- {
- diag.withPath{
- Diagnostic: diag.ErrorDiagnostic{
- detail: "Value %!s(*big.Float=3.940656458"...,
- summary: "Float64 Type Validation Error",
- },
- path: s"test",
- },
- },
+ nil,
)
```
Should I re-open the case or do you want me to create a new one @bflad ?
opened 08:19PM - 09 Jan 23 UTC
closed 12:40PM - 10 Jan 23 UTC
bug
I'm trying to write my own provider with the plugin framework, but the validatio… n on `schema.Float64Attribute` fails.
### Module version
<!---
Inspect your go.mod as below to find the version, and paste the result between the ``` marks below.
go list -m github.com/hashicorp/terraform-plugin-framework/...
If you are not running the latest version of the framework, please try upgrading
because your bug may have already been fixed.
-->
```
github.com/hashicorp/terraform-plugin-framework v1.0.1
```
### Relevant provider source code
<!--
Paste any Go code that you believe to be relevant to the bug
e.g. schema or implementation of CRUD for a given resource or data source
-->
```go
"latitude": schema.Float64Attribute{
Description: "Latitude of the resource location",
MarkdownDescription: "Latitude of the resource location",
Optional: true,
},
```
### Terraform Configuration Files
<!--
Paste the relevant parts of your Terraform configuration between the ``` marks below.
For large Terraform configs, please use a service like Dropbox and share a link to the ZIP file. For security, you can also encrypt the files using our GPG public key.
-->
```hcl
latitude = "123.2"
```
<!--
### Debug Output
Full debug output can be obtained by running Terraform with the environment variable `TF_LOG=trace`. Please create a GitHub Gist containing the debug output. Please do _not_ paste the debug output in the issue, since debug output is long.
Debug output may contain sensitive information. Please review it before posting publicly, and if you are concerned feel free to encrypt the files using the HashiCorp security public key.
-->
### Expected Behavior
<!--
What should have happened?
-->
The expected result should be, that floating point numbers like latitude and longitude in this case should be accepted.
### Actual Behavior
<!--
What actually happened?
-->
Terraform validation fails, when I try to set a value, which is not a “whole” number or a float ending with “.5” So these numbers work (123, 123.5), but all other numbers like (123.1, 123.2, etc.) fail.
Error message:
```
Float64 Type Validation Error: Value %!s(*big.Float=123.6) cannot be represented as a 64-bit floating point.
```
<!--
### Steps to Reproduce
Please list the full steps required to reproduce the issue, for example:
1. `terraform init`
2. `terraform apply`
-->
### References
<!--
Are there any other GitHub issues (open or closed) or Pull Requests that should be linked here? For example:
- #6017
-->
I posted this on the Hashicorp discuss forum as well:
https://discuss.hashicorp.com/t/terraform-plugin-framework-float64-type-validation-error/48645
bflad
February 2, 2023, 5:08pm
4
If you could open a new issue, that would be very appreciated. Thank you.