Skip to content

Better arn parsing #6163

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Better arn parsing #6163

wants to merge 3 commits into from

Conversation

RanVaknin
Copy link
Contributor

Motivation and Context

Addresses #6108 .

Currently the SDK uses exceptions as control flow for arn parsing. This is an anti pattern in Java and can cause performance overhead.

Modifications

This PR adds another arn parsing method tryFromString(arn) that would return an Optional of Arn when the arn is valid, and an empty when it is invalid - this avoids the hot path of throwing exceptions as control flow.

Testing

Unit tests:

  • Valid ARNs
  • Invalid ARNs
  • Exception throwing ARNs (valid ARNs missing a partition or service)

@RanVaknin RanVaknin requested a review from a team as a code owner June 6, 2025 18:14
Copy link

sonarqubecloud bot commented Jun 6, 2025

Quality Gate Failed Quality Gate failed

Failed conditions
27.9% Coverage on New Code (required ≥ 80%)

See analysis details on SonarQube Cloud

Copy link
Contributor

@davidh44 davidh44 left a comment

Choose a reason for hiding this comment

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

Looks good, just need a a changelog

Copy link
Contributor

@zoewangg zoewangg left a comment

Choose a reason for hiding this comment

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

Don't we need to update account ID code path to use this new method?

* @param arn A string containing an ARN to parse.
* @return An {@link Optional} containing the parsed {@link Arn} if valid, or empty if invalid.
*/
public static Optional<Arn> tryFromString(String arn) {
Copy link
Contributor

Choose a reason for hiding this comment

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

It contains a lot of duplicate code. Can we extract common code?

One idea is to create a helper method that takes a boolean for throwing behavior private static Optional<Arn> parseArn(String arn, boolean throwOnError) {...}

Comment on lines +388 to +392
public void tryFromString_InvalidArns_ShouldThrowExceptions(String testName, String arnString) {
assertThrows(IllegalArgumentException.class, () -> {
Arn.tryFromString(arnString);
});
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm, I thought it would no longer throw exception, did I miss anything?

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.

3 participants