Skip to content
This repository has been archived by the owner on Apr 24, 2021. It is now read-only.

Commit

Permalink
feat(type): fix issue #28
Browse files Browse the repository at this point in the history
  • Loading branch information
overbool committed Sep 20, 2018
1 parent b8ab489 commit 3687083
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
44 changes: 44 additions & 0 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const (
Bool = reflect.Bool
Int = reflect.Int
Uint = reflect.Uint
Int64 = reflect.Int64
Uint64 = reflect.Uint64
Float = reflect.Float64
String = reflect.String
)
Expand Down Expand Up @@ -95,6 +97,20 @@ var converters = map[reflect.Kind]converter{
}
return uint(val), err
},
Int64: func(v string) (interface{}, error) {
val, err := strconv.ParseInt(v, 0, 64)
if err != nil {
return nil, err
}
return val, err
},
Uint64: func(v string) (interface{}, error) {
val, err := strconv.ParseUint(v, 0, 64)
if err != nil {
return nil, err
}
return val, err
},
Float: func(v string) (interface{}, error) {
return strconv.ParseFloat(v, 64)
},
Expand Down Expand Up @@ -152,6 +168,12 @@ func IntOption(names ...string) Option {
func UintOption(names ...string) Option {
return NewOption(Uint, names...)
}
func Int64Option(names ...string) Option {
return NewOption(Int64, names...)
}
func Uint64Option(names ...string) Option {
return NewOption(Uint64, names...)
}
func FloatOption(names ...string) Option {
return NewOption(Float, names...)
}
Expand Down Expand Up @@ -209,6 +231,28 @@ func (ov *OptionValue) Uint() (value uint, found bool, err error) {
return val, ov.ValueFound, err
}

func (ov *OptionValue) Int64() (value int64, found bool, err error) {
if ov == nil || !ov.ValueFound && ov.Value == nil {
return 0, false, nil
}
val, ok := ov.Value.(int64)
if !ok {
err = fmt.Errorf("expected type %T, got %T", val, ov.Value)
}
return val, ov.ValueFound, err
}

func (ov *OptionValue) Uint64() (value uint64, found bool, err error) {
if ov == nil || !ov.ValueFound && ov.Value == nil {
return 0, false, nil
}
val, ok := ov.Value.(uint64)
if !ok {
err = fmt.Errorf("expected type %T, got %T", val, ov.Value)
}
return val, ov.ValueFound, err
}

func (ov *OptionValue) Float() (value float64, found bool, err error) {
if ov == nil || !ov.ValueFound && ov.Value == nil {
return 0, false, nil
Expand Down
6 changes: 6 additions & 0 deletions option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"reflect"
"strings"
"testing"
"math"
)

func TestOptionValueExtractBoolNotFound(t *testing.T) {
Expand Down Expand Up @@ -77,6 +78,11 @@ func TestParse(t *testing.T) {
{opt: IntOption("int2"), str: "-42", v: -42},
{opt: UintOption("uint1"), str: "23", v: uint(23)},
{opt: UintOption("uint2"), str: "-23", err: `strconv.ParseUint: parsing "-23": invalid syntax`},
{opt: Int64Option("int3"), str: "100001", v: int64(100001)},
{opt: Int64Option("int3"), str: "2147483648", v: int64(math.MaxInt32 + 1)},
{opt: Int64Option("int3"), str: "fly", err: `strconv.ParseInt: parsing "fly": invalid syntax`},
{opt: Uint64Option("uint3"), str: "23", v: uint64(23)},
{opt: Uint64Option("uint3"), str: "-23", err: `strconv.ParseUint: parsing "-23": invalid syntax`},
{opt: BoolOption("true"), str: "true", v: true},
{opt: BoolOption("true"), str: "", v: true},
{opt: BoolOption("false"), str: "false", v: false},
Expand Down

0 comments on commit 3687083

Please sign in to comment.