forked from openrewrite/rewrite
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Anchor and Alias support to the Yaml model and JsonPathMatcher (o…
…penrewrite#2272) Yaml Anchor / Alias support - Add a new `EntryKey` interface for `Mapping.Entry.key` values which are either scalar or alias blocks. - Update `YamlParser` to handle `Mapping.Entry.key` as an instance of `EntryKey` - New `ReplaceAliasWithAnchorValueVisitor`. - Update `JsonPathMatcher` to replace alias trees with their associated anchor value before searching. - Update `CoalesceProperties` to prevent transforming documents having anchor/alias pairs. - Update `org.openrewrite.yaml.DeleteProperty` to prevent deleting properties from documents having anchor/alias pars.
- Loading branch information
Showing
14 changed files
with
317 additions
and
14 deletions.
There are no files selected for viewing
This file contains 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 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 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 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
50 changes: 50 additions & 0 deletions
50
rewrite-yaml/src/main/java/org/openrewrite/yaml/ReplaceAliasWithAnchorValueVisitor.java
This file contains 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,50 @@ | ||
/* | ||
* Copyright 2022 the original author or authors. | ||
* <p> | ||
* 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 | ||
* <p> | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* 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.openrewrite.yaml; | ||
|
||
import org.openrewrite.yaml.tree.Yaml; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class ReplaceAliasWithAnchorValueVisitor<P> extends YamlVisitor<P> { | ||
Map<String, Yaml> anchorValues = new HashMap<>(); | ||
@Override | ||
public Yaml visitMapping(Yaml.Mapping mapping, P p) { | ||
if (mapping.getAnchor() != null) { | ||
anchorValues.put(mapping.getAnchor().getKey(), mapping.withAnchor(null)); | ||
} | ||
return super.visitMapping(mapping, p); | ||
} | ||
|
||
@Override | ||
public Yaml visitScalar(Yaml.Scalar scalar, P p) { | ||
if (scalar.getAnchor() != null) { | ||
anchorValues.put(scalar.getAnchor().getKey(), scalar.withAnchor(null)); | ||
} | ||
return super.visitScalar(scalar, p); | ||
} | ||
|
||
@Override | ||
public Yaml visitAlias(Yaml.Alias alias, P p) { | ||
Yaml.Alias al = (Yaml.Alias) super.visitAlias(alias, p); | ||
Yaml anchorVal = anchorValues.get(al.getAnchor().getKey()); | ||
if (anchorVal != null) { | ||
return anchorVal; | ||
} | ||
return al; | ||
} | ||
} |
This file contains 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 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
26 changes: 26 additions & 0 deletions
26
rewrite-yaml/src/main/java/org/openrewrite/yaml/tree/YamlKey.java
This file contains 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,26 @@ | ||
/* | ||
* Copyright 2022 the original author or authors. | ||
* <p> | ||
* 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 | ||
* <p> | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* 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.openrewrite.yaml.tree; | ||
|
||
/** | ||
* Yaml Keys are either Scalar or Alias blocks | ||
*/ | ||
public interface YamlKey extends Yaml { | ||
String getValue(); | ||
YamlKey copyPaste(); | ||
|
||
YamlKey withPrefix(String prefix); | ||
} |
130 changes: 130 additions & 0 deletions
130
rewrite-yaml/src/test/java/org/openrewrite/yaml/ReplaceAliasWithAnchorValueTest.java
This file contains 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,130 @@ | ||
/* | ||
* Copyright 2022 the original author or authors. | ||
* <p> | ||
* 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 | ||
* <p> | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* 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.openrewrite.yaml; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.openrewrite.test.RecipeSpec; | ||
import org.openrewrite.test.RewriteTest; | ||
|
||
import static org.openrewrite.yaml.Assertions.yaml; | ||
|
||
class ReplaceAliasWithAnchorValueTest implements RewriteTest { | ||
@Override | ||
public void defaults(RecipeSpec spec) { | ||
spec.recipe(RewriteTest.toRecipe(ReplaceAliasWithAnchorValueVisitor::new)); | ||
} | ||
|
||
@Test | ||
void simpleCase() { | ||
rewriteRun( | ||
yaml(""" | ||
bar: | ||
&abc yo: friend | ||
baz: | ||
*abc: friendly | ||
""", | ||
""" | ||
bar: | ||
&abc yo: friend | ||
baz: | ||
yo: friendly | ||
""" | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void aliasRefersToLastKnownAnchorValue() { | ||
rewriteRun( | ||
yaml(""" | ||
definitions: | ||
steps: | ||
- step: &build-test | ||
name: Build and test | ||
- step2: &build-deploy | ||
name: Build and Deploy | ||
pipelines: | ||
branches: | ||
develop: | ||
- step: *build-test | ||
master: | ||
- step: *build-deploy | ||
environments: | ||
branches: | ||
test: | ||
- step: &build-test | ||
name: ReUsed Build and Test | ||
qa: | ||
- step: *build-test | ||
""", | ||
""" | ||
definitions: | ||
steps: | ||
- step: &build-test | ||
name: Build and test | ||
- step2: &build-deploy | ||
name: Build and Deploy | ||
pipelines: | ||
branches: | ||
develop: | ||
- step: | ||
name: Build and test | ||
master: | ||
- step: | ||
name: Build and Deploy | ||
environments: | ||
branches: | ||
test: | ||
- step: &build-test | ||
name: ReUsed Build and Test | ||
qa: | ||
- step: | ||
name: ReUsed Build and Test | ||
""" | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void howAboutSequences() { | ||
rewriteRun( | ||
yaml(""" | ||
stages: | ||
- id: &id ping_api_endpoint | ||
name: *id | ||
- id: &id get_token | ||
name: *id | ||
- id: &id do_something | ||
name: *id | ||
- id: &id revoke_token | ||
name: *id | ||
""", | ||
""" | ||
stages: | ||
- id: &id ping_api_endpoint | ||
name: ping_api_endpoint | ||
- id: &id get_token | ||
name: get_token | ||
- id: &id do_something | ||
name: do_something | ||
- id: &id revoke_token | ||
name: revoke_token | ||
""" | ||
) | ||
); | ||
} | ||
|
||
} |
This file contains 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
Oops, something went wrong.