Add support for more depth in function calls

It allow pipes to be used in nested functions. Before, pipes were added
to a variable before variable content was evaluated/compiled by
another function. This commit ensures that the commits are evaluted in
expected order.

The issue is solved by stopping precompiling functions. It makes code
less complex. It adds to compile time of the script file but nothing
noticable and something optimizable.

The problem was that the call trees we're not executed in expected
order. E.g. let's say we have functionA that outputs something like
"Hello {{ $name| pipe }}", and we have function B calling with "name:
dear {{ $firstName}}", and at last we have a script that's calling
function B with "firstName: undergroundwires". Before, expressions were
evaluated directly, meaning that function A would become:
"Hello Dear {{ $firstName}}", as you see the pipe in function A
is lost here after being applied to function B and not reaching
$firstTime input value. Parsing expressions in the end allows for pipes
etc. to not get lost.

The commit also does necessary name refactorings and folder refactorings
to reflect logical changes. `FunctionCompiler` is renamed to
`SharedFunctionsParser` as precompiling is removed and it just simply
parses now. `/FunctionCall/` is moved to `/Function/Call`.

Finally, it improves documentation and adds more tests.
This commit is contained in:
undergroundwires
2021-10-04 18:13:25 +01:00
parent f39ee76c0c
commit 20b7d283b0
56 changed files with 1678 additions and 869 deletions

View File

@@ -1,6 +1,6 @@
import { IExpressionEvaluationContext } from '@/application/Parser/Script/Compiler/Expressions/Expression/ExpressionEvaluationContext';
import { IPipelineCompiler } from '@/application/Parser/Script/Compiler/Expressions/Pipes/IPipelineCompiler';
import { IReadOnlyFunctionCallArgumentCollection } from '@/application/Parser/Script/Compiler/FunctionCall/Argument/IFunctionCallArgumentCollection';
import { IReadOnlyFunctionCallArgumentCollection } from '@/application/Parser/Script/Compiler/Function/Call/Argument/IFunctionCallArgumentCollection';
import { FunctionCallArgumentCollectionStub } from './FunctionCallArgumentCollectionStub';
import { PipelineCompilerStub } from './PipelineCompilerStub';

View File

