-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.go
More file actions
58 lines (49 loc) · 1.38 KB
/
utils.go
File metadata and controls
58 lines (49 loc) · 1.38 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
package iter
import (
"fmt"
"reflect"
)
// Functions that are not worth an import dependency are in here, they come from
// another package of mine, go-refutil and are well tested.
// indirect will perform recursive indirection on the given value. It should
// never panic and will return a value unless indirection is impossible due to
// infinite recursion in cases like `type Element *Element`.
func indirect(value interface{}) interface{} {
// Just to be safe, recursion should not be possible but I may be
// missing an edge case.
for {
val := reflect.ValueOf(value)
if !val.IsValid() || val.Kind() != reflect.Ptr {
// Value is not a pointer.
return value
}
res := reflect.Indirect(val)
if !res.IsValid() || !res.CanInterface() {
// Invalid value or can't be returned as interface{}.
return value
}
// Test for a circular type.
if res.Kind() == reflect.Ptr && val.Pointer() == res.Pointer() {
return value
}
// Next round.
value = res.Interface()
}
}
// recoverFn will attempt to execute f, if f return a non-nil error it will be
// returned. If f panics this function will attempt to recover() and return a
// error instead.
func recoverFn(f func() error) (err error) {
defer func() {
if r := recover(); r != nil {
switch T := r.(type) {
case error:
err = T
default:
err = fmt.Errorf("panic: %v", r)
}
}
}()
err = f()
return
}