Skip to content

Commit

Permalink
Merge pull request #11 from jsonnet-libs/duologic/add_inspect_diff
Browse files Browse the repository at this point in the history
feat(inspect): add diff function
  • Loading branch information
Duologic authored Apr 17, 2023
2 parents c105709 + fa60256 commit 299e0e4
Show file tree
Hide file tree
Showing 3 changed files with 202 additions and 0 deletions.
38 changes: 38 additions & 0 deletions docs/inspect.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,48 @@ local inspect = import "github.com/jsonnet-libs/xtd/inspect.libsonnet"

## Index

* [`fn diff(input1, input2)`](#fn-diff)
* [`fn inspect(object, maxDepth)`](#fn-inspect)

## Fields

### fn diff

```ts
diff(input1, input2)
```

`diff` returns a JSON object describing the differences between two inputs. It
attemps to show diffs in nested objects and arrays too.

Simple example:

```jsonnet
local input1 = {
same: 'same',
change: 'this',
remove: 'removed',
};
local input2 = {
same: 'same',
change: 'changed',
add: 'added',
};
diff(input1, input2),
```

Output:
```json
{
"add +": "added",
"change ~": "~[ this , changed ]",
"remove -": "removed"
}
```


### fn inspect

```ts
Expand Down
96 changes: 96 additions & 0 deletions inspect.libsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,100 @@ local d = import 'doc-util/main.libsonnet';
std.objectFieldsAll(object),
{}
),

'#diff':: d.fn(
|||
`diff` returns a JSON object describing the differences between two inputs. It
attemps to show diffs in nested objects and arrays too.
Simple example:
```jsonnet
local input1 = {
same: 'same',
change: 'this',
remove: 'removed',
};
local input2 = {
same: 'same',
change: 'changed',
add: 'added',
};
diff(input1, input2),
```
Output:
```json
{
"add +": "added",
"change ~": "~[ this , changed ]",
"remove -": "removed"
}
```
|||,
[
d.arg('input1', d.T.any),
d.arg('input2', d.T.any),
]
),

diff(input1, input2)::
if input1 == input2
then ''
else if std.isArray(input1) && std.isArray(input2)
then
[
if input1[i] != input2[i]
then
this.diff(
input1[i],
input2[i]
)
else input2[i]
for i in std.range(0, std.length(input2) - 1)
if std.length(input1) > i
]
+ (if std.length(input1) < std.length(input2)
then [
'+ ' + input2[i]
for i in std.range(std.length(input1), std.length(input2) - 1)
]
else [])
+ (if std.length(input1) > std.length(input2)
then [
'- ' + input1[i]
for i in std.range(std.length(input2), std.length(input1) - 1)
]
else [])

else if std.isObject(input1) && std.isObject(input2)
then std.foldl(
function(acc, k)
acc + (
if k in input1 && input1[k] != input2[k]
then {
[k + ' ~']:
this.diff(
input1[k],
input2[k]
),
}
else if !(k in input1)
then {
[k + ' +']: input2[k],
}
else {}
),
std.objectFields(input2),
{},
)
+ {
[l + ' -']: input1[l]
for l in std.objectFields(input1)
if !(l in input2)
}

else '~[ %s ]' % std.join(' , ', [std.toString(input1), std.toString(input2)]),
}
68 changes: 68 additions & 0 deletions test.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,73 @@ local TestInspect =
: name('maxRecursionDepth');
true;


local TestDiff =
local name(case) = 'TestDiff:%s failed' % case;

assert (
xtd.inspect.diff('', '')
== ''
) : name('noDiff');

assert (
xtd.inspect.diff('string', true)
== '~[ string , true ]'
) : name('typeDiff');

assert (
local input1 = {
same: 'same',
change: 'this',
remove: 'removed',
};
local input2 = {
same: 'same',
change: 'changed',
add: 'added',
};
xtd.inspect.diff(input1, input2)
== {
'add +': 'added',
'change ~': '~[ this , changed ]',
'remove -': 'removed',
}
) : name('objectDiff');

assert (
local input1 = [
'same',
'this',
[
'same',
'this',
],
'remove',
];
local input2 = [
'same',
'changed',
[
'same',
'changed',
'added',
],
];
xtd.inspect.diff(input1, input2)
== [
'same',
'~[ this , changed ]',
[
'same',
'~[ this , changed ]',
'+ added',
],
'- remove',
]
) : name('arrayDiff');

true;

local TestIsLeapYear =
local name(case) = 'TestIsLeapYear:%s failed' % case;

Expand Down Expand Up @@ -249,6 +316,7 @@ true
&& TestEncodeQuery
&& TestCamelCaseSplit
&& TestInspect
&& TestDiff
&& TestIsLeapYear
&& TestDayOfWeek
&& TestDayOfYear

0 comments on commit 299e0e4

Please sign in to comment.