Skip to content

Commit

Permalink
dep: remove inet.af/netaddr and associated deprecated functions
Browse files Browse the repository at this point in the history
  • Loading branch information
vista- committed Nov 12, 2024
1 parent b772227 commit 8c19163
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 82 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ require (
golang.org/x/term v0.26.0
golang.org/x/text v0.20.0
gotest.tools/v3 v3.5.1
inet.af/netaddr v0.0.0-20230525184311-b8eac61e914a
k8s.io/client-go v0.31.2
)

Expand Down
30 changes: 0 additions & 30 deletions internal/funcs/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ import (

"github.com/hairyhenderson/gomplate/v4/conv"
"github.com/hairyhenderson/gomplate/v4/internal/cidr"
"github.com/hairyhenderson/gomplate/v4/internal/deprecated"
"github.com/hairyhenderson/gomplate/v4/net"
"go4.org/netipx"
"inet.af/netaddr"
)

// CreateNetFuncs -
Expand Down Expand Up @@ -58,30 +56,6 @@ func (f NetFuncs) LookupTXT(name interface{}) ([]string, error) {
return net.LookupTXT(conv.ToString(name))
}

// ParseIP -
//
// Deprecated: use [ParseAddr] instead
func (f *NetFuncs) ParseIP(ip interface{}) (netaddr.IP, error) {
deprecated.WarnDeprecated(f.ctx, "net.ParseIP is deprecated - use net.ParseAddr instead")
return netaddr.ParseIP(conv.ToString(ip))
}

// ParseIPPrefix -
//
// Deprecated: use [ParsePrefix] instead
func (f *NetFuncs) ParseIPPrefix(ipprefix interface{}) (netaddr.IPPrefix, error) {
deprecated.WarnDeprecated(f.ctx, "net.ParseIPPrefix is deprecated - use net.ParsePrefix instead")
return netaddr.ParseIPPrefix(conv.ToString(ipprefix))
}

// ParseIPRange -
//
// Deprecated: use [ParseRange] instead
func (f *NetFuncs) ParseIPRange(iprange interface{}) (netaddr.IPRange, error) {
deprecated.WarnDeprecated(f.ctx, "net.ParseIPRange is deprecated - use net.ParseRange instead")
return netaddr.ParseIPRange(conv.ToString(iprange))
}

// ParseAddr -
func (f NetFuncs) ParseAddr(ip interface{}) (netip.Addr, error) {
return netip.ParseAddr(conv.ToString(ip))
Expand All @@ -103,10 +77,6 @@ func (f *NetFuncs) parseNetipPrefix(prefix interface{}) (netip.Prefix, error) {
switch p := prefix.(type) {
case *stdnet.IPNet:
return f.ipPrefixFromIPNet(p), nil
case netaddr.IPPrefix:
deprecated.WarnDeprecated(f.ctx,
"support for netaddr.IPPrefix is deprecated - use net.ParsePrefix to produce a netip.Prefix instead")
return f.ipPrefixFromIPNet(p.Masked().IPNet()), nil
case netip.Prefix:
return p, nil
default:
Expand Down
53 changes: 2 additions & 51 deletions internal/funcs/net_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/hairyhenderson/gomplate/v4/internal/config"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"inet.af/netaddr"
)

func TestCreateNetFuncs(t *testing.T) {
Expand All @@ -37,53 +36,6 @@ func TestNetLookupIP(t *testing.T) {
assert.Equal(t, "127.0.0.1", must(n.LookupIP("localhost")))
}

func TestParseIP(t *testing.T) {
t.Parallel()

n := testNetNS()
_, err := n.ParseIP("not an IP")
require.Error(t, err)

ip, err := n.ParseIP("2001:470:20::2")
require.NoError(t, err)
assert.Equal(t, netaddr.IPFrom16([16]byte{
0x20, 0x01, 0x04, 0x70,
0, 0x20, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0x02,
}), ip)
}

func TestParseIPPrefix(t *testing.T) {
t.Parallel()

n := testNetNS()
_, err := n.ParseIPPrefix("not an IP")
require.Error(t, err)

_, err = n.ParseIPPrefix("1.1.1.1")
require.Error(t, err)

ipprefix, err := n.ParseIPPrefix("192.168.0.2/28")
require.NoError(t, err)
assert.Equal(t, "192.168.0.0/28", ipprefix.Masked().String())
}

func TestParseIPRange(t *testing.T) {
t.Parallel()

n := testNetNS()
_, err := n.ParseIPRange("not an IP")
require.Error(t, err)

_, err = n.ParseIPRange("1.1.1.1")
require.Error(t, err)

iprange, err := n.ParseIPRange("192.168.0.2-192.168.23.255")
require.NoError(t, err)
assert.Equal(t, "192.168.0.2-192.168.23.255", iprange.String())
}

func TestParseAddr(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -154,8 +106,7 @@ func TestCIDRHost(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, "fd00:fd12:3456:7890::22", ip.String())

// inet.af/netaddr.IPPrefix
ipPrefix, _ := n.ParseIPPrefix("10.12.127.0/20")
ipPrefix, _ := n.ParsePrefix("10.12.127.0/20")

ip, err = n.CIDRHost(16, ipPrefix)
require.NoError(t, err)
Expand All @@ -165,7 +116,7 @@ func TestCIDRHost(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, "10.12.113.12", ip.String())

ipPrefix, _ = n.ParseIPPrefix("fd00:fd12:3456:7890:00a2::/72")
ipPrefix, _ = n.ParsePrefix("fd00:fd12:3456:7890:00a2::/72")
ip, err = n.CIDRHost(34, ipPrefix)
require.NoError(t, err)
assert.Equal(t, "fd00:fd12:3456:7890::22", ip.String())
Expand Down

0 comments on commit 8c19163

Please sign in to comment.