Skip to content

Commit

Permalink
stdlib: implement json merge
Browse files Browse the repository at this point in the history
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
mikedanese committed Jan 8, 2016
1 parent e0b974b commit 72e829e
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 0 deletions.
26 changes: 26 additions & 0 deletions stdlib/std.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,32 @@ limitations under the License.
aux(a, b, i, j + 1, acc) tailstrict;
aux(a, b, 0, 0, []) tailstrict,

merge(target, patch)::
if std.type(patch) == "object" then
local _1 =
if std.type(target) != "object" then
{}
else
target;
local _2 = _1 +
{ [k]: null
for k in std.objectFields(patch)
if patch[k] == null &&
std.objectHas(_1, k)};
local _3 = _2 +
{ [k]: patch[k]
for k in std.objectFields(patch)
if patch[k] != null &&
!std.objectHas(_1, k)};
local _4 = _3 +
{ [k]: std.merge(_1[k], patch[k]) tailstrict
for k in std.objectFields(patch)
if patch[k] != null &&
std.objectHas(_1, k)};
_4
else
patch,

objectFields(o)::
std.objectFieldsEx(o, false),

Expand Down
96 changes: 96 additions & 0 deletions test_suite/merge.jsonnet
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)

0 comments on commit 72e829e

Please sign in to comment.