-
Notifications
You must be signed in to change notification settings - Fork 5
/
types.go
69 lines (55 loc) · 1.04 KB
/
types.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
package errors
import (
"reflect"
"sort"
)
func deepAppendTypes(types []string, err error) []string {
walk(err, func(err error) {
types = appendTypes(types, err)
})
return dedupeTypes(types)
}
func appendTypes(types []string, err error) []string {
if e, ok := err.(errorTypes); ok {
types = append(types, e.Types()...)
}
t := reflect.TypeOf(err)
v := reflect.ValueOf(err)
for i, n := 0, t.NumMethod(); i != n; i++ {
mt := t.Method(i)
mv := v.Method(i)
if f, ok := mv.Interface().(func() bool); ok && f() {
types = append(types, mt.Name)
}
}
return types
}
func copyTypes(types []string) []string {
if len(types) == 0 {
return nil
}
cpy := make([]string, len(types))
copy(cpy, types)
return cpy
}
func dedupeTypes(types []string) []string {
if len(types) == 0 {
return nil
}
sortTypes(types)
prev := types[0]
i := 1
j := 1
n := len(types)
for i != n {
if types[i] != prev {
prev, types[j] = types[i], types[i]
j++
}
i++
}
return types[:j]
}
func sortTypes(types []string) {
sort.Strings(types)
}