Skip to content

Commit f1b7d32

Browse files
committed
more types
1 parent 9e39717 commit f1b7d32

File tree

2 files changed

+81
-22
lines changed

2 files changed

+81
-22
lines changed

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

Lines changed: 64 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { FileContent, getEnv, isNativeSupported, SchemaFileRepository } from '@c
1515
import { NativeInstance, PythonCtx, transpileJs } from '@cubejs-backend/native';
1616
import { UserError } from './UserError';
1717
import { ErrorReporter, ErrorReporterOptions, SyntaxErrorInterface } from './ErrorReporter';
18-
import { CONTEXT_SYMBOLS, CubeSymbols } from './CubeSymbols';
18+
import { CONTEXT_SYMBOLS, CubeDefinition, CubeSymbols } from './CubeSymbols';
1919
import { ViewCompilationGate } from './ViewCompilationGate';
2020
import { TranspilerInterface } from './transpilers';
2121
import { CompilerInterface } from './PrepareCompiler';
@@ -302,7 +302,24 @@ export class DataSchemaCompiler {
302302
return results.filter(f => !!f);
303303
};
304304

305-
const compilePhase = async (compilers: CompileCubeFilesCompilers, stage: 0 | 1 | 2 | 3) => this.compileCubeFiles(compilers, await transpile(stage), errorsReport);
305+
let cubes: CubeDefinition[] = [];
306+
let exports: Record<string, Record<string, any>> = {};
307+
let contexts: Record<string, any>[] = [];
308+
let compiledFiles: Record<string, boolean> = {};
309+
let asyncModules: CallableFunction[] = [];
310+
311+
const compilePhase = async (compilers: CompileCubeFilesCompilers, stage: 0 | 1 | 2 | 3) => {
312+
const res = this.compileCubeFiles(cubes, exports, contexts, compiledFiles, asyncModules, compilers, await transpile(stage), errorsReport);
313+
314+
// clear the objects for the next phase
315+
cubes = [];
316+
exports = {};
317+
contexts = [];
318+
compiledFiles = {};
319+
asyncModules = [];
320+
321+
return res;
322+
};
306323

307324
return compilePhase({ cubeCompilers: this.cubeNameCompilers }, 0)
308325
.then(() => compilePhase({ cubeCompilers: this.preTranspileCubeCompilers.concat([this.viewCompilationGate]) }, 1))
@@ -348,7 +365,11 @@ export class DataSchemaCompiler {
348365
return this.compilePromise;
349366
}
350367

