mirror of
https://github.com/anchore/syft.git
synced 2025-11-17 16:33:21 +01:00
497 stable sorted CPE array (JSON and SPDX) (#522)
* add small sorting change to our specificity Signed-off-by: Christopher Angelo Phillips <christopher.phillips@anchore.com>
This commit is contained in:
parent
6480f06173
commit
5e4b668e5a
@ -18,10 +18,18 @@ func (c BySpecificity) Less(i, j int) bool {
|
|||||||
iScore := weightedCountForSpecifiedFields(c[i])
|
iScore := weightedCountForSpecifiedFields(c[i])
|
||||||
jScore := weightedCountForSpecifiedFields(c[j])
|
jScore := weightedCountForSpecifiedFields(c[j])
|
||||||
|
|
||||||
if iScore == jScore {
|
// check weighted sort first
|
||||||
|
if iScore != jScore {
|
||||||
|
return iScore > jScore
|
||||||
|
}
|
||||||
|
|
||||||
|
// sort longer fields to top
|
||||||
|
if countFieldLength(c[i]) != countFieldLength(c[j]) {
|
||||||
return countFieldLength(c[i]) > countFieldLength(c[j])
|
return countFieldLength(c[i]) > countFieldLength(c[j])
|
||||||
}
|
}
|
||||||
return iScore > jScore
|
|
||||||
|
// if score and length are equal then text sort
|
||||||
|
return c[i].BindToFmtString() < c[j].BindToFmtString()
|
||||||
}
|
}
|
||||||
|
|
||||||
func countFieldLength(cpe wfn.Attributes) int {
|
func countFieldLength(cpe wfn.Attributes) int {
|
||||||
|
|||||||
@ -80,6 +80,25 @@ func TestCPESpecificity(t *testing.T) {
|
|||||||
mustCPE("cpe:2.3:a:1:*:333:*:*:*:*:*:*:*"),
|
mustCPE("cpe:2.3:a:1:*:333:*:*:*:*:*:*:*"),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "sort by mix of field length, specificity, dash",
|
||||||
|
input: []pkg.CPE{
|
||||||
|
mustCPE("cpe:2.3:a:alpine:alpine_keys:2.3-r1:*:*:*:*:*:*:*"),
|
||||||
|
mustCPE("cpe:2.3:a:alpine_keys:alpine_keys:2.3-r1:*:*:*:*:*:*:*"),
|
||||||
|
mustCPE("cpe:2.3:a:alpine-keys:alpine_keys:2.3-r1:*:*:*:*:*:*:*"),
|
||||||
|
mustCPE("cpe:2.3:a:alpine:alpine-keys:2.3-r1:*:*:*:*:*:*:*"),
|
||||||
|
mustCPE("cpe:2.3:a:alpine-keys:alpine-keys:2.3-r1:*:*:*:*:*:*:*"),
|
||||||
|
mustCPE("cpe:2.3:a:alpine_keys:alpine-keys:2.3-r1:*:*:*:*:*:*:*"),
|
||||||
|
},
|
||||||
|
expected: []pkg.CPE{
|
||||||
|
mustCPE("cpe:2.3:a:alpine-keys:alpine-keys:2.3-r1:*:*:*:*:*:*:*"),
|
||||||
|
mustCPE("cpe:2.3:a:alpine-keys:alpine_keys:2.3-r1:*:*:*:*:*:*:*"),
|
||||||
|
mustCPE("cpe:2.3:a:alpine_keys:alpine-keys:2.3-r1:*:*:*:*:*:*:*"),
|
||||||
|
mustCPE("cpe:2.3:a:alpine_keys:alpine_keys:2.3-r1:*:*:*:*:*:*:*"),
|
||||||
|
mustCPE("cpe:2.3:a:alpine:alpine-keys:2.3-r1:*:*:*:*:*:*:*"),
|
||||||
|
mustCPE("cpe:2.3:a:alpine:alpine_keys:2.3-r1:*:*:*:*:*:*:*"),
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user