Skip to content

Commit

Permalink
chore: reviews returns
Browse files Browse the repository at this point in the history
  • Loading branch information
42atomys committed Sep 20, 2024
1 parent 46a484b commit 8df7504
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
6 changes: 3 additions & 3 deletions docs/registries/network.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import "github.com/go-sprout/sprout/registry/network"

### <mark style="color:purple;">parseIP</mark>

ParseIP parses a string representation of an IP address and returns its net.IP form. It attempts to parse the string as either an IPv4 or IPv6 address.
ParseIP parses a string representation of an IP address and returns its [`net.IP`](https://pkg.go.dev/net#IP) form. It attempts to parse the string as either an IPv4 or IPv6 address.

<table data-header-hidden>
<thead><tr><th width="174">Name</th><th>Value</th></tr></thead>
Expand All @@ -35,7 +35,7 @@ ParseIP parses a string representation of an IP address and returns its net.IP f

### <mark style="color:purple;">parseMAC</mark>

ParseMAC parses a string representation of a MAC address and returns its net.HardwareAddr form. It attempts to parse the string as a MAC address.
ParseMAC parses a string representation of a MAC address and returns its [`net.HardwareAddr`](https://pkg.go.dev/net#HardwareAddr) form. It attempts to parse the string as a MAC address.

<table data-header-hidden>
<thead><tr><th width="174">Name</th><th>Value</th></tr></thead>
Expand All @@ -52,7 +52,7 @@ ParseMAC parses a string representation of a MAC address and returns its net.Har

### <mark style="color:purple;">parseCIDR</mark>

ParseCIDR parses a string representation of an IP address and prefix length (CIDR notation) and returns its *net.IPNet form. It attempts to parse the provided string as a CIDR (Classless Inter-Domain Routing) block.
ParseCIDR parses a string representation of an IP address and prefix length (CIDR notation) and returns its [`*net.IPNet`](https://pkg.go.dev/net#IPNet) form. It attempts to parse the provided string as a CIDR (Classless Inter-Domain Routing) block.

<table data-header-hidden>
<thead><tr><th width="174">Name</th><th>Value</th></tr></thead>
Expand Down
6 changes: 3 additions & 3 deletions registry/network/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,14 @@ func (nr *NetworkRegistry) ParseCIDR(str string) (*net.IPNet, error) {
//
// Returns:
//
// int - the IP version, 4 for IPv4 or 16 for IPv6.
// uint8 - the IP version, 4 for IPv4 or 16 for IPv6.
// error - an error if the IP address is invalid or cannot be parsed.
//
// Example:
//
// {{ ipVersion "192.168.0.1" }} // Output: 4
// {{ ipVersion "2001:db8::" }} // Output: 6
func (nr *NetworkRegistry) IPVersion(ipStr string) (int, error) {
func (nr *NetworkRegistry) IPVersion(ipStr string) (uint8, error) {
ip, err := nr.ParseIP(ipStr)
if err != nil {
return 0, err
Expand Down Expand Up @@ -369,7 +369,7 @@ func (nr *NetworkRegistry) CIDRContains(cidrStr string, ip string) (bool, error)
func (nr *NetworkRegistry) CIDRSize(cidrStr string) (*big.Int, error) {
cidr, err := nr.ParseCIDR(cidrStr)
if err != nil {
return nil, fmt.Errorf("invalid CIDR block: %w", err)
return nil, err
}

ones, bits := cidr.Mask.Size()
Expand Down
4 changes: 2 additions & 2 deletions registry/network/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (
//
// Returns:
//
// int - 4 for IPv4, 6 for IPv6.
func (nr *NetworkRegistry) determineIPVersion(ip net.IP) int {
// byte - 4 for IPv4, 6 for IPv6.
func (nr *NetworkRegistry) determineIPVersion(ip net.IP) uint8 {
// Check if it's an IPv4 address
if len(ip) == net.IPv4len || (len(ip) == net.IPv6len && ip.To4() != nil) {
return 4
Expand Down
10 changes: 7 additions & 3 deletions registry/network/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

// TestDetermineIPVersion tests the determineIPVersion function.
Expand All @@ -29,17 +30,20 @@ func TestCalculateLastIP(t *testing.T) {
nr := &NetworkRegistry{}

// Test case: IPv4 CIDR
_, cidrIPv4, _ := net.ParseCIDR("192.168.0.0/24")
_, cidrIPv4, err := net.ParseCIDR("192.168.0.0/24")
require.NoError(t, err)
expectedIPv4 := net.ParseIP("192.168.0.255").To4() // Use To4() to ensure it's in 4-byte format
assert.Equal(t, expectedIPv4, nr.calculateLastIP(cidrIPv4).To4())

// Test case: IPv6 CIDR
_, cidrIPv6, _ := net.ParseCIDR("2001:db8::/64")
_, cidrIPv6, err := net.ParseCIDR("2001:db8::/64")
require.NoError(t, err)
expectedIPv6 := net.ParseIP("2001:db8::ffff:ffff:ffff:ffff")
assert.Equal(t, expectedIPv6, nr.calculateLastIP(cidrIPv6))

// Test case: Smaller IPv4 CIDR block
_, cidrIPv4Small, _ := net.ParseCIDR("192.168.1.0/30")
_, cidrIPv4Small, err := net.ParseCIDR("192.168.1.0/30")
require.NoError(t, err)
expectedIPv4Small := net.ParseIP("192.168.1.3").To4() // Use To4() to ensure it's in 4-byte format
assert.Equal(t, expectedIPv4Small, nr.calculateLastIP(cidrIPv4Small).To4())
}

0 comments on commit 8df7504

Please sign in to comment.