-
Notifications
You must be signed in to change notification settings - Fork 19
/
pointer_or_nil.go
189 lines (165 loc) · 4.24 KB
/
pointer_or_nil.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
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
package pointer
import (
"time"
)
/*
Order as in spec:
bool byte complex64 complex128 error float32 float64
int int8 int16 int32 int64 rune string
uint uint8 uint16 uint32 uint64 uintptr
time.Duration time.Time
*/
// ToBoolOrNil returns a pointer to the passed bool value, or nil, if passed value is a zero value.
func ToBoolOrNil(b bool) *bool {
if b == false {
return nil
}
return &b
}
// ToByteOrNil returns a pointer to the passed byte value, or nil, if passed value is a zero value.
func ToByteOrNil(b byte) *byte {
if b == 0 {
return nil
}
return &b
}
// ToComplex64OrNil returns a pointer to the passed complex64 value, or nil, if passed value is a zero value.
func ToComplex64OrNil(c complex64) *complex64 {
if c == 0 {
return nil
}
return &c
}
// ToComplex128OrNil returns a pointer to the passed complex128 value, or nil, if passed value is a zero value.
func ToComplex128OrNil(c complex128) *complex128 {
if c == 0 {
return nil
}
return &c
}
// ToErrorOrNil returns a pointer to the passed error value, or nil, if passed value is a zero value.
func ToErrorOrNil(e error) *error {
if e == nil {
return nil
}
return &e
}
// ToFloat32OrNil returns a pointer to the passed float32 value, or nil, if passed value is a zero value.
func ToFloat32OrNil(f float32) *float32 {
if f == 0 {
return nil
}
return &f
}
// ToFloat64OrNil returns a pointer to the passed float64 value, or nil, if passed value is a zero value.
func ToFloat64OrNil(f float64) *float64 {
if f == 0 {
return nil
}
return &f
}
// ToIntOrNil returns a pointer to the passed int value, or nil, if passed value is a zero value.
func ToIntOrNil(i int) *int {
if i == 0 {
return nil
}
return &i
}
// ToInt8OrNil returns a pointer to the passed int8 value, or nil, if passed value is a zero value.
func ToInt8OrNil(i int8) *int8 {
if i == 0 {
return nil
}
return &i
}
// ToInt16OrNil returns a pointer to the passed int16 value, or nil, if passed value is a zero value.
func ToInt16OrNil(i int16) *int16 {
if i == 0 {
return nil
}
return &i
}
// ToInt32OrNil returns a pointer to the passed int32 value, or nil, if passed value is a zero value.
func ToInt32OrNil(i int32) *int32 {
if i == 0 {
return nil
}
return &i
}
// ToInt64OrNil returns a pointer to the passed int64 value, or nil, if passed value is a zero value.
func ToInt64OrNil(i int64) *int64 {
if i == 0 {
return nil
}
return &i
}
// ToRuneOrNil returns a pointer to the passed rune value, or nil, if passed value is a zero value.
func ToRuneOrNil(r rune) *rune {
if r == 0 {
return nil
}
return &r
}
// ToStringOrNil returns a pointer to the passed string value, or nil, if passed value is a zero value.
func ToStringOrNil(s string) *string {
if s == "" {
return nil
}
return &s
}
// ToUintOrNil returns a pointer to the passed uint value, or nil, if passed value is a zero value.
func ToUintOrNil(u uint) *uint {
if u == 0 {
return nil
}
return &u
}
// ToUint8OrNil returns a pointer to the passed uint8 value, or nil, if passed value is a zero value.
func ToUint8OrNil(u uint8) *uint8 {
if u == 0 {
return nil
}
return &u
}
// ToUint16OrNil returns a pointer to the passed uint16 value, or nil, if passed value is a zero value.
func ToUint16OrNil(u uint16) *uint16 {
if u == 0 {
return nil
}
return &u
}
// ToUint32OrNil returns a pointer to the passed uint32 value, or nil, if passed value is a zero value.
func ToUint32OrNil(u uint32) *uint32 {
if u == 0 {
return nil
}
return &u
}
// ToUint64OrNil returns a pointer to the passed uint64 value, or nil, if passed value is a zero value.
func ToUint64OrNil(u uint64) *uint64 {
if u == 0 {
return nil
}
return &u
}
// ToUintptrOrNil returns a pointer to the passed uintptr value, or nil, if passed value is a zero value.
func ToUintptrOrNil(u uintptr) *uintptr {
if u == 0 {
return nil
}
return &u
}
// ToDurationOrNil returns a pointer to the passed time.Duration value, or nil, if passed value is a zero value.
func ToDurationOrNil(d time.Duration) *time.Duration {
if d == 0 {
return nil
}
return &d
}
// ToTimeOrNil returns a pointer to the passed time.Time value, or nil, if passed value is a zero value (t.IsZero() returns true).
func ToTimeOrNil(t time.Time) *time.Time {
if t.IsZero() {
return nil
}
return &t
}