Skip to content

Commit

Permalink
adopt new grammar-JSON transformation API endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
akphi committed Mar 24, 2022
1 parent b9fc174 commit 24d02e8
Show file tree
Hide file tree
Showing 22 changed files with 218 additions and 349 deletions.
5 changes: 5 additions & 0 deletions .changeset/fluffy-gorillas-drum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@finos/legend-graph': patch
---

Adopt new `grammar - JSON` transformation API endpoints.
5 changes: 5 additions & 0 deletions .changeset/smooth-hornets-join.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@finos/legend-graph': patch
'@finos/legend-query': patch
'@finos/legend-studio': patch
---
5 changes: 5 additions & 0 deletions .changeset/sour-houses-promise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@finos/legend-graph': patch
'@finos/legend-query': patch
'@finos/legend-studio': patch
---
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ export abstract class AbstractPureGraphManager {
abstract pureCodeToLambda(
lambda: string,
lambdaId?: string,
): Promise<RawLambda | undefined>;
): Promise<RawLambda>;
abstract lambdaToPureCode(
lambda: RawLambda,
lambdaId?: string,
Expand All @@ -186,7 +186,7 @@ export abstract class AbstractPureGraphManager {
abstract pureCodeToRelationalOperationElement(
operation: string,
operationId: string,
): Promise<RawRelationalOperationElement | undefined>;
): Promise<RawRelationalOperationElement>;
abstract relationalOperationElementToPureCode(
operations: Map<string, RawRelationalOperationElement>,
): Promise<Map<string, string>>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1434,9 +1434,9 @@ export class V1_PureGraphManager extends AbstractPureGraphManager {
async pureCodeToLambda(
lambda: string,
lambdaId = 'stub_lambdaId',
): Promise<RawLambda | undefined> {
): Promise<RawLambda> {
const result = await this.engine.transformCodeToLambda(lambda, lambdaId);
return result ? new RawLambda(result.parameters, result.body) : undefined;
return new RawLambda(result.parameters, result.body);
}

async lambdaToPureCode(
Expand Down Expand Up @@ -1465,7 +1465,7 @@ export class V1_PureGraphManager extends AbstractPureGraphManager {
pureCodeToRelationalOperationElement(
operation: string,
operationId: string,
): Promise<RawRelationalOperationElement | undefined> {
): Promise<RawRelationalOperationElement> {
return this.engine.transformPureCodeToRelationalOperationElement(
operation,
operationId,
Expand Down
136 changes: 65 additions & 71 deletions packages/legend-graph/src/models/protocols/pure/v1/engine/V1_Engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
HttpStatus,
NetworkClientError,
returnUndefOnError,
deseralizeMap,
} from '@finos/legend-shared';
import { GRAPH_MANAGER_LOG_EVENT } from '../../../../../graphManager/GraphManagerLogEvent';
import {
Expand All @@ -41,11 +42,6 @@ import { TEMPORARY__AbstractEngineConfig } from '../../../../../graphManager/act
import { V1_EngineServerClient } from './V1_EngineServerClient';
import type { V1_PureModelContextData } from '../model/context/V1_PureModelContextData';
import type { V1_LambdaReturnTypeResult } from '../engine/compilation/V1_LambdaReturnTypeResult';
import {
V1_JsonToGrammarInput,
V1_RenderStyle,
} from '../engine/grammar/V1_JsonToGrammarInput';
import { V1_GrammarToJsonInput } from '../engine/grammar/V1_GrammarToJson';
import type { V1_RawLambda } from '../model/rawValueSpecification/V1_RawLambda';
import {
V1_deserializePureModelContextData,
Expand All @@ -63,8 +59,6 @@ import { V1_ParserError } from '../engine/grammar/V1_ParserError';
import { V1_CompilationError } from '../engine/compilation/V1_CompilationError';
import type { V1_RawRelationalOperationElement } from '../model/packageableElements/store/relational/model/V1_RawRelationalOperationElement';
import type { RawRelationalOperationElement } from '../../../../metamodels/pure/packageableElements/store/relational/model/RawRelationalOperationElement';
import { V1_RelationalOperationElementJsonToGrammarInput } from './grammar/V1_RelationalOperationElementJsonToGrammarInput';
import { V1_RelationalOperationElementGrammarToJsonInput } from './grammar/V1_RelationalOperationElementGrammarToJson';
import { V1_GraphTransformerContextBuilder } from '../transformation/pureGraph/from/V1_GraphTransformerContext';
import type { PureProtocolProcessorPlugin } from '../../PureProtocolProcessorPlugin';
import {
Expand Down Expand Up @@ -96,6 +90,7 @@ import type { ExecutionOptions } from '../../../../../graphManager/AbstractPureG
import type { ExternalFormatDescription } from '../../../../../graphManager/action/externalFormat/ExternalFormatDescription';
import { V1_ExternalFormatDescription } from './externalFormat/V1_ExternalFormatDescription';
import { V1_ExternalFormatModelGenerationInput } from './externalFormat/V1_ExternalFormatModelGeneration';
import { V1_RenderStyle } from './grammar/V1_RenderStyle';

class V1_EngineConfig extends TEMPORARY__AbstractEngineConfig {
private engine: V1_Engine;
Expand Down Expand Up @@ -191,15 +186,12 @@ export class V1_Engine {

// ------------------------------------------- Grammar -------------------------------------------

async pureModelContextDataToPureCode(
pureModelContextDataToPureCode(
graph: V1_PureModelContextData,
): Promise<string> {
return (
((
await this.engineServerClient.transformJSONToGrammar({
modelDataContext: this.serializePureModelContextData(graph),
})
).code as string | undefined) ?? ''
return this.engineServerClient.JSONToGrammar_model(
this.serializePureModelContextData(graph),
V1_RenderStyle.STANDARD,
);
}

Expand All @@ -208,28 +200,31 @@ export class V1_Engine {
options?: { onError?: () => void },
): Promise<V1_PureModelContextData> {
return V1_deserializePureModelContextData(
await this.pureCodeToPureModelContextDataJson(code, options),
await this.pureCodeToPureModelContextDataJSON(code, options),
);
}

private async pureCodeToPureModelContextDataJson(
private async pureCodeToPureModelContextDataJSON(
code: string,
options?: { onError?: () => void },
): Promise<PlainObject<V1_PureModelContextData>> {
const parsingResult = await this.engineServerClient.transformGrammarToJSON({
code,
});
if (parsingResult.codeError) {
try {
return await this.engineServerClient.grammarToJSON_model(code);
} catch (error) {
assertErrorThrown(error);
options?.onError?.();
throw V1_buildParserError(
V1_ParserError.serialization.fromJson(
parsingResult.codeError as PlainObject<V1_ParserError>,
),
);
if (
error instanceof NetworkClientError &&
error.response.status === HttpStatus.BAD_REQUEST
) {
throw V1_buildCompilationError(
V1_ParserError.serialization.fromJson(
error.payload as PlainObject<V1_ParserError>,
),
);
}
throw error;
}
return guaranteeNonNullable(
parsingResult.modelDataContext,
) as PlainObject<V1_PureModelContextData>;
}

async transformLambdasToCode(
Expand All @@ -248,30 +243,31 @@ export class V1_Engine {
),
);
});
const result = V1_GrammarToJsonInput.serialization.fromJson(
await this.engineServerClient.transformJSONToGrammar({
isolatedLambdas: { lambdas },
renderStyle: pretty ? V1_RenderStyle.PRETTY : V1_RenderStyle.STANDARD,
}),
return deseralizeMap(
await this.engineServerClient.JSONToGrammar_lambda_batch(
lambdas,
pretty ? V1_RenderStyle.PRETTY : V1_RenderStyle.STANDARD,
),
);
return result.isolatedLambdas ?? new Map<string, string>();
}

async transformCodeToLambda(
lambda: string,
lambdaId: string,
): Promise<V1_RawLambda | undefined> {
const result = V1_JsonToGrammarInput.serialization.fromJson(
await this.engineServerClient.transformGrammarToJSON({
isolatedLambdas: { [lambdaId]: lambda },
}),
);
const lambdaResult = guaranteeNonNullable(result.isolatedLambdas);
const parserError = lambdaResult.lambdaErrors?.get(lambdaId);
): Promise<V1_RawLambda> {
const result = await this.engineServerClient.grammarToJSON_lambda_batch({
[lambdaId]: lambda,
});
const parserError = result.errors?.[lambdaId];
if (parserError) {
throw V1_buildParserError(parserError);
throw V1_buildParserError(
V1_ParserError.serialization.fromJson(parserError),
);
}
return lambdaResult.lambdas?.get(lambdaId);
return guaranteeNonNullable(
result.result?.[lambdaId],
`Can't extract parsed lambda with ID '${lambdaId}' from parse result`,
);
}

async transformRelationalOperationElementsToPureCode(
Expand All @@ -285,34 +281,34 @@ export class V1_Engine {
operations[key] =
inputOperation as PlainObject<V1_RawRelationalOperationElement>;
});
const result =
V1_RelationalOperationElementGrammarToJsonInput.serialization.fromJson(
await this.engineServerClient.transformRelationalOperationElementJSONToGrammar(
{
operations,
},
),
);
return result.operations;
return deseralizeMap(
await this.engineServerClient.JSONToGrammar_relationalOperationElement_batch(
operations,
V1_RenderStyle.STANDARD,
),
);
}

async transformPureCodeToRelationalOperationElement(
operation: string,
operationId: string,
): Promise<V1_RawRelationalOperationElement | undefined> {
): Promise<V1_RawRelationalOperationElement> {
const result =
V1_RelationalOperationElementJsonToGrammarInput.serialization.fromJson(
await this.engineServerClient.transformRelationalOperationElementGrammarToJSON(
{
operations: { [operationId]: operation },
},
),
await this.engineServerClient.grammarToJSON_relationalOperationElement_batch(
{
[operationId]: operation,
},
);
const parserError = result.operationErrors?.get(operationId);
const parserError = result.errors?.[operationId];
if (parserError) {
throw V1_buildParserError(parserError);
throw V1_buildParserError(
V1_ParserError.serialization.fromJson(parserError),
);
}
return result.operations.get(operationId);
return guaranteeNonNullable(
result.result?.[operationId],
`Can't extract parsed relational operation element with ID '${operationId}' from parse result`,
);
}

// ------------------------------------------- Compile -------------------------------------------
Expand Down Expand Up @@ -347,7 +343,7 @@ export class V1_Engine {
compileContext?: V1_PureModelContextData,
options?: { onError?: () => void },
): Promise<V1_PureModelContextData> {
const mainGraph = await this.pureCodeToPureModelContextDataJson(
const mainGraph = await this.pureCodeToPureModelContextDataJSON(
graphText,
options,
);
Expand Down Expand Up @@ -521,15 +517,13 @@ export class V1_Engine {
async generateModel(
input: V1_ExternalFormatModelGenerationInput,
): Promise<string> {
const pmcd = (await this.engineServerClient.generateModel(
const model = (await this.engineServerClient.generateModel(
V1_ExternalFormatModelGenerationInput.serialization.toJson(input),
)) as unknown as PlainObject<V1_PureModelContextData>;
const pureCode =
((
await this.engineServerClient.transformJSONToGrammar({
modelDataContext: pmcd,
})
).code as string | undefined) ?? '';
const pureCode = await this.engineServerClient.JSONToGrammar_model(
model,
V1_RenderStyle.STANDARD,
);
return pureCode;
}

Expand Down
Loading

0 comments on commit 24d02e8

Please sign in to comment.