-
Notifications
You must be signed in to change notification settings - Fork 0
/
inspect.go
137 lines (126 loc) · 3.41 KB
/
inspect.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package zrr
import (
"errors"
"time"
)
// IsImmutable returns true if error err is instance of Error and is immutable.
func IsImmutable(err error) bool {
var e *Error
if errors.As(err, &e) && e != nil {
return e.imm
}
return false
}
// HasKey returns true if error err is instance of Error and has the key set.
func HasKey(err error, key string) bool {
if err, ok := err.(*Error); ok && err != nil {
_, ok := err.meta[key]
return ok
}
return false
}
// HasCode returns true if error err is instance of Error and has any of the codes.
func HasCode(err error, codes ...string) bool {
var e *Error
if errors.As(err, &e) && e != nil {
for _, code := range codes {
if code == e.code {
return true
}
}
}
return false
}
// GetCode returns error code if error err is instance of Error.
// If error code is not set it will return empty string.
func GetCode(err error) string {
var e *Error
if errors.As(err, &e) && e != nil {
return e.code
}
return ""
}
// GetStr returns the key as a string if err is an instance of Error and key
// exists. If key does not exist, or it's not a string it will return
// false as the second return value.
func GetStr(err error, key string) (string, bool) {
var e *Error
if errors.As(err, &e) && e != nil {
if val, ok := e.meta[key]; ok {
if ret, ok := val.(string); ok {
return ret, true
}
}
}
return "", false
}
// GetInt returns the key as an integer if err is an instance of Error and key
// exists. If key does not exist, or it's not an integer it will return
// false as the second return value.
func GetInt(err error, key string) (int, bool) {
var e *Error
if errors.As(err, &e) && e != nil {
if val, ok := e.meta[key]; ok {
if ret, ok := val.(int); ok {
return ret, true
}
}
}
return 0, false
}
// GetInt64 returns the key as an int64 if err is an instance of Error and key
// exists. If key does not exist, or it's not an int64 it will return
// false as the second return value.
func GetInt64(err error, key string) (int64, bool) {
var e *Error
if errors.As(err, &e) && e != nil {
if val, ok := e.meta[key]; ok {
if ret, ok := val.(int64); ok {
return ret, true
}
}
}
return 0, false
}
// GetFloat64 returns the key as a float64 if err is an instance of Error
// and key exists. If key does not exist, or it's not a float64 it will return
// false as the second return value.
func GetFloat64(err error, key string) (float64, bool) {
var e *Error
if errors.As(err, &e) && e != nil {
if val, ok := e.meta[key]; ok {
if ret, ok := val.(float64); ok {
return ret, true
}
}
}
return 0, false
}
// GetTime returns the key as a time.Time if err is an instance of Error
// and key exists. If key does not exist, or it's not a time.Time it will return
// false as the second return value.
func GetTime(err error, key string) (time.Time, bool) {
var e *Error
if errors.As(err, &e) && e != nil {
if val, ok := e.meta[key]; ok {
if ret, ok := val.(time.Time); ok {
return ret, true
}
}
}
return time.Time{}, false
}
// GetBool returns the key as a boolean if err is an instance of Error and key
// exists. If key does not exist, or it is not a boolean it will return
// false as the second return value.
func GetBool(err error, key string) (bool, bool) {
var e *Error
if errors.As(err, &e) && e != nil {
if val, ok := e.meta[key]; ok {
if ret, ok := val.(bool); ok {
return ret, true
}
}
}
return false, false
}