Skip to content
Open

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import type { CubeEvaluator } from './CubeEvaluator';
import type { ErrorReporter } from './ErrorReporter';
import { CompilerInterface } from './PrepareCompiler';

export interface ContextInput {
name: string;
contextMembers: any;
}

export interface CompiledContext {
name: string;
contextMembers: any;
}

export class ContextEvaluator implements CompilerInterface {
private readonly cubeEvaluator: CubeEvaluator;

private contextDefinitions: Map<string, CompiledContext>;

public constructor(cubeEvaluator: CubeEvaluator) {
this.cubeEvaluator = cubeEvaluator;
this.contextDefinitions = new Map<string, CompiledContext>();
}

public compile(contexts: ContextInput[], errorReporter?: ErrorReporter): void {
if (contexts.length === 0) {
return;
}

this.contextDefinitions = new Map<string, CompiledContext>();
for (const context of contexts) {
if (errorReporter && this.contextDefinitions.has(context.name)) {
errorReporter.error(`Context "${context.name}" already exists!`);
} else {
this.contextDefinitions.set(context.name, this.compileContext(context));
}
}
}

private compileContext(context: ContextInput): CompiledContext {
return {
name: context.name,
contextMembers: this.cubeEvaluator.evaluateReferences(null, context.contextMembers)
};
}

public get contextList(): string[] {
return Array.from(this.contextDefinitions.keys());
}
}
20 changes: 0 additions & 20 deletions packages/cubejs-schema-compiler/src/compiler/CubeDictionary.js

This file was deleted.

31 changes: 31 additions & 0 deletions packages/cubejs-schema-compiler/src/compiler/CubeDictionary.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import type { ErrorReporter } from './ErrorReporter';
import { TranspilerCubeResolver } from './transpilers';
import { CompilerInterface } from './PrepareCompiler';

export interface Cube {
name: string;
[key: string]: any;
}

export class CubeDictionary implements TranspilerCubeResolver, CompilerInterface {
public byId: Map<string, Cube>;

public constructor() {
this.byId = new Map<string, Cube>();
}

public compile(cubes: Cube[], _errorReporter?: ErrorReporter): void {
this.byId = new Map<string, Cube>();
for (const cube of cubes) {
this.byId.set(cube.name, cube);
}
}

public resolveCube(name: string): Cube | undefined {
return this.byId.get(name);
}

public free(): void {
this.byId = new Map<string, Cube>();
}
}
23 changes: 17 additions & 6 deletions packages/cubejs-schema-compiler/src/compiler/CubeSymbols.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import { camelizeCube } from './utils';

import type { ErrorReporter } from './ErrorReporter';
import { TranspilerSymbolResolver } from './transpilers';
import { CompilerInterface } from './PrepareCompiler';

export type ToString = { toString(): string };

export type GranularityDefinition = {
sql?: (...args: any[]) => string;
name?: string;
title?: string;
interval?: string;
offset?: string;
Expand Down Expand Up @@ -139,6 +141,17 @@ export type ViewIncludedMember = {
name: string;
};

export type FolderMember = {
type?: 'folder';
name: string;
includes?: FolderMember[];
};

export type Folder = {
name: string;
includes: FolderMember[];
};

export interface CubeDefinition {
name: string;
extends?: (...args: Array<unknown>) => { __cubeName: string };
Expand All @@ -158,7 +171,7 @@ export interface CubeDefinition {
accessPolicy?: AccessPolicyDefinition[];
// eslint-disable-next-line camelcase
access_policy?: any[];
folders?: any[];
folders?: Folder[];
includes?: any;
excludes?: any;
cubes?: any;
Expand Down Expand Up @@ -215,7 +228,7 @@ export const CONTEXT_SYMBOLS = {

export const CURRENT_CUBE_CONSTANTS = ['CUBE', 'TABLE'];

export class CubeSymbols implements TranspilerSymbolResolver {
export class CubeSymbols implements TranspilerSymbolResolver, CompilerInterface {
public symbols: Record<string | symbol, CubeSymbolsDefinition>;

private builtCubes: Record<string, CubeDefinitionExtended>;
Expand Down Expand Up @@ -913,9 +926,7 @@ export class CubeSymbols implements TranspilerSymbolResolver {
* refactoring.
*/
protected evaluateContextFunction(cube: any, contextFn: any, context: any = {}) {
const cubeEvaluator = this;

return cubeEvaluator.resolveSymbolsCall(contextFn, (name: string) => {
return this.resolveSymbolsCall(contextFn, (name: string) => {
const resolvedSymbol = this.resolveSymbol(cube, name);
if (resolvedSymbol) {
return resolvedSymbol;
Expand All @@ -930,7 +941,7 @@ export class CubeSymbols implements TranspilerSymbolResolver {
});
}

protected evaluateReferences<T extends ToString | Array<ToString>>(
public evaluateReferences<T extends ToString | Array<ToString>>(
cube: string | null,
referencesFn: (...args: Array<unknown>) => T,
options: { collectJoinHints?: boolean, originalSorting?: boolean } = {}
Expand Down
Loading
Loading