Skip to content

Commit 688174c

Browse files
committed
Prepare transpileYaml in native + schema compiler
1 parent 6ce8365 commit 688174c

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

packages/cubejs-backend-native/js/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,16 @@ export const transpileJs = async (transpileRequests: TransformConfig[]): Promise
517517
throw new Error('TranspileJs native implementation not found!');
518518
};
519519

520+
export const transpileYaml = async (transpileRequests: TransformConfig[]): Promise<TransformResponse[]> => {
521+
const native = loadNative();
522+
523+
if (native.transpileYaml) {
524+
return native.transpileYaml(transpileRequests);
525+
}
526+
527+
throw new Error('TranspileYaml native implementation not found!');
528+
};
529+
520530
export interface PyConfiguration {
521531
repositoryFactory?: (ctx: unknown) => Promise<unknown>,
522532
logger?: (msg: string, params: Record<string, any>) => void,

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

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import workerpool from 'workerpool';
1313
import { LRUCache } from 'lru-cache';
1414

1515
import { FileContent, getEnv, isNativeSupported, SchemaFileRepository } from '@cubejs-backend/shared';
16-
import { NativeInstance, PythonCtx, transpileJs } from '@cubejs-backend/native';
16+
import { NativeInstance, PythonCtx, transpileJs, transpileYaml } from '@cubejs-backend/native';
1717
import { UserError } from './UserError';
1818
import { ErrorReporter, ErrorReporterOptions, SyntaxErrorInterface } from './ErrorReporter';
1919
import { CONTEXT_SYMBOLS, CubeDefinition, CubeSymbols } from './CubeSymbols';
@@ -630,10 +630,13 @@ export class DataSchemaCompiler {
630630
return undefined;
631631
}
632632

633+
// We update the yaml file content to the transpiled js content
634+
// and raise related flag so it will go JS transpilation flow afterward
635+
// avoiding costly YAML/Python parsing again.
633636
private async transpileYamlFile(
634637
file: FileContent,
635638
errorsReport: ErrorReporter,
636-
{ cubeNames, cubeSymbols, contextSymbols, transpilerNames, compilerId, stage }: TranspileOptions
639+
{ cubeNames, cubeSymbols, compilerId }: TranspileOptions
637640
): Promise<(FileContent | undefined)> {
638641
const cacheKey = crypto.createHash('md5').update(JSON.stringify(file.content)).digest('hex');
639642

@@ -644,9 +647,27 @@ export class DataSchemaCompiler {
644647
return { ...file };
645648
}
646649

647-
/* if (getEnv('transpilationNative')) {
650+
if (getEnv('transpilationNative')) {
651+
const reqData = {
652+
fileName: file.fileName,
653+
fileContent: file.content,
654+
transpilers: [],
655+
compilerId: compilerId || '',
656+
};
657+
658+
errorsReport.inFile(file);
659+
const res = await transpileYaml([reqData]);
660+
errorsReport.addErrors(res[0].errors);
661+
errorsReport.addWarnings(res[0].warnings as unknown as SyntaxErrorInterface[]);
662+
errorsReport.exitFile();
663+
664+
file.content = res[0].code;
665+
file.convertedToJs = true;
666+
667+
this.compiledYamlCache.set(cacheKey, res[0].code);
648668

649-
} else */ if (getEnv('transpilationWorkerThreads')) {
669+
return { ...file };
670+
} else if (getEnv('transpilationWorkerThreads')) {
650671
const data = {
651672
fileName: file.fileName,
652673
content: file.content,
@@ -664,14 +685,11 @@ export class DataSchemaCompiler {
664685

665686
this.compiledYamlCache.set(cacheKey, res.content);
666687

667-
return { ...file, content: res.content };
688+
return { ...file };
668689
} else {
669690
const transpiledFile = this.yamlCompiler.transpileYamlFile(file, errorsReport);
670691

671692
if (transpiledFile) {
672-
// We update the yaml file content to the transpiled js content
673-
// and raise related flag so it will go JS transpilation flow afterward
674-
// avoiding costly YAML/Python parsing again.
675693
file.content = transpiledFile.content;
676694
file.convertedToJs = true;
677695
}

0 commit comments

Comments
 (0)