Skip to content

Commit 614e31d

Browse files
committed
move ContextEvaluator to ts
1 parent f71c752 commit 614e31d

File tree

3 files changed

+49
-39
lines changed

3 files changed

+49
-39
lines changed

packages/cubejs-schema-compiler/src/compiler/ContextEvaluator.js

Lines changed: 0 additions & 35 deletions
This file was deleted.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import type { CubeEvaluator } from './CubeEvaluator';
2+
import type { ErrorReporter } from './ErrorReporter';
3+
import { CompilerInterface } from './PrepareCompiler';
4+
5+
export interface ContextInput {
6+
name: string;
7+
contextMembers: any;
8+
}
9+
10+
export interface CompiledContext {
11+
name: string;
12+
contextMembers: any;
13+
}
14+
15+
export class ContextEvaluator implements CompilerInterface {
16+
private cubeEvaluator: CubeEvaluator;
17+
18+
private contextDefinitions: Record<string, CompiledContext>;
19+
20+
public constructor(cubeEvaluator: CubeEvaluator) {
21+
this.cubeEvaluator = cubeEvaluator;
22+
this.contextDefinitions = {};
23+
}
24+
25+
public compile(contexts: ContextInput[], _errorReporter?: ErrorReporter): void {
26+
if (contexts.length === 0) {
27+
return;
28+
}
29+
30+
// TODO: handle duplications, context names must be uniq
31+
this.contextDefinitions = {};
32+
for (const context of contexts) {
33+
this.contextDefinitions[context.name] = this.compileContext(context);
34+
}
35+
}
36+
37+
private compileContext(context: ContextInput): CompiledContext {
38+
return {
39+
name: context.name,
40+
contextMembers: this.cubeEvaluator.evaluateReferences(null, context.contextMembers)
41+
};
42+
}
43+
44+
public get contextList(): string[] {
45+
return Object.keys(this.contextDefinitions);
46+
}
47+
}

packages/cubejs-schema-compiler/src/compiler/CubeSymbols.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -914,9 +914,7 @@ export class CubeSymbols implements TranspilerSymbolResolver, CompilerInterface
914914
* refactoring.
915915
*/
916916
protected evaluateContextFunction(cube: any, contextFn: any, context: any = {}) {
917-
const cubeEvaluator = this;
918-
919-
return cubeEvaluator.resolveSymbolsCall(contextFn, (name: string) => {
917+
return this.resolveSymbolsCall(contextFn, (name: string) => {
920918
const resolvedSymbol = this.resolveSymbol(cube, name);
921919
if (resolvedSymbol) {
922920
return resolvedSymbol;
@@ -931,7 +929,7 @@ export class CubeSymbols implements TranspilerSymbolResolver, CompilerInterface
931929
});
932930
}
933931

934-
protected evaluateReferences<T extends ToString | Array<ToString>>(
932+
public evaluateReferences<T extends ToString | Array<ToString>>(
935933
cube: string | null,
936934
referencesFn: (...args: Array<unknown>) => T,
937935
options: { collectJoinHints?: boolean, originalSorting?: boolean } = {}

0 commit comments

Comments
 (0)