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 support for creating regex and subnet-based ReadFrom instances from a single string #3016

Merged
merged 1 commit into from
Oct 18, 2024

Conversation

nosan
Copy link
Contributor

@nosan nosan commented Oct 15, 2024

Closes #3013

Before this commit, it was not possible to use ReadFrom.valueOf for subnet and regex types. This commit introduces support for these types, as well as the use of names in underscore format

Copy link
Collaborator

@tishun tishun left a comment

Choose a reason for hiding this comment

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

Awesome, thanks for the contribution!

And thanks for the tests. I just have a few of notes.

src/main/java/io/lettuce/core/ReadFrom.java Show resolved Hide resolved
return subnet(value.split(","));
}
if (type.equalsIgnoreCase("regex")) {
return regex(Pattern.compile(value));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Possibly throws PatternSyntaxException

If we want to avoid expanding the contract (this is a runtime exception, but assuming existing applications could be catching the IllegalArgumentsException it might constitute a breach of contract to them) we should perhaps wrap the PatternSyntaxException inside a IllegalArgumentsException? What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

that makes sense! My bad. fixed

src/main/java/io/lettuce/core/ReadFrom.java Outdated Show resolved Hide resolved
@nosan
Copy link
Contributor Author

nosan commented Oct 15, 2024

Thank you, @tishun

PR has been updated

@nosan nosan requested a review from tishun October 15, 2024 15:10
@nosan nosan force-pushed the gh-3013 branch 2 times, most recently from 9a4c11b to d41760c Compare October 15, 2024 15:31
@nosan
Copy link
Contributor Author

nosan commented Oct 15, 2024

I didn't add support for the kebab-case format, but it might be worth considering. For instance, order-sensitive-regex is more readable than orderSensitiveRegex. However, as an alternative, users can opt for ORDER_SENSITIVE_REGEX

@nosan
Copy link
Contributor Author

nosan commented Oct 15, 2024

I'm not sure that ReadFrom.valueOf(...) needs to support order-sensitive-regex. For me, it seems like an unnecessary overhead. For more complex configurations like this, Spring Boot already provides LettuceClientConfigurationBuilderCustomizer.

What do you think, @wilkinsona?

@wilkinsona
Copy link

I'd defer to @tishun and @mp911de on that one. If use of an order-sensitive regex is at least as common as a "normal" regex, I think it would be good for valueOf to support them both.

@nosan nosan force-pushed the gh-3013 branch 3 times, most recently from 8d3d3d0 to 17f9b1f Compare October 15, 2024 19:03
@mp911de
Copy link
Collaborator

mp911de commented Oct 16, 2024

I didn't add support for the kebab-case format, but it might be worth considering. For instance, order-sensitive-regex is more readable than orderSensitiveRegex. However, as an alternative, users can opt for ORDER_SENSITIVE_REGEX

In doubt, I suggest defaulting to non-order-sensitive to simplify the problem space. Once a future requirement comes along that asks for order-sensitive regex (the same could be for order-sensitive subnet) we can still sort that issue out later.

For now, I suggest including the changes that fit in nicely and where we know that we like their design.

@tishun
Copy link
Collaborator

tishun commented Oct 16, 2024

Would you consider adding support for the following formats as well? ANY_REPLICA, any-replica?

Actually IMHO we should go with the kebab-case (only, without snake_case) instead.

  1. The Spring Boot application property names are already following this case and it would be mixing of styles if we use a third case (we are already using camelCase so we can't fully avoid mixing styles)
  2. It seems that other projects have adopted the same case for the values, at least based on a very quick search (see for example this result)

@tishun
Copy link
Collaborator

tishun commented Oct 16, 2024

In doubt, I suggest defaulting to non-order-sensitive to simplify the problem space. Once a future requirement comes along that asks for order-sensitive regex (the same could be for order-sensitive subnet) we can still sort that issue out later.

For now, I suggest including the changes that fit in nicely and where we know that we like their design.

@mp911de you were discussing if we should support order-sensitive pattern and not the case here, right?
If so - I also agree we should start with the non-order-sensitive and we can extend the contract if we decide we need to.

@nosan
Copy link
Contributor Author

nosan commented Oct 16, 2024

Thanks, @mp911de and @tishun

  1. I replaced the snake_case format with the kebab-case format.
  2. I removed support for order-sensitive-regex

@nosan
Copy link
Contributor Author

nosan commented Oct 16, 2024

@tishun
Still, it’s worth considering support for snake_case as well or instead of kebab-case, as it is more familiar and commonly used for constant names (e.g., in enums).

Even the initial request in Spring Boot expected the snake_case format: spring-projects/spring-boot#42576 (comment)

@tishun
Copy link
Collaborator

tishun commented Oct 16, 2024

@tishun Still, it’s worth considering support for snake_case as well or instead of kebab-case, as it is more familiar and commonly used for constant names (e.g., in enums).

Even the initial request in Spring Boot expected the snake_case format: spring-projects/spring-boot#42576 (comment)

Oh ... I see. Haven't considered that.
I've asked the original author of the issue about their use case.
Not entirely sure they are describing a case where they convert between enums.

But the enum conversion still makes sense to be honest - if another enum has these values (for some reason) we would simply convert based on the name (and not have to have another string representation in the alternative enum).

Perhaps we need to support all three then.

I will agree to both solutions (CamelCase + kebab-case and CamelCase + kebab-case + snake_case).
If we go with the first one we can extend the contract later if needed, but it is a relatively easy change to support from the start.

@nosan
Copy link
Contributor Author

nosan commented Oct 16, 2024

Added support for all three formats.

@nosan
Copy link
Contributor Author

nosan commented Oct 16, 2024

@tishun

I read this comment spring-projects/spring-boot#42576 (comment)

And actually, it makes sense. Initially, the scope of this PR was to support regex and subnet types.

Adding additional formats like snake_case and kebab-case should not be included here at all.

In Spring Boot it will be possible to use the following format:

spring.data.redis.lettuce.read-from=upstreamPreferred
spring.data.redis.lettuce.read-from=subnet:...

So, I propose to revert everything that is related to format changes and just add support for regex and subnet.

Supporting order-sensitive regex has diverted me from my original focus. My apologies.

UPDATE: I reverted the changes related to the formats. They can always be added later if necessary.

…om a single string redis#3013

Before this commit, it was not possible to use ReadFrom.valueOf(name) for subnet and regex types.
This commit introduces a new syntax subnet:192.168.0.0/16,2001:db8:abcd:0000::/52 and regex:.*region-1.* respectively
@tishun
Copy link
Collaborator

tishun commented Oct 17, 2024

@tishun

I read this comment spring-projects/spring-boot#42576 (comment)

And actually, it makes sense. Initially, the scope of this PR was to support regex and subnet types.

Adding additional formats like snake_case and kebab-case should not be included here at all.

In Spring Boot it will be possible to use the following format:

spring.data.redis.lettuce.read-from=upstreamPreferred spring.data.redis.lettuce.read-from=subnet:...

So, I propose to revert everything that is related to format changes and just add support for regex and subnet.

Supporting order-sensitive regex has diverted me from my original focus. My apologies.

UPDATE: I reverted the changes related to the formats. They can always be added later if necessary.

I agree. Thanks for working on this!

@tishun tishun added this to the 6.5.0.RELEASE milestone Oct 18, 2024
@tishun tishun added the type: feature A new feature label Oct 18, 2024
@tishun tishun merged commit 8fc5a3f into redis:main Oct 18, 2024
5 checks passed
@nosan
Copy link
Contributor Author

nosan commented Oct 18, 2024

Thank you very much, @tishun

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: feature A new feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for creating regex- and subnet-based ReadFrom instances from a single string
4 participants