Skip to content

Commit 60f0d4b

Browse files
committed
js-core-1.5.0 - fix critical bug in v1.4.0
1 parent cc94b3a commit 60f0d4b

8 files changed

Lines changed: 186 additions & 126 deletions

File tree

javascript/json-transform-core/package-lock.json

Lines changed: 9 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

javascript/json-transform-core/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@nlighten/json-transform-core",
33
"description": "Core types and utilities for handling JSON transformers",
4-
"version": "1.4.0",
4+
"version": "1.5.0",
55
"main": "dist/json-transform-core.js",
66
"umd:main": "dist/json-transform-core.umd.js",
77
"module": "dist/json-transform-core.module.js",
@@ -40,10 +40,10 @@
4040
},
4141
"homepage": "https://github.com/nlighten-oss/json-transform#readme",
4242
"peerDependencies": {
43-
"@nlighten/json-schema-utils": "^1.0.2"
43+
"@nlighten/json-schema-utils": "^1.0.3"
4444
},
4545
"devDependencies": {
46-
"@nlighten/json-schema-utils": "^1.0.2",
46+
"@nlighten/json-schema-utils": "^1.0.3",
4747
"microbundle": "^0.15.1",
4848
"prettier": "3.1.1",
4949
"typescript": "^5.3.3",

javascript/json-transform-core/src/ParseContext.ts

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@ import { ContextVariablesSchemas } from "./functions/context";
33
import { FunctionDescriptor } from "./functions/types";
44

55
export class ParseContext {
6-
paths?: Record<string, TypeSchema> = {};
7-
additionalContext?: Record<string, TypeSchema>;
8-
knownVariables: Set<string>;
6+
private readonly paths?: Record<string, TypeSchema>;
7+
private readonly additionalContext?: Record<string, TypeSchema>;
8+
private knownVariables: Set<string>;
99

1010
constructor(
1111
paths?: Record<string, TypeSchema>,
1212
additionalContext?: Record<string, TypeSchema>,
1313
previousPaths?: string[],
1414
) {
1515
this.paths = paths;
16-
this.additionalContext = additionalContext ?? ContextVariablesSchemas;
16+
this.additionalContext = additionalContext;
1717
this.knownVariables = new Set();
1818
if (paths) {
1919
for (const path in paths) {
@@ -35,14 +35,46 @@ export class ParseContext {
3535
}
3636
}
3737

38+
hasPaths() {
39+
return Boolean(this.paths);
40+
}
41+
42+
hasPath(path: string) {
43+
return typeof this.paths?.[path] !== "undefined";
44+
}
45+
46+
/**
47+
* If you are about to change the result, use this and not 'resolve()'
48+
* @param path
49+
*/
50+
getPath(path: string) {
51+
return this.paths?.[path];
52+
}
53+
54+
setPath(path: string, type: TypeSchema) {
55+
if (!this.paths) return;
56+
this.paths[path] = type;
57+
const v = path.split(/[.[]/, 1)[0];
58+
this.knownVariables.add(v);
59+
}
60+
61+
removePaths(paths: string[], variableToRemove?: string) {
62+
for (const path of paths) {
63+
delete this.paths?.[path];
64+
}
65+
if (variableToRemove) {
66+
this.knownVariables.delete(variableToRemove);
67+
}
68+
}
69+
3870
resolve(key: string) {
39-
return this.additionalContext?.[key] ?? this.paths?.[key];
71+
return ContextVariablesSchemas[key] ?? this.additionalContext?.[key] ?? this.paths?.[key];
4072
}
4173

42-
isJsonPathReference(path: any): boolean {
43-
if (typeof path !== "string") return false;
74+
isReferencingKnownVariable(path: any): boolean {
75+
if (typeof path !== "string" || path.startsWith("$$")) return false;
4476
const v = path.split(/[.[]/, 1)[0];
45-
return path === v || path.startsWith(v + ".") || path.startsWith(v + "[");
77+
return this.knownVariables.has(v) && (path === v || path.startsWith(v + ".") || path.startsWith(v + "["));
4678
}
4779
}
4880

javascript/json-transform-core/src/functions/functionsParser.ts

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class FunctionsParser {
9494
this.handleClientFunction = handler;
9595
}
9696

97-
get(name: string) {
97+
get(name: string, args?: Record<string, any>) {
9898
return this.clientFunctions?.[name] ?? embeddedDefinitions[name as EmbeddedTransformerFunction];
9999
}
100100

@@ -134,14 +134,7 @@ class FunctionsParser {
134134
if (func.overrides || callback) {
135135
const argsWithoutParenthesis = m[3];
136136
const args = parseArgs(func, argsWithoutParenthesis);
137-
if (func.overrides) {
138-
for (const override of func.overrides) {
139-
if (override.if.every(c => args[c.argument]?.toString().toUpperCase() === c.equals)) {
140-
func = override.then; // replace func instance based on args
141-
break;
142-
}
143-
}
144-
}
137+
func = getOverriddenFunction(func, args);
145138
if (callback) {
146139
const inlineFunctionValue = m[5];
147140
callback(funcName, func, inlineFunctionValue, args);
@@ -160,15 +153,7 @@ class FunctionsParser {
160153
const match = this.matchObject(value, true);
161154
if (match) return match;
162155
}
163-
let func = this.get(funcName);
164-
if (func.overrides) {
165-
for (const override of func.overrides) {
166-
if (override.if.every(c => data[c.argument]?.toString().toUpperCase() === c.equals)) {
167-
func = override.then; // replace func instance based on args
168-
break;
169-
}
170-
}
171-
}
156+
const func = getOverriddenFunction(this.get(funcName), data);
172157
return {
173158
name: funcName,
174159
func,
@@ -214,3 +199,17 @@ export const parseArgs = (func: FunctionDescriptor, args?: string) => {
214199
{} as Record<string, any>, // we can't really infer type, and strings are the only ones that matter anyway
215200
);
216201
};
202+
203+
export const getOverriddenFunction = (func: FunctionDescriptor, args?: Record<string, any>) => {
204+
if (!func.overrides || !args) return func;
205+
for (const override of func.overrides) {
206+
if (
207+
override.if.every(
208+
c => (args[c.argument] ?? override.then.defaultValues?.[c.argument])?.toString().toUpperCase() === c.equals,
209+
)
210+
) {
211+
return override.then; // replace func instance based on args
212+
}
213+
}
214+
return func;
215+
};

javascript/json-transform-core/src/functions/parseSchemas.ts

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
1-
import { JSONSchemaUtils, TypeSchema } from "@nlighten/json-schema-utils";
2-
import { FunctionDescriptor } from "./types";
1+
import { JSONSchemaUtils } from "@nlighten/json-schema-utils";
2+
import { Argument, FunctionDescriptor } from "./types";
3+
4+
const getDefaultValues = (args?: Argument[]) => {
5+
const defaultValues: Record<string, any> = {};
6+
if (Array.isArray(args)) {
7+
for (let i = 0; i < args.length; i++) {
8+
const arg = args[i];
9+
if (arg.default !== undefined) {
10+
defaultValues[arg.name] = arg.default;
11+
}
12+
}
13+
}
14+
return defaultValues;
15+
};
316

417
function parseSchemas<T extends string>(
518
functions: Record<T, FunctionDescriptor>,
@@ -10,16 +23,23 @@ function parseSchemas<T extends string>(
1023
if (func.outputSchema) {
1124
func.parsedOutputSchema = JSONSchemaUtils.parse(func.outputSchema);
1225
}
26+
func.defaultValues = getDefaultValues(func.arguments);
27+
1328
if (custom) {
1429
func.custom = true;
1530
}
1631
if (Array.isArray(func.overrides)) {
1732
for (let i = 0; i < func.overrides.length; i++) {
1833
const schemaOverride = func.overrides[i].then.outputSchema;
1934
func.overrides[i] = {
20-
...func,
21-
overrides: null,
22-
parsedOutputSchema: schemaOverride && JSONSchemaUtils.parse(schemaOverride),
35+
if: func.overrides[i].if,
36+
then: {
37+
...func,
38+
...func.overrides[i].then,
39+
overrides: undefined,
40+
parsedOutputSchema: schemaOverride && JSONSchemaUtils.parse(schemaOverride),
41+
defaultValues: getDefaultValues(func.overrides[i].then.arguments),
42+
},
2343
};
2444
}
2545
}

javascript/json-transform-core/src/functions/types.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export enum EmbeddedTransformerFunction {
8686
export const EmbeddedTransformerFunctions = Object.keys(EmbeddedTransformerFunction) as EmbeddedTransformerFunction[];
8787

8888
export type Argument = {
89-
name?: string;
89+
name: string;
9090
type: string;
9191
description?: string;
9292
enum?: string[];
@@ -108,7 +108,7 @@ export type ConditionalOverrides = {
108108

109109
export type FunctionDescriptor = {
110110
aliasTo?: string;
111-
inputSchema?: Argument;
111+
inputSchema?: Omit<Argument, "name">;
112112
arguments?: Argument[];
113113
description: string;
114114
notes?: string;
@@ -123,4 +123,5 @@ export type FunctionDescriptor = {
123123
custom?: boolean;
124124
/** (when outputSchema) Parsed in advance to get all possible paths */
125125
parsedOutputSchema?: ParsedSchema;
126+
defaultValues?: Record<string, any>;
126127
};

javascript/json-transform-core/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export {
44
getFunctionObjectSignature,
55
functionsParser,
66
parseArgs,
7+
getOverriddenFunction,
78
} from "./functions/functionsParser";
89
export {
910
type Argument,

0 commit comments

Comments
 (0)