Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion scw/custom_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"encoding/json"
"fmt"
"io"
"net"
"strings"
"time"

"github.com/scaleway/scaleway-sdk-go/internal/errors"
Expand Down Expand Up @@ -123,7 +125,7 @@ type TimeSeriesPoint struct {
Value float32
}

func (tsp *TimeSeriesPoint) MarshalJSON() ([]byte, error) {
func (tsp TimeSeriesPoint) MarshalJSON() ([]byte, error) {
timestamp := tsp.Timestamp.Format(time.RFC3339)
value, err := json.Marshal(tsp.Value)
if err != nil {
Expand Down Expand Up @@ -164,3 +166,36 @@ func (tsp *TimeSeriesPoint) UnmarshalJSON(b []byte) error {

return nil
}

// IPNet inherits net.IPNet and represents an IP network.
type IPNet struct {
net.IPNet
}

func (n IPNet) MarshalJSON() ([]byte, error) {
return []byte(`"` + n.String() + `"`), nil
}

func (n *IPNet) UnmarshalJSON(b []byte) error {
var str *string

err := json.Unmarshal(b, &str)
if err != nil {
return err
}
if str == nil {
return nil
}

if !strings.Contains(*str, "/") && net.ParseIP(*str) != nil {
*str += "/32"
}

_, value, err := net.ParseCIDR(*str)
if err != nil {
return errors.Wrap(err, "cannot decode JSON")
}
n.IPNet = *value

return nil
}
76 changes: 75 additions & 1 deletion scw/custom_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package scw
import (
"encoding/json"
"io/ioutil"
"net"
"testing"
"time"

Expand Down Expand Up @@ -67,7 +68,7 @@ func TestTimeSeries_MarshallJSON(t *testing.T) {
}
}

func TestTimeSeries_UnmarshallJSON(t *testing.T) {
func TestTimeSeries_UnmarshalJSON(t *testing.T) {
cases := []struct {
name string
json string
Expand Down Expand Up @@ -166,3 +167,76 @@ func TestFile_UnmarshalJSON(t *testing.T) {
content: []byte("\x00\x00\x00\n"),
}))
}

func TestIPNet_MarshallJSON(t *testing.T) {
cases := []struct {
name string
ipRange IPNet
want string
err error
}{
{
name: "ip",
ipRange: IPNet{IPNet: net.IPNet{IP: net.IPv4(42, 42, 42, 42), Mask: net.CIDRMask(32, 32)}},
want: `"42.42.42.42/32"`,
},
{
name: "network",
ipRange: IPNet{IPNet: net.IPNet{IP: net.IPv4(42, 42, 42, 42), Mask: net.CIDRMask(16, 32)}},
want: `"42.42.42.42/16"`,
},
}

for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
got, err := json.Marshal(c.ipRange)

testhelpers.Equals(t, c.err, err)
if c.err == nil {
testhelpers.Equals(t, c.want, string(got))
}
})
}
}

func TestIPNet_UnmarshalJSON(t *testing.T) {
cases := []struct {
name string
json string
want IPNet
err string
}{
{
name: "IP with CIDR",
json: `"42.42.42.42/32"`,
want: IPNet{IPNet: net.IPNet{IP: net.IPv4(42, 42, 42, 42), Mask: net.CIDRMask(32, 32)}},
},
{
name: "IP with network",
json: `"192.0.2.1/24"`,
want: IPNet{IPNet: net.IPNet{IP: net.IPv4(192, 0, 2, 0), Mask: net.CIDRMask(24, 32)}},
},
{
name: "IP alone",
json: `"42.42.42.42"`,
want: IPNet{IPNet: net.IPNet{IP: net.IPv4(42, 42, 42, 42), Mask: net.CIDRMask(32, 32)}},
},
{
name: "with timestamp error",
json: `"name"`,
err: "scaleway-sdk-go: cannot decode JSON: invalid CIDR address: name",
},
}

for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
ipNet := &IPNet{}
err := json.Unmarshal([]byte(c.json), ipNet)
if err != nil {
testhelpers.Equals(t, c.err, err.Error())
}

testhelpers.Equals(t, c.want.String(), ipNet.String())
})
}
}