Skip to content
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

Expose JSON Patch operations as public API #1089

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

ikhoon
Copy link
Contributor

@ikhoon ikhoon commented Jan 14, 2025

Motivation:

JSON Patch syntax is not easy to write in string. An API will prevent users from writing raw operations in string and help to build JSON Patch operations type-safely.

Modifications:

  • Move JSON Patch operations to common.jsonpatch from internal.jsonpatch.
    • Fix Change.ofJsonPatch() to create JSON patch the JsonPatchOperations.
    • Add factory methods to JsonPatchOperation.
  • Create JsonPatchConflictException and TextPatchConflitException to distinguish exceptions easily and provide a detailed message.
  • Fix GitRepository to allow an empty message JSON patch because test or testAbsent do not have changes.
    • In addition, JSON patch operations have their own validation mechanism.

Result:

// Add
AddOperation add = JsonPatchOperation.add(JsonPointer.compile("/b"), new IntNode(2));
// Copy
CopyOperation copy = JsonPatchOperation.copy(JsonPointer.compile("/a"),
                                             JsonPointer.compile("/b"));
// Move
MoveOperation move = JsonPatchOperation.move(JsonPointer.compile("/a"),
                                             JsonPointer.compile("/b"));
// Remove
RemoveOperation remove = JsonPatchOperation.remove(JsonPointer.compile("/a"));
// Remove if exists 
RemoveIfExistsOperation removeIfExists = JsonPatchOperation.removeIfExists(JsonPointer.compile("/a"));
// Replace
ReplaceOperation replace = JsonPatchOperation.replace(JsonPointer.compile("/a"), new IntNode(2));
// Safe replace (aka. compare and set)
SafeReplaceOperation safeReplace =
        JsonPatchOperation.safeReplace(JsonPointer.compile("/a"), new IntNode(1), new IntNode(2));
// Test if a value exists in a node
TestOperation test = JsonPatchOperation.test(JsonPointer.compile("/a"), new IntNode(1));
// test absent
TestAbsenceOperation testAbsence = JsonPatchOperation.testAbsence(JsonPointer.compile("/b"));

The operations above can be used to create a change and push to a Central Dogma server.

// Create a change with JSON patch operations
Change<JsonNode> change = Change.ofJsonPatch("/a.json",
                                             List.of(add, move, remove, safeReplace, ...));
repository.commit("json patch operations", change)
          .push()
          .join();

/**
* Returns the name of the repository.
*/
public String repositoryName() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The getters will be useful when logging.

@ikhoon ikhoon marked this pull request as ready for review January 17, 2025 10:46
@ikhoon ikhoon added this to the 0.74.0 milestone Jan 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Expose JsonPatch as public API
1 participant