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

fix: put should return previous value instead of Optional #2595

Closed
wants to merge 4 commits into from

Conversation

metacosm
Copy link
Collaborator

  • refactor: add unit tests for DefaultManagedDependentResourceContext
  • refactor: add putOrRemove to DefaultManagedDependentResourceContext
  • refactor: deprecate ManagedDependentResourceContext#put
  • fix: implementation of put should return value instead of Optional

robobario and others added 4 commits November 19, 2024 14:37
Signed-off-by: Robert Young <robeyoun@redhat.com>
The javadoc of the existing put method states that it returns Optional.
The implementation also returns Optional, but the method signature
declares it returns T. Meaning the caller has to cast it to Optional
to get at the return value.

This new method will supercede the existing put method

Signed-off-by: Robert Young <robeyoun@redhat.com>
The javadoc declares it returns Optional and the implementation returns
Optional, but the method declares it returns T. put is replaced by
putOrRemove with a consistent signature.

Signed-off-by: Robert Young <robeyoun@redhat.com>
Signed-off-by: Chris Laprun <claprun@redhat.com>
@metacosm metacosm self-assigned this Nov 19, 2024
@metacosm metacosm requested review from csviri and xstefank November 19, 2024 17:19
@openshift-ci openshift-ci bot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Nov 19, 2024
@metacosm
Copy link
Collaborator Author

Extension to #2587 to properly (according to me) fix the implementation to properly return the previous value as the implementation was done instead of returning Optional as the javadoc suggests (the implementation had been changed in a refactoring without the doc being properly updated).

@metacosm metacosm marked this pull request as ready for review November 19, 2024 17:23
@openshift-ci openshift-ci bot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Nov 19, 2024
@metacosm
Copy link
Collaborator Author

metacosm commented Nov 20, 2024

Obsolete, was done as part of #2587

@metacosm metacosm closed this Nov 20, 2024
return (T) previous;
}

// only for testing purposes
Copy link
Collaborator

Choose a reason for hiding this comment

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

Seems this is not only used in testing.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The whole purpose of this method is to be overridden in the test so that we can check that the logging really happens without having to set up a complex logging fixture just for that purpose.

if (value == null) {
return (T) Optional.ofNullable(attributes.remove(key));
return (T) attributes.remove(key);
Copy link
Collaborator

Choose a reason for hiding this comment

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

IMHO, there should be a reset method. This seems unintuitive to me.

Copy link
Collaborator

Choose a reason for hiding this comment

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

agree, or simply a remove.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Was debating that, indeed. Not sure how many people would actually need this so decided against it (people would usually put stuff in the context and get it back instead of removing them so I think this is an edge case).

Copy link
Collaborator

Choose a reason for hiding this comment

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

That is true, that the scope of this is the actual reconciliation. So maybe we don't even need remove at all. But if we do would be nice to have it explicit.

@Test
void putNewValueThrowsExceptionIfTypesDiffer() {
// to check that we properly log things without setting up a complex fixture
final String[] messages = new String[1];
Copy link
Collaborator

Choose a reason for hiding this comment

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

This doesn't need to be an array.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Unless I am mistaken, it does. Simply using a final String doesn't work since there is no way for Java to check that it is always assigned and therefore complains that you're assigning a value to a final variable in the overridden method. Not using a final String won't work either because this is effectively a capture so the variable needs to be final so the only way that I know of to do this is to use a final array and change the element.

@metacosm metacosm deleted the fork/robobario/fix-that-sig branch November 21, 2024 08:41
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.

4 participants