Skip to content

Commit 5329938

Browse files
marshal nil should return string zero value
1 parent 889baea commit 5329938

File tree

2 files changed

+20
-17
lines changed

2 files changed

+20
-17
lines changed

scw/custom_types.go

+17-14
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import (
77
"io"
88
"net"
99
"time"
10-
11-
"github.com./scaleway/scaleway-sdk-go/internal/errors"
1210
)
1311

1412
// ServiceInfo contains API metadata
@@ -143,23 +141,23 @@ func (tsp *TimeSeriesPoint) UnmarshalJSON(b []byte) error {
143141
}
144142

145143
if len(point) != 2 {
146-
return errors.New("invalid point array")
144+
return fmt.Errorf("invalid point array")
147145
}
148146

149147
strTimestamp, isStrTimestamp := point[0].(string)
150148
if !isStrTimestamp {
151-
return errors.New("%s timestamp is not a string in RFC 3339 format", point[0])
149+
return fmt.Errorf("%s timestamp is not a string in RFC 3339 format", point[0])
152150
}
153151
timestamp, err := time.Parse(time.RFC3339, strTimestamp)
154152
if err != nil {
155-
return errors.New("%s timestamp is not in RFC 3339 format", point[0])
153+
return fmt.Errorf("%s timestamp is not in RFC 3339 format", point[0])
156154
}
157155
tsp.Timestamp = timestamp
158156

159157
// By default, JSON unmarshal a float in float64 but the TimeSeriesPoint is a float32 value.
160158
value, isValue := point[1].(float64)
161159
if !isValue {
162-
return errors.New("%s is not a valid float32 value", point[1])
160+
return fmt.Errorf("%s is not a valid float32 value", point[1])
163161
}
164162
tsp.Value = float32(value)
165163

@@ -172,30 +170,35 @@ type IPNet struct {
172170
}
173171

174172
func (n IPNet) MarshalJSON() ([]byte, error) {
175-
return []byte(`"` + n.String() + `"`), nil
173+
value := n.String()
174+
if value == "<nil>" {
175+
value = ""
176+
}
177+
return []byte(`"` + value + `"`), nil
176178
}
177179

178180
func (n *IPNet) UnmarshalJSON(b []byte) error {
179-
var str *string
181+
var str string
180182

181183
err := json.Unmarshal(b, &str)
182184
if err != nil {
183185
return err
184186
}
185-
if str == nil {
187+
if str == "" {
188+
*n = IPNet{}
186189
return nil
187190
}
188191

189-
switch ip := net.ParseIP(*str); {
192+
switch ip := net.ParseIP(str); {
190193
case ip.To4() != nil:
191-
*str += "/32"
194+
str += "/32"
192195
case ip.To16() != nil:
193-
*str += "/128"
196+
str += "/128"
194197
}
195198

196-
_, value, err := net.ParseCIDR(*str)
199+
_, value, err := net.ParseCIDR(str)
197200
if err != nil {
198-
return errors.Wrap(err, "cannot decode JSON")
201+
return err
199202
}
200203
n.IPNet = *value
201204

scw/custom_types_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ package scw
22

33
import (
44
"encoding/json"
5+
"fmt"
56
"io/ioutil"
67
"net"
78
"testing"
89
"time"
910

10-
"github.com./scaleway/scaleway-sdk-go/internal/errors"
1111
"github.com./scaleway/scaleway-sdk-go/internal/testhelpers"
1212
)
1313

@@ -109,7 +109,7 @@ func TestTimeSeries_UnmarshalJSON(t *testing.T) {
109109
{
110110
name: "with timestamp error",
111111
json: `{"name":"cpu_usage","points":[["2019/08/08T15-00-00Z",0.2]]}`,
112-
err: errors.New("2019/08/08T15-00-00Z timestamp is not in RFC 3339 format"),
112+
err: fmt.Errorf("2019/08/08T15-00-00Z timestamp is not in RFC 3339 format"),
113113
},
114114
}
115115

@@ -234,7 +234,7 @@ func TestIPNet_UnmarshalJSON(t *testing.T) {
234234
{
235235
name: "invalid CIDR error",
236236
json: `"invalidvalue"`,
237-
err: "scaleway-sdk-go: cannot decode JSON: invalid CIDR address: invalidvalue",
237+
err: "invalid CIDR address: invalidvalue",
238238
},
239239
}
240240

0 commit comments

Comments
 (0)