|
| 1 | + /* |
| 2 | + * Copyright 2024 the original author or authors. |
| 3 | + * <p> |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * <p> |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * <p> |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + package com.anacoders.cookbook.yaml; |
| 17 | + |
| 18 | + import lombok.EqualsAndHashCode; |
| 19 | + import lombok.Value; |
| 20 | + import org.jspecify.annotations.NullMarked; |
| 21 | + import org.jspecify.annotations.Nullable; |
| 22 | + import org.openrewrite.*; |
| 23 | + import org.openrewrite.yaml.JsonPathMatcher; |
| 24 | + import org.openrewrite.yaml.YamlIsoVisitor; |
| 25 | + import org.openrewrite.yaml.tree.Yaml; |
| 26 | + |
| 27 | + import java.util.concurrent.atomic.AtomicBoolean; |
| 28 | + |
| 29 | + /** |
| 30 | + * Updates a YAML property value only when another property in the same document matches a |
| 31 | + condition. |
| 32 | + * Handles multi-document YAML files correctly, evaluating each document independently. |
| 33 | + */ |
| 34 | + @NullMarked |
| 35 | + @Value |
| 36 | + @EqualsAndHashCode(callSuper = false) |
| 37 | + public class ChangeYamlPropertyConditionally extends Recipe { |
| 38 | + |
| 39 | + @Option(displayName = "Condition JsonPath", |
| 40 | + description = "JsonPath to the property that must match for the update to occur.", |
| 41 | + example = "$.metadata.labels.environment") |
| 42 | + String conditionJsonPath; |
| 43 | + |
| 44 | + @Option(displayName = "Condition value", |
| 45 | + description = "The value that conditionJsonPath must equal for the update to occur.", |
| 46 | + example = "production") |
| 47 | + String conditionValue; |
| 48 | + |
| 49 | + @Option(displayName = "Target JsonPath", |
| 50 | + description = "JsonPath to the property to update.", |
| 51 | + example = "$.spec.replicas") |
| 52 | + String targetJsonPath; |
| 53 | + |
| 54 | + @Option(displayName = "New value", |
| 55 | + description = "The new value to set at targetJsonPath.", |
| 56 | + example = "3") |
| 57 | + String newValue; |
| 58 | + |
| 59 | + @Option(displayName = "File pattern", |
| 60 | + description = "A glob expression for files to process. Blank/null matches all.", |
| 61 | + required = false, |
| 62 | + example = "**/k8s/**/*.yaml") |
| 63 | + @Nullable |
| 64 | + String filePattern; |
| 65 | + |
| 66 | + @Override |
| 67 | + public String getDisplayName() { |
| 68 | + return "Change YAML property conditionally"; |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public String getDescription() { |
| 73 | + return "Updates a YAML property value only when another property in the same document " + |
| 74 | + "matches a specified condition. " + |
| 75 | + "Useful for updating values in multi-document YAML files where each document " + |
| 76 | + "should be evaluated independently."; |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public String getInstanceNameSuffix() { |
| 81 | + return String.format("`%s` to `%s` when `%s` = `%s`", targetJsonPath, newValue, |
| 82 | + conditionJsonPath, conditionValue); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public TreeVisitor<?, ExecutionContext> getVisitor() { |
| 87 | + JsonPathMatcher conditionMatcher = new JsonPathMatcher(conditionJsonPath); |
| 88 | + JsonPathMatcher targetMatcher = new JsonPathMatcher(targetJsonPath); |
| 89 | + |
| 90 | + return Preconditions.check(new FindSourceFiles(filePattern), new |
| 91 | + YamlIsoVisitor<ExecutionContext>() { |
| 92 | + |
| 93 | + @Override |
| 94 | + public Yaml.Document visitDocument(Yaml.Document document, ExecutionContext ctx) { |
| 95 | + // Check if this document meets the condition |
| 96 | + AtomicBoolean conditionMet = new AtomicBoolean(false); |
| 97 | + |
| 98 | + new YamlIsoVisitor<ExecutionContext>() { |
| 99 | + @Override |
| 100 | + public Yaml.Mapping.Entry visitMappingEntry(Yaml.Mapping.Entry entry, |
| 101 | + ExecutionContext ctx) { |
| 102 | + if (conditionMatcher.matches(getCursor())) { |
| 103 | + if (entry.getValue() instanceof Yaml.Scalar) { |
| 104 | + String value = ((Yaml.Scalar) entry.getValue()).getValue(); |
| 105 | + if (conditionValue.equals(value)) { |
| 106 | + conditionMet.set(true); |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + return super.visitMappingEntry(entry, ctx); |
| 111 | + } |
| 112 | + }.visit(document, ctx); |
| 113 | + |
| 114 | + // If condition is met, update the target property |
| 115 | + if (conditionMet.get()) { |
| 116 | + Yaml.Document updated = (Yaml.Document) new YamlIsoVisitor<ExecutionContext>() { |
| 117 | + @Override |
| 118 | + public Yaml.Mapping.Entry visitMappingEntry(Yaml.Mapping.Entry entry, |
| 119 | + ExecutionContext ctx) { |
| 120 | + Yaml.Mapping.Entry e = super.visitMappingEntry(entry, ctx); |
| 121 | + if (targetMatcher.matches(getCursor())) { |
| 122 | + if (e.getValue() instanceof Yaml.Scalar) { |
| 123 | + Yaml.Scalar scalar = (Yaml.Scalar) e.getValue(); |
| 124 | + if (!newValue.equals(scalar.getValue())) { |
| 125 | + e = e.withValue(scalar.withValue(newValue)); |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + return e; |
| 130 | + } |
| 131 | + }.visit(document, ctx); |
| 132 | + return updated != null ? updated : document; |
| 133 | + } |
| 134 | + |
| 135 | + return document; |
| 136 | + } |
| 137 | + }); |
| 138 | + } |
| 139 | + } |
0 commit comments