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

Add getStructured default method, add empty StructuredConfigProperties #6759

Merged
merged 1 commit into from
Oct 2, 2024

Conversation

jack-berg
Copy link
Member

Add StructuredConfigProperties.getStructured(String, StructuredConfigProperties) method to assist with walking trees without null checking. Originated from this conversation.

Copy link

codecov bot commented Oct 2, 2024

Codecov Report

Attention: Patch coverage is 92.85714% with 1 line in your changes missing coverage. Please review.

Project coverage is 90.02%. Comparing base (361f035) to head (6d4891a).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
.../spi/internal/EmptyStructuredConfigProperties.java 90.90% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff            @@
##               main    #6759   +/-   ##
=========================================
  Coverage     90.02%   90.02%           
- Complexity     6475     6488   +13     
=========================================
  Files           718      719    +1     
  Lines         19573    19587   +14     
  Branches       1931     1931           
=========================================
+ Hits          17621    17634   +13     
- Misses         1353     1354    +1     
  Partials        599      599           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Comment on lines +204 to +215
/**
* Returns a list of {@link StructuredConfigProperties} configuration property.
*
* @return a list of map-valued configuration property, or {@code defaultValue} if {@code name}
* has not been configured
* @throws ConfigurationException if the property is not a sequence of mappings
*/
default List<StructuredConfigProperties> getStructuredList(
String name, List<StructuredConfigProperties> defaultValue) {
return defaultIfNull(getStructuredList(name), defaultValue);
}

Copy link
Member

Choose a reason for hiding this comment

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

can you give an example of how you see this method being used?

Copy link
Member Author

Choose a reason for hiding this comment

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

There's a demonstration here in the unit test:

    // Validate common pattern of walking down tree path which is not defined
    // Access string at .foo.bar.baz without null checking and without exception.
    assertThat(
            structuredConfigProps
                .getStructured("foo", empty())
                .getStructured("bar", empty())
                .getString("baz"))
        .isNull();

I give the same example in the javadoc for the new empty() method, but should probably copy it here as well.

Copy link
Member

Choose a reason for hiding this comment

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

Sorry, I meant specifically the list method, it's not clear to me if that variant is needed

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah sorry for misunderstanding.

At that point, it was just for completeness. Every other accessor has a "get or default" variant, and so it feels out of place for this one omission. Its true that it doesn't have utility for tree walking, but that doesn't mean it doesn't have utility at all. For example, this code for interpreting OTLP headers could be simplified to:

config.getStructuredList("headers", List.of()).forEach(header -> {
    String name = header.getString("name");
    String value = header.getString("value");
    if (name != null && value != null) {
      addHeader.accept(name, value);
    }
});

Copy link
Member

Choose a reason for hiding this comment

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

Every other accessor has a "get or default" variant

good point, I hadn't thought about the existing methods already having the default variant

@jack-berg jack-berg merged commit a6a9acb into open-telemetry:main Oct 2, 2024
18 checks passed
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