-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Compile all expressions at once when validating. (#268)
Compile all expressions at once Co-authored-by: Gabriela Lungu <gabriela.lungu@uipath.com>
- Loading branch information
Showing
25 changed files
with
736 additions
and
420 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
using Microsoft.CSharp.Activities; | ||
using Shouldly; | ||
using System; | ||
using System.Activities; | ||
using System.Activities.Statements; | ||
using System.Activities.Validation; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Xunit; | ||
|
||
namespace TestCases.Workflows; | ||
|
||
public class ValidationExtensionsTests | ||
{ | ||
private readonly ValidationSettings _useValidator = new() { ForceExpressionCache = false }; | ||
|
||
[Fact] | ||
public void OnlyOneInstanceOfExtensionTypeIsAdded() | ||
{ | ||
var seq = new Sequence(); | ||
for (var j = 0; j < 10000; j++) | ||
{ | ||
seq.Activities.Add(new ActivityWithValidationExtension()); | ||
} | ||
|
||
var result = ActivityValidationServices.Validate(seq, _useValidator); | ||
result.Errors.Count.ShouldBe(1); | ||
result.Errors.First().Message.ShouldContain(nameof(MockValidationExtension)); | ||
} | ||
|
||
[Fact] | ||
public void ValidationErrorsAreConcatenated() | ||
{ | ||
var seq = new Sequence() | ||
{ | ||
Activities = | ||
{ | ||
new ActivityWithValidationExtension(), | ||
new ActivityWithValidationError(), | ||
new WriteLine { Text = new InArgument<string>(new CSharpValue<string>("var1")) } | ||
} | ||
}; | ||
|
||
var result = ActivityValidationServices.Validate(seq, _useValidator); | ||
result.Errors.Count.ShouldBe(3); | ||
result.Errors.ShouldContain(error => error.Message.Contains(nameof(ActivityWithValidationError))); | ||
result.Errors.ShouldContain(error => error.Message.Contains(nameof(MockValidationExtension))); | ||
result.Errors.ShouldContain(error => error.Message.Contains("The name 'var1' does not exist in the current context")); | ||
} | ||
|
||
class ActivityWithValidationError : CodeActivity | ||
{ | ||
protected override void Execute(CodeActivityContext context) => throw new NotImplementedException(); | ||
|
||
protected override void CacheMetadata(CodeActivityMetadata metadata) => metadata.AddValidationError(nameof(ActivityWithValidationError)); | ||
} | ||
|
||
class ActivityWithValidationExtension : CodeActivity | ||
{ | ||
protected override void Execute(CodeActivityContext context) => throw new NotImplementedException(); | ||
|
||
protected override void CacheMetadata(CodeActivityMetadata metadata) | ||
{ | ||
if (metadata.Environment.IsValidating) | ||
{ | ||
metadata.Environment.Extensions.GetOrAdd(() => new MockValidationExtension()); | ||
} | ||
} | ||
} | ||
|
||
class MockValidationExtension : IValidationExtension | ||
{ | ||
public IEnumerable<ValidationError> PostValidate(Activity activity) => | ||
new List<ValidationError>() { new ValidationError(nameof(MockValidationExtension)) }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// This file is part of Core WF which is licensed under the MIT license. | ||
// See LICENSE file in the project root for full license information. | ||
|
||
namespace System.Activities | ||
{ | ||
internal class EnvironmentExtensions | ||
{ | ||
private readonly Dictionary<Type, object> _extensions = new(); | ||
|
||
/// <summary> | ||
/// Gets the specified extension. | ||
/// If the extension does not exist, | ||
/// it will invoke the <paramref name="createExtensionFactory"/> parameter | ||
/// </summary> | ||
/// <typeparam name="TExtension">The type of the extension</typeparam> | ||
/// <param name="createExtensionFactory">The factory to create the extension</param> | ||
/// <exception cref="ArgumentNullException"></exception> | ||
public TExtension GetOrAdd<TExtension>(Func<TExtension> createExtensionFactory) | ||
where TExtension : class | ||
{ | ||
var type = typeof(TExtension); | ||
if (_extensions.TryGetValue(type, out object extension)) | ||
{ | ||
return extension as TExtension; | ||
} | ||
|
||
return CreateAndAdd(); | ||
|
||
TExtension CreateAndAdd() | ||
{ | ||
var extension = createExtensionFactory(); | ||
if (extension is null) | ||
throw new ArgumentNullException(nameof(extension)); | ||
|
||
_extensions[type] = extension; | ||
return extension; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Retrieves the extension registered for the given type | ||
/// or null otherwise | ||
/// </summary> | ||
/// <typeparam name="T">The type of the extension.</typeparam> | ||
public T Get<T>() where T : class | ||
{ | ||
if (_extensions.TryGetValue(typeof(T), out object extension)) | ||
return extension as T; | ||
return null; | ||
} | ||
|
||
/// <summary> | ||
/// Adds the specified extension to the list. | ||
/// The extension is treated as a singleton, | ||
/// so if a second extension with the same type is added, it will | ||
/// throw an <see cref="InvalidOperationException"/> | ||
/// </summary> | ||
/// <typeparam name="TExtension">The type of the extension</typeparam> | ||
/// <param name="extension">The extension</param> | ||
/// <exception cref="InvalidOperationException"></exception> | ||
public void Add<TExtension>(TExtension extension) where TExtension : class | ||
{ | ||
if (_extensions.ContainsKey(typeof(TExtension))) | ||
throw new InvalidOperationException($"Service '{typeof(TExtension).FullName}' already exists"); | ||
|
||
_extensions[typeof(TExtension)] = extension; | ||
} | ||
|
||
internal IReadOnlyCollection<object> All => _extensions.Values; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/UiPath.Workflow.Runtime/Validation/IValidationExtension.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace System.Activities.Validation | ||
{ | ||
internal interface IValidationExtension | ||
{ | ||
IEnumerable<ValidationError> PostValidate(Activity activity); | ||
} | ||
} |
Oops, something went wrong.