From 77cd490e48c25c9da155c52cb4730ddb40feb15a Mon Sep 17 00:00:00 2001 From: Shivanshu Raj Shrivastava Date: Tue, 29 Apr 2025 17:24:00 +0530 Subject: [PATCH] chore: update normalize funnel steps Signed-off-by: Shivanshu Raj Shrivastava --- pkg/modules/tracefunnel/utils.go | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/pkg/modules/tracefunnel/utils.go b/pkg/modules/tracefunnel/utils.go index 042540bc24..15fdf768f5 100644 --- a/pkg/modules/tracefunnel/utils.go +++ b/pkg/modules/tracefunnel/utils.go @@ -48,17 +48,28 @@ func ValidateFunnelSteps(steps []tracefunnel.FunnelStep) error { return nil } -// NormalizeFunnelSteps normalizes step orders to be sequential +// NormalizeFunnelSteps normalizes step orders to be sequential starting from 1. +// Returns a new slice with normalized step orders, leaving the input slice unchanged. func NormalizeFunnelSteps(steps []tracefunnel.FunnelStep) []tracefunnel.FunnelStep { - sort.Slice(steps, func(i, j int) bool { - return steps[i].Order < steps[j].Order - }) - - for i := range steps { - steps[i].Order = int64(i + 1) + if len(steps) == 0 { + return nil } - return steps + // Create a copy of the input slice + newSteps := make([]tracefunnel.FunnelStep, len(steps)) + copy(newSteps, steps) + + // Sort the copy + sort.Slice(newSteps, func(i, j int) bool { + return newSteps[i].Order < newSteps[j].Order + }) + + // Update orders in the copy + for i := range newSteps { + newSteps[i].Order = int64(i + 1) + } + + return newSteps } func GetClaims(r *http.Request) (*authtypes.Claims, error) {