-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convenience functions for converting to/from standard library types
- Loading branch information
ipfreely-uk
committed
Jan 12, 2025
1 parent
609e369
commit 64b0558
Showing
5 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package ipstd | ||
|
||
import ( | ||
"net" | ||
|
||
"github.com/ipfreely-uk/go/ip" | ||
) | ||
|
||
func ToIP(a ip.Address) net.IP { | ||
return a.Bytes() | ||
} | ||
|
||
func FromIP(a net.IP) ip.Address { | ||
return ip.MustFromBytes(a...) | ||
} | ||
|
||
func ToIPMask(a ip.Address) net.IPMask { | ||
return a.Bytes() | ||
} | ||
|
||
func FromIPMask(a net.IPMask) ip.Address { | ||
return ip.MustFromBytes(a...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package ipstd_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/ipfreely-uk/go/ip" | ||
"github.com/ipfreely-uk/go/ipstd" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestToIP(t *testing.T) { | ||
expected := ip.MustParse(ip.V4(), "127.0.0.1") | ||
n := ipstd.ToIP(expected) | ||
actual := ipstd.FromIP(n) | ||
assert.Equal(t, expected, actual, n.String()) | ||
} | ||
|
||
func TestToIPMask(t *testing.T) { | ||
expected := ip.MustParse(ip.V4(), "127.0.0.1") | ||
n := ipstd.ToIPMask(expected) | ||
actual := ipstd.FromIPMask(n) | ||
assert.Equal(t, expected, actual, n.String()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package ipstd | ||
|
||
import ( | ||
"net/netip" | ||
|
||
"github.com/ipfreely-uk/go/ip" | ||
) | ||
|
||
func ToAddr(a ip.Address) netip.Addr { | ||
r, _ := netip.AddrFromSlice(a.Bytes()) | ||
return r | ||
} | ||
|
||
func FromAddr(a netip.Addr) ip.Address { | ||
return ip.MustFromBytes(a.AsSlice()...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package ipstd_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/ipfreely-uk/go/ip" | ||
"github.com/ipfreely-uk/go/ipstd" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestToAddr(t *testing.T) { | ||
expected := ip.MustParse(ip.V4(), "127.0.0.1") | ||
n := ipstd.ToAddr(expected) | ||
actual := ipstd.FromAddr(n) | ||
assert.Equal(t, expected, actual) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/* | ||
Convenience functions to convert to & from standard library types. | ||
*/ | ||
package ipstd |