Skip to content

Secondary resource access with Optional #782

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 2 commits into from
Jan 4, 2022
Merged

Conversation

csviri
Copy link
Collaborator

@csviri csviri commented Jan 3, 2022

This PR should be reviewed first:
#775

@csviri csviri self-assigned this Jan 3, 2022
@@ -6,5 +6,9 @@

Optional<RetryInfo> getRetryInfo();

<T> T getSecondaryResource(Class<T> expectedType, String... qualifier);
default <T> Optional<T> getSecondaryResource(Class<T> expectedType) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Not sure I like this change…

Comment on lines 74 to 80
Optional<Tomcat> tomcatValue = context.getSecondaryResource(Tomcat.class);
if (tomcatValue.isEmpty()) {
throw new IllegalStateException("Cannot find Tomcat " + webapp.getSpec().getTomcat()
+ " for Webapp " + webapp.getMetadata().getName() + " in namespace "
+ webapp.getMetadata().getNamespace());
}

Tomcat tomcat = tomcatValue.get();
Copy link
Collaborator

Choose a reason for hiding this comment

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

This makes the usage a little bit more complex, though I guess it forces users to handle the null case. This is something I often wonder about actually when designing an API: should we use Optional in similar cases or follow a Map-like convention of returning null if the element is not present?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

IMHO, map like convention was there before Optional was present in Java. It make sense and happens in multiple examples that the secondary resources is checked for null. It should be, since it's not necessary present, if not created yet for example. For that case it's better to use Optional I think. Exactly as you say, that it forces users to check, also to eliminate null checks

@metacosm metacosm force-pushed the secondary-resource-optional branch from 601aa5d to cc32576 Compare January 3, 2022 20:33
@csviri csviri merged commit a499e5c into main Jan 4, 2022
@csviri csviri deleted the secondary-resource-optional branch January 4, 2022 07:56
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