-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathoptional.go
216 lines (186 loc) · 4.66 KB
/
optional.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
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
package utility
import "time"
// This file includes conversion functions for using pointers as optional
// values.
// TruePtr returns a pointer to a true value.
func TruePtr() *bool {
res := true
return &res
}
// FalsePtr returns a pointer to a false value.
func FalsePtr() *bool {
res := false
return &res
}
// ToBoolPtr returns a pointer to a bool value.
func ToBoolPtr(in bool) *bool {
return &in
}
// FromBoolPtr returns the resolved boolean value from the input boolean
// pointer. For nil pointers, it returns false (i.e. the default value is false
// when the boolean is unspecified).
func FromBoolPtr(in *bool) bool {
if in == nil {
return false
}
return *in
}
// FromBoolTPtr returns the resolved boolean value from the input boolean
// pointer. For nil pointers, it returns true (i.e. the default value is true
// when the boolean is unspecified).
func FromBoolTPtr(in *bool) bool {
if in == nil {
return true
}
return *in
}
// BoolPtrCopy creates a new pointer with the same value as in.
func BoolPtrCopy(in *bool) *bool {
if in == nil {
return nil
}
return ToBoolPtr(*in)
}
// ToIntPtr returns a pointer to an int value.
func ToIntPtr(in int) *int {
return &in
}
// FromIntPtr returns the resolved int value from the input int pointer. For nil
// pointers, it returns 0.
func FromIntPtr(in *int) int {
if in == nil {
return 0
}
return *in
}
// ToInt64Ptr returns a pointer to an int64 value.
func ToInt64Ptr(in int64) *int64 {
return &in
}
// FromInt64Ptr returns the resolved int64 value from the input int64 pointer.
// For nil pointers, it returns 0.
func FromInt64Ptr(in *int64) int64 {
if in == nil {
return 0
}
return *in
}
// ToInt32Ptr returns a pointer to an int32 value.
func ToInt32Ptr(in int32) *int32 {
return &in
}
// FromInt32Ptr returns the resolved int32 value from the input int32 pointer.
// For nil pointers, it returns 0.
func FromInt32Ptr(in *int32) int32 {
if in == nil {
return 0
}
return *in
}
// ToUintPtr returns a pointer to a uint value.
func ToUintPtr(in uint) *uint {
return &in
}
// FromUintPtr returns the resolved uint value from the input uint pointer. For
// nil pointers, it returns 0.
func FromUintPtr(in *uint) uint {
if in == nil {
return 0
}
return *in
}
// ToBytePtr returns a pointer to a byte value.
func ToBytePtr(in byte) *byte {
return &in
}
// FromBytePtr returns the resolved byte value from the input byte pointer. For
// nil pointers, it returns 0.
func FromBytePtr(in *byte) byte {
if in == nil {
return 0
}
return *in
}
// ToFloat64Ptr returns a pointer to a float64 value.
func ToFloat64Ptr(in float64) *float64 {
return &in
}
// FromFloat64Ptr returns the resolved float64 value from the input float64
// pointer. For nil pointers, it returns 0.
func FromFloat64Ptr(in *float64) float64 {
if in == nil {
return 0
}
return *in
}
// ToFloat32Ptr returns a pointer to a float32 value.
func ToFloat32Ptr(in float32) *float32 {
return &in
}
// FromFloat32Ptr returns the resolved float32 value from the input float32
// pointer. For nil pointers, it returns 0.
func FromFloat32Ptr(in *float32) float32 {
if in == nil {
return 0
}
return *in
}
// ToTimeDurationPtr returns a pointer to a time.Duration value.
func ToTimeDurationPtr(in time.Duration) *time.Duration {
return &in
}
// FromTimeDurationPtr returns the resolved time.Duration value from the input
// time.Duration pointer. For nil pointers, it returns time.Duration(0).
func FromTimeDurationPtr(in *time.Duration) time.Duration {
if in == nil {
return 0
}
return *in
}
// ToTimePtr returns a pointer to a time.Time value.
func ToTimePtr(in time.Time) *time.Time {
return &in
}
// FromTimePtr returns the resolved time.Time value from the input time.Time
// pointer. For nil pointers, it returns the zero time.
func FromTimePtr(in *time.Time) time.Time {
if in == nil {
return time.Time{}
}
return *in
}
// ToStringPtr returns a pointer to a string.
func ToStringPtr(in string) *string {
return &in
}
// FromStringPtr returns the resolved string value from the input string
// pointer. For nil pointers, it returns the empty string.
func FromStringPtr(in *string) string {
if in == nil {
return ""
}
return *in
}
// ToStringPtr returns a slice of string pointers from a slice of strings.
func ToStringPtrSlice(in []string) []*string {
if in == nil {
return nil
}
res := []*string{}
for _, each := range in {
res = append(res, ToStringPtr(each))
}
return res
}
// FromStringPtrSlice returns a slice of strings from a slice of string
// pointers.
func FromStringPtrSlice(in []*string) []string {
if in == nil {
return nil
}
res := []string{}
for _, each := range in {
res = append(res, FromStringPtr(each))
}
return res
}