Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(expressioncompiler): extensibility #239

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/UiPath.Workflow/Activities/CsExpressionValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,11 @@ protected override void UpdateCompilationUnit(ExpressionContainer expressionCont
}
}

protected override string CreateValidationCode(string types, string names, string code) =>
$"public static Expression<Func<{types}>> CreateExpression() => ({names}) => {code};";
protected override string CreateValidationCode(string types, string names, ExpressionContainer expressionContainer)
{
var code = expressionContainer.ExpressionToValidate.Code;
return $"public static Expression<Func<{types}>> CreateExpression() => ({names}) => {code};";
}

protected override SyntaxTree GetSyntaxTreeForExpression(ExpressionContainer expressionContainer) =>
CSharpSyntaxTree.ParseText(expressionContainer.ExpressionToValidate.Code, s_csScriptParseOptions);
Expand Down
5 changes: 5 additions & 0 deletions src/UiPath.Workflow/Activities/ExpressionContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,9 @@ public class ExpressionContainer
/// Diagnostics reported by validating the expression.
/// </summary>
public IEnumerable<TextExpressionCompilerError> Diagnostics { get; set; }

/// <summary>
/// populated before CreateValidationCode step
/// </summary>
public (string Name, Type Type)[] ResolvedIdentifiers { get; set; }
}
10 changes: 6 additions & 4 deletions src/UiPath.Workflow/Activities/RoslynExpressionValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void AddRequiredAssembly(Assembly assembly)
/// </summary>
/// <param name="expressionContainer">expression container</param>
/// <returns>MetadataReference objects for all required assemblies</returns>
protected IEnumerable<MetadataReference> GetMetadataReferencesForExpression(ExpressionContainer expressionContainer) =>
protected virtual IEnumerable<MetadataReference> GetMetadataReferencesForExpression(ExpressionContainer expressionContainer) =>
expressionContainer.RequiredAssemblies.Select(asm => TryGetMetadataReference(asm)).Where(mr => mr is not null);

/// <summary>
Expand All @@ -107,7 +107,7 @@ protected IEnumerable<MetadataReference> GetMetadataReferencesForExpression(Expr
/// <param name="names">list of parameter names in comma-separated string</param>
/// <param name="code">expression code</param>
/// <returns>expression wrapped in a method or function that returns a LambdaExpression</returns>
protected abstract string CreateValidationCode(string types, string names, string code);
protected abstract string CreateValidationCode(string types, string names, ExpressionContainer expressionContainer);

/// <summary>
/// Updates the <see cref="Compilation" /> object for the expression.
Expand Down Expand Up @@ -180,7 +180,7 @@ public virtual IEnumerable<ValidationError> Validate<TResult>(Activity currentAc
var diagnostics = expressionContainer
.CompilationUnit
.GetDiagnostics()
.Where(d=> d.Severity == DiagnosticSeverity.Error)
.Where(d => d.Severity == DiagnosticSeverity.Error)
.Select(diagnostic =>
new TextExpressionCompilerError
{
Expand Down Expand Up @@ -269,14 +269,16 @@ private void PrepValidation(ExpressionContainer expressionContainer)
.Where(var => var.Type != null)
.ToArray();

expressionContainer.ResolvedIdentifiers = resolvedIdentifiers;

var names = string.Join(Comma, resolvedIdentifiers.Select(var => var.Name));
var types = string.Join(Comma,
resolvedIdentifiers
.Select(var => var.Type)
.Concat(new[] { expressionContainer.ResultType })
.Select(GetTypeName));

var lambdaFuncCode = CreateValidationCode(types, names, expressionContainer.ExpressionToValidate.Code);
var lambdaFuncCode = CreateValidationCode(types, names, expressionContainer);
var sourceText = SourceText.From(lambdaFuncCode);
var newSyntaxTree = syntaxTree.WithChangedText(sourceText);
expressionContainer.CompilationUnit = expressionContainer.CompilationUnit.ReplaceSyntaxTree(syntaxTree, newSyntaxTree);
Expand Down
8 changes: 6 additions & 2 deletions src/UiPath.Workflow/Activities/VbExpressionValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,12 @@ protected override void UpdateCompilationUnit(ExpressionContainer expressionCont
}
}

protected override string CreateValidationCode(string types, string names, string code) =>
$"Public Shared Function CreateExpression() As Expression(Of Func(Of {types}))\nReturn Function({names}) ({code})\nEnd Function";
protected override string CreateValidationCode(string types, string names, ExpressionContainer expressionContainer)
{
var code = expressionContainer.ExpressionToValidate.Code;
return
$"Public Shared Function CreateExpression() As Expression(Of Func(Of {types}))\nReturn Function({names}) ({code})\nEnd Function";
}

protected override SyntaxTree GetSyntaxTreeForExpression(ExpressionContainer expressionContainer) =>
VisualBasicSyntaxTree.ParseText("? " + expressionContainer.ExpressionToValidate.Code, s_vbScriptParseOptions);
Expand Down