-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TS - add jsonpatch, jsonpath and jsonpointer functions
- Loading branch information
Showing
10 changed files
with
340 additions
and
10 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
25 changes: 25 additions & 0 deletions
25
javascript/json-transform/src/__tests__/functions/TransformerFunctionJsonPatch.test.ts
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,25 @@ | ||
import { describe, test } from "vitest"; | ||
import { assertTransformation } from "../BaseTransformationTest"; | ||
import { BigDecimal } from "../../functions/common/FunctionHelpers"; | ||
|
||
describe("TransformerFunctionJsonPatch", () => { | ||
test("add", async () => { | ||
await assertTransformation( | ||
{ | ||
a: { | ||
b: "c", | ||
}, | ||
}, | ||
{ | ||
$$jsonpatch: "$", | ||
ops: [{ op: "add", path: "/a/d", value: "e" }], | ||
}, | ||
{ | ||
a: { | ||
b: "c", | ||
d: "e", | ||
}, | ||
}, | ||
); | ||
}); | ||
}); |
41 changes: 41 additions & 0 deletions
41
javascript/json-transform/src/__tests__/functions/TransformerFunctionJsonPath.test.ts
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,41 @@ | ||
import { describe, test } from "vitest"; | ||
import { assertTransformation } from "../BaseTransformationTest"; | ||
import { BigDecimal } from "../../functions/common/FunctionHelpers"; | ||
|
||
describe("TransformerFunctionJsonPath", () => { | ||
test("yoDawg", async () => { | ||
await assertTransformation( | ||
{ | ||
path: "$.path", | ||
}, | ||
"$$jsonpath($.path):$", | ||
"$.path", | ||
); | ||
}); | ||
|
||
test("inArray", async () => { | ||
await assertTransformation( | ||
{ | ||
arr: [null, "boo"], | ||
}, | ||
"$$jsonpath('\\\\$.arr[1]'):$", | ||
"boo", | ||
); | ||
}); | ||
|
||
test("multipleResults", async () => { | ||
await assertTransformation( | ||
[ | ||
{ id: 1, active: true }, | ||
{ id: 3, active: false }, | ||
{ id: 4, active: true }, | ||
{ id: 5, active: false }, | ||
], | ||
"$$jsonpath('\\\\$[?(@.active == true)]'):$", | ||
[ | ||
{ id: 1, active: true }, | ||
{ id: 4, active: true }, | ||
], | ||
); | ||
}); | ||
}); |
68 changes: 68 additions & 0 deletions
68
javascript/json-transform/src/__tests__/functions/TransformerFunctionJsonPointer.test.ts
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,68 @@ | ||
import { describe, test } from "vitest"; | ||
import { assertTransformation } from "../BaseTransformationTest"; | ||
import { BigDecimal } from "../../functions/common/FunctionHelpers"; | ||
|
||
describe("TransformerFunctionJsonPointer", () => { | ||
test("get", async () => { | ||
await assertTransformation( | ||
{ | ||
b: [ | ||
"c", | ||
"d", | ||
{ | ||
e: "Hello", | ||
}, | ||
], | ||
}, | ||
"$$jsonpointer(GET,/b/2/e):$", | ||
"Hello", | ||
); | ||
}); | ||
|
||
test("getYoDawg", async () => { | ||
await assertTransformation( | ||
{ | ||
pointer: "/pointer", | ||
}, | ||
"$$jsonpointer(get,$.pointer):$", | ||
"/pointer", | ||
); | ||
}); | ||
|
||
test("set", async () => { | ||
await assertTransformation( | ||
{ | ||
b: [ | ||
"c", | ||
"d", | ||
{ | ||
e: "Hello", | ||
}, | ||
], | ||
}, | ||
"$$jsonpointer(SET,/b,'$.b[2]'):$", | ||
{ | ||
b: { | ||
e: "Hello", | ||
}, | ||
}, | ||
); | ||
}); | ||
test("remove", async () => { | ||
await assertTransformation( | ||
{ | ||
b: [ | ||
"c", | ||
"d", | ||
{ | ||
e: "Hello", | ||
}, | ||
], | ||
}, | ||
"$$jsonpointer(REMOVE,/b/2):$", | ||
{ | ||
b: ["c", "d"], | ||
}, | ||
); | ||
}); | ||
}); |
31 changes: 31 additions & 0 deletions
31
javascript/json-transform/src/functions/TransformerFunctionJsonPatch.ts
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,31 @@ | ||
import JsonPatch from "fast-json-patch"; | ||
import TransformerFunction from "./common/TransformerFunction"; | ||
import { ArgType } from "./common/ArgType"; | ||
import { FunctionDescription } from "./common/FunctionDescription"; | ||
import FunctionContext from "./common/FunctionContext"; | ||
|
||
const DESCRIPTION: FunctionDescription = { | ||
aliases: ["jsonpatch"], | ||
inputType: ArgType.Any, | ||
description: "", | ||
arguments: { | ||
ops: { type: ArgType.Array, position: 0, required: true, description: "A list of operations" }, | ||
}, | ||
outputType: ArgType.Any, | ||
}; | ||
class TransformerFunctionJsonPatch extends TransformerFunction { | ||
constructor() { | ||
super(DESCRIPTION); | ||
} | ||
|
||
override async apply(context: FunctionContext): Promise<any> { | ||
const source = await context.getJsonElement(null); | ||
if (source == null) { | ||
return null; | ||
} | ||
const ops = (await context.getJsonArray("ops")) ?? []; | ||
return JsonPatch.applyPatch(source, ops, true).newDocument; | ||
} | ||
} | ||
|
||
export default TransformerFunctionJsonPatch; |
65 changes: 65 additions & 0 deletions
65
javascript/json-transform/src/functions/TransformerFunctionJsonPath.ts
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,65 @@ | ||
import jp from "jsonpath"; | ||
import TransformerFunction from "./common/TransformerFunction"; | ||
import { ArgType } from "./common/ArgType"; | ||
import { FunctionDescription } from "./common/FunctionDescription"; | ||
import FunctionContext from "./common/FunctionContext"; | ||
import DocumentContext from "../DocumentContext"; | ||
|
||
const DESCRIPTION: FunctionDescription = { | ||
aliases: ["jsonpath"], | ||
inputType: ArgType.Any, | ||
description: "", | ||
arguments: { | ||
path: { type: ArgType.String, position: 0, required: true, description: "JSONPath expression" }, | ||
options: { | ||
type: ArgType.ArrayOfString, | ||
position: 1, | ||
defaultIsNull: true, | ||
description: | ||
"A list of options [by jayway](https://github.com/json-path/JsonPath?tab=readme-ov-file#tweaking-configuration)", | ||
}, | ||
}, | ||
outputType: ArgType.Any, | ||
}; | ||
class TransformerFunctionJsonPath extends TransformerFunction { | ||
constructor() { | ||
super(DESCRIPTION); | ||
} | ||
|
||
override async apply(context: FunctionContext): Promise<any> { | ||
const source = await context.getJsonElement(null); | ||
if (source == null) { | ||
return null; | ||
} | ||
const path = await context.getString("path"); | ||
if (path == null) { | ||
return null; | ||
} | ||
|
||
const optionsArray = await context.getJsonArray("options"); | ||
if (optionsArray != null && optionsArray.length > 0) { | ||
const options = optionsArray.reduce( | ||
(acc, val) => { | ||
acc[val] = true; | ||
return acc; | ||
}, | ||
{} as Record<string, true>, | ||
); | ||
|
||
if (options.ALWAYS_RETURN_LIST) { | ||
try { | ||
return jp.query(source, path); | ||
} catch (e: any) { | ||
if (options.SUPPRESS_EXCEPTIONS) { | ||
return []; | ||
} | ||
throw e; | ||
} | ||
} | ||
} | ||
|
||
return new DocumentContext(source).read(path); | ||
} | ||
} | ||
|
||
export default TransformerFunctionJsonPath; |
Oops, something went wrong.