Skip to content

Commit a22f07a

Browse files
committed
Make JsonPatchStep round-trippable
1 parent 27b4b7f commit a22f07a

File tree

2 files changed

+42
-19
lines changed

2 files changed

+42
-19
lines changed

lib/src/main/java/com/diffplug/spotless/json/JsonPatchStep.java

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023 DiffPlug
2+
* Copyright 2023-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,25 +15,40 @@
1515
*/
1616
package com.diffplug.spotless.json;
1717

18-
import java.io.IOException;
1918
import java.io.Serializable;
2019
import java.lang.reflect.Constructor;
2120
import java.lang.reflect.InvocationTargetException;
2221
import java.util.List;
2322
import java.util.Map;
2423
import java.util.Objects;
2524

25+
import javax.annotation.Nullable;
26+
2627
import com.diffplug.spotless.FormatterFunc;
2728
import com.diffplug.spotless.FormatterStep;
2829
import com.diffplug.spotless.JarState;
2930
import com.diffplug.spotless.Provisioner;
31+
import com.diffplug.spotless.RoundedStep;
32+
33+
public class JsonPatchStep implements RoundedStep {
34+
private static final long serialVersionUID = 1L;
35+
private static final String MAVEN_COORDINATE = "com.flipkart.zjsonpatch:zjsonpatch";
36+
private static final String DEFAULT_VERSION = "0.4.14";
37+
public static final String NAME = "apply-json-patch";
3038

31-
public class JsonPatchStep {
32-
// https://mvnrepository.com/artifact/com.flipkart.zjsonpatch/zjsonpatch
33-
static final String MAVEN_COORDINATE = "com.flipkart.zjsonpatch:zjsonpatch";
34-
static final String DEFAULT_VERSION = "0.4.14";
39+
private final JarState.Promised jarState;
40+
@Nullable
41+
private final List<Map<String, Object>> patch;
42+
@Nullable
43+
private final String patchString;
3544

36-
private JsonPatchStep() {}
45+
private JsonPatchStep(JarState.Promised jarState,
46+
@Nullable String patchString,
47+
@Nullable List<Map<String, Object>> patch) {
48+
this.jarState = jarState;
49+
this.patchString = patchString;
50+
this.patch = patch;
51+
}
3752

3853
public static FormatterStep create(String patchString, Provisioner provisioner) {
3954
return create(DEFAULT_VERSION, patchString, provisioner);
@@ -43,7 +58,10 @@ public static FormatterStep create(String zjsonPatchVersion, String patchString,
4358
Objects.requireNonNull(zjsonPatchVersion, "zjsonPatchVersion cannot be null");
4459
Objects.requireNonNull(patchString, "patchString cannot be null");
4560
Objects.requireNonNull(provisioner, "provisioner cannot be null");
46-
return FormatterStep.createLazy("apply-json-patch", () -> new State(zjsonPatchVersion, patchString, provisioner), State::toFormatter);
61+
return FormatterStep.create(NAME,
62+
new JsonPatchStep(JarState.promise(() -> JarState.from(MAVEN_COORDINATE + ":" + zjsonPatchVersion, provisioner)), patchString, null),
63+
JsonPatchStep::equalityState,
64+
State::toFormatter);
4765
}
4866

4967
public static FormatterStep create(List<Map<String, Object>> patch, Provisioner provisioner) {
@@ -54,26 +72,31 @@ public static FormatterStep create(String zjsonPatchVersion, List<Map<String, Ob
5472
Objects.requireNonNull(zjsonPatchVersion, "zjsonPatchVersion cannot be null");
5573
Objects.requireNonNull(patch, "patch cannot be null");
5674
Objects.requireNonNull(provisioner, "provisioner cannot be null");
57-
return FormatterStep.createLazy("apply-json-patch", () -> new State(zjsonPatchVersion, patch, provisioner), State::toFormatter);
75+
return FormatterStep.create(NAME,
76+
new JsonPatchStep(JarState.promise(() -> JarState.from(MAVEN_COORDINATE + ":" + zjsonPatchVersion, provisioner)), null, patch),
77+
JsonPatchStep::equalityState,
78+
State::toFormatter);
79+
}
80+
81+
private State equalityState() {
82+
return new State(jarState.get(), patchString, patch);
5883
}
5984

6085
static final class State implements Serializable {
6186
private static final long serialVersionUID = 1L;
6287

6388
private final JarState jarState;
89+
@Nullable
6490
private final List<Map<String, Object>> patch;
91+
@Nullable
6592
private final String patchString;
6693

67-
private State(String zjsonPatchVersion, List<Map<String, Object>> patch, Provisioner provisioner) throws IOException {
68-
this.jarState = JarState.from(MAVEN_COORDINATE + ":" + zjsonPatchVersion, provisioner);
69-
this.patch = patch;
70-
this.patchString = null;
71-
}
72-
73-
private State(String zjsonPatchVersion, String patchString, Provisioner provisioner) throws IOException {
74-
this.jarState = JarState.from(MAVEN_COORDINATE + ":" + zjsonPatchVersion, provisioner);
75-
this.patch = null;
94+
State(JarState jarState,
95+
@Nullable String patchString,
96+
@Nullable List<Map<String, Object>> patch) {
97+
this.jarState = jarState;
7698
this.patchString = patchString;
99+
this.patch = patch;
77100
}
78101

79102
FormatterFunc toFormatter() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {

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",
54+
"gson", "jacksonJson", "apply-json-patch",
5555
"importOrder", "Remove unnecessary semicolons").contains(onlyStepName)) {
5656
supportsRoundTrip = true;
5757
}

0 commit comments

Comments
 (0)