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

Commit e48dff0

Browse files
committed
feat(type): fix issue #28
1 parent b8ab489 commit e48dff0

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

option.go

+44
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ const (
1313
Bool = reflect.Bool
1414
Int = reflect.Int
1515
Uint = reflect.Uint
16+
Int64 = reflect.Int64
17+
Uint64 = reflect.Uint64
1618
Float = reflect.Float64
1719
String = reflect.String
1820
)
@@ -95,6 +97,20 @@ var converters = map[reflect.Kind]converter{
9597
}
9698
return uint(val), err
9799
},
100+
Int64: func(v string) (interface{}, error) {
101+
val, err := strconv.ParseInt(v, 0, 64)
102+
if err != nil {
103+
return nil, err
104+
}
105+
return val, err
106+
},
107+
Uint64: func(v string) (interface{}, error) {
108+
val, err := strconv.ParseUint(v, 0, 64)
109+
if err != nil {
110+
return nil, err
111+
}
112+
return val, err
113+
},
98114
Float: func(v string) (interface{}, error) {
99115
return strconv.ParseFloat(v, 64)
100116
},
@@ -152,6 +168,12 @@ func IntOption(names ...string) Option {
152168
func UintOption(names ...string) Option {
153169
return NewOption(Uint, names...)
154170
}
171+
func Int64Option(names ...string) Option {
172+
return NewOption(Int64, names...)
173+
}
174+
func Uint64Option(names ...string) Option {
175+
return NewOption(Uint64, names...)
176+
}
155177
func FloatOption(names ...string) Option {
156178
return NewOption(Float, names...)
157179
}
@@ -209,6 +231,28 @@ func (ov *OptionValue) Uint() (value uint, found bool, err error) {
209231
return val, ov.ValueFound, err
210232
}
211233

234+
func (ov *OptionValue) Int64() (value int64, found bool, err error) {
235+
if ov == nil || !ov.ValueFound && ov.Value == nil {
236+
return 0, false, nil
237+
}
238+
val, ok := ov.Value.(int64)
239+
if !ok {
240+
err = fmt.Errorf("expected type %T, got %T", val, ov.Value)
241+
}
242+
return val, ov.ValueFound, err
243+
}
244+
245+
func (ov *OptionValue) Uint64() (value uint64, found bool, err error) {
246+
if ov == nil || !ov.ValueFound && ov.Value == nil {
247+
return 0, false, nil
248+
}
249+
val, ok := ov.Value.(uint64)
250+
if !ok {
251+
err = fmt.Errorf("expected type %T, got %T", val, ov.Value)
252+
}
253+
return val, ov.ValueFound, err
254+
}
255+
212256
func (ov *OptionValue) Float() (value float64, found bool, err error) {
213257
if ov == nil || !ov.ValueFound && ov.Value == nil {
214258
return 0, false, nil

option_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"reflect"
55
"strings"
66
"testing"
7+
"math"
78
)
89

910
func TestOptionValueExtractBoolNotFound(t *testing.T) {
@@ -77,6 +78,11 @@ func TestParse(t *testing.T) {
7778
{opt: IntOption("int2"), str: "-42", v: -42},
7879
{opt: UintOption("uint1"), str: "23", v: uint(23)},
7980
{opt: UintOption("uint2"), str: "-23", err: `strconv.ParseUint: parsing "-23": invalid syntax`},
81+
{opt: Int64Option("int3"), str: "100001", v: int64(100001)},
82+
{opt: Int64Option("int3"), str: "2147483648", v: int64(math.MaxInt32+1)},
83+
{opt: Int64Option("int3"), str: "fly", err: `strconv.ParseInt: parsing "fly": invalid syntax`},
84+
{opt: Uint64Option("uint3"), str: "23", v: uint64(23)},
85+
{opt: Uint64Option("uint3"), str: "-23", err: `strconv.ParseUint: parsing "-23": invalid syntax`},
8086
{opt: BoolOption("true"), str: "true", v: true},
8187
{opt: BoolOption("true"), str: "", v: true},
8288
{opt: BoolOption("false"), str: "false", v: false},

0 commit comments

Comments
 (0)