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

Feature - YAML search - find properties with a specific scalar value #4657

Merged
merged 3 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import java.util.HashSet;
import java.util.Iterator;
import java.util.Objects;
import java.util.Set;

@Value
Expand All @@ -46,6 +47,12 @@ public class FindProperty extends Recipe {
@Nullable
Boolean relaxedBinding;

@Option(displayName = "Property value",
sambsnyd marked this conversation as resolved.
Show resolved Hide resolved
description = "If provided, only properties specified in propertyKey having this value will be found. Works only for scalar values",
required = false)
@Nullable
String propertyValue;

@Override
public String getDisplayName() {
return "Find YAML properties";
Expand All @@ -66,7 +73,16 @@ public Yaml.Mapping.Entry visitMappingEntry(Yaml.Mapping.Entry entry, ExecutionC
if (!Boolean.FALSE.equals(relaxedBinding) ?
NameCaseConvention.matchesGlobRelaxedBinding(prop, propertyKey) :
StringUtils.matchesGlob(prop, propertyKey)) {
e = e.withValue(SearchResult.found(e.getValue()));
if (!Objects.isNull(propertyValue)) {
if (entry.getValue() instanceof Yaml.Scalar) {
Yaml.Scalar scalar = (Yaml.Scalar) entry.getValue();
if (scalar.getValue().equals(propertyValue)) {
e = e.withValue(SearchResult.found(e.getValue()));
}
}
} else {
e = e.withValue(SearchResult.found(e.getValue()));
}
}
return e;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.openrewrite.yaml.search;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
Expand All @@ -30,7 +31,7 @@ class FindPropertyTest implements RewriteTest {
@Test
void findProperty() {
rewriteRun(
spec -> spec.recipe(new FindProperty("management.metrics.binders.files.enabled", null)),
spec -> spec.recipe(new FindProperty("management.metrics.binders.files.enabled", null, null)),
yaml(
"management.metrics.binders.files.enabled: true",
"management.metrics.binders.files.enabled: ~~>true"
Expand All @@ -41,14 +42,59 @@ void findProperty() {
@Test
void findGlobProperty() {
rewriteRun(
spec -> spec.recipe(new FindProperty("management.metrics.binders.*.enabled", null)),
spec -> spec.recipe(new FindProperty("management.metrics.binders.*.enabled", null, null)),
yaml(
"management.metrics.binders.files.enabled: true",
"management.metrics.binders.files.enabled: ~~>true"
)
);
}

@Test
void findPropertyWithSpecificValueMatch() {
rewriteRun(
spec -> spec.recipe(new FindProperty("my.cool.property", null, "my-matching-value")),
yaml(
"my.cool.property: my-matching-value",
"my.cool.property: ~~>my-matching-value"
)
);
}

@Test
void findPropertyWithSpecificValueMatchSingleQuotes() {
rewriteRun(
spec -> spec.recipe(new FindProperty("my.cool.property", null, "my-matching-value")),
yaml(
"my.cool.property: 'my-matching-value'",
"my.cool.property: ~~>'my-matching-value'"
)
);
}

@Test
void findPropertyWithSpecificValueMatchDoubleQuotes() {
rewriteRun(
spec -> spec.recipe(new FindProperty("my.cool.property", null, "my-matching-value")),
yaml(
"my.cool.property: \"my-matching-value\"",
"my.cool.property: ~~>\"my-matching-value\""
)
);
}

@Test
@Disabled("how do I test that the search has no hits?")
void findPropertyWithSpecificValueNoMatch() {
rewriteRun(
spec -> spec.recipe(new FindProperty("my.cool.property", null, "my-non-matching-value")),
yaml(
"my.cool.property: my-matching-value",
"my.cool.property: my-matching-value"
)
);
}

@ParameterizedTest
@ValueSource(strings = {
"acme.my-project.person.first-name",
Expand All @@ -58,9 +104,9 @@ void findGlobProperty() {
@Issue("https://github.com/openrewrite/rewrite/issues/1168")
void relaxedBinding(String propertyKey) {
rewriteRun(
spec -> spec.recipe(new FindProperty(propertyKey, true)),
spec -> spec.recipe(new FindProperty(propertyKey, true, null)),
yaml(
"""
"""
acme.my-project.person.first-name: example
acme.myProject.person.firstName: example
acme.my_project.person.first_name: example
Expand All @@ -78,9 +124,9 @@ void relaxedBinding(String propertyKey) {
@Issue("https://github.com/openrewrite/rewrite/issues/1168")
void exactMatch() {
rewriteRun(
spec -> spec.recipe(new FindProperty("acme.my-project.person.first-name", false)),
spec -> spec.recipe(new FindProperty("acme.my-project.person.first-name", false, null)),
yaml(
"""
"""
acme.my-project.person.first-name: example
acme.myProject.person.firstName: example
acme.my_project.person.first_name: example
Expand Down