-
Notifications
You must be signed in to change notification settings - Fork 682
Add LocalVariableNameFactory. #3271
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
christophstrobl
wants to merge
2
commits into
4.0.x
Choose a base branch
from
issue/3270
base: 4.0.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
src/main/java/org/springframework/data/repository/aot/generate/LocalVariableNameFactory.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Copyright 2025 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.data.repository.aot.generate; | ||
|
||
import java.util.Set; | ||
|
||
import org.springframework.util.LinkedMultiValueMap; | ||
import org.springframework.util.MultiValueMap; | ||
|
||
/** | ||
* {@link VariableNameFactory} implementation keeping track of defined names resolving name clashes using internal | ||
* counter appending {@code _%d} to a suggested name in case of a clash. | ||
* | ||
* @author Christoph Strobl | ||
* @since 4.0 | ||
*/ | ||
class LocalVariableNameFactory implements VariableNameFactory { | ||
|
||
private final MultiValueMap<String, VariableName> variables; | ||
|
||
static LocalVariableNameFactory forMethod(MethodMetadata methodMetadata) { | ||
return of(methodMetadata.getMethodArguments().keySet()); | ||
} | ||
|
||
static LocalVariableNameFactory empty() { | ||
return of(Set.of()); | ||
} | ||
|
||
static LocalVariableNameFactory of(Set<String> variables) { | ||
return new LocalVariableNameFactory(variables); | ||
} | ||
|
||
LocalVariableNameFactory(Iterable<String> predefinedVariableNames) { | ||
|
||
variables = new LinkedMultiValueMap<>(); | ||
for (String parameterName : predefinedVariableNames) { | ||
variables.add(parameterName, new VariableName(parameterName)); | ||
} | ||
} | ||
|
||
@Override | ||
onobc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public VariableName generateName(String suggestedName) { | ||
|
||
if (!variables.containsKey(suggestedName)) { | ||
VariableName variableName = new VariableName(suggestedName); | ||
variables.add(suggestedName, variableName); | ||
return variableName; | ||
} | ||
|
||
String targetName = suggestTargetName(suggestedName); | ||
VariableName variableName = new VariableName(suggestedName, targetName); | ||
variables.add(suggestedName, variableName); | ||
variables.add(targetName, variableName); | ||
return variableName; | ||
} | ||
|
||
String suggestTargetName(String suggested) { | ||
return suggestTargetName(suggested, 1); | ||
} | ||
|
||
String suggestTargetName(String suggested, int counter) { | ||
|
||
String targetName = "%s_%s".formatted(suggested, counter); | ||
if (!variables.containsKey(targetName)) { | ||
return targetName; | ||
} | ||
return suggestTargetName(suggested, counter + 1); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/org/springframework/data/repository/aot/generate/VariableNameFactory.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright 2025 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.data.repository.aot.generate; | ||
|
||
/** | ||
* @author Christoph Strobl | ||
onobc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* @since 4.0 | ||
*/ | ||
public interface VariableNameFactory { | ||
|
||
VariableName generateName(String suggestedName); | ||
|
||
record VariableName(String source, String target) { | ||
onobc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
public VariableName(String source) { | ||
this(source, source); | ||
} | ||
|
||
public String name() { | ||
return target; | ||
} | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
...a/org/springframework/data/repository/aot/generate/LocalVariableNameFactoryUnitTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright 2025 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.data.repository.aot.generate; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.util.Set; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
/** | ||
* @author Christoph Strobl | ||
*/ | ||
class LocalVariableNameFactoryUnitTests { | ||
onobc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
LocalVariableNameFactory variableNameFactory; | ||
|
||
@BeforeEach | ||
void beforeEach() { | ||
variableNameFactory = LocalVariableNameFactory.of(Set.of("firstname", "lastname", "sort")); | ||
} | ||
|
||
@Test // GH-3270 | ||
void resolvesNameClashesInNames() { | ||
|
||
assertThat(variableNameFactory.generateName("name").name()).isEqualTo("name"); | ||
assertThat(variableNameFactory.generateName("name").name()).isEqualTo("name_1"); | ||
assertThat(variableNameFactory.generateName("name").name()).isEqualTo("name_2"); | ||
assertThat(variableNameFactory.generateName("name1").name()).isEqualTo("name1"); | ||
assertThat(variableNameFactory.generateName("name3").name()).isEqualTo("name3"); | ||
assertThat(variableNameFactory.generateName("name3").name()).isEqualTo("name3_1"); | ||
assertThat(variableNameFactory.generateName("name4_1").name()).isEqualTo("name4_1"); | ||
assertThat(variableNameFactory.generateName("name4").name()).isEqualTo("name4"); | ||
assertThat(variableNameFactory.generateName("name4_1_1").name()).isEqualTo("name4_1_1"); | ||
assertThat(variableNameFactory.generateName("name4_1").name()).isEqualTo("name4_1_2"); | ||
assertThat(variableNameFactory.generateName("name4_1").name()).isEqualTo("name4_1_3"); | ||
} | ||
|
||
@Test // GH-3270 | ||
void considersPredefinedNames() { | ||
assertThat(variableNameFactory.generateName("firstname").name()).isEqualTo("firstname_1"); | ||
} | ||
|
||
@Test // GH-3270 | ||
void considersCase() { | ||
assertThat(variableNameFactory.generateName("firstName").name()).isEqualTo("firstName"); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A simplification of this could be:
Wdyt?