Skip to content

Commit

Permalink
Improve forEach loop functionality in worker actions
Browse files Browse the repository at this point in the history
The forEach loop functionality has been enhanced to include better parameter management and formatting within iterator items. This includes the renaming of parameters inline with the current index and removal of outdated logging, which optimizes code readability and execution efficiency. The mechanism for setting the `CurrentStep` has also been updated for improved logic flow.
  • Loading branch information
PiotrFerenc committed Apr 20, 2024
1 parent 4fc5fb4 commit 3411251
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions cmd/worker/actions/common/for-each.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package common

import (
"fmt"
"github.com/Jeffail/gabs"
"github.com/PiotrFerenc/mash2/cmd/worker/actions"
"github.com/PiotrFerenc/mash2/internal/types"
"log"
"strings"
)

func CreateForEachLoop() actions.Action {
Expand Down Expand Up @@ -66,24 +67,30 @@ func (d *forEachLoop) Execute(process types.Process) (types.Process, error) {
forEachBody := process.CurrentStep.ForeachBody
currentIndex := process.CurrentStep.Sequence

for key, child := range items {
for _, _ = range items {
for _, s := range forEachBody.Steps {
currentIndex = currentIndex + 1
s.Sequence = currentIndex
st := types.MapToStep(s)
process.Steps = append(process.Steps, *st)
if currentIndex == process.CurrentStep.Sequence+1 {
process.CurrentStep = *st
for key, value := range forEachBody.Parameters {
if strings.Contains(key, st.Name) {
newName := fmt.Sprintf("%s%d", st.Name, st.Sequence)
keyName := strings.Replace(key, st.Name, newName, 1)
st.Name = newName
process.Parameters[keyName] = value
}
}
process.Steps = append(process.Steps, *st)
}

log.Printf("%s %s", key, child.Data())
}
for i, s := range process.Steps {
if s.Sequence > currentIndex {
s.Sequence = currentIndex + i
}
}

process.CurrentStep = process.Steps[0]

return process, nil
}

0 comments on commit 3411251

Please sign in to comment.