Skip to content

Commit

Permalink
fix: root mappings (#86)
Browse files Browse the repository at this point in the history
* feat: add support for root mappings

* feat: add support for root mappings

* feat: add support for root mappings
  • Loading branch information
koladilip authored Jun 13, 2024
1 parent 4c6bb8e commit cc4893f
Show file tree
Hide file tree
Showing 13 changed files with 201 additions and 198 deletions.
2 changes: 1 addition & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ coverage
test
commitlint.config.js
jest.config.ts
vite.config.ts
vite.config.mts
app.ts
2 changes: 1 addition & 1 deletion commitlint.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export default {
module.exports = {
extends: ['@commitlint/config-conventional'],
};
247 changes: 113 additions & 134 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"description": "A library for evaluating JSON template expressions.",
"main": "build/index.js",
"types": "build/index.d.ts",
"type": "module",
"keywords": [
"json",
"jsonpath",
Expand All @@ -25,8 +24,8 @@
},
"devDependencies": {
"@babel/eslint-parser": "^7.19.1",
"@commitlint/cli": "^17.4.4",
"@commitlint/config-conventional": "^17.4.4",
"@commitlint/cli": "^17.8.1",
"@commitlint/config-conventional": "^17.8.1",
"@types/jest": "^29.4.0",
"@types/mocha": "^10.0.1",
"@types/node": "^18.14.6",
Expand Down
21 changes: 16 additions & 5 deletions src/utils/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,17 +204,28 @@ function processFlatMappingPart(
return objectExpr.props;
}

function processFlatMapping(flatMapping, currentOutputPropsAST) {
for (let i = 0; i < flatMapping.outputExpr.parts.length; i++) {
currentOutputPropsAST = processFlatMappingPart(flatMapping, i, currentOutputPropsAST);
}
}
/**
* Convert Flat to Object Mappings
*/
export function convertToObjectMapping(flatMappingAST: FlatMappingAST[]): ObjectExpression {
export function convertToObjectMapping(
flatMappingASTs: FlatMappingAST[],
): ObjectExpression | PathExpression {
const outputAST: ObjectExpression = createObjectExpression();

for (const flatMapping of flatMappingAST) {
let currentOutputPropsAST = outputAST.props;
for (let i = 0; i < flatMapping.outputExpr.parts.length; i++) {
currentOutputPropsAST = processFlatMappingPart(flatMapping, i, currentOutputPropsAST);
for (let i = 0; i < flatMappingASTs.length; i++) {
const flatMapping = flatMappingASTs[i];
if (flatMapping.outputExpr.parts.length === 0) {
if (outputAST.props.length === 0 && i === flatMappingASTs.length - 1) {
return flatMapping.inputExpr;
}
throw new Error(`Invalid output mapping: ${flatMapping.output}`);
}
processFlatMapping(flatMapping, outputAST.props);
}

return outputAST;
Expand Down
71 changes: 26 additions & 45 deletions test/scenarios/mappings/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,7 @@ const input = {
};
export const data: Scenario[] = [
{
containsMappings: true,
templatePath: 'all_features.json',
options: {
defaultPathType: PathType.JSON,
},
mappingsPath: 'all_features.json',
input,
output: {
events: [
Expand Down Expand Up @@ -115,11 +111,8 @@ export const data: Scenario[] = [
},
},
{
containsMappings: true,
templatePath: 'filters.json',
options: {
defaultPathType: PathType.JSON,
},
mappingsPath: 'filters.json',

input,
output: {
items: [
Expand All @@ -137,11 +130,7 @@ export const data: Scenario[] = [
},
},
{
containsMappings: true,
templatePath: 'index_mappings.json',
options: {
defaultPathType: PathType.JSON,
},
mappingsPath: 'index_mappings.json',
input,
output: {
events: [
Expand All @@ -157,27 +146,25 @@ export const data: Scenario[] = [
},
},
{
containsMappings: true,
templatePath: 'invalid_array_mappings.json',
options: {
defaultPathType: PathType.JSON,
},
mappingsPath: 'invalid_array_mappings.json',
error: 'Failed to process output mapping',
},
{
containsMappings: true,
templatePath: 'invalid_object_mappings.json',
options: {
defaultPathType: PathType.JSON,
},
mappingsPath: 'invalid_object_mappings.json',
error: 'Invalid object mapping',
},
{
containsMappings: true,
templatePath: 'mappings_with_root_fields.json',
options: {
defaultPathType: PathType.JSON,
},
description: 'Root mapping is used after other mappings',
mappingsPath: 'invalid_root_mapping1.json',
error: 'Invalid output mapping',
},
{
description: 'Root mapping is used before other mappings',
mappingsPath: 'invalid_root_mapping2.json',
error: 'Invalid output mapping',
},
{
mappingsPath: 'mappings_with_root_fields.json',
input,
output: {
items: [
Expand Down Expand Up @@ -205,11 +192,7 @@ export const data: Scenario[] = [
},
},
{
containsMappings: true,
templatePath: 'nested_mappings.json',
options: {
defaultPathType: PathType.JSON,
},
mappingsPath: 'nested_mappings.json',
input,
output: {
items: [
Expand Down Expand Up @@ -256,11 +239,8 @@ export const data: Scenario[] = [
},
},
{
containsMappings: true,
templatePath: 'object_mappings.json',
options: {
defaultPathType: PathType.JSON,
},
mappingsPath: 'object_mappings.json',

input: {
traits1: {
name: 'John Doe',
Expand Down Expand Up @@ -291,11 +271,12 @@ export const data: Scenario[] = [
},
},
{
containsMappings: true,
templatePath: 'transformations.json',
options: {
defaultPathType: PathType.JSON,
},
mappingsPath: 'only_root_mapping.json',
input: { a: 1 },
output: { a: 1 },
},
{
mappingsPath: 'transformations.json',
input,
output: {
items: [
Expand Down
10 changes: 10 additions & 0 deletions test/scenarios/mappings/invalid_root_mapping1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
{
"input": "$.a",
"output": "$.b"
},
{
"input": "$",
"output": "$"
}
]
10 changes: 10 additions & 0 deletions test/scenarios/mappings/invalid_root_mapping2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
{
"input": "$",
"output": "$"
},
{
"input": "$.a",
"output": "$.b"
}
]
6 changes: 6 additions & 0 deletions test/scenarios/mappings/only_root_mapping.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{
"input": "$",
"output": "$"
}
]
6 changes: 3 additions & 3 deletions test/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ export type Scenario = {
description?: string;
input?: unknown;
templatePath?: string;
mappingsPath?: string;
template?: string;
containsMappings?: true;
options?: EngineOptions;
bindings?: Record<string, unknown> | undefined;
output?: unknown;
Expand All @@ -17,8 +17,8 @@ export namespace Scenario {
if (scenario.templatePath) {
return scenario.templatePath;
}
if (scenario.containsMappings) {
return 'mappings.json';
if (scenario.mappingsPath) {
return scenario.mappingsPath;
}
return 'template.jt';
}
Expand Down
13 changes: 10 additions & 3 deletions test/utils/scenario.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,20 @@ function getTemplate(scenarioDir: string, scenario: Scenario): string {
const templatePath = join(scenarioDir, Scenario.getTemplatePath(scenario));
return readFileSync(templatePath, 'utf-8');
}

function getDefaultPathType(scenario: Scenario): PathType {
return scenario.mappingsPath ? PathType.JSON : PathType.SIMPLE;
}

function initializeScenario(scenarioDir: string, scenario: Scenario) {
scenario.options = scenario.options || {};
scenario.options.defaultPathType = scenario.options.defaultPathType || PathType.SIMPLE;
scenario.options.defaultPathType =
scenario.options.defaultPathType || getDefaultPathType(scenario);
let template = scenario.template ?? getTemplate(scenarioDir, scenario);
if (scenario.containsMappings) {
if (scenario.mappingsPath) {
template = JsonTemplateEngine.convertMappingsToTemplate(
JSON.parse(template) as FlatMappingPaths[],
scenario.options,
);
}
scenario.template = JsonTemplateEngine.reverseTranslate(
Expand All @@ -24,7 +31,7 @@ function initializeScenario(scenarioDir: string, scenario: Scenario) {

export function evaluateScenario(scenarioDir: string, scenario: Scenario): any {
initializeScenario(scenarioDir, scenario);
if (scenario.containsMappings) {
if (scenario.mappingsPath) {
return JsonTemplateEngine.evaluateAsSync(
scenario.template,
scenario.options,
Expand Down
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */

/* Language and Environment */
"target": "es2021" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
"target": "ESNext" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
// "jsx": "preserve", /* Specify what JSX code is generated. */
"experimentalDecorators": true /* Enable experimental support for TC39 stage 2 draft decorators. */,
Expand All @@ -25,7 +25,7 @@
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */

/* Modules */
"module": "commonjs" /* Specify what module code is generated. */,
"module": "ESNext" /* Specify what module code is generated. */,
"rootDir": "./src" /* Specify the root folder within your source files. */,
"moduleResolution": "node" /* Specify how TypeScript looks up a file from a given module specifier. */,
"baseUrl": "./" /* Specify the base directory to resolve non-relative module names. */,
Expand Down
2 changes: 1 addition & 1 deletion vite.config.ts → vite.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default defineConfig({
lib: {
entry: resolve(__dirname, 'src/index.ts'),
formats: ['es'],
fileName: 'json-template',
fileName: () => 'json-template.js',
},
outDir: 'build',
},
Expand Down

0 comments on commit cc4893f

Please sign in to comment.