Skip to content

Adding AtLeastSumOf, AtMostSumOf, EqualToSumOf Validators #29

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 11 commits into from
Jul 20, 2022

Conversation

bendbennett
Copy link
Contributor

@bendbennett bendbennett commented May 27, 2022

Closes: #20

Couple of things:

  • If the attributes that are being summed are null this validator silently ignores that fact.
  • If all of the attributes that are being summed are unknown, no errors are emitted as it's not possible to perform the validation.
  • If this looks like it might be useful, let me know and I'll add atMostSumOf and any other permutations that you think are appropriate into the same PR. Have added atLeastSumOf and equalToSumOf.

@bendbennett bendbennett requested a review from a team as a code owner May 27, 2022 15:59
@bendbennett bendbennett force-pushed the bendbennett/issues-20 branch from a11d52a to a56e62f Compare May 27, 2022 16:01
Comment on lines +39 to +43
requestConfigRaw: map[string]tftypes.Value{
"one": tftypes.NewValue(tftypes.Number, 15),
"two": tftypes.NewValue(tftypes.Number, 15),
},
Copy link
Contributor

Choose a reason for hiding this comment

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

Drive-by note: It'd be great to ensure there are unit tests covering when the one/two attributes are null and/or unknown as well. Or all three are null/unknown.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Have added some additional tests.

Copy link
Contributor

Choose a reason for hiding this comment

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

These look great! Another good test case might be verifying that giving a path to an unexpected type (e.g. boolean attribute) always returns an error diagnostic. It appears the code should already be doing this, but it might be good to cover this for future code changes. Seems like it should be popping an additional attribute into the test schema, then referencing it in a new test case. 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Have added a test case for attributePath(s) referencing unexpected types in at_least|at_most|equal_to_sum_of_test.

Copy link
Contributor

@detro detro left a comment

Choose a reason for hiding this comment

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

I think this is looking already good. I'd just include more testing for unknowns/nulls scenarios (what @bflad said) and I'd offer these validators in a set:

  • atLeastSumOf (i.e. Attr >= Sum)
  • atMostSumOf (i.e. Attr <= Sum)
  • sumOf (i.e. Attr = Sum)

@bendbennett bendbennett requested review from detro and bflad May 30, 2022 13:02
@bendbennett bendbennett force-pushed the bendbennett/issues-20 branch from 5937dfd to 49cdefb Compare May 30, 2022 13:27
Copy link
Contributor

@detro detro left a comment

Choose a reason for hiding this comment

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

👩‍🍳 💋

@detro detro modified the milestones: v0.2.0, v0.3.0 May 31, 2022
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.

Getting pumped for this. Another thing we may want to consider, similar to #32, is whether we should wait for hashicorp/terraform-plugin-framework#81 to land and release upstream, as it will necessitate a breaking change for anything accepting attribute paths (or hopefully attribute path "expressions" in this use case).

return
}

sumOfAttribs += attribToSum.Value
Copy link
Contributor

@bflad bflad Jun 7, 2022

Choose a reason for hiding this comment

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

For code reader clarity, I'm not sure if its worth explicitly putting a conditional such as the below or a quick code comment why Value is safe to always add (because its zero-value is 0)

if attribToSum.IsNull() || attribToSum.IsUnknown() {
  continue
}

sumOfAttribs += attribToSum.Value

Copy link
Contributor

@bflad bflad Jun 7, 2022

Choose a reason for hiding this comment

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

Per #29 (comment), it may be better to implement something like:

if attribToSum.IsNull() {
  continue
}

if attribToSum.IsUnknown() {
  return
}

sumOfAttribs += attribToSum.Value

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Have added continue statements for Null and Unknown to at_least|at_most|equal_to_sum.

