forked from fission/fission-workflows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
priority.go
63 lines (53 loc) · 1.27 KB
/
priority.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package typedvalues
import (
"sort"
"strconv"
"github.com/sirupsen/logrus"
)
const (
MetadataPriority = "priority"
)
// NamedInput provides the TypedValue along with an associated key.
type NamedInput struct {
Key string
Val *TypedValue
}
type namedInputSlice []NamedInput
func (n namedInputSlice) Len() int { return len(n) }
func (n namedInputSlice) Swap(i, j int) { n[i], n[j] = n[j], n[i] }
func (n namedInputSlice) Less(i, j int) bool {
return priority(n[i].Val) < priority(n[j].Val)
}
func sortNamedInputSlices(inputs []NamedInput) {
sort.Sort(sort.Reverse(namedInputSlice(inputs)))
}
func priority(t *TypedValue) int {
var p int
if ps, ok := t.GetMetadata()[MetadataPriority]; ok {
i, err := strconv.Atoi(ps)
if err != nil {
logrus.Warnf("Ignoring invalid priority: %v", ps)
} else {
p = i
}
}
return p
}
func toNamedInputs(inputs map[string]*TypedValue) []NamedInput {
out := make([]NamedInput, len(inputs))
var i int
for k, v := range inputs {
out[i] = NamedInput{
Val: v,
Key: k,
}
i++
}
return out
}
// Prioritize sorts the inputs based on the priority label (descending order)
func Prioritize(inputs map[string]*TypedValue) []NamedInput {
namedInputs := toNamedInputs(inputs)
sortNamedInputSlices(namedInputs)
return namedInputs
}