-
Notifications
You must be signed in to change notification settings - Fork 16
/
sort.go
76 lines (65 loc) · 1.67 KB
/
sort.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
64
65
66
67
68
69
70
71
72
73
74
75
76
package dynaml
import (
"github.com/mandelsoft/spiff/yaml"
"sort"
)
func func_sort(arguments []interface{}, binding Binding) (result interface{}, info EvaluationInfo, ok bool) {
info = DefaultInfo()
if len(arguments) < 1 || len(arguments) > 2 {
return info.Error("sort takes one or two arguments")
}
list, ok := arguments[0].([]yaml.Node)
if !ok {
return info.Error("argument for sort must be a list")
}
var less Less
if len(arguments) == 2 {
lambda, ok := arguments[1].(LambdaValue)
if !ok {
return info.Error("second argument for sort must be a lambda function")
}
less = LambdaLess(lambda, list, binding)
} else {
less = ValueLess(list)
}
defer CatchEvaluationError(&result, &info, &ok, "sort failed")
sort.SliceStable(list, less)
return list, info, true
}
type Less func(i, j int) bool
func ValueLess(list []yaml.Node) Less {
return func(i, j int) bool {
switch a := list[i].Value().(type) {
case string:
b, ok := list[j].Value().(string)
if ok {
return a < b
}
case int64:
b, ok := list[j].Value().(int64)
if ok {
return a < b
}
}
RaiseEvaluationErrorf("list elements must either be strings or integers")
return false
}
}
func LambdaLess(lambda LambdaValue, list []yaml.Node, binding Binding) Less {
return func(i, j int) bool {
inp := []interface{}{list[i].Value(), list[j].Value()}
resolved, v, info, ok := lambda.Evaluate(false, false, false, nil, inp, binding, false)
if !ok || !resolved {
RaiseEvaluationError(resolved, info, ok)
}
b, ok := v.(bool)
if !ok {
i, ok := v.(int64)
if !ok {
RaiseEvaluationErrorf("lambda must return a bool or integer")
}
b = i < 0
}
return b
}
}