Skip to content

Commit

Permalink
Merge pull request #18 from go-faster/feat/ip-stringer
Browse files Browse the repository at this point in the history
feat(proto): implement Stringer for IPv{4,6}
  • Loading branch information
ernado authored Jan 6, 2022
2 parents f323d41 + c852ee2 commit f3c919d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
4 changes: 4 additions & 0 deletions proto/ipv4.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ import (
// Use ToIP helper for convenience.
type IPv4 uint32

func (v IPv4) String() string {
return v.ToIP().String()
}

// ToIP represents IPv4 as netaddr.IP.
func (v IPv4) ToIP() netaddr.IP {
var buf [4]byte
Expand Down
10 changes: 10 additions & 0 deletions proto/ipv4_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ import (
"github.com/go-faster/ch/internal/gold"
)

func TestIPv4_String(t *testing.T) {
for _, v := range []netaddr.IP{
netaddr.MustParseIP("127.0.0.1"),
netaddr.MustParseIP("1.1.1.1"),
} {
d := ToIPv4(v)
require.Equal(t, v.String(), d.String())
}
}

func TestColIPv4_NetAddr(t *testing.T) {
input := []netaddr.IP{
netaddr.MustParseIP("127.0.0.1"),
Expand Down
6 changes: 6 additions & 0 deletions proto/ipv6.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@ import (
)

// IPv6 represents IPv6 address.
//
// Same as FixedString(16) internally in ClickHouse.
type IPv6 [16]byte

func (v IPv6) String() string {
return v.ToIP().String()
}

// ToIP represents IPv6 as netaddr.IP.
func (v IPv6) ToIP() netaddr.IP {
return netaddr.IPv6Raw(v)
Expand Down
10 changes: 10 additions & 0 deletions proto/ipv6_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ import (
"github.com/go-faster/ch/internal/gold"
)

func TestIPv6_String(t *testing.T) {
for _, v := range []netaddr.IP{
netaddr.MustParseIP("2001:db8:ac10:fe01:feed:babe:cafe:0"),
netaddr.MustParseIP("2001:4860:4860::8888"),
} {
d := ToIPv6(v)
require.Equal(t, v.String(), d.String())
}
}

func IPv6FromInt(v int) IPv6 {
s := IPv6{}
binary.BigEndian.PutUint64(s[:], uint64(v))
Expand Down

0 comments on commit f3c919d

Please sign in to comment.