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

Replace Optional with Spring's ObjectProvider to manage the optionali… #5573

Merged
merged 1 commit into from Apr 11, 2024

Conversation

kratosmy
Copy link
Contributor

@kratosmy kratosmy commented Apr 6, 2024

Motivation:

Optional is not as compatible as ObjectProvider, which is included in Spring Framework since Spring 4.3, so use ObjectProvider would improve the programmatic resolution of Beans.

Modifications:

Two file are affected, AbstractArmeriaAutoConfiguration.java and ArmeriaReactiveWebServerFactoryAutoConfiguration.java

Result:

Closes #5527 .

@kratosmy kratosmy force-pushed the main branch 2 times, most recently from c163eda to 5814bea Compare April 6, 2024 14:00
@kratosmy kratosmy marked this pull request as ready for review April 7, 2024 01:37
Copy link
Contributor

@jrhee17 jrhee17 left a comment

Choose a reason for hiding this comment

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

Nice 👍 Left a super-nit optional comment. Thanks for the quick fix @kratosmy 🙇 👍 🙇

Comment on lines +80 to +81
if (!armeriaServerConfigurators.stream().findAny().isPresent() &&
!armeriaServerBuilderConsumers.stream().findAny().isPresent()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Optional) Not strong on this, but intelliJ is complaining and generally inverse conditions are slightly harder to reason about (although I think this case is simple enough)

Suggested change
if (!armeriaServerConfigurators.stream().findAny().isPresent() &&
!armeriaServerBuilderConsumers.stream().findAny().isPresent()) {
if (armeriaServerConfigurators.stream().findAny().isEmpty() &&
armeriaServerBuilderConsumers.stream().findAny().isEmpty()) {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for your suggestion and yes I have used isEmpty() at first, but local test failed due to this, not sure the exact reason because error message is garbled characters.

Copy link
Contributor

Choose a reason for hiding this comment

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

Optional.isEmpty() is introduced in Java 11 but our baseline of Java is 8.

@@ -69,16 +69,16 @@ public Server armeriaServer(
ArmeriaSettings armeriaSettings,
InternalServices internalService,
Optional<MeterRegistry> meterRegistry,
Optional<List<MetricCollectingServiceConfigurator>> metricCollectingServiceConfigurators,
ObjectProvider<MetricCollectingServiceConfigurator> metricCollectingServiceConfigurators,
Copy link
Contributor

Choose a reason for hiding this comment

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

Note: This API has been around since 4.3, so there's probably next to no user impact. The change is now that beans are correctly ordered by spring's priority system

@jrhee17 jrhee17 added this to the 1.29.0 milestone Apr 8, 2024
Copy link
Contributor

@ikhoon ikhoon left a comment

Choose a reason for hiding this comment

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

Looks great! 👍🙇‍♂️

Copy link
Member

@trustin trustin left a comment

Choose a reason for hiding this comment

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

Thanks for fixing this, @kratosmy 🙇

@@ -69,16 +69,16 @@ public Server armeriaServer(
ArmeriaSettings armeriaSettings,
InternalServices internalService,
Optional<MeterRegistry> meterRegistry,
Copy link
Member

Choose a reason for hiding this comment

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

Question: Can we also use ObjectProvider for this type?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, we can. But it may require something like meterIdPrefixFunction.getIfAvailable() ? meterIdPrefixFunction.getIfAvailable() : MeterIdPrefixFunction.ofDefault("armeria.server"), seems not so concise.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, let's keep it as is. Thanks!

Copy link
Member

@minwoox minwoox left a comment

Choose a reason for hiding this comment

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

Thanks a lot, @kratosmy! 👍 👍 👍

@jrhee17 jrhee17 modified the milestones: 1.29.0, 1.28.0 Apr 11, 2024
@jrhee17 jrhee17 merged commit 8d4ddea into line:main Apr 11, 2024
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Replace Optional<List<T>> parameter to ObjectProvider<T>
5 participants