Skip to content

Commit 3261a13

Browse files
committed
Make JacksonYamlStep round-trippable
1 parent 43d57dc commit 3261a13

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed

lib/src/main/java/com/diffplug/spotless/yaml/JacksonYamlStep.java

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021-2023 DiffPlug
2+
* Copyright 2021-2024 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,7 +15,6 @@
1515
*/
1616
package com.diffplug.spotless.yaml;
1717

18-
import java.io.IOException;
1918
import java.io.Serializable;
2019
import java.lang.reflect.Constructor;
2120
import java.lang.reflect.InvocationTargetException;
@@ -25,18 +24,26 @@
2524
import com.diffplug.spotless.FormatterStep;
2625
import com.diffplug.spotless.JarState;
2726
import com.diffplug.spotless.Provisioner;
27+
import com.diffplug.spotless.RoundedStep;
2828

2929
/**
3030
* Simple YAML formatter which reformats the file according to Jackson YAMLFactory.
3131
*/
3232
// https://stackoverflow.com/questions/14515994/convert-json-string-to-pretty-print-json-output-using-jackson
3333
// https://stackoverflow.com/questions/60891174/i-want-to-load-a-yaml-file-possibly-edit-the-data-and-then-dump-it-again-how
34-
public class JacksonYamlStep {
35-
static final String MAVEN_COORDINATE = "com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:";
36-
// https://mvnrepository.com/artifact/com.fasterxml.jackson.dataformat/jackson-dataformat-yaml
37-
static final String DEFAULT_VERSION = "2.14.1";
34+
public class JacksonYamlStep implements RoundedStep {
35+
private static final long serialVersionUID = 1L;
36+
private static final String MAVEN_COORDINATE = "com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:";
37+
private static final String DEFAULT_VERSION = "2.14.1";
38+
public static final String NAME = "jacksonYaml";
3839

39-
private JacksonYamlStep() {}
40+
private final JarState.Promised jarState;
41+
private final JacksonYamlConfig jacksonConfig;
42+
43+
private JacksonYamlStep(JarState.Promised jarState, JacksonYamlConfig jacksonConfig) {
44+
this.jarState = jarState;
45+
this.jacksonConfig = jacksonConfig;
46+
}
4047

4148
public static String defaultVersion() {
4249
return DEFAULT_VERSION;
@@ -47,28 +54,29 @@ public static FormatterStep create(JacksonYamlConfig jacksonConfig,
4754
Provisioner provisioner) {
4855
Objects.requireNonNull(jacksonConfig, "jacksonConfig cannot be null");
4956
Objects.requireNonNull(provisioner, "provisioner cannot be null");
50-
return FormatterStep.createLazy("yaml",
51-
() -> new State(jacksonConfig, jacksonVersion, provisioner),
57+
return FormatterStep.create(NAME,
58+
new JacksonYamlStep(JarState.promise(() -> JarState.from(MAVEN_COORDINATE + jacksonVersion, provisioner)), jacksonConfig),
59+
JacksonYamlStep::equalityState,
5260
State::toFormatter);
5361
}
5462

5563
public static FormatterStep create(Provisioner provisioner) {
5664
return create(new JacksonYamlConfig(), defaultVersion(), provisioner);
5765
}
5866

67+
private State equalityState() {
68+
return new State(jarState.get(), jacksonConfig);
69+
}
70+
5971
private static final class State implements Serializable {
6072
private static final long serialVersionUID = 1L;
6173

6274
private final JacksonYamlConfig jacksonConfig;
63-
6475
private final JarState jarState;
6576

66-
private State(JacksonYamlConfig jacksonConfig,
67-
String jacksonVersion,
68-
Provisioner provisioner) throws IOException {
77+
State(JarState jarState, JacksonYamlConfig jacksonConfig) {
78+
this.jarState = jarState;
6979
this.jacksonConfig = jacksonConfig;
70-
71-
this.jarState = JarState.from(JacksonYamlStep.MAVEN_COORDINATE + jacksonVersion, provisioner);
7280
}
7381

7482
FormatterFunc toFormatter() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException,

testlib/src/main/java/com/diffplug/spotless/StepHarnessBase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ protected StepHarnessBase(Formatter formatter) {
5151
supportsRoundTrip = true;
5252
} else if (Set.of("black", "buf", "clang", "ktlint", "ktfmt", "scalafmt", "palantir-java-format", "google-java-format",
5353
"removeUnusedImports", "cleanthat", "No line break between type annotation and type",
54-
"gson", "jacksonJson", "apply-json-patch", "jsonSimple", "sortPom",
54+
"gson", "jacksonJson", "apply-json-patch", "jsonSimple", "sortPom", "jacksonYaml",
5555
"importOrder", "Remove unnecessary semicolons").contains(onlyStepName)) {
5656
supportsRoundTrip = true;
5757
}

0 commit comments

Comments
 (0)