-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpointers.go
More file actions
260 lines (232 loc) · 4.87 KB
/
pointers.go
File metadata and controls
260 lines (232 loc) · 4.87 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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package pointers
import (
"fmt"
"reflect"
"time"
)
// Any returns a pointer to interface{} value x.
func Any(x interface{}, opts ...bool) interface{} {
if nilify(x, opts...) {
return nil
}
r := reflect.New(reflect.TypeOf(x))
reflect.ValueOf(r.Interface()).Elem().Set(reflect.ValueOf(x))
return r.Interface()
}
// Bool returns a pointer to bool value x.
func Bool(x bool, opts ...bool) *bool {
if nilify(x, opts...) {
return nil
}
return &x
}
// Byte returns a pointer to byte value x.
func Byte(x byte, opts ...bool) *byte {
if nilify(x, opts...) {
return nil
}
return &x
}
// Bytes returns a pointer to []byte value x.
func Bytes(x []byte, opts ...bool) *[]byte {
if nilify(x, opts...) {
return nil
}
return &x
}
// Int returns a pointer to int value x.
func Int(x int, opts ...bool) *int {
if nilify(x, opts...) {
return nil
}
return &x
}
// Int8 returns a pointer to int8 value x.
func Int8(x int8, opts ...bool) *int8 {
if nilify(x, opts...) {
return nil
}
return &x
}
// Int16 returns a pointer to int16 value x.
func Int16(x int16, opts ...bool) *int16 {
if nilify(x, opts...) {
return nil
}
return &x
}
// Int32 returns a pointer to int32 value x.
func Int32(x int32, opts ...bool) *int32 {
if nilify(x, opts...) {
return nil
}
return &x
}
// Int64 returns a pointer to int64 value x.
func Int64(x int64, opts ...bool) *int64 {
if nilify(x, opts...) {
return nil
}
return &x
}
// Uint returns a pointer to uint value x.
func Uint(x uint, opts ...bool) *uint {
if nilify(x, opts...) {
return nil
}
return &x
}
// Uint8 returns a pointer to uint8 value x.
func Uint8(x uint8, opts ...bool) *uint8 {
if nilify(x, opts...) {
return nil
}
return &x
}
// Uint16 returns a pointer to uint16 value x.
func Uint16(x uint16, opts ...bool) *uint16 {
if nilify(x, opts...) {
return nil
}
return &x
}
// Uint32 returns a pointer to uint32 value x.
func Uint32(x uint32, opts ...bool) *uint32 {
if nilify(x, opts...) {
return nil
}
return &x
}
// Uint64 returns a pointer to uint64 value x.
func Uint64(x uint64, opts ...bool) *uint64 {
if nilify(x, opts...) {
return nil
}
return &x
}
// Uintptr returns a pointer to uintptr value x.
func Uintptr(x uintptr, opts ...bool) *uintptr {
if nilify(x, opts...) {
return nil
}
return &x
}
// Float32 returns a pointer to float32 value x.
func Float32(x float32, opts ...bool) *float32 {
if nilify(x, opts...) {
return nil
}
return &x
}
// Float64 returns a pointer to float64 value x.
func Float64(x float64, opts ...bool) *float64 {
if nilify(x, opts...) {
return nil
}
return &x
}
// Complex64 returns a pointer to complex64 value x.
func Complex64(x complex64, opts ...bool) *complex64 {
if nilify(x, opts...) {
return nil
}
return &x
}
// Complex128 returns a pointer to complex128 value x.
func Complex128(x complex128, opts ...bool) *complex128 {
if nilify(x, opts...) {
return nil
}
return &x
}
// String returns a pointer to string value x.
func String(x string, opts ...bool) *string {
if nilify(x, opts...) {
return nil
}
return &x
}
// Rune returns a pointer to rune value x.
func Rune(x rune, opts ...bool) *rune {
if nilify(x, opts...) {
return nil
}
return &x
}
// Time returns a pointer to type value x.
func Time(x time.Time, opts ...bool) *time.Time {
if nilify(x, opts...) {
return nil
}
return &x
}
// Duration returns a pointer to time.Duration value x.
func Duration(x time.Duration, opts ...bool) *time.Duration {
if nilify(x, opts...) {
return nil
}
return &x
}
const Nilify bool = true // New sets to nil when input is zero value.
var AlwaysNilify = !Nilify // Controls default behaviour for func New.
// nilify returns true input x is zero value and one the following is
// also true: opts[0] or AlwaysNilify. Else, returns false.
func nilify(x interface{}, opts ...bool) bool {
nilify := AlwaysNilify
if len(opts) > 0 {
nilify = opts[0]
}
return nilify && isZero(x)
}
// isZero returns true if the underlying value of x is
// the zero-value of its type.
func isZero(x interface{}) bool {
switch t := x.(type) {
case bool:
return !t
case int:
return t == 0
case int8:
return t == 0
case int16:
return t == 0
case int32: // <=> rune
return t == 0
case int64:
return t == 0
case uint:
return t == 0
case uint8: // <=> byte
return t == 0
case uint16:
return t == 0
case uint32:
return t == 0
case uint64:
return t == 0
case uintptr:
return t == 0
case float32:
return t == 0
case float64:
return t == 0
case complex64:
return real(t) == 0 && imag(t) == 0
case complex128:
return real(t) == 0 && imag(t) == 0
case string:
return t == ""
case time.Time:
return t.IsZero()
case time.Duration:
return t == 0
case []byte:
return len(t) == 00
default:
return x == nil
}
}
// testName describe the current test.
func testName(f string, x interface{}) string {
return fmt.Sprintf("Test %s(x %T = %v)", f, x, x)
}