351-
private async transpileFile(file: FileContent, errorsReport: ErrorReporter, options: TranspileOptions = {}) {
368+
private async transpileFile(
369+
file: FileContent,
370+
errorsReport: ErrorReporter,
371+
options: TranspileOptions = {}
372+
): Promise<(FileContent | undefined)> {
352373
if (file.fileName.endsWith('.jinja') ||
353374
(file.fileName.endsWith('.yml') || file.fileName.endsWith('.yaml'))
354375
// TODO do Jinja syntax check with jinja compiler
@@ -377,7 +398,11 @@ export class DataSchemaCompiler {
377398
* Right now it is used only for transpilation in native,
378399
* so no checks for transpilation type inside this method
379400
*/
380-
private async transpileJsFilesBulk(files: FileContent[], errorsReport: ErrorReporter, { cubeNames, cubeSymbols, contextSymbols, transpilerNames, compilerId, stage }: TranspileOptions) {
401+
private async transpileJsFilesBulk(
402+
files: FileContent[],
403+
errorsReport: ErrorReporter,
404+
{ cubeNames, cubeSymbols, contextSymbols, transpilerNames, compilerId, stage }: TranspileOptions
405+
): Promise<(FileContent | undefined)[]> {
381406
// for bulk processing this data may be optimized even more by passing transpilerNames, compilerId only once for a bulk
382407
// but this requires more complex logic to be implemented in the native side.
383408
// And comparing to the file content sizes, a few bytes of JSON data is not a big deal here
@@ -411,7 +436,11 @@ export class DataSchemaCompiler {
411436
});
412437
}
413438

414-
private async transpileJsFile(file: FileContent, errorsReport: ErrorReporter, { cubeNames, cubeSymbols, contextSymbols, transpilerNames, compilerId, stage }: TranspileOptions) {
439+
private async transpileJsFile(
440+
file: FileContent,
441+
errorsReport: ErrorReporter,
442+
{ cubeNames, cubeSymbols, contextSymbols, transpilerNames, compilerId, stage }: TranspileOptions
443+
): Promise<(FileContent | undefined)> {
415444
try {
416445
if (getEnv('transpilationNative')) {
417446
const reqData = {
@@ -496,13 +525,16 @@ export class DataSchemaCompiler {
496525
return this.currentQuery;
497526
}
498527

499-
private async compileCubeFiles(compilers: CompileCubeFilesCompilers, toCompile: FileContent[], errorsReport: ErrorReporter) {
500-
const cubes = [];
501-
const exports = {};
502-
const contexts = [];
503-
const compiledFiles = {};
504-
const asyncModules = [];
505-
528+
private async compileCubeFiles(
529+
cubes: CubeDefinition[],
530+
exports: Record<string, Record<string, any>>,
531+
contexts: Record<string, any>[],
532+
compiledFiles: Record<string, boolean>,
533+
asyncModules: CallableFunction[],
534+
compilers: CompileCubeFilesCompilers,
535+
toCompile: FileContent[],
536+
errorsReport: ErrorReporter
537+
) {
506538
toCompile
507539
.forEach((file) => {
508540
this.compileFile(
@@ -526,7 +558,15 @@ export class DataSchemaCompiler {
526558
}
527559

528560
private compileFile(
529-
file: FileContent, errorsReport: ErrorReporter, cubes, exports, contexts, toCompile, compiledFiles, asyncModules, { doSyntaxCheck } = { doSyntaxCheck: false }
561+
file: FileContent,
562+
errorsReport: ErrorReporter,
563+
cubes: CubeDefinition[],
564+
exports: Record<string, Record<string, any>>,
565+
contexts: Record<string, any>[],
566+
toCompile: FileContent[],
567+
compiledFiles: Record<string, boolean>,
568+
asyncModules: CallableFunction[],
569+
{ doSyntaxCheck } = { doSyntaxCheck: false }
530570
) {
531571
if (compiledFiles[file.fileName]) {
532572
return;
@@ -584,7 +624,17 @@ export class DataSchemaCompiler {
584624
return script;
585625
}
586626

587-
public compileJsFile(file: FileContent, errorsReport: ErrorReporter, cubes, contexts, exports, asyncModules, toCompile, compiledFiles, { doSyntaxCheck } = { doSyntaxCheck: false }) {
627+
public compileJsFile(
628+
file: FileContent,
629+
errorsReport: ErrorReporter,
630+
cubes: CubeDefinition[],
631+
contexts: Record<string, any>[],
632+
exports: Record<string, Record<string, any>>,
633+
asyncModules: CallableFunction[],
634+
toCompile: FileContent[],
635+
compiledFiles: Record<string, boolean>,
636+
{ doSyntaxCheck } = { doSyntaxCheck: false }
637+
) {
588638
if (doSyntaxCheck) {
589639
// There is no need to run syntax check for data model files
590640
// because they were checked during transpilation/transformation phase

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

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import type { FileContent } from '@cubejs-backend/shared';
1010
import { getEnv } from '@cubejs-backend/shared';
1111
import { CubePropContextTranspiler, transpiledFields, transpiledFieldsPatterns } from './transpilers';
1212
import { PythonParser } from '../parser/PythonParser';
13-
import { CubeSymbols } from './CubeSymbols';
13+
import { CubeDefinition, CubeSymbols } from './CubeSymbols';
1414
import { DataSchemaCompiler } from './DataSchemaCompiler';
1515
import { nonStringFields } from './CubeValidator';
1616
import { CubeDictionary } from './CubeDictionary';
@@ -66,12 +66,12 @@ export class YamlCompiler {
6666
public async compileYamlWithJinjaFile(
6767
file: FileContent,
6868
errorsReport: ErrorReporter,
69-
cubes,
70-
contexts,
71-
exports,
72-
asyncModules,
73-
toCompile,
74-
compiledFiles,
69+
cubes: CubeDefinition[],
70+
contexts: Record<string, any>[],
71+
exports: Record<string, Record<string, any>>,
72+
asyncModules: CallableFunction[],
73+
toCompile: FileContent[],
74+
compiledFiles: Record<string, boolean>,
7575
compileContext,
7676
pythonContext: PythonCtx
7777
) {
@@ -89,7 +89,16 @@ export class YamlCompiler {
8989
);
9090
}
9191

92-
public compileYamlFile(file: FileContent, errorsReport: ErrorReporter, cubes, contexts, exports, asyncModules, toCompile, compiledFiles) {
92+
public compileYamlFile(
93+
file: FileContent,
94+
errorsReport: ErrorReporter,
95+
cubes: CubeDefinition[],
96+
contexts: Record<string, any>[],
97+
exports: Record<string, Record<string, any>>,
98+
asyncModules: CallableFunction[],
99+
toCompile: FileContent[],
100+
compiledFiles: Record<string, boolean>
101+
) {
93102
if (!file.content.trim()) {
94103
return;
95104
}

0 commit comments

Comments
 (0)