Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions gen.values_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,3 +434,36 @@ func (v *ListenPort) Get() interface{} {
func (v *ListenPort) Type() string {
return "ListenPort"
}


var _ Value = &IPNet{}

// String implements flag.Value interface.
func (v *IPNet) String() string {
if v == nil || v.value == nil {
return ""
}
return fmt.Sprint(*v.value)
}

// Set implements flag.Value interface.
func (v *IPNet) Set(s string) error {
err := v.set(s)
if err != nil {
v.value = nil
}
return err
}

// Get implements flag.Getter interface.
func (v *IPNet) Get() interface{} {
if v.value == nil {
return nil
}
return *v.value
}

// Type implements pflag.Value interface.
func (v *IPNet) Type() string {
return "IPNet"
}
11 changes: 11 additions & 0 deletions gen.values_must.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,14 @@ func MustListenPort(s string) ListenPort {
}
return v
}


// MustIPNet returns IPNet initialized with given value or panics.
func MustIPNet(s string) IPNet {
var v IPNet
err := v.Set(s)
if err != nil {
panic(err)
}
return v
}
13 changes: 13 additions & 0 deletions values.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"math"
"net"
"net/url"
"strconv"
"strings"
Expand Down Expand Up @@ -212,3 +213,15 @@ func (v *ListenPort) set(s string) error {
v.value = &i
return nil
}

// IPNet can be set to CIDR address.
type IPNet struct{ value **net.IPNet }

func (v *IPNet) set(s string) error {
_, ipNet, err := net.ParseCIDR(s)
if err != nil {
return err
}
v.value = &ipNet
return nil
}
15 changes: 14 additions & 1 deletion values_custom.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package appcfg

import "time"
import (
"net"
"time"
)

// Value is like Get except it returns zero value and set *err to
// RequiredError if unset.
Expand Down Expand Up @@ -91,3 +94,13 @@ func MustIntBetween(s string, min, max int) IntBetween {
}
return v
}

// Value is like Get except it returns zero value and set *err to
// RequiredError if unset.
func (v *IPNet) Value(err *error) (val *net.IPNet) { //nolint:gocritic // ptrToRefParam.
if v.value == nil {
*err = &RequiredError{v}
return val
}
return *v.value
}
2 changes: 1 addition & 1 deletion values_impl.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:generate gobin -m -run github.com/cheekybits/genny -in=$GOFILE -out=gen.$GOFILE gen "Duration=Bool,String,NotEmptyString,OneOfString,Endpoint,Int,Int64,Uint,Uint64,Float64,IntBetween,Port,ListenPort"
//go:generate gobin -m -run github.com/cheekybits/genny -in=$GOFILE -out=gen.$GOFILE gen "Duration=Bool,String,NotEmptyString,OneOfString,Endpoint,Int,Int64,Uint,Uint64,Float64,IntBetween,Port,ListenPort,IPNet"
//go:generate sed -i -e "\\,^//go:generate,d" gen.$GOFILE

package appcfg
Expand Down
2 changes: 1 addition & 1 deletion values_must.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:generate gobin -m -run github.com/cheekybits/genny -in=$GOFILE -out=gen.$GOFILE gen "Duration=Bool,String,NotEmptyString,Endpoint,Int,Int64,Uint,Uint64,Float64,Port,ListenPort"
//go:generate gobin -m -run github.com/cheekybits/genny -in=$GOFILE -out=gen.$GOFILE gen "Duration=Bool,String,NotEmptyString,Endpoint,Int,Int64,Uint,Uint64,Float64,Port,ListenPort,IPNet"
//go:generate sed -i -e "\\,^//go:generate,d" gen.$GOFILE

package appcfg
Expand Down
32 changes: 32 additions & 0 deletions values_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,35 @@ func TestUint(tt *testing.T) {
t.PanicMatch(func() { v = appcfg.MustUint("") }, "invalid")
t.PanicMatch(func() { v = appcfg.MustUint("-1") }, "invalid")
}

func TestIPNet(tt *testing.T) {
t := check.T(tt)
t.Parallel()

var v appcfg.IPNet
t.Equal(v.Type(), "IPNet")

t.Equal(v.String(), "")
t.Nil(v.Get())
var err error
t.Zero(v.Value(&err))
t.Match(err, "required")

t.Nil(v.Set("0.0.0.0/0"))

t.Equal(v.String(), "0.0.0.0/0")
t.NotNil(v.Get())
err = nil
t.NotZero(v.Value(&err))
t.Nil(err)

v = appcfg.MustIPNet("192.168.2.1/24")

t.Equal(v.String(), "192.168.2.0/24")
t.NotNil(v.Get())
err = nil
t.Equal(v.Value(&err).String(), "192.168.2.0/24")
t.Nil(err)

t.PanicMatch(func() { v = appcfg.MustIPNet("") }, "invalid")
}