Skip to content

Commit 8228456

Browse files
committed
Prepare transpileYaml in native + schema compiler
1 parent 20a1967 commit 8228456

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';
@@ -634,10 +634,13 @@ export class DataSchemaCompiler {
634634
return undefined;
635635
}
636636

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

@@ -648,9 +651,27 @@ export class DataSchemaCompiler {
648651
return { ...file };
649652
}
650653

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

653-
} else */ if (getEnv('transpilationWorkerThreads')) {
673+
return { ...file };
674+
} else if (getEnv('transpilationWorkerThreads')) {
654675
const data = {
655676
fileName: file.fileName,
656677
content: file.content,
@@ -668,14 +689,11 @@ export class DataSchemaCompiler {
668689

669690
this.compiledYamlCache.set(cacheKey, res.content);
670691

671-
return { ...file, content: res.content };
692+
return { ...file };
672693
} else {
673694
const transpiledFile = this.yamlCompiler.transpileYamlFile(file, errorsReport);
674695

675696
if (transpiledFile) {
676-
// We update the yaml file content to the transpiled js content
677-
// and raise related flag so it will go JS transpilation flow afterward
678-
// avoiding costly YAML/Python parsing again.
679697
file.content = transpiledFile.content;
680698
file.convertedToJs = true;
681699
}

0 commit comments

Comments
 (0)