Skip to content

Commit

Permalink
fix(lwseverity): use stable sorting for severity (#1161)
Browse files Browse the repository at this point in the history
GROW-1432
  • Loading branch information
hazedav committed Feb 24, 2023
1 parent 555cfb9 commit f927763
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 20 deletions.
4 changes: 2 additions & 2 deletions lwseverity/severity.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func ShouldFilter(severity, threshold string) bool {

// Sort a slice of Severity interfaces from critical -> info
func SortSlice[S Severity](s []S) {
sort.Slice(s, func(i, j int) bool {
sort.SliceStable(s, func(i, j int) bool {
sevI, _ := Normalize(s[i].GetSeverity())
sevJ, _ := Normalize(s[j].GetSeverity())
return sevI < sevJ
Expand All @@ -162,7 +162,7 @@ func SortSlice[S Severity](s []S) {

// Sort a slice of Severity interfaces from info -> critical
func SortSliceA[S Severity](s []S) {
sort.Slice(s, func(i, j int) bool {
sort.SliceStable(s, func(i, j int) bool {
sevI, _ := Normalize(s[i].GetSeverity())
sevJ, _ := Normalize(s[j].GetSeverity())
return sevI > sevJ
Expand Down
34 changes: 16 additions & 18 deletions lwseverity/severity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ func TestShouldFilterTest(t *testing.T) {
}

type myStruct struct {
alertID int
severity string
}

Expand All @@ -100,31 +101,28 @@ type myStructs []myStruct

func TestSort(t *testing.T) {
m := myStructs{
myStruct{
severity: "Low",
},
myStruct{
severity: "High",
},
myStruct{alertID: 6, severity: "Low"},
myStruct{alertID: 9, severity: "High"},
myStruct{alertID: 1, severity: "Low"},
myStruct{alertID: 3, severity: "Low"},
myStruct{alertID: 8, severity: "Low"},
}
expected := myStructs{
myStruct{
severity: "High",
},
myStruct{
severity: "Low",
},
myStruct{alertID: 9, severity: "High"},
myStruct{alertID: 6, severity: "Low"},
myStruct{alertID: 1, severity: "Low"},
myStruct{alertID: 3, severity: "Low"},
myStruct{alertID: 8, severity: "Low"},
}
lwseverity.SortSlice(m)
assert.Equal(t, expected, m)

expected = myStructs{
myStruct{
severity: "Low",
},
myStruct{
severity: "High",
},
myStruct{alertID: 6, severity: "Low"},
myStruct{alertID: 1, severity: "Low"},
myStruct{alertID: 3, severity: "Low"},
myStruct{alertID: 8, severity: "Low"},
myStruct{alertID: 9, severity: "High"},
}
lwseverity.SortSliceA(m)
assert.Equal(t, expected, m)
Expand Down

0 comments on commit f927763

Please sign in to comment.