forked from java-json-tools/json-patch
-
Notifications
You must be signed in to change notification settings - Fork 6
Introduce composed JsonPatchOperation as opposed to inherited #5
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
Open
hnguyen08
wants to merge
1
commit into
crate-metadata:master
Choose a base branch
from
hnguyen08:json_patch_operation_composition
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
15 changes: 15 additions & 0 deletions
15
src/main/java/com/github/fge/jsonpatch/action/JsonPatchOperationAction.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,15 @@ | ||
package com.github.fge.jsonpatch.action; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.JsonSerializable; | ||
import com.github.fge.jsonpatch.JsonPatchException; | ||
import com.github.fge.jsonpatch.operation.policy.PathMissingPolicy; | ||
|
||
/** | ||
* | ||
*/ | ||
public interface JsonPatchOperationAction extends JsonSerializable | ||
{ | ||
public JsonNode apply(final JsonNode node, final PathMissingPolicy pathMissingPolicy) | ||
throws JsonPatchException; | ||
} |
93 changes: 93 additions & 0 deletions
93
src/main/java/com/github/fge/jsonpatch/action/OmitAction.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,93 @@ | ||
package com.github.fge.jsonpatch.action; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.JsonSerializable; | ||
import com.fasterxml.jackson.databind.SerializerProvider; | ||
import com.fasterxml.jackson.databind.jsontype.TypeSerializer; | ||
import com.fasterxml.jackson.databind.node.ArrayNode; | ||
import com.fasterxml.jackson.databind.node.MissingNode; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import com.github.fge.jackson.JsonNumEquals; | ||
import com.github.fge.jackson.jsonpointer.JsonPointer; | ||
import com.github.fge.jsonpatch.JsonPatchException; | ||
import com.github.fge.jsonpatch.JsonPatchMessages; | ||
import com.github.fge.jsonpatch.operation.policy.PathMissingPolicy; | ||
import com.github.fge.jsonpatch.properties.PathValueProperties; | ||
import com.github.fge.msgsimple.bundle.MessageBundle; | ||
import com.github.fge.msgsimple.load.MessageBundles; | ||
import com.google.common.base.Equivalence; | ||
import com.google.common.collect.Iterables; | ||
|
||
import java.io.IOException; | ||
|
||
public final class OmitAction | ||
implements JsonPatchOperationAction | ||
{ | ||
private final PathValueProperties properties; | ||
|
||
private static final MessageBundle BUNDLE | ||
= MessageBundles.getBundle(JsonPatchMessages.class); | ||
private static final Equivalence<JsonNode> EQUIVALENCE | ||
= JsonNumEquals.getInstance(); | ||
|
||
public OmitAction(final PathValueProperties properties) | ||
{ | ||
this.properties = properties; | ||
} | ||
|
||
public JsonNode apply(final JsonNode node, final PathMissingPolicy pathMissingPolicy) | ||
throws JsonPatchException | ||
{ | ||
final String op = properties.getOp(); | ||
final JsonPointer path = properties.getPath(); | ||
final JsonNode value = properties.getValue(); | ||
|
||
final JsonNode ret = node.deepCopy(); | ||
if (path.isEmpty()) { | ||
if (EQUIVALENCE.equivalent(ret, value)) { | ||
return MissingNode.getInstance(); | ||
} else { | ||
return ret; | ||
} | ||
} | ||
final JsonNode valueAtPath = path.path(ret); | ||
if (valueAtPath.isMissingNode()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if you move this above the previous if/else, it feels like this policy could be killed and turned into a "resolver" which gets applied before the "action" that gets called. Might be easier for us to talk about in chat. |
||
switch (pathMissingPolicy) { | ||
case THROW: | ||
throw new JsonPatchException(BUNDLE.getMessage( | ||
"jsonPatch.noSuchPath")); | ||
case SKIP: | ||
return ret; | ||
default: | ||
throw new JsonPatchException(BUNDLE.getMessage( | ||
"jsonPatch.invalidPolicy")); | ||
} | ||
} | ||
|
||
if (EQUIVALENCE.equivalent(valueAtPath, value)) { | ||
final JsonNode parent = path.parent().get(ret); | ||
final String rawToken = Iterables.getLast(path).getToken().getRaw(); | ||
if (parent.isObject()) | ||
((ObjectNode) parent).remove(rawToken); | ||
else | ||
((ArrayNode) parent).remove(Integer.parseInt(rawToken)); | ||
} | ||
return ret; | ||
} | ||
|
||
public final void serialize(final JsonGenerator jgen, | ||
final SerializerProvider provider) | ||
throws IOException, JsonProcessingException | ||
{ | ||
properties.serialize(jgen, provider); | ||
} | ||
|
||
public final void serializeWithType(final JsonGenerator jgen, | ||
final SerializerProvider provider, final TypeSerializer typeSer) | ||
throws IOException, JsonProcessingException | ||
{ | ||
properties.serializeWithType(jgen, provider, typeSer); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/main/java/com/github/fge/jsonpatch/operation/JsonPatchOperationComposed.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,47 @@ | ||
package com.github.fge.jsonpatch.operation; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.SerializerProvider; | ||
import com.fasterxml.jackson.databind.jsontype.TypeSerializer; | ||
import com.github.fge.jsonpatch.JsonPatchException; | ||
import com.github.fge.jsonpatch.action.JsonPatchOperationAction; | ||
import com.github.fge.jsonpatch.operation.policy.PathMissingPolicy; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* TODO: rename me! | ||
*/ | ||
public class JsonPatchOperationComposed implements JsonPatchOperation | ||
{ | ||
private JsonPatchOperationAction action; | ||
private PathMissingPolicy pathMissingPolicy; | ||
|
||
public JsonPatchOperationComposed(JsonPatchOperationAction action, PathMissingPolicy pathMissingPolicy) | ||
{ | ||
this.action = action; | ||
this.pathMissingPolicy = pathMissingPolicy; | ||
} | ||
|
||
public JsonNode apply(final JsonNode node) | ||
throws JsonPatchException | ||
{ | ||
return action.apply(node, pathMissingPolicy); | ||
} | ||
|
||
public final void serialize(final JsonGenerator jgen, | ||
final SerializerProvider provider) | ||
throws IOException, JsonProcessingException | ||
{ | ||
action.serialize(jgen, provider); | ||
} | ||
|
||
public final void serializeWithType(final JsonGenerator jgen, | ||
final SerializerProvider provider, final TypeSerializer typeSer) | ||
throws IOException, JsonProcessingException | ||
{ | ||
action.serializeWithType(jgen, provider, typeSer); | ||
} | ||
} |
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
27 changes: 24 additions & 3 deletions
27
src/main/java/com/github/fge/jsonpatch/operation/OmitOperationFactory.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 |
---|---|---|
@@ -1,13 +1,34 @@ | ||
package com.github.fge.jsonpatch.operation; | ||
|
||
public final class OmitOperationFactory extends JsonPatchOperationFactoryBase | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.github.fge.jackson.JacksonUtils; | ||
import com.github.fge.jsonpatch.JsonPatchException; | ||
import com.github.fge.jsonpatch.JsonPatchMessages; | ||
import com.github.fge.jsonpatch.action.OmitAction; | ||
import com.github.fge.jsonpatch.operation.policy.PathMissingPolicy; | ||
import com.github.fge.jsonpatch.properties.PathValueProperties; | ||
import com.github.fge.msgsimple.bundle.MessageBundle; | ||
import com.github.fge.msgsimple.load.MessageBundles; | ||
|
||
import java.io.IOException; | ||
|
||
public final class OmitOperationFactory implements JsonPatchOperationFactory | ||
{ | ||
private static final MessageBundle BUNDLE | ||
= MessageBundles.getBundle(JsonPatchMessages.class); | ||
|
||
public String getOperationName() | ||
{ | ||
return OmitOperation.OPERATION_NAME; | ||
} | ||
public Class<? extends JsonPatchOperation> getOperationClass() | ||
public JsonPatchOperation create(JsonNode node) | ||
throws JsonPatchException | ||
{ | ||
return OmitOperation.class; | ||
try { | ||
PathValueProperties properties = JacksonUtils.getReader().withType(PathValueProperties.class).readValue(node); | ||
return new JsonPatchOperationComposed(new OmitAction(properties), PathMissingPolicy.THROW); | ||
} catch (IOException e) { | ||
throw new JsonPatchException(BUNDLE.getMessage("jsonPatch.deserFailed"), e); | ||
} | ||
} | ||
} |
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 |
---|---|---|
|
@@ -5,5 +5,5 @@ | |
*/ | ||
public enum PathMissingPolicy | ||
{ | ||
THROW, SKIP | ||
THROW, SKIP, NOT_APPLICABLE | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/github/fge/jsonpatch/properties/JsonPatchOperationProperties.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,10 @@ | ||
package com.github.fge.jsonpatch.properties; | ||
|
||
import com.fasterxml.jackson.databind.JsonSerializable; | ||
|
||
/** | ||
* | ||
*/ | ||
interface JsonPatchOperationProperties extends JsonSerializable | ||
{ | ||
} |
87 changes: 87 additions & 0 deletions
87
src/main/java/com/github/fge/jsonpatch/properties/PathValueProperties.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,87 @@ | ||
package com.github.fge.jsonpatch.properties; | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.SerializerProvider; | ||
import com.fasterxml.jackson.databind.annotation.JsonSerialize; | ||
import com.fasterxml.jackson.databind.jsontype.TypeSerializer; | ||
import com.github.fge.jackson.jsonpointer.JsonPointer; | ||
import com.github.fge.jsonpatch.JsonPatchMessages; | ||
import com.github.fge.msgsimple.bundle.MessageBundle; | ||
import com.github.fge.msgsimple.load.MessageBundles; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* | ||
*/ | ||
public class PathValueProperties | ||
implements JsonPatchOperationProperties | ||
{ | ||
protected static final MessageBundle BUNDLE | ||
= MessageBundles.getBundle(JsonPatchMessages.class); | ||
|
||
protected final String op; | ||
|
||
protected final JsonPointer path; | ||
|
||
@JsonSerialize | ||
protected final JsonNode value; | ||
|
||
/** | ||
* @param op operation name | ||
* @param path affected path | ||
* @param value JSON value | ||
*/ | ||
@JsonCreator | ||
public PathValueProperties(@JsonProperty("op") final String op, | ||
@JsonProperty("path") final JsonPointer path, | ||
@JsonProperty("value") final JsonNode value) | ||
{ | ||
this.op = op; | ||
this.path = path; | ||
this.value = value.deepCopy(); | ||
} | ||
|
||
public String getOp() | ||
{ | ||
return op; | ||
} | ||
|
||
public JsonPointer getPath() | ||
{ | ||
return path; | ||
} | ||
|
||
public JsonNode getValue() | ||
{ | ||
return value.deepCopy(); | ||
} | ||
|
||
public final void serialize(final JsonGenerator jgen, | ||
final SerializerProvider provider) | ||
throws IOException, JsonProcessingException | ||
{ | ||
jgen.writeStartObject(); | ||
jgen.writeStringField("op", op); | ||
jgen.writeStringField("path", path.toString()); | ||
jgen.writeFieldName("value"); | ||
jgen.writeTree(value); | ||
jgen.writeEndObject(); | ||
} | ||
|
||
public final void serializeWithType(final JsonGenerator jgen, | ||
final SerializerProvider provider, final TypeSerializer typeSer) | ||
throws IOException, JsonProcessingException | ||
{ | ||
serialize(jgen, provider); | ||
} | ||
|
||
public final String toString() | ||
{ | ||
return "op: " + op + "; path: \"" + path + "\"; value: " + value; | ||
} | ||
} |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can this not move below this if/else and just return node in the else clause?