Skip to content

Commit 46ad702

Browse files
authored
Fix API Type Unstructured Conversion (#140)
1 parent a25eb23 commit 46ad702

File tree

3 files changed

+28
-34
lines changed

3 files changed

+28
-34
lines changed

charts/core/Chart.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ description: A Helm chart for deploying Unikorn Core
44

55
type: application
66

7-
version: v1.1.0-rc4
8-
appVersion: v1.1.0-rc4
7+
version: v1.1.0-rc5
8+
appVersion: v1.1.0-rc5
99

1010
icon: https://assets.unikorn-cloud.org/images/logos/dark-on-light/icon.svg
1111

pkg/apis/unikorn/v1alpha1/types.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package v1alpha1
2020
import (
2121
"encoding/json"
2222
"errors"
23+
"fmt"
2324
"net"
2425

2526
"github.com/Masterminds/semver/v3"
@@ -132,7 +133,7 @@ func (a *IPv4Address) UnmarshalJSON(b []byte) error {
132133

133134
ip := net.ParseIP(str)
134135
if ip == nil {
135-
return ErrJSONUnmarshal
136+
return fmt.Errorf("%w: not an IPv4 address '%s'", ErrJSONUnmarshal, str)
136137
}
137138

138139
a.IP = ip
@@ -196,11 +197,11 @@ func (p *IPv4Prefix) UnmarshalJSON(b []byte) error {
196197

197198
_, network, err := net.ParseCIDR(str)
198199
if err != nil {
199-
return ErrJSONUnmarshal
200+
return fmt.Errorf("%w: not an IPv4 prefix '%s'", ErrJSONUnmarshal, str)
200201
}
201202

202203
if network == nil {
203-
return ErrJSONUnmarshal
204+
return fmt.Errorf("%w: not an IPv4 prefix '%s'", ErrJSONUnmarshal, str)
204205
}
205206

206207
p.IPNet = *network
@@ -216,7 +217,7 @@ func (p *IPv4Prefix) MarshalJSON() ([]byte, error) {
216217
}
217218

218219
func (p *IPv4Prefix) ToUnstructured() any {
219-
return p.IP.String()
220+
return p.String()
220221
}
221222

222223
// There is no interface defined for these. See

pkg/apis/unikorn/v1alpha1/types_test.go

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@ import (
2828

2929
const (
3030
// Expect IP addresses to be marshalled as strings in dotted quad format.
31-
testAddressMarshaled = `"192.168.0.1"`
31+
testAddressMarshaled = `"192.168.0.1"`
32+
testAddressUnstructured = "192.168.0.1"
3233

3334
// Expect IP prefixes to be marshalled as strings in dotted quad CIDR format.
34-
testPrefixMarshaled = `"192.168.0.0/16"`
35+
testPrefixMarshaled = `"192.168.0.0/16"`
36+
testPrefixUnstructured = "192.168.0.0/16"
3537
)
3638

3739
var (
@@ -51,6 +53,7 @@ func TestSemanticVersionCanonical(t *testing.T) {
5153
t.Parallel()
5254

5355
jsonSemver := `"1.2.3-foo+bar"`
56+
unstructuredSemver := "1.2.3-foo+bar"
5457

5558
out := &v1alpha1.SemanticVersion{}
5659

@@ -62,6 +65,9 @@ func TestSemanticVersionCanonical(t *testing.T) {
6265
marshalled, err := out.MarshalJSON()
6366
require.NoError(t, err)
6467
require.Equal(t, jsonSemver, string(marshalled))
68+
69+
unstructured := out.ToUnstructured()
70+
require.Equal(t, unstructuredSemver, unstructured)
6571
}
6672

6773
func TestSemanticVersion(t *testing.T) {
@@ -111,13 +117,8 @@ func TestIPv4AddressUnmarshal(t *testing.T) {
111117

112118
output := &v1alpha1.IPv4Address{}
113119

114-
if err := output.UnmarshalJSON(input); err != nil {
115-
t.Fatal(err)
116-
}
117-
118-
if !output.Equal(testAddressUnmarshaled) {
119-
t.Fatal("address mismatch")
120-
}
120+
require.NoError(t, output.UnmarshalJSON(input))
121+
require.Equal(t, testAddressUnmarshaled, output.IP)
121122
}
122123

123124
func TestIPv4AddressMarshal(t *testing.T) {
@@ -126,13 +127,12 @@ func TestIPv4AddressMarshal(t *testing.T) {
126127
input := &v1alpha1.IPv4Address{IP: testAddressUnmarshaled}
127128

128129
output, err := input.MarshalJSON()
129-
if err != nil {
130-
t.Fatal()
131-
}
130+
require.NoError(t, err)
132131

133-
if string(output) != testAddressMarshaled {
134-
t.Fatal("address mismatch")
135-
}
132+
require.Equal(t, testAddressMarshaled, string(output))
133+
134+
unstructured := input.ToUnstructured()
135+
require.Equal(t, testAddressUnstructured, unstructured)
136136
}
137137

138138
func TestIPv4PrefixUnmarshal(t *testing.T) {
@@ -142,13 +142,8 @@ func TestIPv4PrefixUnmarshal(t *testing.T) {
142142

143143
output := &v1alpha1.IPv4Prefix{}
144144

145-
if err := output.UnmarshalJSON(input); err != nil {
146-
t.Fatal(nil)
147-
}
148-
149-
if output.String() != testPrefixUnmarshaled.String() {
150-
t.Fatal("prefix mismatch")
151-
}
145+
require.NoError(t, output.UnmarshalJSON(input))
146+
require.Equal(t, testPrefixUnmarshaled.String(), output.String())
152147
}
153148

154149
func TestIPv4PrefixMarshal(t *testing.T) {
@@ -157,11 +152,9 @@ func TestIPv4PrefixMarshal(t *testing.T) {
157152
input := &v1alpha1.IPv4Prefix{IPNet: testPrefixUnmarshaled}
158153

159154
output, err := input.MarshalJSON()
160-
if err != nil {
161-
t.Fatal(err)
162-
}
155+
require.NoError(t, err)
156+
require.Equal(t, testPrefixMarshaled, string(output))
163157

164-
if string(output) != testPrefixMarshaled {
165-
t.Fatal("prefix mismatch")
166-
}
158+
unstructured := input.ToUnstructured()
159+
require.Equal(t, testPrefixUnstructured, unstructured)
167160
}

0 commit comments

Comments
 (0)