Skip to content

Commit c7ab7bc

Browse files
compare string slices unordered lists
1 parent 2de1666 commit c7ab7bc

3 files changed

Lines changed: 29 additions & 10 deletions

File tree

pkg/apic/apiservicerevision.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ import (
77
"errors"
88
"fmt"
99
"net/http"
10-
"reflect"
1110
"regexp"
12-
"sort"
1311
"strconv"
1412
"strings"
1513
"text/template"
@@ -165,7 +163,15 @@ func (c *ServiceClient) getRevisionsIfUpdating(serviceBody *ServiceBody) ([]*man
165163

166164
// checkAndUpdateExistingRevision checks if a revision with the same hash exists and updates tags if needed
167165
func (c *ServiceClient) checkAndUpdateExistingRevision(serviceBody *ServiceBody, apiServiceRevisions []*management.APIServiceRevision) (bool, error) {
166+
// attempt to use the stripped spec hash
168167
revName, found := serviceBody.specHashes[serviceBody.specHash]
168+
if !found && serviceBody.originalSpecHash != "" {
169+
// check if the original spec hash matches an existing revision,
170+
// this is to cover the case where the spec content has not changed since the last publish,
171+
// but the hash has changed due to non-content related changes (e.g. stripping servers)
172+
revName, found = serviceBody.specHashes[serviceBody.originalSpecHash]
173+
}
174+
169175
if !found {
170176
return false, nil
171177
}
@@ -205,19 +211,15 @@ func (c *ServiceClient) checkAndUpdateExistingRevision(serviceBody *ServiceBody,
205211
// different, return the updated tags
206212
func (c *ServiceClient) getUpdatedTagKeys(serviceBodyTags map[string]interface{}, revisionTags []string) []string {
207213
// Extract values from map and convert to []string
208-
var mapValues []string
214+
mapValues := []string{}
209215
for _, v := range serviceBodyTags {
210216
if strVal, ok := v.(string); ok {
211217
mapValues = append(mapValues, strVal)
212218
}
213219
}
214220

215-
// Sort both slices to allow unordered comparison
216-
sort.Strings(mapValues)
217-
sort.Strings(revisionTags)
218-
219221
// Compare
220-
if reflect.DeepEqual(mapValues, revisionTags) {
222+
if util.StringSlicesEqualUnordered(mapValues, revisionTags) {
221223
return []string{} // return empty string slice if equal
222224
}
223225

pkg/apic/client.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"encoding/json"
55
"fmt"
66
"net/http"
7-
"slices"
87
"strconv"
98
"strings"
109
"sync"
@@ -870,7 +869,7 @@ func (c *ServiceClient) updateSpecORCreateResourceInstance(data *apiv1.ResourceI
870869
method = coreapi.PUT
871870

872871
// check if either hash, tags or title have changed and mark for update
873-
equalTags := slices.Equal(existingRI.GetTags(), data.GetTags())
872+
equalTags := util.StringSlicesEqualUnordered(existingRI.GetTags(), data.GetTags())
874873
oldHash, _ := util.GetAgentDetailsValue(existingRI, defs.AttrSpecHash)
875874
newHash, _ := util.GetAgentDetailsValue(data, defs.AttrSpecHash)
876875
if oldHash == newHash && existingRI.Title == data.Title && equalTags {

pkg/util/util.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,3 +595,21 @@ func IsInArray[K comparable](arr []K, val K) bool {
595595
}
596596
return false
597597
}
598+
599+
func StringSlicesEqualUnordered(a, b []string) bool {
600+
if len(a) != len(b) {
601+
return false
602+
}
603+
604+
counts := make(map[string]int)
605+
for _, item := range a {
606+
counts[item]++
607+
}
608+
for _, item := range b {
609+
counts[item]--
610+
if counts[item] < 0 {
611+
return false
612+
}
613+
}
614+
return true
615+
}

0 commit comments

Comments
 (0)