elsa-workflows / elsa-core

A .NET workflows library
https://v3.elsaworkflows.io/
MIT License
5.88k stars 1.06k forks source link

[FEAT] Map Activity to Transform Collections with Custom Logic #5246

Open sfmskywalker opened 3 weeks ago

sfmskywalker commented 3 weeks ago

Summary: Introduce a new Map activity that allows users to apply a transformation to each item in a collection. The transformation logic should be defined within a Body property, which can host any activity like Sequence or Flowchart.

Details:

Expected Behavior:

Use Case: This feature would be beneficial for scenarios where a set of items needs to be individually processed and transformed, potentially using complex logic encapsulated within other activities like Sequence or Flowchart.

Think of it like the Select for LINQ and Array.map in JavaScript.

glime-ai[bot] commented 3 weeks ago

To implement a new Map activity in Elsa Workflows that allows users to apply a transformation to each item in a collection, you would need to follow these steps:

  1. Define the Map Activity Class: Create a new class for the Map activity that inherits from a suitable base class, such as Activity or another more specific base if applicable in Elsa.

  2. Specify Inputs and Outputs: Use the [ActivityInput] attribute to define the input collection and the [ActivityOutput] attribute to define the output list that will hold the transformed items.

  3. Implement the Body Property: The Body property should be of type IActivity and will contain the transformation logic. This property can be implemented using the ActivityBuilder class or similar, which allows for the inclusion of other activities like Sequence or Flowchart.

  4. Implement the Iteration Mechanism: In the OnExecute or ExecuteAsync method of the Map activity, implement the logic to iterate over the input collection. For each item, execute the Body activity and collect the result.

  5. Collect the Transformed Items: As the Body activity executes for each item, collect the outputs into a new list. This list will be the final output of the Map activity.

  6. Handle Different Collection Types: Ensure that the Map activity can handle different types of collections by either using interfaces like IEnumerable or by providing overloads or generic versions of the activity.

  7. Optimize for Large Collections: Consider performance implications for large collections. You may need to implement asynchronous execution or batching to ensure efficient processing.

  8. Testing: Write unit tests to verify that the Map activity behaves as expected, including its ability to handle various collection types and sizes.

  9. Documentation: Document the new Map activity, explaining how to use it, the expected inputs and outputs, and providing examples of common use cases.

  10. Integration with Elsa Designer: If you want the Map activity to be available in the Elsa Designer, you will need to create a corresponding front-end component that allows users to configure the Map activity's properties, including the Body transformation logic.

This new Map activity would be similar to the ForEach activity already present in Elsa but with the added capability of transforming each item and collecting the results. It would be a valuable addition for users who need to perform complex transformations on collections without writing custom looping logic.

This comment was generated by Glime.