/
lang.go
61 lines (52 loc) · 1.4 KB
/
lang.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
package helpers
// IntValue converts the supplied int pointer to an int, returning zero if
// the pointer is nil.
func IntValue(v *int) int {
if v == nil {
return 0
}
return *v
}
// IntPtr converts the supplied int to a pointer to that int.
func IntPtr(p int) *int { return &p }
// BoolPtr converts the supplied bool to a pointer to that bool
func BoolPtr(p bool) *bool { return &p }
// BoolValue converts the supplied bool pointer to an bool, returning false if
// the pointer is nil.
func BoolValue(v *bool) bool {
if v == nil {
return false
}
return *v
}
// StringPtr converts the supplied string to a pointer to that string.
func StringPtr(p string) *string { return &p }
// StringValue converts the supplied string pointer to a string, returning the
// empty string if the pointer is nil.
func StringValue(v *string) string {
if v == nil {
return ""
}
return *v
}
// LateInitializeBool implements late initialization for bool type.
func LateInitializeBool(b *bool, from bool) *bool {
if b != nil || !from {
return b
}
return &from
}
// LateInitializeInt implements late initialization for int type.
func LateInitializeInt(i *int, from int) *int {
if i != nil || from == 0 {
return i
}
return &from
}
// LateInitializeString implements late initialization for string type.
func LateInitializeString(s *string, from string) *string {
if s != nil || from == "" {
return s
}
return &from
}