Skip to content

Commit

Permalink
YAML CoalesceProperties
Browse files Browse the repository at this point in the history
  • Loading branch information
jkschneider committed Jul 1, 2020
1 parent 3c6add3 commit ae8607f
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
public class ChangePropertyKey extends YamlRefactorVisitor {
private String property;
private String toProperty;
private boolean coalesce = true;

public ChangePropertyKey() {
setCursoringOn();
Expand All @@ -49,6 +50,10 @@ public void setToProperty(String toProperty) {
this.toProperty = toProperty;
}

public void setCoalesce(boolean coalesce) {
this.coalesce = coalesce;
}

@Override
public boolean isIdempotent() {
return false;
Expand Down Expand Up @@ -80,9 +85,10 @@ public Yaml visitMappingEntry(Yaml.Mapping.Entry entry) {
propertyToTest,
entry.getValue()
));

andThen(new DeleteProperty(entry));

if (coalesce) {
maybeCoalesceProperties();
}
break;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2020 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.ArrayList;
import java.util.List;

public class CoalesceProperties extends YamlRefactorVisitor {
@Override
public Yaml visitMapping(Yaml.Mapping mapping) {
Yaml.Mapping m = refactor(mapping, super::visitMapping);

boolean changed = false;
List<Yaml.Mapping.Entry> entries = new ArrayList<>();

for (Yaml.Mapping.Entry entry : m.getEntries()) {
if (entry.getValue() instanceof Yaml.Mapping) {
Yaml.Mapping valueMapping = (Yaml.Mapping) entry.getValue();
if (valueMapping.getEntries().size() == 1) {
Yaml.Mapping.Entry subEntry = valueMapping.getEntries().iterator().next();
Yaml.Scalar coalescedKey = entry.getKey().withValue(entry.getKey().getValue() + "." + subEntry.getKey().getValue());

entries.add(entry.withKey(coalescedKey)
.withValue(subEntry.getValue()));

andThen(new ShiftFormatLeft(subEntry.getValue(),
formatter.wholeSourceIndent().getIndentToUse()));

changed = true;
} else {
entries.add(entry);
}
} else {
entries.add(entry);
}
}

if (changed) {
m = m.withEntries(entries);
}

return m;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2020 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;

public class ShiftFormatLeft extends YamlRefactorVisitor {
private final Yaml scope;
private final int shift;

public ShiftFormatLeft(Yaml scope, int shift) {
this.scope = scope;
this.shift = shift;
setCursoringOn();
}

@Override
public boolean isIdempotent() {
return false;
}

@Override
public Yaml visitMappingEntry(Yaml.Mapping.Entry entry) {
Yaml.Mapping.Entry e = refactor(entry, super::visitMappingEntry);
if (getCursor().isScopeInPath(scope)) {
String prefix = e.getFormatting().getPrefix();
e = e.withPrefix(prefix.substring(0, prefix.indexOf('\n') + 1) +
prefix.substring(prefix.indexOf('\n') + shift + 1));
}
return e;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,54 +17,53 @@

import org.openrewrite.RefactorVisitorSupport;
import org.openrewrite.Tree;
import org.openrewrite.refactor.Formatter;
import org.openrewrite.yaml.tree.Yaml;

public class YamlRefactorVisitor extends YamlSourceVisitor<Yaml> implements RefactorVisitorSupport {
protected Formatter formatter;

@Override
public Yaml defaultTo(Tree t) {
return (Yaml) t;
}

@Override
public Yaml visitDocuments(Yaml.Documents documents) {
Yaml.Documents d = documents;
d = d.withDocuments(refactor(d.getDocuments()));
return d;
formatter = new Formatter(documents);
return documents.withDocuments(refactor(documents.getDocuments()));
}

@Override
public Yaml visitDocument(Yaml.Document document) {
Yaml.Document d = document;
d = d.withBlocks(refactor(d.getBlocks()));
return d;
return document.withBlocks(refactor(document.getBlocks()));
}

@Override
public Yaml visitMapping(Yaml.Mapping mapping) {
Yaml.Mapping m = mapping;
m = m.withEntries(refactor(m.getEntries()));
return m;
return mapping.withEntries(refactor(mapping.getEntries()));
}

@Override
public Yaml visitMappingEntry(Yaml.Mapping.Entry entry) {
Yaml.Mapping.Entry e = entry;
e = e.withKey(refactor(e.getKey()));
e = e.withValue(refactor(e.getValue()));
return e;
return e.withValue(refactor(e.getValue()));
}

@Override
public Yaml visitSequence(Yaml.Sequence sequence) {
Yaml.Sequence s = sequence;
s = s.withEntries(refactor(s.getEntries()));
return s;
return sequence.withEntries(refactor(sequence.getEntries()));
}

@Override
public Yaml visitSequenceEntry(Yaml.Sequence.Entry entry) {
Yaml.Sequence.Entry e = entry;
e = e.withBlock(refactor(e.getBlock()));
return e;
return entry.withBlock(refactor(entry.getBlock()));
}

public void maybeCoalesceProperties() {
if (andThen().stream().noneMatch(CoalesceProperties.class::isInstance)) {
andThen(new CoalesceProperties());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ class ChangePropertyKeyTest : YamlParser() {

assertRefactored(fixed, """
management.metrics:
binders:
jvm.enabled: true
binders.jvm.enabled: true
enable.process.files: true
""".trimIndent())
}
Expand All @@ -66,8 +65,7 @@ class ChangePropertyKeyTest : YamlParser() {
val fixed = y.refactor().visit(changeProp).fix().fixed

assertRefactored(fixed, """
management.metrics:
enable.process.files: true
management.metrics.enable.process.files: true
""".trimIndent())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2020 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

class CoalescePropertiesTest : YamlParser() {
@Test
fun fold() {
val y = parse("""
management:
metrics:
enable.process.files: true
endpoint:
health:
show-components: always
show-details: always
""".trimIndent())

val fixed = y.refactor().visit(CoalesceProperties()).fix().fixed

assertRefactored(fixed, """
management:
metrics.enable.process.files: true
endpoint.health:
show-components: always
show-details: always
""".trimIndent())
}

// @Test
// fun group() {
// val y = parse("""
// management.metrics.enable.process.files: true
// management.metrics.enable.jvm: true
// """.trimIndent())
//
// val fixed = y.refactor().visit(CoalesceProperties()).fix().fixed
//
// assertRefactored(fixed, """
// management.metrics.enable:
// process.files: true
// jvm: true
// """.trimIndent())
// }
}

0 comments on commit ae8607f

Please sign in to comment.