-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
executable file
·61 lines (47 loc) · 1.04 KB
/
utils.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
package goerrors
import (
"reflect"
)
// Concatenate 2 string lists
func _concat(l1, l2 []string) []string {
if (l1 == nil) && (l2 == nil) {
return nil
}
sz1 := len(l1)
sz2 := len(l2)
res := make([]string, sz1+sz2)
if sz1 != 0 {
copy(res, l1)
}
if sz2 != 0 {
copy(res[sz1:], l2)
}
return res
}
// Get type hierarchy from an error type passed as `this_type` parameter.
// The `final_type` parameter represents type of `GoError` structure.
func _getTypeHierarchy(thisType, finalType reflect.Type) []string {
if (thisType == nil) || (thisType.Kind() != reflect.Struct) {
return []string{}
}
res := []string{thisType.PkgPath() + "." + thisType.Name()}
if thisType == finalType {
return res
}
list := make([]string, 0)
n := thisType.NumField()
for i := 0; i < n; i++ {
field := thisType.Field(i)
if !field.Anonymous {
continue
}
parents := _getTypeHierarchy(field.Type, finalType)
if len(parents) > 0 {
list = _concat(list, parents)
}
}
if len(list) == 0 {
return list
}
return _concat(res, list)
}