-
Notifications
You must be signed in to change notification settings - Fork 481
Add JsonPatchStep #1753
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
Merged
nedtwigg
merged 6 commits into
diffplug:main
from
opencastsoftware:apply-json-patch-step
Jul 31, 2023
Merged
Add JsonPatchStep #1753
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a12b7ca
Add ApplyJsonPatchStep
DavidGregory084 7f9ed12
Remove unnecessary configuration of ObjectMapper's pretty printer in …
DavidGregory084 43a8372
Merge branch 'main' of github.com:opencastsoftware/spotless into appl…
DavidGregory084 98943c3
Update changelogs and documentation
DavidGregory084 e518e36
Merge branch 'main' of github.com:diffplug/spotless into apply-json-p…
DavidGregory084 1eb2a4f
Rename `applyJsonPatch` functionality to `jsonPatch`
DavidGregory084 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.