-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsorts.go
162 lines (131 loc) · 4.94 KB
/
sorts.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package sdk
// EXISTING_CODE
import (
"fmt"
"sort"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/types"
)
// EXISTING_CODE
type SortOrder = types.SortOrder
const (
Asc SortOrder = types.Ascending
Dec SortOrder = types.Descending
)
type SortSpec struct {
Fields []string `json:"fields"`
Order []SortOrder `json:"orders"`
}
func SortMonitors(monitors []types.Monitor, sortSpec SortSpec) error {
if len(sortSpec.Fields) != len(sortSpec.Order) {
return fmt.Errorf("fields and order must have the same length")
}
sorts := make([]func(p1, p2 types.Monitor) bool, len(sortSpec.Fields))
for i, field := range sortSpec.Fields {
if !types.IsValidMonitorField(field) {
return fmt.Errorf("%s is not an Monitor sort field", field)
}
sorts[i] = types.MonitorBy(types.MonitorField(field), types.SortOrder(sortSpec.Order[i]))
}
sort.Slice(monitors, types.MonitorCmp(monitors, sorts...))
return nil
}
func SortNames(names []types.Name, sortSpec SortSpec) error {
if len(sortSpec.Fields) != len(sortSpec.Order) {
return fmt.Errorf("fields and order must have the same length")
}
sorts := make([]func(p1, p2 types.Name) bool, len(sortSpec.Fields))
for i, field := range sortSpec.Fields {
if !types.IsValidNameField(field) {
return fmt.Errorf("%s is not an Name sort field", field)
}
sorts[i] = types.NameBy(types.NameField(field), types.SortOrder(sortSpec.Order[i]))
}
sort.Slice(names, types.NameCmp(names, sorts...))
return nil
}
func SortChunkRecords(chunkrecords []types.ChunkRecord, sortSpec SortSpec) error {
if len(sortSpec.Fields) != len(sortSpec.Order) {
return fmt.Errorf("fields and order must have the same length")
}
sorts := make([]func(p1, p2 types.ChunkRecord) bool, len(sortSpec.Fields))
for i, field := range sortSpec.Fields {
if !types.IsValidChunkRecordField(field) {
return fmt.Errorf("%s is not an ChunkRecord sort field", field)
}
sorts[i] = types.ChunkRecordBy(types.ChunkRecordField(field), types.SortOrder(sortSpec.Order[i]))
}
sort.Slice(chunkrecords, types.ChunkRecordCmp(chunkrecords, sorts...))
return nil
}
func SortChunkStats(chunkstats []types.ChunkStats, sortSpec SortSpec) error {
if len(sortSpec.Fields) != len(sortSpec.Order) {
return fmt.Errorf("fields and order must have the same length")
}
sorts := make([]func(p1, p2 types.ChunkStats) bool, len(sortSpec.Fields))
for i, field := range sortSpec.Fields {
if !types.IsValidChunkStatsField(field) {
return fmt.Errorf("%s is not an ChunkStats sort field", field)
}
sorts[i] = types.ChunkStatsBy(types.ChunkStatsField(field), types.SortOrder(sortSpec.Order[i]))
}
sort.Slice(chunkstats, types.ChunkStatsCmp(chunkstats, sorts...))
return nil
}
func SortCacheItems(cacheitems []types.CacheItem, sortSpec SortSpec) error {
if len(sortSpec.Fields) != len(sortSpec.Order) {
return fmt.Errorf("fields and order must have the same length")
}
sorts := make([]func(p1, p2 types.CacheItem) bool, len(sortSpec.Fields))
for i, field := range sortSpec.Fields {
if !types.IsValidCacheItemField(field) {
return fmt.Errorf("%s is not an CacheItem sort field", field)
}
sorts[i] = types.CacheItemBy(types.CacheItemField(field), types.SortOrder(sortSpec.Order[i]))
}
sort.Slice(cacheitems, types.CacheItemCmp(cacheitems, sorts...))
return nil
}
func SortChains(chains []types.Chain, sortSpec SortSpec) error {
if len(sortSpec.Fields) != len(sortSpec.Order) {
return fmt.Errorf("fields and order must have the same length")
}
sorts := make([]func(p1, p2 types.Chain) bool, len(sortSpec.Fields))
for i, field := range sortSpec.Fields {
if !types.IsValidChainField(field) {
return fmt.Errorf("%s is not an Chain sort field", field)
}
sorts[i] = types.ChainBy(types.ChainField(field), types.SortOrder(sortSpec.Order[i]))
}
sort.Slice(chains, types.ChainCmp(chains, sorts...))
return nil
}
func SortAbis(abis []types.Abi, sortSpec SortSpec) error {
if len(sortSpec.Fields) != len(sortSpec.Order) {
return fmt.Errorf("fields and order must have the same length")
}
sorts := make([]func(p1, p2 types.Abi) bool, len(sortSpec.Fields))
for i, field := range sortSpec.Fields {
if !types.IsValidAbiField(field) {
return fmt.Errorf("%s is not an Abi sort field", field)
}
sorts[i] = types.AbiBy(types.AbiField(field), types.SortOrder(sortSpec.Order[i]))
}
sort.Slice(abis, types.AbiCmp(abis, sorts...))
return nil
}
func SortFunctions(functions []types.Function, sortSpec SortSpec) error {
if len(sortSpec.Fields) != len(sortSpec.Order) {
return fmt.Errorf("fields and order must have the same length")
}
sorts := make([]func(p1, p2 types.Function) bool, len(sortSpec.Fields))
for i, field := range sortSpec.Fields {
if !types.IsValidFunctionField(field) {
return fmt.Errorf("%s is not an Function sort field", field)
}
sorts[i] = types.FunctionBy(types.FunctionField(field), types.SortOrder(sortSpec.Order[i]))
}
sort.Slice(functions, types.FunctionCmp(functions, sorts...))
return nil
}
// EXISTING_CODE
// EXISTING_CODE