-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpointer.go
123 lines (102 loc) · 2.67 KB
/
pointer.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
package pointer
import "time"
// String returns a pointer to the provided string value
func String(value string) *string {
return &value
}
// StringOrDefault returns the pointer if not nil, otherwise the default value
func StringOrDefault(value *string, defaultValue string) *string {
if value != nil {
return value
}
return &defaultValue
}
// StringIfValid returns a pointer to the value if it's valid, otherwise nil
func StringIfValid(valid bool, value string) *string {
if valid {
return &value
}
return nil
}
// StringCopy returns a pointer that's a copy of the provided value
func StringCopy(value *string) *string {
if value == nil {
return nil
}
return String(*value)
}
// Uint64 returns a pointer to the provided uint64 value
func Uint64(value uint64) *uint64 {
return &value
}
// Uint64OrDefault returns the pointer if not nil, otherwise the default value
func Uint64OrDefault(value *uint64, defaultValue uint64) *uint64 {
if value != nil {
return value
}
return &defaultValue
}
// Uint64IfValid returns a pointer to the value if it's valid, otherwise nil
func Uint64IfValid(valid bool, value uint64) *uint64 {
if valid {
return &value
}
return nil
}
// Uint64Copy returns a pointer that's a copy of the provided value
func Uint64Copy(value *uint64) *uint64 {
if value == nil {
return nil
}
return Uint64(*value)
}
// Float64 returns a pointer to the provided float64 value
func Float64(value float64) *float64 {
return &value
}
// Float64OrDefault returns the pointer if not nil, otherwise the default value
func Float64OrDefault(value *float64, defaultValue float64) *float64 {
if value != nil {
return value
}
return &defaultValue
}
// Float64IfValid returns a pointer to the value if it's valid, otherwise nil
func Float64IfValid(valid bool, value float64) *float64 {
if valid {
return &value
}
return nil
}
// Float64Copy returns a pointer that's a copy of the provided value
func Float64Copy(value *float64) *float64 {
if value == nil {
return nil
}
return Float64(*value)
}
// Time returns a pointer to the provided time.Time value
func Time(value time.Time) *time.Time {
return &value
}
// TimeOrDefault returns the pointer if not nil, otherwise the default value
func TimeOrDefault(value *time.Time, defaultValue time.Time) *time.Time {
if value != nil {
return value
}
return &defaultValue
}
// TimeIfValid returns a pointer to the value if it's valid, otherwise nil
func TimeIfValid(valid bool, value time.Time) *time.Time {
if valid {
return &value
}
return nil
}
// TimeCopy returns a pointer that's a copy of the provided value
func TimeCopy(value *time.Time) *time.Time {
if value == nil {
return nil
}
return Time(*value)
}