-
Notifications
You must be signed in to change notification settings - Fork 18
/
recursion.go
82 lines (70 loc) · 1.77 KB
/
recursion.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
// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Open Component Model contributors.
//
// SPDX-License-Identifier: Apache-2.0
package errors
import (
"fmt"
"reflect"
)
type RecursionError struct {
wrapped error
kind string
elem interface{}
hist []interface{}
}
// ErrRecusion describes a resursion errors caused by a dedicated element with an element history.
func ErrRecusion(kind string, elem interface{}, hist interface{}) error {
return &RecursionError{nil, kind, elem, ToInterfaceSlice(hist)}
}
func ErrRecusionWrap(err error, kind string, elem interface{}, hist interface{}) error {
return &RecursionError{err, kind, elem, ToInterfaceSlice(hist)}
}
func (e *RecursionError) Error() string {
msg := fmt.Sprintf("%s recursion: use of %v", e.kind, e.elem)
if len(e.hist) > 0 {
s := ""
sep := ""
for _, h := range e.hist {
s = fmt.Sprintf("%s%s%v", s, sep, h)
sep = "->"
}
msg = fmt.Sprintf("%s for %s", msg, s)
}
if e.wrapped != nil {
return msg + ": " + e.wrapped.Error()
}
return msg
}
func (e *RecursionError) Unwrap() error {
return e.wrapped
}
func (e *RecursionError) Elem() interface{} {
return e.elem
}
func (e *RecursionError) Kind() string {
return e.kind
}
func IsErrRecusion(err error) bool {
return IsA(err, &RecursionError{})
}
func IsErrRecursionKind(err error, kind string) bool {
var uerr *RecursionError
if err == nil || !As(err, &uerr) {
return false
}
return uerr.kind == kind
}
func ToInterfaceSlice(list interface{}) []interface{} {
if list == nil {
return nil
}
v := reflect.ValueOf(list)
if v.Kind() != reflect.Array && v.Kind() != reflect.Slice {
panic("no array or slice")
}
r := make([]interface{}, v.Len())
for i := 0; i < v.Len(); i++ {
r[i] = v.Index(i).Interface()
}
return r
}