From 2a625b75a4922f3f8982b304f9a31d8b484632e1 Mon Sep 17 00:00:00 2001 From: Daniel Schmidt Date: Wed, 14 Feb 2024 18:20:15 +0100 Subject: [PATCH] handle reordering explicitly --- .../command/jsonformat/collections/slice.go | 26 ++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/internal/command/jsonformat/collections/slice.go b/internal/command/jsonformat/collections/slice.go index b52cc7d6c0..c6dd335f07 100644 --- a/internal/command/jsonformat/collections/slice.go +++ b/internal/command/jsonformat/collections/slice.go @@ -38,7 +38,7 @@ func ProcessSlice[Input any](before, after []Input, process ProcessIndices, isOb // If before and after are the same length we want to compare elements // on an individual basis - if len(before) == len(after) { + if len(before) == len(after) && !isReorder(before, after) { for ix := range before { process(ix, ix) } @@ -83,3 +83,27 @@ func ProcessSlice[Input any](before, after []Input, process ProcessIndices, isOb } } } + +// Returns if every item of before can be found in after +func isReorder[Input any](before, after []Input) bool { + // To be a reorder the length needs to be the same + if len(before) != len(after) { + return false + } + + for _, b := range before { + hasMatch := false + for _, a := range after { + if reflect.DeepEqual(b, a) { + // Match found, no need to search anymore + hasMatch = true + break + } + } + if !hasMatch { + return false + } + } + + return true +}