@@ -31,12 +31,17 @@ public class ForEachStateProcessor
31
31
32
32
/// <inheritdoc/>
33
33
public ForEachStateProcessor ( ILoggerFactory loggerFactory , IWorkflowRuntimeContext context , IWorkflowActivityProcessorFactory activityProcessorFactory ,
34
- IOptions < ApplicationOptions > options , V1WorkflowActivity activity , ForEachStateDefinition state )
34
+ IJsonSerializer jsonSerializer , IOptions < ApplicationOptions > options , V1WorkflowActivity activity , ForEachStateDefinition state )
35
35
: base ( loggerFactory , context , activityProcessorFactory , options , activity , state )
36
36
{
37
-
37
+ this . JsonSerializer = jsonSerializer ;
38
38
}
39
39
40
+ /// <summary>
41
+ /// Gets the service used to serialize/deserialize to/from JSON
42
+ /// </summary>
43
+ protected IJsonSerializer JsonSerializer { get ; }
44
+
40
45
/// <inheritdoc/>
41
46
protected override IWorkflowActivityProcessor CreateProcessorFor ( V1WorkflowActivity activity )
42
47
{
@@ -65,7 +70,10 @@ protected override async Task InitializeAsync(CancellationToken cancellationToke
65
70
}
66
71
var iterationParamValue = inputCollection . First ( ) ;
67
72
var input = ( DynamicObject ) this . Activity . Input ! ;
68
- input . Set ( this . State . IterationParameter ! , iterationParamValue ) ;
73
+ var iterationParam = this . State . IterationParameter ;
74
+ if ( string . IsNullOrWhiteSpace ( iterationParam ) )
75
+ iterationParam = "item" ;
76
+ input . Set ( iterationParam , iterationParamValue ) ;
69
77
var metadata = new Dictionary < string , string > ( )
70
78
{
71
79
{ V1WorkflowActivityMetadata . State , this . State . Name ! } ,
@@ -141,13 +149,31 @@ protected virtual async Task OnIterationResultAsync(IterationProcessor processor
141
149
|| inputCollection . Count ( ) - 1 <= iterationIndex )
142
150
{
143
151
var outputCollection = await this . GetOutputCollectionAsync ( cancellationToken ) ;
144
- foreach ( var itarationActivity in ( await this . Context . Workflow . GetActivitiesAsync ( this . Activity , cancellationToken ) )
145
- . Where ( a => a . Type == V1WorkflowActivityType . Iteration && a . Status == V1WorkflowActivityStatus . Completed )
152
+ foreach ( var iterationActivity in ( await this . Context . Workflow . GetActivitiesAsync ( this . Activity , cancellationToken ) )
153
+ . Where ( a => a . Type == V1WorkflowActivityType . Iteration && a . Status == V1WorkflowActivityStatus . Completed && a . Output != null )
146
154
. ToList ( ) )
147
155
{
148
- outputCollection . Add ( itarationActivity . Output ! . ToObject ( ) ! ) ;
156
+ outputCollection . Add ( iterationActivity . Output ! . ToObject ( ) ! ) ;
157
+ }
158
+ var output = this . Activity . Input . ToObject ( ) ;
159
+ var expression = this . State . IterationParameter ;
160
+ if ( string . IsNullOrWhiteSpace ( expression ) )
161
+ expression = "item" ;
162
+ if ( expression . StartsWith ( "${" ) )
163
+ expression = expression [ 2 ..^ 1 ] ;
164
+ expression = $ ". |= del(.{ expression } )";
165
+ if ( output != null )
166
+ output = await this . Context . EvaluateAsync ( expression , output , cancellationToken ) ;
167
+ if ( ! string . IsNullOrWhiteSpace ( this . State . OutputCollection ) )
168
+ {
169
+ expression = this . State . OutputCollection . Trim ( ) ;
170
+ var outputCollectionJson = await this . JsonSerializer . SerializeAsync ( outputCollection , cancellationToken ) ;
171
+ if ( expression . StartsWith ( "${" ) )
172
+ expression = expression [ 2 ..^ 1 ] ;
173
+ expression = $ "{ expression } = { outputCollectionJson } ";
174
+ output = await this . Context . EvaluateAsync ( expression , output , cancellationToken ) ;
149
175
}
150
- await this . OnNextAsync ( new V1WorkflowActivityCompletedIntegrationEvent ( this . Activity . Id , outputCollection ) , cancellationToken ) ;
176
+ await this . OnNextAsync ( new V1WorkflowActivityCompletedIntegrationEvent ( this . Activity . Id , output ) , cancellationToken ) ;
151
177
return ;
152
178
}
153
179
iterationIndex += 1 ;
0 commit comments