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

fieldpath: add DeleteField function to delete elements from Paved #344

Merged
merged 3 commits into from
Aug 9, 2022

Conversation

muvaf
Copy link
Member

@muvaf muvaf commented Aug 5, 2022

Description of your changes

Introduces DeleteField to delete a field from the given object.

Fixes #343

I have:

  • Read and followed Crossplane's contribution process.
  • Run make reviewable test to ensure this PR is ready for review.

How has this code been tested

Unit tests with 100% coverage.

Signed-off-by: Muvaffak Onus <me@muvaf.com>
@muvaf muvaf requested review from negz and turkenh August 5, 2022 11:31
@muvaf muvaf changed the title fieldpath: add DeleteField function to delete elements from Paved fieldpath: add DeleteField function to delete elements from Paved Aug 5, 2022
…nables unexpected linters

Signed-off-by: Muvaffak Onus <me@muvaf.com>
@muvaf
Copy link
Member Author

muvaf commented Aug 5, 2022

I've noticed that two cases weren't handled and added them:

  • Top-level field could not be removed since iteration assumed there is at least two segments, i.e. accessing i+1. Now we handle this as special case.
  • In cases where the given path does not exist even before the last node, i.e. metadata.some.another.other where there is no metadata.some, it returned error. Now it's no-op since the target path is already gone.

Added unit tests for both but couldn't reduce cyclomatic complexity without breaking p.delete into pieces that don't make much sense.

@turkenh could you take another look?

…d it should be able to delete a top-level field

Signed-off-by: Muvaffak Onus <me@muvaf.com>
// on that index is removed and the next ones are pulled
// back. If it is a field on a map, the field is
// removed from the map.
func (p *Paved) DeleteField(path string) error {
Copy link
Member Author

Choose a reason for hiding this comment

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

BTW, I could be convinced to rename this to DeleteKey.

Copy link
Member

Choose a reason for hiding this comment

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

Key wouldn't be a good fit if array. What about DeleteItem ?

Copy link
Member

Choose a reason for hiding this comment

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

Would simply Delete work? As in paved.Delete to delete whatever is at the supplied field path?

Copy link
Member Author

Choose a reason for hiding this comment

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

@turkenh I feel like Field is more familiar than Item in this context for the users of fieldpath.

@negz I think it'd read nice if we had Get/Set/Delete but given that there is GetValue/SetValue, paved.Delete feels a bit off without a noun that says what you're deleting.

Thanks for the suggestions folks!

@muvaf muvaf merged commit d9c25ae into crossplane:master Aug 9, 2022
@muvaf muvaf deleted the i-am-gone branch August 9, 2022 07:38
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.

Deletion is missing in fieldpath library
3 participants