@@ -1,6 +1,8 @@
import { IExpressionsCompiler } from '@/application/Parser/Script/Compiler/Expressions/IExpressionsCompiler';
import { IReadOnlyFunctionCallArgumentCollection } from '@/application/Parser/Script/Compiler/FunctionCall/Argument/IFunctionCallArgumentCollection';
import { IReadOnlyFunctionCallArgumentCollection } from '@/application/Parser/Script/Compiler/Function/Call/Argument/IFunctionCallArgumentCollection';
import { scrambledEqual } from '@/application/Common/Array';
import { ISharedFunction } from '@/application/Parser/Script/Compiler/Function/ISharedFunction';
import { FunctionCallArgumentCollectionStub } from '@tests/unit/stubs/FunctionCallArgumentCollectionStub';
export class ExpressionsCompilerStub implements IExpressionsCompiler {
@@ -8,32 +10,34 @@ export class ExpressionsCompilerStub implements IExpressionsCompiler {
private readonly scenarios = new Array<ITestScenario>();
public setup(
code: string,
parameters: IReadOnlyFunctionCallArgumentCollection,
result: string): ExpressionsCompilerStub {
this.scenarios.push({ code, parameters, result });
public setup(scenario: ITestScenario): ExpressionsCompilerStub {
this.scenarios.push(scenario);
return this;
}
public setupToReturnFunctionCode(func: ISharedFunction, givenArgs: FunctionCallArgumentCollectionStub) {
return this
.setup({ givenCode: func.body.code.do, givenArgs, result: func.body.code.do })
.setup({ givenCode: func.body.code.revert, givenArgs, result: func.body.code.revert });
}
public compileExpressions(
code: string,
parameters: IReadOnlyFunctionCallArgumentCollection): string {
this.callHistory.push({ code, parameters});
const scenario = this.scenarios.find((s) => s.code === code && deepEqual(s.parameters, parameters));
const scenario = this.scenarios.find((s) => s.givenCode === code && deepEqual(s.givenArgs, parameters));
if (scenario) {
return scenario.result;
}
const parametersAndValues = parameters
.getAllParameterNames()
.map((name) => `${name}=${parameters.getArgument(name).argumentValue}`)
.join('", "');
return `[ExpressionsCompilerStub] code: "${code}" | parameters: "${parametersAndValues}"`;
.join('\n\t');
return `[ExpressionsCompilerStub]\ncode: "${code}"\nparameters: ${parametersAndValues}`;
}
}
interface ITestScenario {
readonly code: string;
readonly parameters: IReadOnlyFunctionCallArgumentCollection;
readonly givenCode: string;
readonly givenArgs: IReadOnlyFunctionCallArgumentCollection;
readonly result: string;
}
@@ -46,8 +50,8 @@ function deepEqual(
return false;
}
for (const parameterName of expectedParameterNames) {
const expectedValue = expected.getArgument(parameterName);
const actualValue = expected.getArgument(parameterName);
const expectedValue = expected.getArgument(parameterName).argumentValue;
const actualValue = actual.getArgument(parameterName).argumentValue;
if (expectedValue !== actualValue) {
return false;
}

View File

@@ -1,6 +1,6 @@
// tslint:disable-next-line:max-line-length
import { IFunctionCallArgument } from '@/application/Parser/Script/Compiler/FunctionCall/Argument/IFunctionCallArgument';
import { IFunctionCallArgumentCollection } from '@/application/Parser/Script/Compiler/FunctionCall/Argument/IFunctionCallArgumentCollection';
import { IFunctionCallArgument } from '@/application/Parser/Script/Compiler/Function/Call/Argument/IFunctionCallArgument';
import { IFunctionCallArgumentCollection } from '@/application/Parser/Script/Compiler/Function/Call/Argument/IFunctionCallArgumentCollection';
import { FunctionCallArgumentStub } from './FunctionCallArgumentStub';
export class FunctionCallArgumentCollectionStub implements IFunctionCallArgumentCollection {

View File

@@ -1,5 +1,5 @@
// tslint:disable-next-line:max-line-length
import { IFunctionCallArgument } from '@/application/Parser/Script/Compiler/FunctionCall/Argument/IFunctionCallArgument';
import { IFunctionCallArgument } from '@/application/Parser/Script/Compiler/Function/Call/Argument/IFunctionCallArgument';
export class FunctionCallArgumentStub implements IFunctionCallArgument {
public parameterName = 'stub-parameter-name';

View File

@@ -1,26 +1,40 @@
import { ICompiledCode } from '@/application/Parser/Script/Compiler/Function/Call/Compiler/ICompiledCode';
import { IFunctionCallCompiler } from '@/application/Parser/Script/Compiler/Function/Call/Compiler/IFunctionCallCompiler';
import { ISharedFunctionCollection } from '@/application/Parser/Script/Compiler/Function/ISharedFunctionCollection';
import { ICompiledCode } from '@/application/Parser/Script/Compiler/FunctionCall/ICompiledCode';
import { IFunctionCallCompiler } from '@/application/Parser/Script/Compiler/FunctionCall/IFunctionCallCompiler';
import { FunctionCallData, ScriptFunctionCallData } from 'js-yaml-loader!@/*';
import { IFunctionCall } from '@/application/Parser/Script/Compiler/Function/Call/IFunctionCall';
interface Scenario { call: ScriptFunctionCallData; functions: ISharedFunctionCollection; result: ICompiledCode; }
interface IScenario {
calls: IFunctionCall[];
functions: ISharedFunctionCollection;
result: ICompiledCode;
}
export class FunctionCallCompilerStub implements IFunctionCallCompiler {
public scenarios = new Array<Scenario>();
public setup(call: ScriptFunctionCallData, functions: ISharedFunctionCollection, result: ICompiledCode) {
this.scenarios.push({ call, functions, result });
public scenarios = new Array<IScenario>();
public setup(
calls: IFunctionCall[],
functions: ISharedFunctionCollection,
result: ICompiledCode) {
this.scenarios.push({ calls, functions, result });
}
public compileCall(
call: ScriptFunctionCallData,
calls: IFunctionCall[],
functions: ISharedFunctionCollection): ICompiledCode {
const predefined = this.scenarios.find((s) => s.call === call && s.functions === functions);
const predefined = this.scenarios.find((s) => areEqual(s.calls, calls) && s.functions === functions);
if (predefined) {
return predefined.result;
}
const callee = functions.getFunctionByName((call as FunctionCallData).function);
return {
code: callee.code,
revertCode: callee.revertCode,
code: 'function code [FunctionCallCompilerStub]',
revertCode: 'function revert code [FunctionCallCompilerStub]',
};
}
}
function areEqual(
first: readonly IFunctionCall[],
second: readonly IFunctionCall[]) {
const comparer = (a: IFunctionCall, b: IFunctionCall) => a.functionName.localeCompare(b.functionName);
const printSorted = (calls: readonly IFunctionCall[]) => JSON.stringify([...calls].sort(comparer));
return printSorted(first) === printSorted(second);
}

View File

@@ -0,0 +1,15 @@
import { FunctionCallData, FunctionCallParametersData } from 'js-yaml-loader!@/*';
export class FunctionCallDataStub implements FunctionCallData {
public function = 'callDatStubCalleeFunction';
public parameters: { [index: string]: string } = { testParameter : 'testArgument' };
public withName(functionName: string) {
this.function = functionName;
return this;
}
public withParameters(parameters: FunctionCallParametersData) {
this.parameters = parameters;
return this;
}
}

View File

@@ -0,0 +1,24 @@
import { IFunctionCall } from '@/application/Parser/Script/Compiler/Function/Call/IFunctionCall';
import { FunctionCallArgumentCollectionStub } from './FunctionCallArgumentCollectionStub';
export class FunctionCallStub implements IFunctionCall {
public functionName = 'functionCallStub';
public args = new FunctionCallArgumentCollectionStub();
public withFunctionName(functionName: string) {
this.functionName = functionName;
return this;
}
public withArgument(parameterName: string, argumentValue: string) {
this.args.withArgument(parameterName, argumentValue);
return this;
}
public withArguments(args: { readonly [index: string]: string }) {
this.args.withArguments(args);
return this;
}
public withArgumentCollection(args: FunctionCallArgumentCollectionStub) {
this.args = args;
return this;
}
}

View File

@@ -0,0 +1,14 @@
import { IFunctionCode } from '@/application/Parser/Script/Compiler/Function/ISharedFunction';
export class FunctionCodeStub implements IFunctionCode {
public do: string = 'do code (function-code-stub)';
public revert?: string = 'revert code (function-code-stub)';
public withDo(code: string) {
this.do = code;
return this;
}
public withRevert(revert: string) {
this.revert = revert;
return this;
}
}

View File

@@ -1,4 +1,5 @@
import { FunctionData, ParameterDefinitionData, ScriptFunctionCallData } from 'js-yaml-loader!@/*';
import { FunctionData, ParameterDefinitionData, FunctionCallsData } from 'js-yaml-loader!@/*';
import { FunctionCallDataStub } from './FunctionCallDataStub';
export class FunctionDataStub implements FunctionData {
public static createWithCode() {
@@ -6,7 +7,7 @@ export class FunctionDataStub implements FunctionData {
.withCode('stub-code')
.withRevertCode('stub-revert-code');
}
public static createWithCall(call?: ScriptFunctionCallData) {
public static createWithCall(call?: FunctionCallsData) {
let instance = new FunctionDataStub();
if (call) {
instance = instance.withCall(call);
@@ -22,10 +23,10 @@ export class FunctionDataStub implements FunctionData {
public name = 'functionDataStub';
public code: string;
public revertCode: string;
public call?: ScriptFunctionCallData;
public call?: FunctionCallsData;
public parameters?: readonly ParameterDefinitionData[];
private constructor() { }
private constructor() { /* use static factory methods to create an instance */ }
public withName(name: string) {
this.name = name;
@@ -46,12 +47,12 @@ export class FunctionDataStub implements FunctionData {
this.revertCode = revertCode;
return this;
}
public withCall(call: ScriptFunctionCallData) {
public withCall(call: FunctionCallsData) {
this.call = call;
return this;
}
public withMockCall() {
this.call = { function: 'func' };
this.call = new FunctionCallDataStub();
return this;
}
}

View File

@@ -1,5 +1,6 @@
import { RecommendationLevel } from '@/domain/RecommendationLevel';
import { ScriptFunctionCallData, ScriptData } from 'js-yaml-loader!@/*';
import { FunctionCallData, ScriptData } from 'js-yaml-loader!@/*';
import { FunctionCallDataStub } from '@tests/unit/stubs/FunctionCallDataStub';
export class ScriptDataStub implements ScriptData {
public static createWithCode(): ScriptDataStub {
@@ -7,7 +8,7 @@ export class ScriptDataStub implements ScriptData {
.withCode('stub-code')
.withRevertCode('stub-revert-code');
}
public static createWithCall(call?: ScriptFunctionCallData): ScriptDataStub {
public static createWithCall(call?: FunctionCallData): ScriptDataStub {
let instance = new ScriptDataStub();
if (call) {
instance = instance.withCall(call);
@@ -27,7 +28,7 @@ export class ScriptDataStub implements ScriptData {
public recommend = RecommendationLevel[RecommendationLevel.Standard].toLowerCase();
public docs = ['hello.com'];
private constructor() { }
private constructor() { /* use static methods for constructing */ }
public withName(name: string): ScriptDataStub {
this.name = name;
@@ -46,10 +47,10 @@ export class ScriptDataStub implements ScriptData {
return this;
}
public withMockCall(): ScriptDataStub {
this.call = { function: 'func', parameters: [] };
this.call = new FunctionCallDataStub();
return this;
}
public withCall(call: ScriptFunctionCallData): ScriptDataStub {
public withCall(call: FunctionCallData): ScriptDataStub {
this.call = call;
return this;
}

View File

@@ -1,18 +1,21 @@
import { ISharedFunction } from '@/application/Parser/Script/Compiler/Function/ISharedFunction';
import { ISharedFunctionCollection } from '@/application/Parser/Script/Compiler/Function/ISharedFunctionCollection';
import { SharedFunctionStub } from './SharedFunctionStub';
import { FunctionBodyType } from '@/application/Parser/Script/Compiler/Function/ISharedFunction';
export class SharedFunctionCollectionStub implements ISharedFunctionCollection {
private readonly functions = new Map<string, ISharedFunction>();
public withFunction(func: ISharedFunction) {
this.functions.set(func.name, func);
public withFunction(...funcs: readonly ISharedFunction[]) {
for (const func of funcs) {
this.functions.set(func.name, func);
}
return this;
}
public getFunctionByName(name: string): ISharedFunction {
if (this.functions.has(name)) {
return this.functions.get(name);
}
return new SharedFunctionStub()
return new SharedFunctionStub(FunctionBodyType.Code)
.withName(name)
.withCode('code by SharedFunctionCollectionStub')
.withRevertCode('revert-code by SharedFunctionCollectionStub');

View File

@@ -1,13 +1,33 @@
import { ISharedFunction } from '@/application/Parser/Script/Compiler/Function/ISharedFunction';
import { ISharedFunction, ISharedFunctionBody, FunctionBodyType } from '@/application/Parser/Script/Compiler/Function/ISharedFunction';
import { IReadOnlyFunctionParameterCollection } from '@/application/Parser/Script/Compiler/Function/Parameter/IFunctionParameterCollection';
import { FunctionParameterCollectionStub } from './FunctionParameterCollectionStub';
import { FunctionCallStub } from './FunctionCallStub';
import { IFunctionCall } from '@/application/Parser/Script/Compiler/Function/Call/IFunctionCall';
export class SharedFunctionStub implements ISharedFunction {
public name = 'shared-function-stub-name';
public parameters: IReadOnlyFunctionParameterCollection = new FunctionParameterCollectionStub()
.withParameterName('shared-function-stub-parameter-name');
public code = 'shared-function-stub-code';
public revertCode = 'shared-function-stub-revert-code';
private code = 'shared-function-stub-code';
private revertCode = 'shared-function-stub-revert-code';
private bodyType: FunctionBodyType = FunctionBodyType.Code;
private calls: IFunctionCall[] = [ new FunctionCallStub() ];
constructor(type: FunctionBodyType) {
this.bodyType = type;
}
public get body(): ISharedFunctionBody {
return {
type: this.bodyType,
code: this.bodyType === FunctionBodyType.Code ? {
do: this.code,
revert: this.revertCode,
} : undefined,
calls: this.bodyType === FunctionBodyType.Calls ? this.calls : undefined,
};
}
public withName(name: string) {
this.name = name;
@@ -25,6 +45,10 @@ export class SharedFunctionStub implements ISharedFunction {
this.parameters = parameters;
return this;
}
public withCalls(...calls: readonly IFunctionCall[]) {
this.calls = [...calls];
return this;
}
public withParameterNames(...parameterNames: readonly string[]) {
let collection = new FunctionParameterCollectionStub();
for (const name of parameterNames) {

View File

@@ -1,10 +1,10 @@
import { sequenceEqual } from '@/application/Common/Array';
import { IFunctionCompiler } from '@/application/Parser/Script/Compiler/Function/IFunctionCompiler';
import { ISharedFunctionCollection } from '@/application/Parser/Script/Compiler/Function/ISharedFunctionCollection';
import { ISharedFunctionsParser } from '@/application/Parser/Script/Compiler/Function/ISharedFunctionsParser';
import { FunctionData } from 'js-yaml-loader!@/*';
import { SharedFunctionCollectionStub } from './SharedFunctionCollectionStub';
export class FunctionCompilerStub implements IFunctionCompiler {
export class SharedFunctionsParserStub implements ISharedFunctionsParser {
private setupResults = new Array<{
functions: readonly FunctionData[],
result: ISharedFunctionCollection,
@@ -14,7 +14,7 @@ export class FunctionCompilerStub implements IFunctionCompiler {
this.setupResults.push( { functions, result });
}
public compileFunctions(functions: readonly FunctionData[]): ISharedFunctionCollection {
public parseFunctions(functions: readonly FunctionData[]): ISharedFunctionCollection {
const result = this.findResult(functions);
return result || new SharedFunctionCollectionStub();
}