Skip to content

Commit

Permalink
fix: removed netip mappers because they are supported out of the box
Browse files Browse the repository at this point in the history
  • Loading branch information
marsom committed May 21, 2024
1 parent f449f3d commit 8727b63
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 148 deletions.
39 changes: 0 additions & 39 deletions mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"io"
"math/bits"
"net"
"net/netip"
"net/url"
"os"
"reflect"
Expand Down Expand Up @@ -291,8 +290,6 @@ func (r *Registry) RegisterDefaults() *Registry {
RegisterType(reflect.TypeOf(&regexp.Regexp{}), regexMapper()).
RegisterType(reflect.TypeOf(&net.IP{}), netIPMapper()).
RegisterType(reflect.TypeOf(&net.IPNet{}), netIPNetMapper()).
RegisterType(reflect.TypeOf(netip.Addr{}), netipAddrMapper()).
RegisterType(reflect.TypeOf(netip.Prefix{}), netipPrefixMapper()).
RegisterName("path", pathMapper(r)).
RegisterName("existingfile", existingFileMapper(r)).
RegisterName("existingdir", existingDirMapper(r)).
Expand Down Expand Up @@ -801,42 +798,6 @@ func netIPNetMapper() MapperFunc {
}
}

func netipAddrMapper() MapperFunc {
return func(ctx *DecodeContext, target reflect.Value) error {
var value string
if err := ctx.Scan.PopValueInto("ip", &value); err != nil {
return err
}

ip, err := netip.ParseAddr(value)
if err != nil {
return fmt.Errorf("expected ip addresss but got %q: %w", value, err)
}

target.Set(reflect.ValueOf(ip))

return nil
}
}

func netipPrefixMapper() MapperFunc {
return func(ctx *DecodeContext, target reflect.Value) error {
var value string
if err := ctx.Scan.PopValueInto("cidr", &value); err != nil {
return err
}

prefix, err := netip.ParsePrefix(value)
if err != nil {
return fmt.Errorf("expected ipnet but got %q: %w", value, err)
}

target.Set(reflect.ValueOf(prefix))

return nil
}
}

type ptrMapper struct {
r *Registry
}
Expand Down
245 changes: 136 additions & 109 deletions mapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,136 +141,164 @@ func TestDurationMapperJSONResolver(t *testing.T) {
assert.Equal(t, time.Second*5, cli.Flag)
}

