Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for or mappings #109

Merged
merged 1 commit into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions src/utils/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
IndexFilterExpression,
BlockExpression,
TokenType,
BinaryExpression,
} from '../types';
import { createBlockExpression, getLastElement } from './common';

Expand Down Expand Up @@ -255,6 +256,27 @@
return inputExpr;
}

function handleLastOutputPart(
flatMapping: FlatMappingAST,
currentOutputPropsAST: ObjectPropExpression[],
key: string,
) {
const outputPropAST = currentOutputPropsAST.find((prop) => prop.key === key);
if (!outputPropAST) {
currentOutputPropsAST.push({
type: SyntaxType.OBJECT_PROP_EXPR,
key,
value: refineLeafOutputPropAST(flatMapping.inputExpr),
} as ObjectPropExpression);
} else {
outputPropAST.value = {

Check warning on line 272 in src/utils/converter.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
type: SyntaxType.LOGICAL_OR_EXPR,

Check warning on line 273 in src/utils/converter.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
op: '||',

Check warning on line 274 in src/utils/converter.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
args: [outputPropAST.value, refineLeafOutputPropAST(flatMapping.inputExpr)],

Check warning on line 275 in src/utils/converter.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
} as BinaryExpression;

Check warning on line 276 in src/utils/converter.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

Check warning on line 277 in src/utils/converter.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

Check warning on line 277 in src/utils/converter.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

Check warning on line 277 in src/utils/converter.ts

View check run for this annotation

Codecov / codecov/patch

src/utils/converter.ts#L272-L277

Added lines #L272 - L277 were not covered by tests
}
koladilip marked this conversation as resolved.
Show resolved Hide resolved

function processFlatMappingPart(
flatMapping: FlatMappingAST,
partNum: number,
Expand All @@ -267,11 +289,7 @@
const key = outputPart.prop.value;

if (partNum === flatMapping.outputExpr.parts.length - 1) {
currentOutputPropsAST.push({
type: SyntaxType.OBJECT_PROP_EXPR,
key,
value: refineLeafOutputPropAST(flatMapping.inputExpr),
} as ObjectPropExpression);
handleLastOutputPart(flatMapping, currentOutputPropsAST, key);
return currentOutputPropsAST;
}

Expand Down
53 changes: 53 additions & 0 deletions test/scenarios/mappings/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,59 @@ export const data: Scenario[] = [
],
},
},
{
mappingsPath: 'or_mappings.json',
input: {
context: {
properties: {
name: 'John',
age: 30,
},
},
},
output: {
user: {
name: 'John',
age: 30,
},
},
},
{
mappingsPath: 'or_mappings.json',
input: {
properties: {
name: 'John Doe',
age: 30,
},
context: {
properties: {
name: 'John',
age: 30,
},
},
},
output: {
user: {
name: 'John Doe',
age: 30,
},
},
},
{
mappingsPath: 'or_mappings.json',
input: {
properties: {
name: 'John Doe',
age: 30,
},
},
output: {
user: {
name: 'John Doe',
age: 30,
},
},
},
{
mappingsPath: 'root_array_mappings.json',
input: [
Expand Down
18 changes: 18 additions & 0 deletions test/scenarios/mappings/or_mappings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[
{
"input": "$.properties.name",
"output": "$.user.name"
},
{
"input": "$.properties.age",
"output": "$.user.age"
},
{
"input": "$.context.properties.name",
"output": "$.user.name"
},
{
"input": "$.context.properties.age",
"output": "$.user.age"
}
]
Loading