Skip to content

Commit

Permalink
Merge pull request #13 from port-labs/PORT-8223-control-the-payload-s…
Browse files Browse the repository at this point in the history
…pread-objects-operation

PORT-8223-control-the-payload-spread-objects-operation
  • Loading branch information
pazhersh authored May 22, 2024
2 parents 844daf1 + 3b86451 commit a9fec13
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
13 changes: 13 additions & 0 deletions lib/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,19 @@ const renderRecursively = (inputJson, template, execOptions = {}) => {
if (typeof template === 'object' && template !== null) {
return Object.fromEntries(
Object.entries(template).flatMap(([key, value]) => {
const SPREAD_KEYWORD = "spreadValue";
const keywordMatcher = `^\\{\\{\\s*${SPREAD_KEYWORD}\\(\\s*\\)\\s*\\}\\}$`; // matches {{ <Keyword>() }} with white spaces where you'd expect them

if (key.trim().match(keywordMatcher)) {
const evaluatedValue = renderRecursively(inputJson, value, execOptions);
if (typeof evaluatedValue !== "object") {
throw new Error(
`Evaluated value should be an object if the key is ${key}. Original value: ${value}, evaluated to: ${JSON.stringify(evaluatedValue)}`
);
}
return Object.entries(evaluatedValue);
}

const evaluatedKey = renderRecursively(inputJson, key, execOptions);
if (!['undefined', 'string'].includes(typeof evaluatedKey) && evaluatedKey !== null) {
throw new Error(
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "@port-labs/jq-node-bindings",
"version": "v0.0.12",
"version": "v0.0.13",
"description": "Node.js bindings for JQ",
"jq-node-bindings": "0.0.12",
"jq-node-bindings": "0.0.13",
"main": "lib/index.js",
"scripts": {
"configure": "node-gyp configure",
Expand Down
12 changes: 12 additions & 0 deletions test/template.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,14 @@ describe('template', () => {
expect(render({'{{""}}': 'bar'})).toEqual({});
expect(render({'{{\'\'}}': 'bar'})).toEqual({});
});
it('testing spread key', () => {
const json = { foo: "bar" };
const render = (input) => jq.renderRecursively(json, input);

expect(render({ "{{spreadValue()}}": { foo: "bar" } })).toEqual({foo: "bar"});
expect(render({ " {{ spreadValue( ) }} ": { foo: "bar" } })).toEqual({foo: "bar"});
expect(render({ "{{spreadValue()}}": "{{ . }}" })).toEqual({ foo: "bar" });
});
it('recursive templates should work', () => {
const json = { foo: 'bar', bar: 'foo' };
const render = (input) => jq.renderRecursively(json, input);
Expand Down Expand Up @@ -165,6 +173,10 @@ describe('template', () => {
expect(() => { jq.renderRecursively({foo: "bar"}, '{{.foo + 1}}', {throwOnError: true}) }).toThrow("jq: error: string (\"bar\") and number (1) cannot be added");
expect(() => { jq.renderRecursively({}, '{{foo}}/{{bar}}', {throwOnError: true}) }).toThrow("jq: compile error: foo/0 is not defined at <top-level>, line 1:");
expect(() => { jq.renderRecursively({}, '/{{foo}}/', {throwOnError: true}) }).toThrow("jq: compile error: foo/0 is not defined at <top-level>, line 1:");
expect(() => { jq.renderRecursively({}, { "{{ spreadValue() }}": "str" }, { throwOnError: true }) })
.toThrow('Evaluated value should be an object if the key is {{ spreadValue() }}. Original value: str, evaluated to: "str"');
expect(() => { jq.renderRecursively({}, { "{{ spreadValue() }}": "{{ \"str\" }}" }, { throwOnError: true }) })
.toThrow('Evaluated value should be an object if the key is {{ spreadValue() }}. Original value: {{ \"str\" }}, evaluated to: "str"');
})
})

0 comments on commit a9fec13

Please sign in to comment.