Skip to content

Commit

Permalink
Feature - YAML search - find properties with a specific scalar value (o…
Browse files Browse the repository at this point in the history
…penrewrite#4657)

* working draft

* IntelliJ formatting

* Update rewrite-yaml/src/main/java/org/openrewrite/yaml/search/FindProperty.java

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

---------

Co-authored-by: Sam Snyder <[email protected]>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored and MBoegers committed Dec 18, 2024
1 parent cb0b4a7 commit 885dc51
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 7 deletions.
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,13 @@ public class FindProperty extends Recipe {
@Nullable
Boolean relaxedBinding;

@Option(displayName = "Property value",
example = "false",
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 +74,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

0 comments on commit 885dc51

Please sign in to comment.