fixup panic #33

Merged
vtolstov merged 2 commits from fixup into v3 2022-03-21 20:06:14 +03:00
3 changed files with 27 additions and 20 deletions
Showing only changes of commit 3064141583 - Show all commits

View File

@ -49,7 +49,7 @@ func (c *flagConfig) Init(opts ...config.Option) error {
if !ok { if !ok {
continue continue
} }
fn, fv, fd := getFlagOpts(tf) fn, fd, fv := getFlagOpts(tf)
rcheck := true rcheck := true

View File

@ -18,12 +18,15 @@ func TestLoad(t *testing.T) {
os.Args = append(os.Args, "-time", time.RFC822) os.Args = append(os.Args, "-time", time.RFC822)
os.Args = append(os.Args, "-metadata", "key=20") os.Args = append(os.Args, "-metadata", "key=20")
type Config struct { type Config struct {
Broker string `flag:"name=broker,desc='description with, comma',default='127.0.0.1:9092'"` Broker string `flag:"name=broker,desc='description with, comma',default='127.0.0.1:9092'"`
Verbose bool `flag:"name=verbose,desc='verbose output',default='false'"` Verbose bool `flag:"name=verbose,desc='verbose output',default='false'"`
Addr []string `flag:"name=addr,desc='addrs',default='127.0.0.1:9092'"` Addr []string `flag:"name=addr,desc='addrs',default='127.0.0.1:9092'"`
Wait time.Duration `flag:"name=wait,desc='wait time',default='2s'"` Wait time.Duration `flag:"name=wait,desc='wait time',default='2s'"`
Time time.Time `flag:"name=time,desc='some time',default='02 Jan 06 15:04 MST'"` Time time.Time `flag:"name=time,desc='some time',default='02 Jan 06 15:04 MST'"`
Metadata map[string]int `flag:"name=metadata,desc='some meta',default=''"` Metadata map[string]int `flag:"name=metadata,desc='some meta',default=''"`
WithoutDefault string `flag:"name=without_default,desc='with'"`
WithoutDesc string `flag:"name=without_desc,default='without_default'"`
WithoutAll string `flag:"name=without_all"`
} }
ctx := context.Background() ctx := context.Background()

30
util.go
View File

@ -105,7 +105,7 @@ func (c *flagConfig) flagMap(v reflect.Value, fn, fv, fd string) error {
delim = d delim = d
} }
} }
flag.Func(fn, fd, func(s string) error { flag.Func(fn, fv, func(s string) error {
ps := strings.Split(s, delim) ps := strings.Split(s, delim)
if len(ps) == 0 { if len(ps) == 0 {
return nil return nil
@ -172,7 +172,7 @@ func (c *flagConfig) flagDuration(v reflect.Value, fn, fv, fd string) error {
if !ok { if !ok {
return ErrInvalidValue return ErrInvalidValue
} }
i, err := time.ParseDuration(fd) i, err := time.ParseDuration(fv)
if err != nil { if err != nil {
return err return err
} }
@ -185,7 +185,7 @@ func (c *flagConfig) flagBool(v reflect.Value, fn, fv, fd string) error {
if !ok { if !ok {
return ErrInvalidValue return ErrInvalidValue
} }
i, err := strconv.ParseBool(fd) i, err := strconv.ParseBool(fv)
if err != nil { if err != nil {
return err return err
} }
@ -207,11 +207,11 @@ func (c *flagConfig) flagInt(v reflect.Value, fn, fv, fd string) error {
if !ok { if !ok {
return ErrInvalidValue return ErrInvalidValue
} }
i, err := strconv.ParseInt(fd, 10, 64) i, err := strconv.ParseInt(fv, 10, 64)
if err != nil { if err != nil {
return err return err
} }
flag.IntVar(nv, fn, int(i), fd) flag.IntVar(nv, fn, int(i), fv)
return nil return nil
} }
@ -220,11 +220,11 @@ func (c *flagConfig) flagInt64(v reflect.Value, fn, fv, fd string) error {
if !ok { if !ok {
return ErrInvalidValue return ErrInvalidValue
} }
i, err := strconv.ParseInt(fd, 10, 64) i, err := strconv.ParseInt(fv, 10, 64)
if err != nil { if err != nil {
return err return err
} }
flag.Int64Var(nv, fn, int64(i), fd) flag.Int64Var(nv, fn, int64(i), fv)
return nil return nil
} }
@ -233,11 +233,11 @@ func (c *flagConfig) flagUint(v reflect.Value, fn, fv, fd string) error {
if !ok { if !ok {
return ErrInvalidValue return ErrInvalidValue
} }
i, err := strconv.ParseUint(fd, 10, 64) i, err := strconv.ParseUint(fv, 10, 64)
if err != nil { if err != nil {
return err return err
} }
flag.UintVar(nv, fn, uint(i), fd) flag.UintVar(nv, fn, uint(i), fv)
return nil return nil
} }
@ -246,11 +246,11 @@ func (c *flagConfig) flagUint64(v reflect.Value, fn, fv, fd string) error {
if !ok { if !ok {
return ErrInvalidValue return ErrInvalidValue
} }
i, err := strconv.ParseUint(fd, 10, 64) i, err := strconv.ParseUint(fv, 10, 64)
if err != nil { if err != nil {
return err return err
} }
flag.Uint64Var(nv, fn, uint64(i), fd) flag.Uint64Var(nv, fn, uint64(i), fv)
return nil return nil
} }
@ -259,11 +259,11 @@ func (c *flagConfig) flagFloat64(v reflect.Value, fn, fv, fd string) error {
if !ok { if !ok {
return ErrInvalidValue return ErrInvalidValue
} }
i, err := strconv.ParseFloat(fd, 64) i, err := strconv.ParseFloat(fv, 64)
if err != nil { if err != nil {
return err return err
} }
flag.Float64Var(nv, fn, float64(i), fd) flag.Float64Var(nv, fn, float64(i), fv)
return nil return nil
} }
@ -295,7 +295,11 @@ func getFlagOpts(tf string) (string, string, string) {
} }
ret[f] = p[1] ret[f] = p[1]
} }
for idx := range ret { for idx := range ret {
if len(ret[idx]) == 0 {
continue
}
if ret[idx][0] == '\'' { if ret[idx][0] == '\'' {
ret[idx] = ret[idx][1:] ret[idx] = ret[idx][1:]
} }