// - Is exclusively at least the sum of the given attributes.
//
// Null (unconfigured) and unknown (known after apply) values are skipped.
func AtLeastSumOf(attributesToSum []*tftypes.AttributePath) tfsdk.AttributeValidator {
Copy link
Contributor

Choose a reason for hiding this comment

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

Something we should probably decide on as a group is a common parameter style for "multiple attribute"/"combination"/"schema" validators -- I think I lean towards preferring a variadic style so consumers can omit the wrapping slice type syntax (e.g. []*tftypes.AttributePath{}) and instead just list out the actual slice values, e.g.

// []*tftypes.AttributePath parameter
int64validator.AtLeastSumOf([]*tftypes.AttributePath{
  tftypes.NewAttributePath().WithAttributeName("one"),
  tftypes.NewAttributePath().WithAttributeName("two"),
})

// ...*tftypes.AttributePath parameter
int64validator.AtLeastSumOf(
  tftypes.NewAttributePath().WithAttributeName("one"),
  tftypes.NewAttributePath().WithAttributeName("two"),
)

If for some reason they already have a slice, then it can be expanded, e.g.

var mySpecialAttributes []*tftypes.AttributePath = []*tftypes.AttributePath{/*...*/}

int64validator.AtLeastSumOf(mySpecialAttributes...)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed. Have amended accordingly.

Comment on lines +39 to +43
requestConfigRaw: map[string]tftypes.Value{
"one": tftypes.NewValue(tftypes.Number, 15),
"two": tftypes.NewValue(tftypes.Number, 15),
},
Copy link
Contributor

Choose a reason for hiding this comment

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

These look great! Another good test case might be verifying that giving a path to an unexpected type (e.g. boolean attribute) always returns an error diagnostic. It appears the code should already be doing this, but it might be good to cover this for future code changes. Seems like it should be popping an additional attribute into the test schema, then referencing it in a new test case. 👍

// attribute value:
//
// - Is a number, which can be represented by a 64-bit integer.
// - Is exclusively equal to the sum of the given attributes.
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: Does the word "exclusively" apply here? 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

@bendbennett bendbennett requested review from bflad and detro June 9, 2022 14:57
@bflad bflad added the enhancement New feature or request label Jun 22, 2022
@bflad
Copy link
Contributor

bflad commented Jun 22, 2022

FYI, moving this to v0.4.0 so we can release v0.3.0 with additional validators not dependent on hashicorp/terraform-plugin-framework#81, upgrade the framework dependency when its released, then focus immediately on these.

@bflad bflad modified the milestones: v0.3.0, v0.4.0 Jun 22, 2022
@bendbennett bendbennett changed the title Adding atLeastSumOfValidator Adding AtLeastSumOf, AtMostSumOf, EqualToSumOf` Validators Jun 27, 2022
@bendbennett bendbennett changed the title Adding AtLeastSumOf, AtMostSumOf, EqualToSumOf` Validators Adding AtLeastSumOf, AtMostSumOf, EqualToSumOf Validators Jun 27, 2022
@bendbennett bendbennett force-pushed the bendbennett/issues-20 branch from 1ac220c to de23465 Compare July 6, 2022 09:24
Copy link
Contributor

@detro detro left a comment

Choose a reason for hiding this comment

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

I think you need to make use of path.Expressions here, instead of pure Paths. I left some details to why in the comment, but I'll also have an example of how I'm approaching this once I finish reworking the other PR.

@bendbennett bendbennett force-pushed the bendbennett/issues-20 branch from de23465 to f37d1cb Compare July 13, 2022 10:33
@bendbennett bendbennett requested a review from detro July 13, 2022 10:35
Copy link
Contributor

@detro detro left a comment

Choose a reason for hiding this comment

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

The update to use the new .PathMatches looks great, but I think we need to iron out what's the best behaviour for unknowns, when you are essentially trying to sum stuff up, to meet the validator's target.

Good thing we have a zoom in 15min.

@detro detro requested a review from bflad July 19, 2022 10:40
@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 Aug 20, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Int64 Amount Validation (AtLeastSumOf, AtMostSumOf, EqualToSumOf)
3 participants