@@ -22,7 +22,14 @@ func pair(a, b *Version) Collection {
22
22
23
23
// String returns a v-prefixed string representation of the k0s version
24
24
func (v * Version ) String () string {
25
- return fmt .Sprintf ("v%s" , v .Version .String ())
25
+ if v == nil {
26
+ return ""
27
+ }
28
+ plain := strings .TrimPrefix (v .Version .String (), "v" )
29
+ if plain == "" {
30
+ return ""
31
+ }
32
+ return fmt .Sprintf ("v%s" , plain )
26
33
}
27
34
28
35
func (v * Version ) urlString () string {
@@ -112,11 +119,19 @@ func (v *Version) Compare(b *Version) int {
112
119
113
120
// MarshalJSON implements the json.Marshaler interface.
114
121
func (v * Version ) MarshalJSON () ([]byte , error ) {
122
+ if v == nil {
123
+ return []byte ("null" ), nil
124
+ }
125
+
115
126
return []byte (fmt .Sprintf ("\" %s\" " , v .String ())), nil
116
127
}
117
128
118
129
// MarshalYAML implements the yaml.Marshaler interface.
119
130
func (v * Version ) MarshalYAML () (interface {}, error ) {
131
+ if v == nil {
132
+ return nil , nil
133
+ }
134
+
120
135
return v .String (), nil
121
136
}
122
137
@@ -125,6 +140,9 @@ func (v *Version) unmarshal(f func(interface{}) error) error {
125
140
if err := f (& s ); err != nil {
126
141
return fmt .Errorf ("failed to decode input: %w" , err )
127
142
}
143
+ if s == "" {
144
+ return nil
145
+ }
128
146
newV , err := NewVersion (s )
129
147
if err != nil {
130
148
return fmt .Errorf ("failed to unmarshal version: %w" , err )
@@ -141,12 +159,21 @@ func (v *Version) UnmarshalYAML(f func(interface{}) error) error {
141
159
// UnmarshalJSON implements the json.Unmarshaler interface.
142
160
func (v * Version ) UnmarshalJSON (b []byte ) error {
143
161
s := strings .TrimSpace (strings .Trim (string (b ), "\" " ))
162
+ if s == "" || s == "null" {
163
+ // go doesn't allow to set nil to a non-pointer struct field, so the result
164
+ // is going to be an empty struct
165
+ return nil
166
+ }
144
167
return v .unmarshal (func (i interface {}) error {
145
168
* (i .(* string )) = s
146
169
return nil
147
170
})
148
171
}
149
172
173
+ func (v * Version ) IsZero () bool {
174
+ return v == nil || v .String () == ""
175
+ }
176
+
150
177
// Satisfies returns true if the version satisfies the supplied constraint
151
178
func (v * Version ) Satisfies (constraint Constraints ) bool {
152
179
return constraint .Check (v )
0 commit comments