-
Notifications
You must be signed in to change notification settings - Fork 460
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
400 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -124,3 +124,6 @@ nb-configuration.xml | |
|
||
# MacOS jenv | ||
.java-version | ||
|
||
# VS Code | ||
.vscode/ |
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
90 changes: 90 additions & 0 deletions
90
lib/src/main/java/com/diffplug/spotless/json/JsonPatchStep.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,90 @@ | ||
/* | ||
* Copyright 2023 DiffPlug | ||
* | ||
* 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 | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* 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 com.diffplug.spotless.json; | ||
|
||
import java.io.IOException; | ||
import java.io.Serializable; | ||
import java.lang.reflect.Constructor; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
import com.diffplug.spotless.FormatterFunc; | ||
import com.diffplug.spotless.FormatterStep; | ||
import com.diffplug.spotless.JarState; | ||
import com.diffplug.spotless.Provisioner; | ||
|
||
public class JsonPatchStep { | ||
// https://mvnrepository.com/artifact/com.flipkart.zjsonpatch/zjsonpatch | ||
static final String MAVEN_COORDINATE = "com.flipkart.zjsonpatch:zjsonpatch"; | ||
static final String DEFAULT_VERSION = "0.4.14"; | ||
|
||
private JsonPatchStep() {} | ||
|
||
public static FormatterStep create(String patchString, Provisioner provisioner) { | ||
return create(DEFAULT_VERSION, patchString, provisioner); | ||
} | ||
|
||
public static FormatterStep create(String zjsonPatchVersion, String patchString, Provisioner provisioner) { | ||
Objects.requireNonNull(zjsonPatchVersion, "zjsonPatchVersion cannot be null"); | ||
Objects.requireNonNull(patchString, "patchString cannot be null"); | ||
Objects.requireNonNull(provisioner, "provisioner cannot be null"); | ||
return FormatterStep.createLazy("apply-json-patch", () -> new State(zjsonPatchVersion, patchString, provisioner), State::toFormatter); | ||
} | ||
|
||
public static FormatterStep create(List<Map<String, Object>> patch, Provisioner provisioner) { | ||
return create(DEFAULT_VERSION, patch, provisioner); | ||
} | ||
|
||
public static FormatterStep create(String zjsonPatchVersion, List<Map<String, Object>> patch, Provisioner provisioner) { | ||
Objects.requireNonNull(zjsonPatchVersion, "zjsonPatchVersion cannot be null"); | ||
Objects.requireNonNull(patch, "patch cannot be null"); | ||
Objects.requireNonNull(provisioner, "provisioner cannot be null"); | ||
return FormatterStep.createLazy("apply-json-patch", () -> new State(zjsonPatchVersion, patch, provisioner), State::toFormatter); | ||
} | ||
|
||
static final class State implements Serializable { | ||
private static final long serialVersionUID = 1L; | ||
|
||
private final JarState jarState; | ||
private final List<Map<String, Object>> patch; | ||
private final String patchString; | ||
|
||
private State(String zjsonPatchVersion, List<Map<String, Object>> patch, Provisioner provisioner) throws IOException { | ||
this.jarState = JarState.from(MAVEN_COORDINATE + ":" + zjsonPatchVersion, provisioner); | ||
this.patch = patch; | ||
this.patchString = null; | ||
} | ||
|
||
private State(String zjsonPatchVersion, String patchString, Provisioner provisioner) throws IOException { | ||
this.jarState = JarState.from(MAVEN_COORDINATE + ":" + zjsonPatchVersion, provisioner); | ||
this.patch = null; | ||
this.patchString = patchString; | ||
} | ||
|
||
FormatterFunc toFormatter() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException { | ||
Class<?> formatterFunc = jarState.getClassLoader().loadClass("com.diffplug.spotless.glue.json.JsonPatchFormatterFunc"); | ||
if (this.patch != null) { | ||
Constructor<?> constructor = formatterFunc.getConstructor(List.class); | ||
return (FormatterFunc) constructor.newInstance(patch); | ||
} else { | ||
Constructor<?> constructor = formatterFunc.getConstructor(String.class); | ||
return (FormatterFunc) constructor.newInstance(patchString); | ||
} | ||
} | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
lib/src/zjsonPatch/java/com/diffplug/spotless/glue/json/JsonPatchFormatterFunc.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,55 @@ | ||
/* | ||
* Copyright 2023 DiffPlug | ||
* | ||
* 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 | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* 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 com.diffplug.spotless.glue.json; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.flipkart.zjsonpatch.JsonPatch; | ||
|
||
import com.diffplug.spotless.FormatterFunc; | ||
|
||
public class JsonPatchFormatterFunc implements FormatterFunc { | ||
private final ObjectMapper objectMapper; | ||
private final List<Map<String, Object>> patch; | ||
private final String patchString; | ||
|
||
public JsonPatchFormatterFunc(String patchString) { | ||
this.objectMapper = new ObjectMapper(); | ||
this.patch = null; | ||
this.patchString = patchString; | ||
} | ||
|
||
public JsonPatchFormatterFunc(List<Map<String, Object>> patch) { | ||
this.objectMapper = new ObjectMapper(); | ||
this.patch = patch; | ||
this.patchString = null; | ||
} | ||
|
||
@Override | ||
public String apply(String input) throws Exception { | ||
var patchNode = this.patch == null | ||
? objectMapper.readTree(patchString) | ||
: objectMapper.valueToTree(patch); | ||
|
||
var inputNode = objectMapper.readTree(input); | ||
|
||
var patchedNode = JsonPatch.apply(patchNode, inputNode); | ||
|
||
return objectMapper.writeValueAsString(patchedNode); | ||
} | ||
} |
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
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.