-
Notifications
You must be signed in to change notification settings - Fork 227
Compile all expressions at once when validating. #268
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0b21f8f
Compile all expressions at once
aoltean16 aa79385
refactor
aoltean16 35d2624
added tests and refactored code
gabriela-lungu-uip 16bca3c
fixed test + small rename
aoltean16 0592fd5
added one more test scenario
gabriela-lungu-uip de8d1c2
fix.
aoltean16 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.