-
Notifications
You must be signed in to change notification settings - Fork 170
/
identifierList.go
47 lines (39 loc) · 1.19 KB
/
identifierList.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package flow
import (
"bytes"
"github.com/rs/zerolog/log"
)
// IdentifierList defines a sortable list of identifiers
type IdentifierList []Identifier
// Len returns length of the IdentiferList in the number of stored identifiers.
// It satisfies the sort.Interface making the IdentifierList sortable.
func (il IdentifierList) Len() int {
return len(il)
}
// Less returns true if element i in the IdentifierList is less than j based on its identifier.
// Otherwise it returns true.
// It satisfies the sort.Interface making the IdentifierList sortable.
func (il IdentifierList) Less(i, j int) bool {
// bytes package already implements Comparable for []byte.
switch bytes.Compare(il[i][:], il[j][:]) {
case -1:
return true
case 0, 1:
return false
default:
log.Error().Msg("not fail-able with `bytes.Comparable` bounded [-1, 1].")
return false
}
}
// Swap swaps the element i and j in the IdentifierList.
// It satisfies the sort.Interface making the IdentifierList sortable.
func (il IdentifierList) Swap(i, j int) {
il[j], il[i] = il[i], il[j]
}
func (il IdentifierList) Strings() []string {
var list []string
for _, id := range il {
list = append(list, id.String())
}
return list
}