From 7d8ef7c1976a6e2cde39e886abf530e6610e9d66 Mon Sep 17 00:00:00 2001 From: Harald Aamot Date: Fri, 8 Nov 2024 10:07:51 +0100 Subject: [PATCH 1/3] working draft --- .../openrewrite/yaml/search/FindProperty.java | 18 ++++++- .../yaml/search/FindPropertyTest.java | 54 +++++++++++++++++-- 2 files changed, 67 insertions(+), 5 deletions(-) diff --git a/rewrite-yaml/src/main/java/org/openrewrite/yaml/search/FindProperty.java b/rewrite-yaml/src/main/java/org/openrewrite/yaml/search/FindProperty.java index af06020f761..72ca99380a3 100644 --- a/rewrite-yaml/src/main/java/org/openrewrite/yaml/search/FindProperty.java +++ b/rewrite-yaml/src/main/java/org/openrewrite/yaml/search/FindProperty.java @@ -28,6 +28,7 @@ import java.util.HashSet; import java.util.Iterator; +import java.util.Objects; import java.util.Set; @Value @@ -46,6 +47,12 @@ public class FindProperty extends Recipe { @Nullable Boolean relaxedBinding; + @Option(displayName = "Property value", + 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"; @@ -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; } diff --git a/rewrite-yaml/src/test/java/org/openrewrite/yaml/search/FindPropertyTest.java b/rewrite-yaml/src/test/java/org/openrewrite/yaml/search/FindPropertyTest.java index 83bc5c7afa0..335a3a62602 100644 --- a/rewrite-yaml/src/test/java/org/openrewrite/yaml/search/FindPropertyTest.java +++ b/rewrite-yaml/src/test/java/org/openrewrite/yaml/search/FindPropertyTest.java @@ -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; @@ -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" @@ -41,7 +42,7 @@ 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" @@ -49,6 +50,51 @@ void findGlobProperty() { ); } + @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", @@ -58,7 +104,7 @@ 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 @@ -78,7 +124,7 @@ 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 From fda3a1474b0c6eab3273f71bb9e0e23fd5b76d1d Mon Sep 17 00:00:00 2001 From: Harald Aamot Date: Fri, 8 Nov 2024 10:18:16 +0100 Subject: [PATCH 2/3] IntelliJ formatting --- .../java/org/openrewrite/yaml/search/FindPropertyTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rewrite-yaml/src/test/java/org/openrewrite/yaml/search/FindPropertyTest.java b/rewrite-yaml/src/test/java/org/openrewrite/yaml/search/FindPropertyTest.java index 335a3a62602..8a89adf1ce1 100644 --- a/rewrite-yaml/src/test/java/org/openrewrite/yaml/search/FindPropertyTest.java +++ b/rewrite-yaml/src/test/java/org/openrewrite/yaml/search/FindPropertyTest.java @@ -106,7 +106,7 @@ void relaxedBinding(String propertyKey) { rewriteRun( 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 @@ -126,7 +126,7 @@ void exactMatch() { rewriteRun( 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 From 36d3b605272b01cb53a41b53adff98615bd53c7a Mon Sep 17 00:00:00 2001 From: Sam Snyder Date: Mon, 11 Nov 2024 18:38:26 -0800 Subject: [PATCH 3/3] 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> --- .../src/main/java/org/openrewrite/yaml/search/FindProperty.java | 1 + 1 file changed, 1 insertion(+) diff --git a/rewrite-yaml/src/main/java/org/openrewrite/yaml/search/FindProperty.java b/rewrite-yaml/src/main/java/org/openrewrite/yaml/search/FindProperty.java index 72ca99380a3..9af08fcaeaf 100644 --- a/rewrite-yaml/src/main/java/org/openrewrite/yaml/search/FindProperty.java +++ b/rewrite-yaml/src/main/java/org/openrewrite/yaml/search/FindProperty.java @@ -48,6 +48,7 @@ public class FindProperty extends Recipe { 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