@narrative.io/data-collaboration-sdk-ts 2.5.0 → 2.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/nql/Ast.d.ts +120 -4
- package/build/nql/Ast.js +36 -0
- package/build/nql/AstParser.d.ts +4 -3
- package/build/nql/AstParser.js +31 -3
- package/build/nql/NqlBuilder.js +7 -4
- package/build/nql/Template.d.ts +23 -0
- package/build/nql/Template.js +170 -0
- package/package.json +2 -2
package/build/nql/Ast.d.ts
CHANGED
|
@@ -1,3 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A mapping of AST (Abstract Syntax Tree) node types to their corresponding TypeScript types.
|
|
3
|
+
* This object acts as a type registry for the various components of the NQL (Notation Query Language) AST.
|
|
4
|
+
*/
|
|
5
|
+
declare const NqlAstTypes: {
|
|
6
|
+
agg_function: NqlAggFunction;
|
|
7
|
+
binary_op: NqlBinaryOp;
|
|
8
|
+
case: NqlCase;
|
|
9
|
+
function: NqlFunction;
|
|
10
|
+
literal: NqlLit<NqlSimpleType>;
|
|
11
|
+
string_literal: NqlLit<NqlStringType>;
|
|
12
|
+
long_literal: NqlLit<NqlLongType>;
|
|
13
|
+
double_literal: NqlLit<NqlDoubleType>;
|
|
14
|
+
boolean_literal: NqlLit<NqlBooleanType>;
|
|
15
|
+
timestamp_literal: NqlLit<NqlTimestampType>;
|
|
16
|
+
column: NqlIdentColumn;
|
|
17
|
+
table: NqlIdentTable;
|
|
18
|
+
join: NqlJoin;
|
|
19
|
+
type_spec: NqlTypeSpec;
|
|
20
|
+
operator: NqlOp;
|
|
21
|
+
window: NqlWindow;
|
|
22
|
+
unknown: NqlUnknown;
|
|
23
|
+
select: NqlSelect;
|
|
24
|
+
create_materialized_view: NqlCreateMaterializedView;
|
|
25
|
+
explain: NqlExplain;
|
|
26
|
+
array: NqlArrayType;
|
|
27
|
+
binary: NqlBinaryType;
|
|
28
|
+
bool: NqlBooleanType;
|
|
29
|
+
double: NqlDoubleType;
|
|
30
|
+
long: NqlLongType;
|
|
31
|
+
interval: NqlIntervalType;
|
|
32
|
+
object: NqlObjectType;
|
|
33
|
+
other: NqlOtherType;
|
|
34
|
+
string: NqlStringType;
|
|
35
|
+
timestamp: NqlTimestampType;
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* Represents the logical operators that can be used to combine constraints in a NQL query.
|
|
39
|
+
*/
|
|
40
|
+
type NqlTemplateConstraintLogicalOperator = "ANY_OF" | "ALL_OF" | "NONE_OF";
|
|
41
|
+
/**
|
|
42
|
+
* Defines a constraint that can be applied in a placeholder node. A constraint can consist of
|
|
43
|
+
* individual conditions or groups of nested conditions combined using a logical operator.
|
|
44
|
+
*
|
|
45
|
+
* @template T The type of conditions or nested constraint groups contained within this constraint.
|
|
46
|
+
*/
|
|
47
|
+
interface NqlTemplateConstraint<T> {
|
|
48
|
+
operator: NqlTemplateConstraintLogicalOperator;
|
|
49
|
+
constraints: (T | NqlTemplateConstraintGroup<T>)[];
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Represents a group of constraints combined under a single logical operator. This allows for
|
|
53
|
+
* nested and complex logical expressions within a NQL query.
|
|
54
|
+
*
|
|
55
|
+
* @extends NqlTemplateConstraint<T> Inherits the structure of NqlConstraint for nested grouping.
|
|
56
|
+
* @template T The type of conditions or nested constraint groups contained within this constraint group.
|
|
57
|
+
*/
|
|
58
|
+
interface NqlTemplateConstraintGroup<T> extends NqlTemplateConstraint<T> {
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Represents the keys from the NqlAstTypes object, used to identify the type of AST node.
|
|
62
|
+
*/
|
|
63
|
+
type NqlAstType = keyof typeof NqlAstTypes;
|
|
64
|
+
/**
|
|
65
|
+
* Extracts the TypeScript type(s) associated with a given NQL AST type or array of AST types.
|
|
66
|
+
* This utility type dynamically maps one or more NqlAstType keys to their corresponding types.
|
|
67
|
+
*
|
|
68
|
+
* @template T A single NqlAstType or an array of NqlAstType values.
|
|
69
|
+
*/
|
|
70
|
+
type ExtractNqlType<T extends NqlAstType | NqlAstType[]> = T extends NqlAstType[] ? {
|
|
71
|
+
[K in keyof T]: T[K] extends NqlAstType ? (typeof NqlAstTypes)[T[K]] : never;
|
|
72
|
+
} : T extends NqlAstType ? (typeof NqlAstTypes)[T] : never;
|
|
73
|
+
/**
|
|
74
|
+
* Extracts the TypeScript type(s) associated with the expected type(s) of a `NqlPlaceholder`.
|
|
75
|
+
* This utility type conditionally checks if a given type `T` is a `NqlPlaceholder` with a generic
|
|
76
|
+
* type parameter `U`. If `T` is a `NqlPlaceholder`, it uses the `ExtractNqlType` utility to map
|
|
77
|
+
* `U` (the generic type parameter of the `NqlPlaceholder`) to its corresponding TypeScript type(s).
|
|
78
|
+
* If `T` is not a `NqlPlaceholder`, the type resolves to `never`.
|
|
79
|
+
*
|
|
80
|
+
* This type is useful for type inference and ensuring type safety when working with AST nodes
|
|
81
|
+
* that may be placeholders, enabling conditional type logic based on the structure of the AST.
|
|
82
|
+
*
|
|
83
|
+
* @template T - The type to check against `NqlPlaceholder`. This can be any type within the
|
|
84
|
+
* `NqlAst` hierarchy, or more broadly, any type in the system.
|
|
85
|
+
*
|
|
86
|
+
* @returns The extracted TypeScript type(s) if `T` is a `NqlPlaceholder`, otherwise `never`.
|
|
87
|
+
*
|
|
88
|
+
* @example
|
|
89
|
+
* // Given a placeholder type with expected type 'string_literal':
|
|
90
|
+
* type PlaceholderStringLiteral = NqlPlaceholder<'string_literal'>;
|
|
91
|
+
*
|
|
92
|
+
* // The extracted type would be `NqlLit<NqlStringType>`:
|
|
93
|
+
* type ExtractedType = ExtractPlaceholderType<PlaceholderStringLiteral>;
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* // When the type is not a `NqlPlaceholder`, `never` is returned:
|
|
97
|
+
* type NonPlaceholderType = NqlAst; // Assuming NqlAst is not a placeholder.
|
|
98
|
+
* type ExtractedType = ExtractPlaceholderType<NonPlaceholderType>; // `never`
|
|
99
|
+
*/
|
|
100
|
+
export type ExtractPlaceholderType<T> = T extends NqlPlaceholder<infer U> ? ExtractNqlType<U> : never;
|
|
101
|
+
/**
|
|
102
|
+
* Represents a placeholder within a NQL query template. Placeholders are used to dynamically insert
|
|
103
|
+
* values, columns, or other AST nodes into a query based on certain constraints and expectations.
|
|
104
|
+
*
|
|
105
|
+
* @template T A single NqlAstType or an array of NqlAstType values indicating the expected type(s)
|
|
106
|
+
* for the AST node(s) that can fill this placeholder.
|
|
107
|
+
*/
|
|
108
|
+
interface NqlPlaceholder<T extends NqlAstType | NqlAstType[] = any> {
|
|
109
|
+
type: "placeholder";
|
|
110
|
+
identifier: string;
|
|
111
|
+
expectedType: T;
|
|
112
|
+
optional: boolean;
|
|
113
|
+
cardinality: "one" | "many";
|
|
114
|
+
constraints?: NqlTemplateConstraint<ExtractNqlType<T>>;
|
|
115
|
+
nql: string;
|
|
116
|
+
}
|
|
1
117
|
type Quantifier = "all" | "distinct";
|
|
2
118
|
interface NqlAggFunction {
|
|
3
119
|
nql: string;
|
|
@@ -77,10 +193,10 @@ interface NqlTimestampType {
|
|
|
77
193
|
type: "timestamp";
|
|
78
194
|
}
|
|
79
195
|
type NqlSimpleType = NqlArrayType | NqlBinaryType | NqlBooleanType | NqlDoubleType | NqlLongType | NqlIntervalType | NqlObjectType | NqlOtherType | NqlStringType | NqlTimestampType;
|
|
80
|
-
interface NqlLit {
|
|
196
|
+
interface NqlLit<T = NqlSimpleType> {
|
|
81
197
|
nql: string;
|
|
82
198
|
as?: string;
|
|
83
|
-
value_type:
|
|
199
|
+
value_type: T;
|
|
84
200
|
value: string | null;
|
|
85
201
|
type: "literal";
|
|
86
202
|
}
|
|
@@ -180,5 +296,5 @@ interface NqlAstList extends Array<NqlAst> {
|
|
|
180
296
|
export declare function isAstArray(n: NqlAst): n is NqlAstList;
|
|
181
297
|
type NqlAst = {
|
|
182
298
|
type: string;
|
|
183
|
-
} & (NqlAstList | NqlAggFunction | NqlBinaryOp | NqlCase | NqlFunction | NqlLit | NqlIdent | NqlJoin | NqlTypeSpec | NqlOp | NqlWindow | NqlUnknown | NqlSelect | NqlCreateMaterializedView | NqlExplain);
|
|
184
|
-
export type { NqlAst, NqlAstList, NqlAggFunction, NqlBinaryOp, NqlBudget, NqlCase, NqlFunction, NqlSimpleType, NqlLit, NqlIdent, NqlIdentColumn, NqlIdentTable, NqlJoin, NqlJoinType, NqlJoinConditionType, NqlTypeSpec, NqlOp, NqlWindow, NqlUnknown, NqlSelect, NqlCreateMaterializedView, NqlExplain, };
|
|
299
|
+
} & (NqlAstList | NqlAggFunction | NqlBinaryOp | NqlCase | NqlFunction | NqlLit | NqlIdent | NqlJoin | NqlTypeSpec | NqlOp | NqlWindow | NqlUnknown | NqlSelect | NqlCreateMaterializedView | NqlExplain | NqlWhen | NqlElse | NqlPlaceholder);
|
|
300
|
+
export type { NqlAst, NqlAstList, NqlAggFunction, NqlBinaryOp, NqlBudget, NqlCase, NqlFunction, NqlSimpleType, NqlLit, NqlIdent, NqlIdentColumn, NqlIdentTable, NqlJoin, NqlJoinType, NqlJoinConditionType, NqlTypeSpec, NqlOp, NqlWindow, NqlUnknown, NqlSelect, NqlCreateMaterializedView, NqlExplain, NqlAstType, NqlPlaceholder, };
|
package/build/nql/Ast.js
CHANGED
|
@@ -1,3 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A mapping of AST (Abstract Syntax Tree) node types to their corresponding TypeScript types.
|
|
3
|
+
* This object acts as a type registry for the various components of the NQL (Notation Query Language) AST.
|
|
4
|
+
*/
|
|
5
|
+
const NqlAstTypes = {
|
|
6
|
+
agg_function: null,
|
|
7
|
+
binary_op: null,
|
|
8
|
+
case: null,
|
|
9
|
+
function: null,
|
|
10
|
+
literal: null,
|
|
11
|
+
string_literal: null,
|
|
12
|
+
long_literal: null,
|
|
13
|
+
double_literal: null,
|
|
14
|
+
boolean_literal: null,
|
|
15
|
+
timestamp_literal: null,
|
|
16
|
+
column: null,
|
|
17
|
+
table: null,
|
|
18
|
+
join: null,
|
|
19
|
+
type_spec: null,
|
|
20
|
+
operator: null,
|
|
21
|
+
window: null,
|
|
22
|
+
unknown: null,
|
|
23
|
+
select: null,
|
|
24
|
+
create_materialized_view: null,
|
|
25
|
+
explain: null,
|
|
26
|
+
array: null,
|
|
27
|
+
binary: null,
|
|
28
|
+
bool: null,
|
|
29
|
+
double: null,
|
|
30
|
+
long: null,
|
|
31
|
+
interval: null,
|
|
32
|
+
object: null,
|
|
33
|
+
other: null,
|
|
34
|
+
string: null,
|
|
35
|
+
timestamp: null,
|
|
36
|
+
};
|
|
1
37
|
export function isAstArray(n) {
|
|
2
38
|
return Array.isArray(n);
|
|
3
39
|
}
|
package/build/nql/AstParser.d.ts
CHANGED
|
@@ -17,8 +17,8 @@ export interface Select {
|
|
|
17
17
|
from: Table[] | Raw;
|
|
18
18
|
where: BooleanExpression | Raw | null;
|
|
19
19
|
}
|
|
20
|
-
export type Output = ColumnRef | Lit | Raw
|
|
21
|
-
export type Expression = BooleanExpression | ColumnRef | Lit | Raw
|
|
20
|
+
export type Output = ColumnRef | Lit | Raw | ast.NqlPlaceholder<ast.NqlAstType>;
|
|
21
|
+
export type Expression = BooleanExpression | ColumnRef | Lit | Raw | ast.NqlPlaceholder<ast.NqlAstType>;
|
|
22
22
|
export interface Raw {
|
|
23
23
|
type: "nql";
|
|
24
24
|
as?: string;
|
|
@@ -30,7 +30,7 @@ export interface Deduplication {
|
|
|
30
30
|
}
|
|
31
31
|
export type ColumnRef = AttributeRef | DatasetColumnRef | RawRef;
|
|
32
32
|
export type BooleanExpression = And | Between | Equals | NotEquals | Gt | Gte | In | IsNull | Like | Lt | Lte | Not | Or;
|
|
33
|
-
export type Table = RosettaTable | DatasetTable | RawTable
|
|
33
|
+
export type Table = RosettaTable | DatasetTable | RawTable | ast.NqlPlaceholder<ast.NqlAstType>;
|
|
34
34
|
export interface RosettaTable {
|
|
35
35
|
type: "rosetta_stone";
|
|
36
36
|
as?: string;
|
|
@@ -166,3 +166,4 @@ export declare function parseWhere(n: ast.NqlAst): BooleanExpression | Raw;
|
|
|
166
166
|
export declare function parseDeduplication(n: ast.NqlSelect): Deduplication | Raw | null;
|
|
167
167
|
export declare function parseExpression(n: ast.NqlAst): Expression;
|
|
168
168
|
export declare function parseOutput(n: ast.NqlAst): Output;
|
|
169
|
+
export declare function extractPlaceholders(n: Node): Node[];
|
package/build/nql/AstParser.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import * as ast from "./Ast";
|
|
2
|
+
import { traverseParsedAst } from "./AstTraverse";
|
|
2
3
|
export function parseStatement(n) {
|
|
3
4
|
switch (n.type) {
|
|
4
5
|
case "create_materialized_view":
|
|
@@ -49,6 +50,8 @@ export function parseFrom(n) {
|
|
|
49
50
|
return parseJoin(n);
|
|
50
51
|
case "table":
|
|
51
52
|
return [parseTable(n)];
|
|
53
|
+
case "placeholder":
|
|
54
|
+
return [n];
|
|
52
55
|
default:
|
|
53
56
|
return [parseRawTable(n)];
|
|
54
57
|
}
|
|
@@ -100,11 +103,11 @@ export function parseDeduplication(n) {
|
|
|
100
103
|
return n.qualify !== null ? nqlOrFail(n.qualify) : null;
|
|
101
104
|
}
|
|
102
105
|
export function parseExpression(n) {
|
|
103
|
-
const parse = oneOf(parseBooleanExpression, parseColumnRef, parseLit, parseStringExpression);
|
|
106
|
+
const parse = oneOf(parseBooleanExpression, parseColumnRef, parseLit, parseStringExpression, parsePlaceholder);
|
|
104
107
|
return parse(n);
|
|
105
108
|
}
|
|
106
109
|
export function parseOutput(n) {
|
|
107
|
-
const parse = oneOf(parseColumnRef, parseLit);
|
|
110
|
+
const parse = oneOf(parseColumnRef, parseLit, parsePlaceholder);
|
|
108
111
|
return parse(n);
|
|
109
112
|
}
|
|
110
113
|
function parseBooleanExpression(n) {
|
|
@@ -214,6 +217,18 @@ function parseColumnRef(n) {
|
|
|
214
217
|
return nqlOrFail(n);
|
|
215
218
|
}
|
|
216
219
|
}
|
|
220
|
+
function parsePlaceholder(n) {
|
|
221
|
+
switch (n.type) {
|
|
222
|
+
case "placeholder": {
|
|
223
|
+
return n;
|
|
224
|
+
}
|
|
225
|
+
default:
|
|
226
|
+
return {
|
|
227
|
+
type: "nql",
|
|
228
|
+
nql: "",
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
}
|
|
217
232
|
function parseLit(n) {
|
|
218
233
|
switch (n.type) {
|
|
219
234
|
case "literal": {
|
|
@@ -416,7 +431,10 @@ function parseJoin(n) {
|
|
|
416
431
|
conditionType: n.condition_type,
|
|
417
432
|
joinType: n.join_type,
|
|
418
433
|
};
|
|
419
|
-
|
|
434
|
+
// ??? I'm not sure about this logic ??? - nj
|
|
435
|
+
if (right[0].type !== "placeholder") {
|
|
436
|
+
right[0].join = join;
|
|
437
|
+
}
|
|
420
438
|
return left.concat(right);
|
|
421
439
|
}
|
|
422
440
|
function parseTable(n) {
|
|
@@ -448,6 +466,16 @@ function parseRawTable(n) {
|
|
|
448
466
|
};
|
|
449
467
|
return table;
|
|
450
468
|
}
|
|
469
|
+
export function extractPlaceholders(n) {
|
|
470
|
+
const placeholders = [];
|
|
471
|
+
const addPlaceholder = (n) => {
|
|
472
|
+
if (n.type === "placeholder") {
|
|
473
|
+
placeholders.push(n);
|
|
474
|
+
}
|
|
475
|
+
};
|
|
476
|
+
traverseParsedAst(n, addPlaceholder);
|
|
477
|
+
return placeholders;
|
|
478
|
+
}
|
|
451
479
|
/**
|
|
452
480
|
* Selects the first successful expression from a list of parsers.
|
|
453
481
|
*
|
package/build/nql/NqlBuilder.js
CHANGED
|
@@ -64,13 +64,15 @@ function compileTableRefs(tables) {
|
|
|
64
64
|
return aliased("narrative.rosetta_stone", table.as, false);
|
|
65
65
|
case "raw_table":
|
|
66
66
|
return aliased(table.nql, table.as, false);
|
|
67
|
+
default:
|
|
68
|
+
throw new Error("unreachable");
|
|
67
69
|
}
|
|
68
70
|
}
|
|
69
71
|
function compileTableRef(table) {
|
|
70
72
|
let stmt = "";
|
|
71
73
|
// NB: by convention the first table in list will never have a join associated with it,
|
|
72
74
|
// hence the leading ' ' below does what we want.
|
|
73
|
-
if (table.join !== undefined) {
|
|
75
|
+
if ("join" in table && table.join !== undefined) {
|
|
74
76
|
switch (table.join.joinType) {
|
|
75
77
|
case "comma": {
|
|
76
78
|
stmt += ",";
|
|
@@ -111,7 +113,8 @@ function compileTableRefs(tables) {
|
|
|
111
113
|
if (
|
|
112
114
|
// Prefer explicit `join !== undefined` to the use of the `?` operator so that typescript
|
|
113
115
|
// compile knows that the join is defined in the subsequent lexical scope.
|
|
114
|
-
|
|
116
|
+
"join" in table &&
|
|
117
|
+
table.join !== undefined &&
|
|
115
118
|
table.join.condition !== null &&
|
|
116
119
|
table.join.conditionType !== "none") {
|
|
117
120
|
switch (table.join.conditionType) {
|
|
@@ -150,7 +153,7 @@ export function compileOutput(n) {
|
|
|
150
153
|
case "nql":
|
|
151
154
|
return raw(n);
|
|
152
155
|
}
|
|
153
|
-
|
|
156
|
+
throw new Error("unreachable");
|
|
154
157
|
}
|
|
155
158
|
export function compileExpression(n) {
|
|
156
159
|
switch (n.type) {
|
|
@@ -191,7 +194,7 @@ export function compileExpression(n) {
|
|
|
191
194
|
case "raw_ref":
|
|
192
195
|
return compileRawRef(n.nql, n.as);
|
|
193
196
|
}
|
|
194
|
-
|
|
197
|
+
throw new Error("unreachable");
|
|
195
198
|
}
|
|
196
199
|
function compileAttributeRef(column, as, aliasParens) {
|
|
197
200
|
const segments = column.split(".");
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { NqlAst, NqlAstType, NqlPlaceholder } from "./Ast";
|
|
2
|
+
/**
|
|
3
|
+
* Traverses an AST (Abstract Syntax Tree) or an array of AST nodes, and replaces all placeholder nodes
|
|
4
|
+
* found within the tree with the result of a provided callback function. This function operates recursively,
|
|
5
|
+
* handling various types of nodes including those with `args`, `left`, `right`, `condition`, `value`, `columns`,
|
|
6
|
+
* `from`, `where`, `having`, `group_by`, `order_by`, `windows`, and `with` properties. The callback is invoked
|
|
7
|
+
* with each placeholder node encountered, and the return value of the callback replaces the placeholder in the AST.
|
|
8
|
+
*
|
|
9
|
+
* @template T A union type of `NqlAstType` or an array of `NqlAstType`, indicating the expected type(s) of the placeholder's content.
|
|
10
|
+
* @param {NqlAst|NqlAst[]} node The root AST node or an array of AST nodes to traverse.
|
|
11
|
+
* @param {function(NqlPlaceholder<T>): NqlAst} callback A function that takes a placeholder node and returns an AST node to replace it.
|
|
12
|
+
* This function is called for every placeholder node found in the AST.
|
|
13
|
+
* @returns {void} This function does not return a value; it modifies the AST nodes in place.
|
|
14
|
+
*
|
|
15
|
+
* @todo Validate that the replaced nodes adhere to the constraints of the placholder node (if any).
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* // Given an AST node or array of nodes `ast` and a function `replacePlaceholder`:
|
|
19
|
+
* visitAndReplacePlaceholders(ast, replacePlaceholder);
|
|
20
|
+
* // This will traverse `ast`, calling `replacePlaceholder` for each placeholder node,
|
|
21
|
+
* // replacing them in place with the returned node from `replacePlaceholder`.
|
|
22
|
+
*/
|
|
23
|
+
export declare function visitAndReplacePlaceholders<T extends NqlAstType | NqlAstType[]>(node: NqlAst | NqlAst[], callback: (placeholderNode: NqlPlaceholder<T>) => NqlAst): void;
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
function isAstNode(node) {
|
|
2
|
+
if (node === undefined) {
|
|
3
|
+
return false;
|
|
4
|
+
}
|
|
5
|
+
if (node === null) {
|
|
6
|
+
return false;
|
|
7
|
+
}
|
|
8
|
+
if (typeof node === "string") {
|
|
9
|
+
return false;
|
|
10
|
+
}
|
|
11
|
+
return !Array.isArray(node) && typeof node === "object" && "type" in node;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Traverses an AST (Abstract Syntax Tree) or an array of AST nodes, and replaces all placeholder nodes
|
|
15
|
+
* found within the tree with the result of a provided callback function. This function operates recursively,
|
|
16
|
+
* handling various types of nodes including those with `args`, `left`, `right`, `condition`, `value`, `columns`,
|
|
17
|
+
* `from`, `where`, `having`, `group_by`, `order_by`, `windows`, and `with` properties. The callback is invoked
|
|
18
|
+
* with each placeholder node encountered, and the return value of the callback replaces the placeholder in the AST.
|
|
19
|
+
*
|
|
20
|
+
* @template T A union type of `NqlAstType` or an array of `NqlAstType`, indicating the expected type(s) of the placeholder's content.
|
|
21
|
+
* @param {NqlAst|NqlAst[]} node The root AST node or an array of AST nodes to traverse.
|
|
22
|
+
* @param {function(NqlPlaceholder<T>): NqlAst} callback A function that takes a placeholder node and returns an AST node to replace it.
|
|
23
|
+
* This function is called for every placeholder node found in the AST.
|
|
24
|
+
* @returns {void} This function does not return a value; it modifies the AST nodes in place.
|
|
25
|
+
*
|
|
26
|
+
* @todo Validate that the replaced nodes adhere to the constraints of the placholder node (if any).
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* // Given an AST node or array of nodes `ast` and a function `replacePlaceholder`:
|
|
30
|
+
* visitAndReplacePlaceholders(ast, replacePlaceholder);
|
|
31
|
+
* // This will traverse `ast`, calling `replacePlaceholder` for each placeholder node,
|
|
32
|
+
* // replacing them in place with the returned node from `replacePlaceholder`.
|
|
33
|
+
*/
|
|
34
|
+
export function visitAndReplacePlaceholders(node, callback) {
|
|
35
|
+
if (Array.isArray(node)) {
|
|
36
|
+
// Using for...of for array iteration
|
|
37
|
+
for (const [i, childNode] of node.entries()) {
|
|
38
|
+
if (childNode.type === "placeholder") {
|
|
39
|
+
node[i] = callback(childNode);
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
visitAndReplacePlaceholders(childNode, callback);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
else if (isAstNode(node)) {
|
|
47
|
+
if ("args" in node) {
|
|
48
|
+
node.args = node.args.map((arg) => {
|
|
49
|
+
if (arg.type === "placeholder") {
|
|
50
|
+
return callback(arg);
|
|
51
|
+
}
|
|
52
|
+
return arg;
|
|
53
|
+
});
|
|
54
|
+
for (const arg of node.args) {
|
|
55
|
+
visitAndReplacePlaceholders(arg, callback);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
if ("left" in node) {
|
|
59
|
+
if (node.left.type === "placeholder") {
|
|
60
|
+
node.left = callback(node.left);
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
visitAndReplacePlaceholders(node.left, callback);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
if ("right" in node) {
|
|
67
|
+
if (node.right.type === "placeholder") {
|
|
68
|
+
node.right = callback(node.right);
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
visitAndReplacePlaceholders(node.right, callback);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
if ("condition" in node && node.condition) {
|
|
75
|
+
if (node.condition.type === "placeholder") {
|
|
76
|
+
node.condition = callback(node.condition);
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
visitAndReplacePlaceholders(node.condition, callback);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
if ("value" in node && isAstNode(node.value)) {
|
|
83
|
+
if (node.value.type === "placeholder") {
|
|
84
|
+
node.value = callback(node.value);
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
visitAndReplacePlaceholders(node.value, callback);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
if ("columns" in node) {
|
|
91
|
+
node.columns = node.columns.map((arg) => {
|
|
92
|
+
if (arg.type === "placeholder") {
|
|
93
|
+
return callback(arg);
|
|
94
|
+
}
|
|
95
|
+
return arg;
|
|
96
|
+
});
|
|
97
|
+
for (const arg of node.columns) {
|
|
98
|
+
visitAndReplacePlaceholders(arg, callback);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
if ("from" in node && node.from) {
|
|
102
|
+
if (node.from.type === "placeholder") {
|
|
103
|
+
node.from = callback(node.from);
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
visitAndReplacePlaceholders(node.from, callback);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
if ("where" in node && node.where) {
|
|
110
|
+
if (node.where.type === "placeholder") {
|
|
111
|
+
node.where = callback(node.where);
|
|
112
|
+
}
|
|
113
|
+
else {
|
|
114
|
+
visitAndReplacePlaceholders(node.where, callback);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
if ("having" in node && node.having) {
|
|
118
|
+
if (node.having.type === "placeholder") {
|
|
119
|
+
node.having = callback(node.having);
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
visitAndReplacePlaceholders(node.having, callback);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
if ("group_by" in node && node.group_by) {
|
|
126
|
+
node.group_by = node.group_by.map((arg) => {
|
|
127
|
+
if (arg.type === "placeholder") {
|
|
128
|
+
return callback(arg);
|
|
129
|
+
}
|
|
130
|
+
return arg;
|
|
131
|
+
});
|
|
132
|
+
for (const arg of node.group_by) {
|
|
133
|
+
visitAndReplacePlaceholders(arg, callback);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
if ("order_by" in node && node.order_by) {
|
|
137
|
+
node.order_by = node.order_by.map((arg) => {
|
|
138
|
+
if (arg.type === "placeholder") {
|
|
139
|
+
return callback(arg);
|
|
140
|
+
}
|
|
141
|
+
return arg;
|
|
142
|
+
});
|
|
143
|
+
for (const arg of node.order_by) {
|
|
144
|
+
visitAndReplacePlaceholders(arg, callback);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
if ("windows" in node && node.windows) {
|
|
148
|
+
node.windows = node.windows.map((arg) => {
|
|
149
|
+
if (arg.type === "placeholder") {
|
|
150
|
+
return callback(arg);
|
|
151
|
+
}
|
|
152
|
+
return arg;
|
|
153
|
+
});
|
|
154
|
+
for (const arg of node.windows) {
|
|
155
|
+
visitAndReplacePlaceholders(arg, callback);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
if ("with" in node && node.with) {
|
|
159
|
+
node.with = node.with.map((arg) => {
|
|
160
|
+
if (arg.type === "placeholder") {
|
|
161
|
+
return callback(arg);
|
|
162
|
+
}
|
|
163
|
+
return arg;
|
|
164
|
+
});
|
|
165
|
+
for (const arg of node.with) {
|
|
166
|
+
visitAndReplacePlaceholders(arg, callback);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@narrative.io/data-collaboration-sdk-ts",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.6.0",
|
|
4
4
|
"main": "build/index.js",
|
|
5
5
|
"repository": "github:narrative-io/data-collaboration-sdk-ts",
|
|
6
6
|
"source": "src/index.ts",
|
|
@@ -42,4 +42,4 @@
|
|
|
42
42
|
"files": [
|
|
43
43
|
"build"
|
|
44
44
|
]
|
|
45
|
-
}
|
|
45
|
+
}
|