-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpointers.go
More file actions
66 lines (54 loc) · 1.4 KB
/
pointers.go
File metadata and controls
66 lines (54 loc) · 1.4 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
package unstructured
// String returns a pointer to the given string value.
// This is useful when you need to pass optional string values to API requests.
func String(s string) *string {
return &s
}
// Bool returns a pointer to the given boolean value.
// This is useful when you need to pass optional boolean values to API requests.
func Bool(b bool) *bool {
return &b
}
// Int returns a pointer to the given integer value.
// This is useful when you need to pass optional integer values to API requests.
func Int(i int) *int {
return &i
}
// ToString converts a string pointer to a string value.
// If the pointer is nil, it returns an empty string.
func ToString(p *string) string {
if p == nil {
return ""
}
return *p
}
// ToBool converts a boolean pointer to a boolean value.
// If the pointer is nil, it returns false.
func ToBool(p *bool) bool {
if p == nil {
return false
}
return *p
}
// ToInt converts an integer pointer to an integer value.
// If the pointer is nil, it returns 0.
func ToInt(p *int) int {
if p == nil {
return 0
}
return *p
}
// Ptr returns a pointer to the given value.
// This is useful when you need to pass optional values to API requests.
func Ptr[T any](v T) *T {
return &v
}
// ToVal converts a pointer to a value.
// If the pointer is nil, it returns the zero value of the type.
func ToVal[T any](p *T) T {
if p == nil {
var val T
return val
}
return *p
}