Skip to content

feat: IPNet type #236

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 14, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
39 changes: 38 additions & 1 deletion scw/custom_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"io"
"net"
"time"

"github.com/scaleway/scaleway-sdk-go/internal/errors"
Expand Down Expand Up @@ -123,7 +124,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 +165,39 @@ 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
}

switch ip := net.ParseIP(*str); {
case ip.To4() != nil:
*str += "/32"
case ip.To16() != nil:
*str += "/128"
}

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

return nil
}
86 changes: 85 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,86 @@ 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: "IPv4 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: "IPv4 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: "IPv6 with network",
json: `"2001:db8:abcd:8000::/50"`,
want: IPNet{IPNet: net.IPNet{IP: net.ParseIP("2001:db8:abcd:8000::"), Mask: net.CIDRMask(50, 128)}},
},
{
name: "IPv4 alone",
json: `"42.42.42.42"`,
want: IPNet{IPNet: net.IPNet{IP: net.IPv4(42, 42, 42, 42), Mask: net.CIDRMask(32, 32)}},
},
{
name: "IPv6 alone",
json: `"2001:db8:abcd:8000::"`,
want: IPNet{IPNet: net.IPNet{IP: net.ParseIP("2001:db8:abcd:8000::"), Mask: net.CIDRMask(128, 128)}},
},
{
name: "invalid CIDR error",
json: `"invalidvalue"`,
err: "scaleway-sdk-go: cannot decode JSON: invalid CIDR address: invalidvalue",
},
}

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())
})
}
}