Skip to content

Commit 0caecf4

Browse files
committed
Add optional.Int, optional.Bool, and optional.Restore*
1 parent 300c45c commit 0caecf4

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

pkg/utils/optional/conversion.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,24 @@ import (
2020
"k8s.io/apimachinery/pkg/conversion"
2121
)
2222

23+
func RestoreString(previous, dst *String) {
24+
if *dst == nil || **dst == "" {
25+
*dst = *previous
26+
}
27+
}
28+
29+
func RestoreInt(previous, dst *Int) {
30+
if *dst == nil || **dst == 0 {
31+
*dst = *previous
32+
}
33+
}
34+
35+
func RestoreBool(previous, dst *Bool) {
36+
if *dst == nil || !**dst {
37+
*dst = *previous
38+
}
39+
}
40+
2341
func Convert_string_To_optional_String(in *string, out *String, _ conversion.Scope) error {
2442
// NOTE: This function has the opposite defaulting behaviour to
2543
// Convert_string_to_Pointer_string defined in apimachinery: it converts
@@ -41,3 +59,39 @@ func Convert_optional_String_To_string(in *String, out *string, _ conversion.Sco
4159
}
4260
return nil
4361
}
62+
63+
func Convert_int_To_optional_Int(in *int, out *Int, _ conversion.Scope) error {
64+
if *in == 0 {
65+
*out = nil
66+
} else {
67+
*out = in
68+
}
69+
return nil
70+
}
71+
72+
func Convert_optional_Int_To_int(in *Int, out *int, _ conversion.Scope) error {
73+
if *in == nil {
74+
*out = 0
75+
} else {
76+
*out = **in
77+
}
78+
return nil
79+
}
80+
81+
func Convert_bool_To_optional_Bool(in *bool, out *Bool, _ conversion.Scope) error {
82+
if !*in {
83+
*out = nil
84+
} else {
85+
*out = in
86+
}
87+
return nil
88+
}
89+
90+
func Convert_optional_Bool_To_bool(in *Bool, out *bool, _ conversion.Scope) error {
91+
if *in == nil {
92+
*out = false
93+
} else {
94+
*out = **in
95+
}
96+
return nil
97+
}

pkg/utils/optional/types.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,16 @@ package optional
1919
// String is a string that can be unspecified. strings which are converted to
2020
// optional.String during API conversion will be converted to nil if the value
2121
// was previously the empty string.
22+
// +optional.
2223
type String *string
24+
25+
// Int is an int that can be unspecified. ints which are converted to
26+
// optional.Int during API conversion will be converted to nil if the value
27+
// was previously 0.
28+
// +optional.
29+
type Int *int
30+
31+
// Bool is a bool that can be unspecified. bools which are converted to
32+
// optional.Bool during API conversion will be converted to nil if the value
33+
// was previously false.
34+
type Bool *bool

0 commit comments

Comments
 (0)