-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathgetter.go
More file actions
73 lines (66 loc) · 2.5 KB
/
getter.go
File metadata and controls
73 lines (66 loc) · 2.5 KB
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
package sortutil
import (
"reflect"
)
// A Getter is a function which takes a reflect.Value for a slice, and returns a
// a slice of reflect.Value, e.g. a slice with a reflect.Value for each of the
// Name fields from a reflect.Value for a slice of a struct type. It is used by
// the sort functions to identify the elements to sort by.
type Getter func(reflect.Value) []reflect.Value
func valueSlice(l int) []reflect.Value {
s := make([]reflect.Value, l, l)
return s
}
// Returns a Getter which returns the values from a reflect.Value for a
// slice. This is the default Getter used if none is passed to Sort.
func SimpleGetter() Getter {
return func(s reflect.Value) []reflect.Value {
vals := valueSlice(s.Len())
for i := range vals {
vals[i] = reflect.Indirect(reflect.Indirect(s.Index(i)))
}
return vals
}
}
// Returns a Getter which gets fields with name from a reflect.Value for a
// slice of a struct type, returning them as a slice of reflect.Value (one
// Value for each field in each struct.) Can be used with Sort to sort an
// []Object by e.g. Object.Name or Object.Date. A runtime panic will occur if
// the specified field isn't exported.
func FieldGetter(name string) Getter {
return func(s reflect.Value) []reflect.Value {
vals := valueSlice(s.Len())
for i := range vals {
vals[i] = reflect.Indirect(reflect.Indirect(s.Index(i)).FieldByName(name))
}
return vals
}
}
// Returns a Getter which gets nested fields corresponding to e.g.
// []int{1, 2, 3} = field 3 of field 2 of field 1 of each struct from a
// reflect.Value for a slice of a struct type, returning them as a slice of
// reflect.Value (one Value for each of the indices in the structs.) Can be
// used with Sort to sort an []Object by the first field in the struct
// value of the first field of each Object. A runtime panic will occur if
// the specified field isn't exported.
func FieldByIndexGetter(index []int) Getter {
return func(s reflect.Value) []reflect.Value {
vals := valueSlice(s.Len())
for i := range vals {
vals[i] = reflect.Indirect(reflect.Indirect(s.Index(i)).FieldByIndex(index))
}
return vals
}
}
// Returns a Getter which gets values with index from a reflect.Value for a
// slice. Can be used with Sort to sort an [][]int by e.g. the second element
// in each nested slice.
func IndexGetter(index int) Getter {
return func(s reflect.Value) []reflect.Value {
vals := valueSlice(s.Len())
for i := range vals {
vals[i] = reflect.Indirect(reflect.Indirect(s.Index(i)).Index(index))
}
return vals
}
}