Skip to content

Commit

Permalink
Merge pull request #564 from jvalue/simple-file-import
Browse files Browse the repository at this point in the history
Simple file import
  • Loading branch information
georg-schwarz authored May 21, 2024
2 parents ed3eaca + 63c607b commit 84d9503
Show file tree
Hide file tree
Showing 50 changed files with 714 additions and 107 deletions.
2 changes: 1 addition & 1 deletion example/electric-vehicles.jv
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ pipeline ElectricVehiclesPipeline {
"Electric Range"
];
outputColumn: "Electric Range (km)";
use: MilesToKilometers;
uses: MilesToKilometers;
}

// 6. Here, we define a transform function, taking parameters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class TableTransformerExecutor extends AbstractBlockExecutor<
context.valueTypeProvider.Primitives.Text,
);
const usedTransform = context.getPropertyValue(
'use',
'uses',
context.valueTypeProvider.Primitives.Transform,
);

Expand Down Expand Up @@ -122,7 +122,7 @@ export class TableTransformerExecutor extends AbstractBlockExecutor<
return R.err({
message: `Type ${inputColumn.valueType.getName()} of column "${inputColumnName}" is not convertible to type ${matchingInputDetails.valueType.getName()}`,
diagnostic: {
node: context.getOrFailProperty('use'),
node: context.getOrFailProperty('uses'),
},
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pipeline TestPipeline {
block TestBlock oftype TableTransformer {
inputColumns: ['index'];
outputColumn: 'index';
use: Double;
uses: Double;
}

block TestLoader oftype TestTableLoader {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pipeline TestPipeline {
block TestBlock oftype TableTransformer {
inputColumns: ['index'];
outputColumn: 'index';
use: ToBool;
uses: ToBool;
}

block TestLoader oftype TestTableLoader {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pipeline TestPipeline {
block TestBlock oftype TableTransformer {
inputColumns: ['index', 'id'];
outputColumn: 'add';
use: Add;
uses: Add;
}

block TestLoader oftype TestTableLoader {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pipeline TestPipeline {
block TestBlock oftype TableTransformer {
inputColumns: ['index'];
outputColumn: 'index2';
use: Double;
uses: Double;
}

block TestLoader oftype TestTableLoader {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pipeline TestPipeline {
block TestBlock oftype TableTransformer {
inputColumns: ['name'];
outputColumn: 'index2';
use: Double;
uses: Double;
}

block TestLoader oftype TestTableLoader {
Expand Down
3 changes: 1 addition & 2 deletions libs/interpreter-lib/src/interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import {
type RuntimeParameterProvider,
type WrapperFactoryProvider,
createJayveeServices,
initializeWorkspace,
internalValueToString,
} from '@jvalue/jayvee-language-server';
import chalk from 'chalk';
Expand Down Expand Up @@ -72,6 +71,7 @@ export async function interpretString(
* Parses a model without executing it.
* Also sets up the environment so that the model can be properly executed.
*
* @param extractAstNodeFn method that extracts the AST node; should also initialize the workspace correctly.
* @returns non-null model, services and loggerFactory on success.
*/
export async function parseModel(
Expand Down Expand Up @@ -101,7 +101,6 @@ export async function parseModel(
}

services = createJayveeServices(NodeFileSystem).Jayvee;
await initializeWorkspace(services);
setupJayveeServices(services, options.env);

try {
Expand Down
65 changes: 27 additions & 38 deletions libs/interpreter-lib/src/parsing-util.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,65 +8,47 @@ import { CachedLogger, DiagnosticSeverity } from '@jvalue/jayvee-execution';
import {
type JayveeServices,
createJayveeServices,
initializeWorkspace,
} from '@jvalue/jayvee-language-server';
import {
type ParseHelperOptions,
expectNoParserAndLexerErrors,
loadTestExtensions,
parseHelper,
readJvTestAssetHelper,
parseTestFileInWorkingDir,
} from '@jvalue/jayvee-language-server/test';
import { type AstNode, type LangiumDocument } from 'langium';
import { NodeFileSystem } from 'langium/node';

import { extractDocumentFromFile, validateDocument } from './parsing-util';

describe('Validation of parsing-util', () => {
let parse: (
input: string,
options?: ParseHelperOptions,
) => Promise<LangiumDocument<AstNode>>;

const WORKING_DIR = path.resolve(__dirname, '../test/assets/parsing-util/');
let services: JayveeServices;

const logger = new CachedLogger(true, undefined, false);

const readJvTestAsset = readJvTestAssetHelper(
__dirname,
'../test/assets/parsing-util/',
);

beforeAll(async () => {
beforeAll(() => {
// Create language services
services = createJayveeServices(NodeFileSystem).Jayvee;

await loadTestExtensions(services, [
path.resolve(
__dirname,
'../test/assets/parsing-util/test-extension/TestBlockTypes.jv',
),
]);

// Parse function for Jayvee (without validation)
parse = parseHelper(services);
});

afterEach(() => {
logger.clearLogs();
});

describe('Function of validateDocument', () => {
async function parseAndValidateDocument(input: string) {
const document = await parse(input);
async function parseAndValidateDocument(relativeTestFilePath: string) {
const document = await parseTestFileInWorkingDir(
WORKING_DIR,
relativeTestFilePath,
services,
);
expectNoParserAndLexerErrors(document);

return validateDocument(document, services, logger);
}

it('should diagnose no error on valid document', async () => {
const text = readJvTestAsset('validateDocument/valid-document.jv');
const relativeTestFilePath = 'validateDocument/valid-document.jv';

await parseAndValidateDocument(text);
await parseAndValidateDocument(relativeTestFilePath);

expect(logger.getLogs(DiagnosticSeverity.ERROR)).toHaveLength(0);
expect(logger.getLogs(DiagnosticSeverity.INFO)).toHaveLength(0);
Expand All @@ -76,12 +58,11 @@ describe('Validation of parsing-util', () => {
});

it('should diagnose error on wrong loader type', async () => {
const text = readJvTestAsset(
'validateDocument/invalid-wrong-loader-type.jv',
);
const relativeTestFilePath =
'validateDocument/invalid-wrong-loader-type.jv';

try {
await parseAndValidateDocument(text);
await parseAndValidateDocument(relativeTestFilePath);
} catch (e) {
expect(logger.getLogs(DiagnosticSeverity.INFO)).toHaveLength(0);
expect(logger.getLogs(DiagnosticSeverity.ERROR)).toHaveLength(2 * 5); // 2 calls that get formated to 5 lines each
Expand All @@ -92,12 +73,10 @@ describe('Validation of parsing-util', () => {
});

it('should diagnose no error on nonErr diagnostics', async () => {
const text = readJvTestAsset(
'validateDocument/valid-simplify-warning.jv',
);
const relativeTestFilePath = 'validateDocument/valid-simplify-warning.jv';

try {
await parseAndValidateDocument(text);
await parseAndValidateDocument(relativeTestFilePath);
} catch (e) {
expect(logger.getLogs(DiagnosticSeverity.INFO)).toHaveLength(0);
expect(logger.getLogs(DiagnosticSeverity.ERROR)).toHaveLength(1);
Expand All @@ -109,6 +88,16 @@ describe('Validation of parsing-util', () => {
});

describe('Function of extractDocumentFromFile', () => {
beforeAll(async () => {
// register test extension dir as not below test files
await initializeWorkspace(services, [
{
name: 'testExtension',
uri: WORKING_DIR,
},
]);
});

it('should diagnose no error on valid model file', async () => {
await extractDocumentFromFile(
path.resolve(
Expand Down
28 changes: 23 additions & 5 deletions libs/interpreter-lib/src/parsing-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ export enum ExitCode {
FAILURE = 1,
}

/**
* Extracts a document from a file that contains a model.
* Does load the directory of this document as the working directory.
*/
export async function extractDocumentFromFile(
fileName: string,
services: LangiumServices,
Expand All @@ -37,16 +41,30 @@ export async function extractDocumentFromFile(
return Promise.reject(ExitCode.FAILURE);
}

const document =
await services.shared.workspace.LangiumDocuments.getOrCreateDocument(
URI.file(path.resolve(fileName)),
);
const workingDirPath = path.dirname(fileName);

await initializeWorkspace(services);
await initializeWorkspace(services, [
{
name: 'projectRoot',
uri: path.resolve(workingDirPath),
},
]);

const document = services.shared.workspace.LangiumDocuments.getDocument(
URI.file(path.resolve(fileName)),
);
if (document === undefined) {
logger.logErr(`Did not load file ${fileName} correctly.`);
return Promise.reject(ExitCode.FAILURE);
}

return await validateDocument(document, services, logger);
}

/**
* Extracts a document from a string that contains a model.
* Does not load an additional working directory.
*/
export async function extractDocumentFromString(
modelString: string,
services: LangiumServices,
Expand Down
Loading

0 comments on commit 84d9503

Please sign in to comment.