func TestNetIP(t *testing.T) {
var cli struct {
Flag net.IP
}
k := mustNew(t, &cli)
_, err := k.Parse([]string{"--flag", "127.0.0.1"})
assert.NoError(t, err)
assert.Equal(t, "127.0.0.1", cli.Flag.String())

_, err = k.Parse([]string{"--flag", "2001:db8:abcd:0012::1"})
assert.NoError(t, err)
assert.Equal(t, "2001:db8:abcd:12::1", cli.Flag.String())
}
func TestIPAddress(t *testing.T) {
tests := []struct {
input string
expected string
}{
{"127.0.0.1", "127.0.0.1"},
{"2001:db8:abcd:0012::1", "2001:db8:abcd:12::1"},
}

t.Run("TestNetPackage", func(t *testing.T) {
var cli1 struct {
Flag net.IP
}
k1 := mustNew(t, &cli1)

func TestNetIPSplice(t *testing.T) {
var cli struct {
Flag []net.IP
}
k := mustNew(t, &cli)
_, err := k.Parse([]string{
"--flag", "127.0.0.1",
"--flag", "192.168.0.1",
"--flag", "2001:db8:abcd:0012::1",
for _, tt := range tests {
_, err := k1.Parse([]string{"--flag", tt.input})
assert.NoError(t, err)
assert.Equal(t, tt.expected, cli1.Flag.String())
}
})
assert.NoError(t, err)

assert.Equal(t, 3, len(cli.Flag))
assert.Equal(t, "127.0.0.1", cli.Flag[0].String())
assert.Equal(t, "192.168.0.1", cli.Flag[1].String())
assert.Equal(t, "2001:db8:abcd:12::1", cli.Flag[2].String())
}

func TestIPNet(t *testing.T) {
var cli struct {
Flag *net.IPNet
}
k := mustNew(t, &cli)
_, err := k.Parse([]string{"--flag", "127.0.0.0/24"})
assert.NoError(t, err)
assert.Equal(t, "127.0.0.0/24", cli.Flag.String())
t.Run("TestNetipPackage", func(t *testing.T) {
var cli1 struct {
Flag netip.Addr
}
k1 := mustNew(t, &cli1)

_, err = k.Parse([]string{"--flag", "2001:db8:abcd:0012::0/64"})
assert.NoError(t, err)
assert.Equal(t, "2001:db8:abcd:12::/64", cli.Flag.String())
for _, tt := range tests {
_, err := k1.Parse([]string{"--flag", tt.input})
assert.NoError(t, err)
assert.Equal(t, tt.expected, cli1.Flag.String())
}
})
}

func TestIPNetSlice(t *testing.T) {
var cli struct {
Test []*net.IPNet
func TestIPAddressSlice(t *testing.T) {
tests := []struct {
input []string
expected []string
}{
{input: []string{"127.0.0.1", "192.168.0.1"}, expected: []string{"127.0.0.1", "192.168.0.1"}},
{input: []string{"2001:db8:abcd:0012::1", "::1"}, expected: []string{"2001:db8:abcd:12::1", "::1"}},
}

k := mustNew(t, &cli)
_, err := k.Parse([]string{
"--test", "127.0.0.0/24",
"--test", "123.0.0.0/23",
"--test", "2001:db8:abcd:0012::0/64",
})
assert.NoError(t, err)
t.Run("TestNetPackage", func(t *testing.T) {
var cli struct {
Flag []net.IP
}
k := mustNew(t, &cli)

assert.Equal(t, 3, len(cli.Test))
assert.Equal(t, "127.0.0.0/24", cli.Test[0].String())
assert.Equal(t, "123.0.0.0/23", cli.Test[1].String())
assert.Equal(t, "2001:db8:abcd:12::/64", cli.Test[2].String())
}
for _, tt := range tests {
_, err := k.Parse([]string{"--flag", strings.Join(tt.input, ",")})
assert.NoError(t, err)
assert.Equal(t, len(tt.input), len(cli.Flag))
}
})

func TestNetipAddr(t *testing.T) {
var cli struct {
Flag netip.Addr
}
k := mustNew(t, &cli)
_, err := k.Parse([]string{"--flag", "127.0.0.1"})
assert.NoError(t, err)
assert.Equal(t, "127.0.0.1", cli.Flag.String())
t.Run("TestNetipPackage", func(t *testing.T) {
var cli struct {
Flag []netip.Addr
}
k := mustNew(t, &cli)

_, err = k.Parse([]string{"--flag", "2001:db8:abcd:0012::1"})
assert.NoError(t, err)
assert.Equal(t, "2001:db8:abcd:12::1", cli.Flag.String())
for _, tt := range tests {
_, err := k.Parse([]string{"--flag", strings.Join(tt.input, ",")})
assert.NoError(t, err)
assert.Equal(t, len(tt.input), len(cli.Flag))
}
})
}

func TestNetipAddrSplice(t *testing.T) {
var cli struct {
Flag []net.IP
func TestCIDR(t *testing.T) {
tests := []struct {
input string
expectedNet string
expectedNetip string
}{
{"127.0.0.1/16", "127.0.0.0/16", "127.0.0.1/16"},
{"2001:db8:abcd:0012::0/64", "2001:db8:abcd:12::/64", "2001:db8:abcd:12::/64"},
}
k := mustNew(t, &cli)
_, err := k.Parse([]string{
"--flag", "127.0.0.1",
"--flag", "192.168.0.1",
"--flag", "2001:db8:abcd:0012::1",
})
assert.NoError(t, err)

assert.Equal(t, 3, len(cli.Flag))
assert.Equal(t, "127.0.0.1", cli.Flag[0].String())
assert.Equal(t, "192.168.0.1", cli.Flag[1].String())
assert.Equal(t, "2001:db8:abcd:12::1", cli.Flag[2].String())
}
t.Run("TestNetPackage", func(t *testing.T) {
var cli1 struct {
Flag *net.IPNet
}
k1 := mustNew(t, &cli1)

func TestNetipPrefix(t *testing.T) {
var cli struct {
Flag netip.Prefix
}
k := mustNew(t, &cli)
for _, tt := range tests {
_, err := k1.Parse([]string{"--flag", tt.input})
assert.NoError(t, err)
assert.Equal(t, tt.expectedNet, cli1.Flag.String())
}
})

_, err := k.Parse([]string{"--flag", "127.0.0.0/24"})
assert.NoError(t, err)
assert.Equal(t, "127.0.0.0/24", cli.Flag.String())
assert.Equal(t, 24, cli.Flag.Bits())
t.Run("TestNetipPackage", func(t *testing.T) {
var cli1 struct {
Flag netip.Prefix
}
k1 := mustNew(t, &cli1)

_, err = k.Parse([]string{"--flag", "2001:db8:abcd:0012::0/64"})
assert.NoError(t, err)
assert.Equal(t, "2001:db8:abcd:12::/64", cli.Flag.String())
for _, tt := range tests {
_, err := k1.Parse([]string{"--flag", tt.input})
assert.NoError(t, err)
assert.Equal(t, tt.expectedNetip, cli1.Flag.String())
}
})
}

func TestNetipPrefixSlice(t *testing.T) {
var cli struct {
Test []*net.IPNet `kong:"sep=','"`
}

k := mustNew(t, &cli)
_, err := k.Parse([]string{
"--test", "127.0.0.0/24",
"--test", "123.0.0.0/23",
"--test", "2001:db8:abcd:0012::0/64",
func TestCIDRSlice(t *testing.T) {
tests := []struct {
input []string
expectedNet []string
expectedNetip []string
}{
{
[]string{"127.0.0.1/16", "192.168.1.0/20"},
[]string{"127.0.0.0/16", "192.168.0.0/20"},
[]string{"127.0.0.1/16", "192.168.1.0/20"},
},
{
[]string{"2001:db8:abcd:0012::0/64", "2001:db8:abcd:0012::0/128"},
[]string{"2001:db8:abcd:12::/64", "2001:db8:abcd:12::/128"},
[]string{"2001:db8:abcd:12::/64", "2001:db8:abcd:12::/128"},
},
}

t.Run("TestNetPackage", func(t *testing.T) {
var cli struct {
Flag []*net.IPNet
}
k := mustNew(t, &cli)

for _, tt := range tests {
_, err := k.Parse([]string{"--flag", strings.Join(tt.input, ",")})
assert.NoError(t, err)
assert.Equal(t, len(tt.input), len(cli.Flag))
for i := 0; i < len(cli.Flag); i++ {
assert.Equal(t, tt.expectedNet[i], cli.Flag[i].String())
}
}
})
assert.NoError(t, err)

assert.Equal(t, 3, len(cli.Test))
assert.Equal(t, "127.0.0.0/24", cli.Test[0].String())
assert.Equal(t, "123.0.0.0/23", cli.Test[1].String())
assert.Equal(t, "2001:db8:abcd:12::/64", cli.Test[2].String())
t.Run("TestNetipPackage", func(t *testing.T) {
var cli struct {
Flag []netip.Prefix
}
k := mustNew(t, &cli)

for _, tt := range tests {
_, err := k.Parse([]string{"--flag", strings.Join(tt.input, ",")})
assert.NoError(t, err)
assert.Equal(t, len(tt.input), len(cli.Flag))
for i := 0; i < len(cli.Flag); i++ {
assert.Equal(t, tt.expectedNetip[i], cli.Flag[i].String())
}
}
})
}

func TestRegex(t *testing.T) {
Expand Down Expand Up @@ -299,7 +327,6 @@ func TestRegexSlice(t *testing.T) {
assert.Equal(t, 2, len(cli.Test))
assert.Equal(t, "foo.+[b-r]{2,4}", cli.Test[0].String())
assert.Equal(t, "foo=bar", cli.Test[1].String())

}

func TestSplitEscaped(t *testing.T) {
Expand Down

0 comments on commit 8727b63

Please sign in to comment.