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

MergeYaml recipe: Handle ParseError LSTs #5097

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
51 changes: 31 additions & 20 deletions rewrite-yaml/src/main/java/org/openrewrite/yaml/MergeYaml.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public Validated<Object> validate() {
return super.validate()
.and(Validated.test("yaml", "Must be valid YAML",
yaml, y -> new YamlParser().parse(yaml)
.filter(it -> it instanceof Yaml.Documents) // Could also be a ParserError
.findFirst()
.map(doc -> !((Yaml.Documents) doc).getDocuments().isEmpty())
.orElse(false)))
Expand Down Expand Up @@ -123,22 +124,13 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(new FindSourceFiles(filePattern), new YamlIsoVisitor<ExecutionContext>() {
final JsonPathMatcher matcher = new JsonPathMatcher(key);

final Yaml incoming = new YamlParser().parse(yaml)
.findFirst()
.map(Yaml.Documents.class::cast)
.map(docs -> {
// Any comments will have been put on the parent Document node, preserve by copying to the mapping
Yaml.Document doc = docs.getDocuments().get(0);
if (doc.getBlock() instanceof Yaml.Mapping) {
Yaml.Mapping m = (Yaml.Mapping) doc.getBlock();
return m.withEntries(ListUtils.mapFirst(m.getEntries(), entry -> entry.withPrefix(doc.getPrefix())));
} else if (doc.getBlock() instanceof Yaml.Sequence) {
Yaml.Sequence s = (Yaml.Sequence) doc.getBlock();
return s.withEntries(ListUtils.mapFirst(s.getEntries(), entry -> entry.withPrefix(doc.getPrefix())));
}
return doc.getBlock().withPrefix(doc.getPrefix());
})
.orElseThrow(() -> new IllegalArgumentException("Could not parse as YAML"));
@Nullable Yaml incoming;
private Yaml incoming() {
if (incoming == null) {
incoming = parse(yaml);
}
return incoming;
}

@Override
public Yaml.Document visitDocument(Yaml.Document document, ExecutionContext ctx) {
Expand All @@ -161,7 +153,7 @@ public Yaml.Document visitDocument(Yaml.Document document, ExecutionContext ctx)
}
// If there is no space between the colon and the value it will not be interpreted as a mapping
String snippet;
if (incoming instanceof Yaml.Mapping) {
if (incoming() instanceof Yaml.Mapping) {
snippet = valueKey + ":\n" + indent(yaml);
} else {
snippet = valueKey + ":" + (yaml.startsWith(" ") ? yaml : " " + yaml);
Expand Down Expand Up @@ -213,7 +205,7 @@ public Yaml.Mapping visitMapping(Yaml.Mapping mapping, ExecutionContext ctx) {
Yaml.Mapping m = super.visitMapping(mapping, ctx);
if (matcher.matches(getCursor())) {
getCursor().putMessageOnFirstEnclosing(Yaml.Document.class, FOUND_MATCHING_ELEMENT, true);
m = (Yaml.Mapping) new MergeYamlVisitor<>(mapping, incoming, Boolean.TRUE.equals(acceptTheirs),
m = (Yaml.Mapping) new MergeYamlVisitor<>(mapping, incoming(), Boolean.TRUE.equals(acceptTheirs),
objectIdentifyingProperty, insertMode, insertProperty).visitNonNull(mapping, ctx, getCursor().getParentOrThrow());
}
return m;
Expand All @@ -223,7 +215,7 @@ public Yaml.Mapping visitMapping(Yaml.Mapping mapping, ExecutionContext ctx) {
public Yaml.Mapping.Entry visitMappingEntry(Yaml.Mapping.Entry entry, ExecutionContext ctx) {
if (matcher.matches(getCursor())) {
getCursor().putMessageOnFirstEnclosing(Yaml.Document.class, FOUND_MATCHING_ELEMENT, true);
Yaml.Block value = (Yaml.Block) new MergeYamlVisitor<>(entry.getValue(), incoming,
Yaml.Block value = (Yaml.Block) new MergeYamlVisitor<>(entry.getValue(), incoming(),
Boolean.TRUE.equals(acceptTheirs), objectIdentifyingProperty, insertMode, insertProperty).visitNonNull(entry.getValue(),
ctx, getCursor());
if (value instanceof Yaml.Scalar && value.getPrefix().isEmpty()) {
Expand All @@ -239,12 +231,31 @@ public Yaml.Sequence visitSequence(Yaml.Sequence sequence, ExecutionContext ctx)
if (matcher.matches(getCursor().getParentOrThrow())) {
getCursor().putMessageOnFirstEnclosing(Yaml.Document.class, FOUND_MATCHING_ELEMENT, true);
return sequence.withEntries(ListUtils.map(sequence.getEntries(),
entry -> entry.withBlock((Yaml.Block) new MergeYamlVisitor<>(entry.getBlock(), incoming,
entry -> entry.withBlock((Yaml.Block) new MergeYamlVisitor<>(entry.getBlock(), incoming(),
Boolean.TRUE.equals(acceptTheirs), objectIdentifyingProperty, insertMode, insertProperty)
.visitNonNull(entry.getBlock(), ctx, new Cursor(getCursor(), entry)))));
}
return super.visitSequence(sequence, ctx);
}
});
}

static Yaml parse(@Language("yml") String yaml) {
return new YamlParser().parse(yaml)
.findFirst()
.map(Yaml.Documents.class::cast)
.map(docs -> {
// Any comments will have been put on the parent Document node, preserve by copying to the mapping
Yaml.Document doc = docs.getDocuments().get(0);
if (doc.getBlock() instanceof Yaml.Mapping) {
Yaml.Mapping m = (Yaml.Mapping) doc.getBlock();
return m.withEntries(ListUtils.mapFirst(m.getEntries(), entry -> entry.withPrefix(doc.getPrefix())));
} else if (doc.getBlock() instanceof Yaml.Sequence) {
Yaml.Sequence s = (Yaml.Sequence) doc.getBlock();
return s.withEntries(ListUtils.mapFirst(s.getEntries(), entry -> entry.withPrefix(doc.getPrefix())));
}
return doc.getBlock().withPrefix(doc.getPrefix());
})
.orElseThrow(() -> new IllegalArgumentException("Could not parse as YAML"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,27 +90,7 @@ public MergeYamlVisitor(Yaml.Block block, Yaml incoming, boolean acceptTheirs, @
}

public MergeYamlVisitor(Yaml scope, @Language("yml") String yamlString, boolean acceptTheirs, @Nullable String objectIdentifyingProperty, @Nullable InsertMode insertMode, @Nullable String insertProperty) {
this(scope,
new YamlParser().parse(yamlString)
.findFirst()
.map(Yaml.Documents.class::cast)
.map(docs -> {
// Any comments will have been put on the parent Yaml.Document node, preserve by copying to the mapping
Yaml.Document doc = docs.getDocuments().get(0);
if (doc.getBlock() instanceof Yaml.Mapping) {
Yaml.Mapping m = (Yaml.Mapping) doc.getBlock();
return m.withEntries(mapFirst(m.getEntries(), entry -> entry.withPrefix(doc.getPrefix())));
} else if (doc.getBlock() instanceof Yaml.Sequence) {
Yaml.Sequence s = (Yaml.Sequence) doc.getBlock();
return s.withEntries(mapFirst(s.getEntries(), entry -> entry.withPrefix(doc.getPrefix())));
}
return doc.getBlock().withPrefix(doc.getPrefix());
})
.orElseThrow(() -> new IllegalArgumentException("Could not parse as YAML")),
acceptTheirs,
objectIdentifyingProperty,
insertMode,
insertProperty);
this(scope, MergeYaml.parse(yamlString), acceptTheirs, objectIdentifyingProperty, insertMode, insertProperty);
}

@Override
Expand Down
20 changes: 20 additions & 0 deletions rewrite-yaml/src/test/java/org/openrewrite/yaml/MergeYamlTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.openrewrite.Issue;
import org.openrewrite.test.RewriteTest;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.openrewrite.yaml.Assertions.yaml;
import static org.openrewrite.yaml.MergeYaml.InsertMode.After;
import static org.openrewrite.yaml.MergeYaml.InsertMode.Before;
Expand Down Expand Up @@ -2820,4 +2821,23 @@ void preventKeysToBeAppendedToPreviousCommentIfManyLineBreaks() {
)
);
}

@Test
void invalidYaml() {
assertThrows(AssertionError.class, () -> rewriteRun(
spec -> spec
.recipe(new MergeYaml(
"$.some.object",
// language=yaml
"""
script: |-ParseError
""",
false,
"name",
null,
null,
null
))
));
}
}