-
Notifications
You must be signed in to change notification settings - Fork 450
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a merge function to the stdlib as described in: https://tools.ietf.org/html/rfc7386 https://tools.ietf.org/html/draft-ietf-appsawg-json-merge-patch-07 Signed-off-by: Mike Danese <[email protected]>
- Loading branch information
1 parent
e0b974b
commit 72e829e
Showing
2 changed files
with
122 additions
and
0 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
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,96 @@ | ||
/* | ||
Copyright 2016 Google Inc. All rights reserved. | ||
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. | ||
*/ | ||
|
||
# These cases are shamelessly lifted from | ||
# https://tools.ietf.org/html/draft-ietf-appsawg-json-merge-patch-07#appendix-A | ||
local cases = [ | ||
{ | ||
target: {"a":"b"}, | ||
patch: {"a":"c"}, | ||
expect: {"a":"c"} | ||
}, | ||
{ | ||
target: {"a":"b"}, | ||
patch: {"b":"c"}, | ||
expect: {"a":"b", "b":"c"} | ||
}, | ||
{ | ||
target: {"a":"b"}, | ||
patch: {"a":null}, | ||
expect: {"a":null}, # spec says to expect {} | ||
}, | ||
{ | ||
target: {"a":"b","b":"c"}, | ||
patch: {"a":null}, | ||
expect: {"a":null,"b":"c"}, # spec says to expect {"b":"c"} | ||
}, | ||
{ | ||
target: {"a":["b"]}, | ||
patch: {"a":"c"}, | ||
expect: {"a":"c"}, | ||
}, | ||
{ | ||
target: {"a":"c"}, | ||
patch: {"a":["b"]}, | ||
expect: {"a":["b"]}, | ||
}, | ||
{ | ||
target: {"a":{"b":"c"}}, | ||
patch: {"a":[1]}, | ||
expect: {"a":[1]}, | ||
}, | ||
{ | ||
target: ["a","b"], | ||
patch: ["c","d"], | ||
expect: ["c","d"], | ||
}, | ||
{ | ||
target: {"a":"b"}, | ||
patch: ["c"], | ||
expect: ["c"], | ||
}, | ||
{ | ||
target: {"a":"foo"}, | ||
patch: null, | ||
expect: null, | ||
}, | ||
{ | ||
target: {"a":"foo"}, | ||
patch: "bar", | ||
expect: "bar", | ||
}, | ||
{ | ||
target: {"e":null}, | ||
patch: {"a":1}, | ||
expect: {"e":null,"a":1}, | ||
}, | ||
{ | ||
target: [1,2], | ||
patch: {"a":"b","c":null}, | ||
expect: {"a":"b"}, | ||
}, | ||
{ | ||
target: {}, | ||
patch: {"a":{"bb":{"ccc":null}}}, | ||
expect: {"a":{"bb":{"ccc":null}}}, # spec says this should be {"a":{"bb":{}}} | ||
}, | ||
]; | ||
|
||
local results = | ||
[ std.assertEqual(std.merge(case.target, case.patch), case.expect) | ||
for case in cases ]; | ||
|
||
std.foldl(function(a,b) a && b, results, true) |