Skip to content

Float64 AtLeast, AtMost and Between validators #18

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

Merged
merged 16 commits into from
May 24, 2022
Merged

Float64 AtLeast, AtMost and Between validators #18

merged 16 commits into from
May 24, 2022

Conversation

ewbankkit
Copy link
Contributor

@ewbankkit ewbankkit commented May 20, 2022

Closes #1.

Copied wholesale from https://github.com/hashicorp/terraform-provider-awscc/tree/main/internal/validate.

% go test -v ./validate   
=== RUN   TestFloatBetweenValidator
=== PAUSE TestFloatBetweenValidator
=== RUN   TestFloatAtLeastValidator
=== PAUSE TestFloatAtLeastValidator
=== RUN   TestFloatAtMostValidator
=== PAUSE TestFloatAtMostValidator
=== CONT  TestFloatBetweenValidator
=== CONT  TestFloatAtMostValidator
=== RUN   TestFloatBetweenValidator/not_a_number
=== RUN   TestFloatAtMostValidator/valid_float_as_Number
=== CONT  TestFloatAtLeastValidator
=== RUN   TestFloatAtLeastValidator/unknown_number
=== RUN   TestFloatBetweenValidator/unknown_number
=== RUN   TestFloatAtMostValidator/valid_float_as_Float64_max
=== RUN   TestFloatBetweenValidator/valid_integer_as_Number
=== RUN   TestFloatAtLeastValidator/null_number
=== RUN   TestFloatBetweenValidator/valid_float_as_Number_min
=== RUN   TestFloatAtLeastValidator/valid_integer_as_Number
=== RUN   TestFloatAtMostValidator/too_large_float_as_Number
=== RUN   TestFloatBetweenValidator/valid_float_as_Float64_min
=== RUN   TestFloatBetweenValidator/valid_float_as_Float64_max
=== RUN   TestFloatAtMostValidator/valid_integer_as_Number
=== RUN   TestFloatAtLeastValidator/valid_integer_as_Float64
=== RUN   TestFloatAtMostValidator/valid_integer_as_Float64
=== RUN   TestFloatBetweenValidator/null_number
=== RUN   TestFloatAtLeastValidator/not_a_number
=== RUN   TestFloatAtMostValidator/null_number
=== RUN   TestFloatBetweenValidator/valid_integer_as_Float64
=== RUN   TestFloatAtLeastValidator/valid_float_as_Number
=== RUN   TestFloatAtMostValidator/valid_float_as_Float64
=== RUN   TestFloatAtLeastValidator/valid_float_as_Float64
=== RUN   TestFloatBetweenValidator/valid_float_as_Number
=== RUN   TestFloatBetweenValidator/valid_float_as_Float64
=== RUN   TestFloatAtMostValidator/valid_float_as_Number_max
=== RUN   TestFloatAtLeastValidator/valid_float_as_Number_min
=== RUN   TestFloatBetweenValidator/valid_float_as_Number_max
=== RUN   TestFloatAtMostValidator/not_a_number
=== RUN   TestFloatBetweenValidator/too_small_float_as_Number
=== RUN   TestFloatAtLeastValidator/valid_float_as_Float64_min
=== RUN   TestFloatAtMostValidator/unknown_number
=== RUN   TestFloatBetweenValidator/too_large_float_as_Number
=== RUN   TestFloatAtLeastValidator/too_small_float_as_Number
--- PASS: TestFloatBetweenValidator (0.00s)
    --- PASS: TestFloatBetweenValidator/not_a_number (0.00s)
    --- PASS: TestFloatBetweenValidator/unknown_number (0.00s)
    --- PASS: TestFloatBetweenValidator/valid_integer_as_Number (0.00s)
    --- PASS: TestFloatBetweenValidator/valid_float_as_Number_min (0.00s)
    --- PASS: TestFloatBetweenValidator/valid_float_as_Float64_min (0.00s)
    --- PASS: TestFloatBetweenValidator/valid_float_as_Float64_max (0.00s)
    --- PASS: TestFloatBetweenValidator/null_number (0.00s)
    --- PASS: TestFloatBetweenValidator/valid_integer_as_Float64 (0.00s)
    --- PASS: TestFloatBetweenValidator/valid_float_as_Number (0.00s)
    --- PASS: TestFloatBetweenValidator/valid_float_as_Float64 (0.00s)
    --- PASS: TestFloatBetweenValidator/valid_float_as_Number_max (0.00s)
    --- PASS: TestFloatBetweenValidator/too_small_float_as_Number (0.00s)
    --- PASS: TestFloatBetweenValidator/too_large_float_as_Number (0.00s)
--- PASS: TestFloatAtMostValidator (0.00s)
    --- PASS: TestFloatAtMostValidator/valid_float_as_Number (0.00s)
    --- PASS: TestFloatAtMostValidator/valid_float_as_Float64_max (0.00s)
    --- PASS: TestFloatAtMostValidator/too_large_float_as_Number (0.00s)
    --- PASS: TestFloatAtMostValidator/valid_integer_as_Number (0.00s)
    --- PASS: TestFloatAtMostValidator/valid_integer_as_Float64 (0.00s)
    --- PASS: TestFloatAtMostValidator/null_number (0.00s)
    --- PASS: TestFloatAtMostValidator/valid_float_as_Float64 (0.00s)
    --- PASS: TestFloatAtMostValidator/valid_float_as_Number_max (0.00s)
    --- PASS: TestFloatAtMostValidator/not_a_number (0.00s)
    --- PASS: TestFloatAtMostValidator/unknown_number (0.00s)
--- PASS: TestFloatAtLeastValidator (0.00s)
    --- PASS: TestFloatAtLeastValidator/unknown_number (0.00s)
    --- PASS: TestFloatAtLeastValidator/null_number (0.00s)
    --- PASS: TestFloatAtLeastValidator/valid_integer_as_Number (0.00s)
    --- PASS: TestFloatAtLeastValidator/valid_integer_as_Float64 (0.00s)
    --- PASS: TestFloatAtLeastValidator/not_a_number (0.00s)
    --- PASS: TestFloatAtLeastValidator/valid_float_as_Number (0.00s)
    --- PASS: TestFloatAtLeastValidator/valid_float_as_Float64 (0.00s)
    --- PASS: TestFloatAtLeastValidator/valid_float_as_Number_min (0.00s)
    --- PASS: TestFloatAtLeastValidator/valid_float_as_Float64_min (0.00s)
    --- PASS: TestFloatAtLeastValidator/too_small_float_as_Number (0.00s)
PASS
ok  	github.com/hashicorp/terraform-plugin-framework-validators/validate	0.877s

@ewbankkit ewbankkit requested a review from a team as a code owner May 20, 2022 21:12
@bflad bflad added the enhancement New feature or request label May 20, 2022
@ewbankkit ewbankkit marked this pull request as draft May 20, 2022 21:15
@bflad bflad added this to the v0.1.0 milestone May 20, 2022
@ewbankkit

This comment was marked as outdated.

Copy link
Contributor

@bflad bflad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very exciting! Thank you for starting this. Some meta feedback first. 😄 Please reach out with any questions.

@bflad bflad self-assigned this May 20, 2022
@ewbankkit
Copy link
Contributor Author

@bflad Any thoughts on Diagnostic format / error messages?

@bflad
Copy link
Contributor

bflad commented May 23, 2022

@bflad Any thoughts on Diagnostic format / error messages?

I'd suggest adding the word "Attribute" to the summary and capitalizing Value, e.g. Invalid Attribute Value and capitalizing the first letter of the detail. It might be nice to use the Description() method in the detail, then append the , got: value. Maybe worth creating a small helper function that could be shared across all validations that helps with this. e.g.

package validatordiag

func AttributeValueDiagnostic(path *tftypes.AttributePath, description string, value string) diag.Diagnostic {
	return diag.NewAttributeErrorDiagnostic(
		path,
		"Invalid Attribute Value",
		capitalize(description) + ", got: " + value,
	)
}

// capitalize will uppercase the first letter in a UTF-8 string.
func capitalize(str string) string {
	if str == "" {
		return ""
	}

	firstRune, size := utf8.DecodeRuneInString(str)

	return string(unicode.ToUpper(firstRune)) + str[size:]
}

With callers:

response.Diagnostics.Append(validatordiag.AttributeValueDiagnostic(
	request.AttributePath,
	validator.Description(ctx),
	fmt.Sprintf("%f", f),
))

@ewbankkit ewbankkit marked this pull request as ready for review May 23, 2022 15:01
@ewbankkit
Copy link
Contributor Author

All review comments addressed.

Copy link
Contributor

@bflad bflad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bflad Are we OK with these Float validators working for both Float64 and Number?

At first I was thinking it might be best if it was strictly just types.Float64, however we could validate Number as long as it can be represented with big.Exact accuracy from (*big.Float).Float64(). That said, I think it is okay if we simplify it only work with Float64 for now, since Number can have up to 512 bits and should likely require specialized validation via the math/big package anyways.

@ewbankkit
Copy link
Contributor Author

Now disallowing types.Number.

Copy link
Contributor

@bflad bflad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome!!! This is looking fantastic. Do you mind making a go-changelog entry? e.g. .changelog/18.txt

```release-note:feature
Introduced `float64validator` package with `AtLeast()`, `AtMost()`, and `Between()` validation functions
```

@bflad bflad merged commit 44393e4 into hashicorp:main May 24, 2022
@ewbankkit ewbankkit deleted the f-add-float64-validators branch May 24, 2022 15:35
@bflad bflad added the type/float64 types.Float64 validators label May 25, 2022
@github-actions
Copy link

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active contributions.
If you have found a problem that seems related to this change, 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 Jun 25, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request type/float64 types.Float64 validators
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Float64 Amount Validation (AtLeast / AtMost / Between)
2 participants