Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert to number error when storing a value in float64 during execution of provider-defined function #914

Closed
bendbennett opened this issue Jan 30, 2024 · 1 comment · Fixed by #919
Labels
bug Something isn't working reflection Issues and PRs about the reflection subsystem used to convert between attr.Values and Go values. types Issues and pull requests about our types abstraction and implementations.

Comments

@bendbennett
Copy link
Contributor

Module version

v1.5.0

Relevant provider source code

var _ function.Function = Float64Function{}

func NewFloat64Function() function.Function {
	return &Float64Function{}
}

type Float64Function struct{}

func (f Float64Function) Metadata(ctx context.Context, req function.MetadataRequest, resp *function.MetadataResponse) {
	resp.Name = "float64"
}

func (f Float64Function) Definition(ctx context.Context, req function.DefinitionRequest, resp *function.DefinitionResponse) {
	resp.Definition = function.Definition{
		Parameters: []function.Parameter{
			function.Float64Parameter{},
		},
		Return: function.Float64Return{},
	}
}

func (f Float64Function) Run(ctx context.Context, req function.RunRequest, resp *function.RunResponse) {
	var arg float64

	resp.Diagnostics.Append(req.Arguments.Get(ctx, &arg)...)

	resp.Diagnostics.Append(resp.Result.Set(ctx, arg)...)
}
func TestFloat64Function_known(t *testing.T) {
	resource.UnitTest(t, resource.TestCase{
		TerraformVersionChecks: []tfversion.TerraformVersionCheck{
			tfversion.SkipBelow(tfversion.Version1_7_0),
		},
		ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
			"framework": providerserver.NewProtocol6WithError(New()),
		},
		Steps: []resource.TestStep{
			{
				Config: `
				output "test" {
					value = provider::framework::float64(1.23)
				}`,
				ConfigStateChecks: []statecheck.StateCheck{
					statecheck.ExpectKnownOutputValue("test", knownvalue.Float64Exact(1.23)),
				},
			},
		},
	})
}

Terraform Configuration Files

Following is the configuration used during execution of the test.

output "test" {
	value = provider::framework::float64(1.23)
}

Debug Output

https://gist.github.com/bendbennett/ea6aadc53b2e53e66eda630f6b638e64

Expected Behavior

Test should run sucessfully.

Actual Behavior

Following error is generated:

=== RUN   TestFloat64Function_known
    float64_function_test.go:20: Step 1/1 error: Error running pre-apply plan: exit status 1
        
        Error: Error in function call
        
          on terraform_plugin_test.tf line 13, in output "test":
          13: 					value = provider::framework::float64(1.23)
            ├────────────────
            │ while calling provider::framework::float64(param)
        
        Call to function "provider::framework::float64" failed: Value Conversion
        Error: An unexpected error was encountered trying to convert to number. This
        is always an error in the provider. Please report the following to the
        provider developer:
        
        cannot store 1.23 in float64.
--- FAIL: TestFloat64Function_known (0.15s)

Steps to Reproduce

  1. Requires a build of the Terraform binary from main (until such time as v1.8.0 is released).
  2. Test described is included as part of Adding Provider-Defined Function Testing, a specific example being TestFloat64Function_known
@bendbennett bendbennett added the bug Something isn't working label Jan 30, 2024
@bflad bflad added types Issues and pull requests about our types abstraction and implementations. reflection Issues and PRs about the reflection subsystem used to convert between attr.Values and Go values. labels Jan 30, 2024
bendbennett added a commit that referenced this issue Feb 6, 2024
)

  * The reflect.Number function was handling float32 and float64 values
    by determining whether the value in the *big.Float generated through
    calling val.As() was an exact representation by examining the accuracy
    produced when calling Float32() or Float64() on the *big.Float
  * The reflect.Number function has been altered to verify whether the
    string representation, generated by calling Text(), of the generated
    *big.Float and a *big.Float created from the float32 or float64 value
    are identical
bendbennett added a commit that referenced this issue Feb 22, 2024
…via string representation (#919)

* Alter handling of float32 and float64 values within reflect.Number (#914)

  * The reflect.Number function was handling float32 and float64 values
    by determining whether the value in the *big.Float generated through
    calling val.As() was an exact representation by examining the accuracy
    produced when calling Float32() or Float64() on the *big.Float
  * The reflect.Number function has been altered to verify whether the
    string representation, generated by calling Text(), of the generated
    *big.Float and a *big.Float created from the float32 or float64 value
    are identical

* Removing AllowRoundingNumbers option and modifying reflect.Number accordingly (#914)
bendbennett added a commit to hashicorp/terraform-provider-corner that referenced this issue Feb 22, 2024
bendbennett added a commit to hashicorp/terraform-provider-corner that referenced this issue Mar 6, 2024
* Adding function tests for framework protocol 5 & 6 and protocol 6 mux providers (#202)

* Adding copyright headers (#202)

* Linting (#202)

* Adding function tests for protocol v5 and protocol v6 providers (#202)

* Amending provider defined function tests for number until bug fix on go.cty is released (#202)

* Adding function testing for mux providers (#202)

* Adding function testing for tf6to5provider (#202)

* Adding copyright headers (#202)

* Bumping terraform-plugin-framework version to latest (#202)

Latest terraform-plugin-framework contains fix for:
  * hashicorp/terraform-plugin-framework#914
  * hashicorp/terraform-plugin-framework#919

* Bumping terraform-plugin-testing to v1.7.0 (#202)

* Refactoring to return function.FuncError (#202)

* Updates following code review (#202)
Copy link

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 24, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working reflection Issues and PRs about the reflection subsystem used to convert between attr.Values and Go values. types Issues and pull requests about our types abstraction and implementations.
Projects
None yet
2 participants