Skip to content

Instantly share code, notes, and snippets.

@ycombinator
Last active August 29, 2015 14:14
Show Gist options
  • Select an option

  • Save ycombinator/e8ab67bcbceb05de347f to your computer and use it in GitHub Desktop.

Select an option

Save ycombinator/e8ab67bcbceb05de347f to your computer and use it in GitHub Desktop.
--------------------------
Document (before): {}
Patch: [{"path": "/domains", "value": [{"domain": "addDomainList.com", "protocol": "http"}], "op": "add"}]
Document (after): {"domains": [{"domain": "addDomainList.com", "protocol": "http"}]}
--------------------------
Document (before): {"domains": []}
Patch: [{"path": "/domains", "value": [{"domain": "addDomainList.com", "protocol": "http"}], "op": "add"}]
Document (after): {"domains": [{"domain": "addDomainList.com", "protocol": "http"}]}
--------------------------
Document (before): {"domains": [{"domain": "existingDomain.com"}]}
Patch: [{"path": "/domains", "value": [{"domain": "addDomainList.com", "protocol": "http"}], "op": "add"}]
Document (after): {"domains": [{"domain": "addDomainList.com", "protocol": "http"}]}
--------------------------
Document (before): {}
Patch: [{"path": "/domains", "value": [{"domain": "addDomainList.com", "protocol": "http"}], "op": "replace"}]
ERROR!!! can't replace non-existent object 'domains'
--------------------------
Document (before): {"domains": []}
Patch: [{"path": "/domains", "value": [{"domain": "addDomainList.com", "protocol": "http"}], "op": "replace"}]
Document (after): {"domains": [{"domain": "addDomainList.com", "protocol": "http"}]}
--------------------------
Document (before): {"domains": [{"domain": "existingDomain.com"}]}
Patch: [{"path": "/domains", "value": [{"domain": "addDomainList.com", "protocol": "http"}], "op": "replace"}]
Document (after): {"domains": [{"domain": "addDomainList.com", "protocol": "http"}]}
--------------------------
Document (before): {}
Patch: [{"path": "/domains", "op": "remove"}]
ERROR!!! can't remove non-existent object 'domains'
--------------------------
Document (before): {"domains": []}
Patch: [{"path": "/domains", "op": "remove"}]
Document (after): {}
--------------------------
Document (before): {"domains": [{"domain": "existingDomain.com"}]}
Patch: [{"path": "/domains", "op": "remove"}]
Document (after): {}
from jsonpatch import JsonPatch, JsonPatchConflict
import json
def print_test(patch, doc):
patch = JsonPatch(patch)
try:
print "--------------------------"
print "Document (before): " + json.dumps(doc)
print "Patch: " + patch.to_string()
result = patch.apply(doc)
print "Document (after): " + json.dumps(result)
except JsonPatchConflict as e:
print "ERROR!!! " + str(e)
print_test([
{
"op": "add",
"path": "/domains",
"value": [
{
"domain": "addDomainList.com",
"protocol": "http"
}
]
}
], {})
print_test([
{
"op": "add",
"path": "/domains",
"value": [
{
"domain": "addDomainList.com",
"protocol": "http"
}
]
}
], {
"domains": []
})
print_test([
{
"op": "add",
"path": "/domains",
"value": [
{
"domain": "addDomainList.com",
"protocol": "http"
}
]
}
], {
"domains": [
{
"domain": "existingDomain.com"
}
]
})
print_test([
{
"op": "replace",
"path": "/domains",
"value": [
{
"domain": "addDomainList.com",
"protocol": "http"
}
]
}
], {})
print_test([
{
"op": "replace",
"path": "/domains",
"value": [
{
"domain": "addDomainList.com",
"protocol": "http"
}
]
}
], {
"domains": []
})
print_test([
{
"op": "replace",
"path": "/domains",
"value": [
{
"domain": "addDomainList.com",
"protocol": "http"
}
]
}
], {
"domains": [
{
"domain": "existingDomain.com"
}
]
})
print_test([
{
"op": "remove",
"path": "/domains"
}
], {})
print_test([
{
"op": "remove",
"path": "/domains"
}
], {
"domains": []
})
print_test([
{
"op": "remove",
"path": "/domains"
}
], {
"domains": [
{
"domain": "existingDomain.com"
}
]
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment