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

feat: add validation for immutable fields #180

Merged
merged 3 commits into from
Oct 14, 2022

Conversation

DavyJ0nes
Copy link

@DavyJ0nes DavyJ0nes commented Oct 13, 2022

Within the Field Behavior AIP there is guidance on how to handle the IMMUTABLE field behavior, suggesting that:

When a service receives an immutable field in an update request (or similar), even if included in the update mask, the service should ignore the field if the value matches, but should error with INVALID_ARGUMENT if a change is requested.

This PR introduces a new validation function fieldbehavior.ValidateImmutableFieldsWithMask to help service owners implement this suggestion and can return an INVALID_ARGUEMNT if it makes sense for their service.

It is heavily influenced by fieldbehavior.ValidateImmutableFieldsWithMask and copies some of that code but I felt it was more readable to copy and paste rather than having a generic implementation for both uses.

As the recommendations are should and not must I didn't feel like this library should be too opinionated and error on the fieldmask.Update function.

I also updated some of the comments and added a simple test for fieldbehavior.ClearFields().

@DavyJ0nes DavyJ0nes self-assigned this Oct 13, 2022
@@ -27,6 +41,12 @@ func TestCopyFields(t *testing.T) {
})
}

func TestValidateRequiredFields(t *testing.T) {
Copy link
Author

@DavyJ0nes DavyJ0nes Oct 13, 2022

Choose a reason for hiding this comment

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

moved this down from higher in file to have test cases in alpha order 😅

@DavyJ0nes DavyJ0nes force-pushed the feat-add-validation-for-required-fields branch from cfe8a76 to a2f4b39 Compare October 13, 2022 16:38
@DavyJ0nes DavyJ0nes marked this pull request as ready for review October 13, 2022 16:57
CreateTime: timestamppb.Now(), // has OUTPUT_ONLY field_behavior; should be cleared.
DisplayName: "site one", // has REQUIRED field_behavior; should not be cleared.
}

Copy link
Member

@odsod odsod Oct 13, 2022

Choose a reason for hiding this comment

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

IMO one t.Run should be readable as one paragraph, i.e. blank lines between t.Runs but not within

return Has(field, annotations.FieldBehavior_IMMUTABLE)
}

func isSetOnWire(m protoreflect.Message, field protoreflect.FieldDescriptor) bool {
Copy link
Member

Choose a reason for hiding this comment

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

is this just an alias for protoreflect.Message.Has? IMO better to stick to the original API even if the naming is not perfect

Copy link
Author

Choose a reason for hiding this comment

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

Yeah, I added it make the conditional easier to skim read but am ok to revert.

Copy link
Member

@odsod odsod left a comment

Choose a reason for hiding this comment

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

LGTM - apart from stylistic comments about preferring blank lines between functions but not within

@DavyJ0nes DavyJ0nes force-pushed the feat-add-validation-for-required-fields branch from a2f4b39 to 1ebc6cb Compare October 13, 2022 18:29
@DavyJ0nes DavyJ0nes merged commit 7ca7dd9 into master Oct 14, 2022
@DavyJ0nes DavyJ0nes deleted the feat-add-validation-for-required-fields branch October 14, 2022 07:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants