Skip to content

Commit

Permalink
Remove checkFieldsWithSort
Browse files Browse the repository at this point in the history
From some debugging for an issue, it became apparent that this function
was not actually being called in situations where it might have been
expected... Basically the switch case that might have called it is not
reachable.

Signed-off-by: Justin Kulikauskas <jkulikau@redhat.com>
  • Loading branch information
JustinKuli committed Oct 12, 2023
1 parent 26ba3bd commit 849d136
Showing 1 changed file with 1 addition and 69 deletions.
70 changes: 1 addition & 69 deletions controllers/configurationpolicy_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func equalObjWithSort(mergedObj interface{}, oldObj interface{}) (areEqual bool)
return true
}

// checFieldsWithSort is a check for maps that uses an arbitrary sort to ensure it is
// checkFieldsWithSort is a check for maps that uses an arbitrary sort to ensure it is
// comparing the right values
func checkFieldsWithSort(mergedObj map[string]interface{}, oldObj map[string]interface{}) (matches bool) {
// needed to compare lists, since merge messes up the order
Expand All @@ -173,12 +173,6 @@ func checkFieldsWithSort(mergedObj map[string]interface{}, oldObj map[string]int
if !checkFieldsWithSort(mVal, oVal) {
return false
}
case []map[string]interface{}:
// if field is a list of maps, use checkListFieldsWithSort to check for a match
oVal, ok := oldObj[i].([]map[string]interface{})
if !ok || !checkListFieldsWithSort(mVal, oVal) {
return false
}
case []interface{}:
// if field is a generic list, sort and iterate through them to make sure each value matches
oVal, ok := oldObj[i].([]interface{})
Expand Down Expand Up @@ -236,68 +230,6 @@ func checkFieldsWithSort(mergedObj map[string]interface{}, oldObj map[string]int
return true
}

// checkListFieldsWithSort is a check for lists of maps that uses an arbitrary sort to ensure it is
// comparing the right values
func checkListFieldsWithSort(mergedObj []map[string]interface{}, oldObj []map[string]interface{}) (matches bool) {
sort.Slice(oldObj, func(i, j int) bool {
return fmt.Sprintf("%v", oldObj[i]) < fmt.Sprintf("%v", oldObj[j])
})
sort.Slice(mergedObj, func(x, y int) bool {
return fmt.Sprintf("%v", mergedObj[x]) < fmt.Sprintf("%v", mergedObj[y])
})

// needed to compare lists, since merge messes up the order
for listIdx, mergedItem := range mergedObj {
oldItem := oldObj[listIdx]

for i, mVal := range mergedItem {
switch mVal := mVal.(type) {
case []interface{}:
// if a map in the list contains a nested list, sort and check for equality
if oVal, ok := oldItem[i].([]interface{}); ok {
return len(mVal) == len(oVal) && checkListsMatch(oVal, mVal)
}

return false
case map[string]interface{}:
// if a map in the list contains another map, check fields for equality
if oVal, ok := oldItem[i].(map[string]interface{}); ok {
return len(mVal) == len(oVal) && checkFieldsWithSort(mVal, oVal)
}

return false
case string:
// extra check to see if value is a byte value
mQty, err := apiRes.ParseQuantity(mVal)
if err != nil {
// An error indicates the value is a regular string, so check equality normally
if fmt.Sprint(oldItem[i]) != fmt.Sprint(mVal) {
return false
}
} else {
// if the value is a quantity of bytes, convert original
oVal, ok := oldItem[i].(string)
if !ok {
return false
}

oQty, err := apiRes.ParseQuantity(oVal)
if err != nil || !oQty.Equal(mQty) {
return false
}
}
default:
// if the field in the map is not an object, just do a generic check
if fmt.Sprint(oldItem[i]) != fmt.Sprint(mVal) {
return false
}
}
}
}

return true
}

// checkListsMatch is a generic list check that uses an arbitrary sort to ensure it is comparing the right values
func checkListsMatch(oldVal []interface{}, mergedVal []interface{}) (m bool) {
if (oldVal == nil && mergedVal != nil) || (oldVal != nil && mergedVal == nil) {
Expand Down

0 comments on commit 849d136

Please sign in to comment.