-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from thygesteffensen/issue/18/apply
Issue/18/apply
- Loading branch information
Showing
19 changed files
with
662 additions
and
35 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
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
30 changes: 30 additions & 0 deletions
30
PowerAutomateMockUp/ExpressionParser/Functions/Storage/ItemsFunction.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,30 @@ | ||
using System; | ||
using Parser.ExpressionParser.Functions.Base; | ||
using Parser.ExpressionParser.Functions.CustomException; | ||
|
||
namespace Parser.ExpressionParser.Functions.Storage | ||
{ | ||
public class ItemsFunction : Function | ||
{ | ||
private readonly IState _variableRetriever; | ||
|
||
public ItemsFunction(IState variableRetriever) : base("items") | ||
{ | ||
_variableRetriever = variableRetriever ?? throw new ArgumentNullException(nameof(variableRetriever)); | ||
} | ||
|
||
public override ValueContainer ExecuteFunction(params ValueContainer[] parameters) | ||
{ | ||
if (parameters.Length != 1) | ||
{ | ||
throw new ArgumentError(parameters.Length > 1 ? "Too many arguments" : "Too few arguments"); | ||
} | ||
|
||
var variableName = parameters[0].GetValue<string>(); | ||
// TODO: Maybe implement another storage option? | ||
var value = _variableRetriever.GetOutputs($"item_{variableName}"); | ||
|
||
return value; | ||
} | ||
} | ||
} |
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
9 changes: 9 additions & 0 deletions
9
PowerAutomateMockUp/FlowParser/ActionExecutors/IScopeActionExecutor.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,9 @@ | ||
using System.Threading.Tasks; | ||
|
||
namespace Parser.FlowParser.ActionExecutors | ||
{ | ||
public interface IScopeActionExecutor | ||
{ | ||
public Task<ActionResult> ExitScope(ActionStatus scopeStatus); | ||
} | ||
} |
104 changes: 104 additions & 0 deletions
104
PowerAutomateMockUp/FlowParser/ActionExecutors/Implementations/ForEachActionExecutor.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,104 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Logging; | ||
using Newtonsoft.Json.Linq; | ||
using Parser.ExpressionParser; | ||
|
||
namespace Parser.FlowParser.ActionExecutors.Implementations | ||
{ | ||
public class ForEachActionExecutor : DefaultBaseActionExecutor, IScopeActionExecutor | ||
{ | ||
private readonly IState _state; | ||
private readonly IScopeDepthManager _scopeDepthManager; | ||
private readonly ILogger<ForEachActionExecutor> _logger; | ||
private readonly IExpressionEngine _expressionEngine; | ||
|
||
private JProperty[] _actionDescriptions; | ||
private string _firstScopeActionName; | ||
|
||
private List<ValueContainer> _items; | ||
|
||
public ForEachActionExecutor( | ||
IState state, | ||
IScopeDepthManager scopeDepthManager, | ||
ILogger<ForEachActionExecutor> logger, | ||
IExpressionEngine expressionEngine) | ||
{ | ||
_state = state ?? throw new ArgumentNullException(nameof(state)); | ||
_scopeDepthManager = scopeDepthManager ?? throw new ArgumentNullException(nameof(scopeDepthManager)); | ||
_logger = logger ?? throw new ArgumentNullException(nameof(logger)); | ||
_expressionEngine = expressionEngine ?? throw new ArgumentNullException(nameof(expressionEngine)); | ||
|
||
_items = new List<ValueContainer>(); | ||
} | ||
|
||
public override Task<ActionResult> Execute() | ||
{ | ||
_logger.LogInformation("Entered foreach..."); | ||
|
||
if (Json == null) | ||
{ | ||
throw new PowerAutomateMockUpException($"Json cannot be null - cannot execute {ActionName}."); | ||
} | ||
|
||
var runOn = (Json.SelectToken("$..foreach") ?? | ||
throw new InvalidOperationException("Json must contain foreach token.")).Value<string>(); | ||
var values = _expressionEngine.ParseToValueContainer(runOn); | ||
|
||
if (values.Type() != ValueContainer.ValueType.Array) | ||
return Task.FromResult(new ActionResult | ||
{ | ||
// TODO: Figure out what happens when you apply for each on non array values | ||
ActionStatus = ActionStatus.Failed | ||
}); | ||
|
||
SetupForEach(values); | ||
|
||
UpdateScopeAndSetItemValue(); | ||
|
||
return Task.FromResult(new ActionResult {NextAction = _firstScopeActionName}); | ||
} | ||
|
||
private void SetupForEach(ValueContainer values) | ||
{ | ||
var scopeActionDescriptions = (Json.SelectToken("$.actions") ?? | ||
throw new InvalidOperationException("Json must contain actions token.")) | ||
.OfType<JProperty>(); | ||
_actionDescriptions = scopeActionDescriptions as JProperty[] ?? scopeActionDescriptions.ToArray(); | ||
_firstScopeActionName = _actionDescriptions.First(ad => | ||
!(ad.Value.SelectToken("$.runAfter") ?? | ||
throw new InvalidOperationException("Json must contain runAfter token.")).Any()).Name; | ||
|
||
// TODO: Add scope relevant storage to store stuff like this, which cannot interfere with the state. | ||
_items = values.GetValue<IEnumerable<ValueContainer>>().ToList(); | ||
} | ||
|
||
private void UpdateScopeAndSetItemValue() | ||
{ | ||
_scopeDepthManager.Push(ActionName, _actionDescriptions, this); | ||
|
||
_state.AddOutputs($"item_{ActionName}", _items.First()); | ||
_items = _items.Skip(1).ToList(); | ||
} | ||
|
||
|
||
public Task<ActionResult> ExitScope(ActionStatus scopeStatus) | ||
{ | ||
if (_items.Count > 0) | ||
{ | ||
_logger.LogInformation("Continuing foreach."); | ||
|
||
UpdateScopeAndSetItemValue(); | ||
|
||
return Task.FromResult(new ActionResult {NextAction = _firstScopeActionName}); | ||
} | ||
else | ||
{ | ||
_logger.LogInformation("Exited foreach..."); | ||
return Task.FromResult(new ActionResult()); | ||
} | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.