Skip to content

Commit

Permalink
key: support parsing hex number (go-ini#178)
Browse files Browse the repository at this point in the history
* parse hex number on map to struct

* pasre hex number on slice int

* add test case
fix: Key().Int() use ParseInt instead of Atoi

* testcase move to L183

* fix names
  • Loading branch information
tendyang authored and unknwon committed Jan 3, 2019
1 parent c498c36 commit f841668
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
18 changes: 10 additions & 8 deletions key.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,23 +186,24 @@ func (k *Key) Float64() (float64, error) {

// Int returns int type value.
func (k *Key) Int() (int, error) {
return strconv.Atoi(k.String())
v, err := strconv.ParseInt(k.String(), 0, 64)
return int(v), err
}

// Int64 returns int64 type value.
func (k *Key) Int64() (int64, error) {
return strconv.ParseInt(k.String(), 10, 64)
return strconv.ParseInt(k.String(), 0, 64)
}

// Uint returns uint type valued.
func (k *Key) Uint() (uint, error) {
u, e := strconv.ParseUint(k.String(), 10, 64)
u, e := strconv.ParseUint(k.String(), 0, 64)
return uint(u), e
}

// Uint64 returns uint64 type value.
func (k *Key) Uint64() (uint64, error) {
return strconv.ParseUint(k.String(), 10, 64)
return strconv.ParseUint(k.String(), 0, 64)
}

// Duration returns time.Duration type value.
Expand Down Expand Up @@ -667,7 +668,8 @@ func (k *Key) parseFloat64s(strs []string, addInvalid, returnOnInvalid bool) ([]
func (k *Key) parseInts(strs []string, addInvalid, returnOnInvalid bool) ([]int, error) {
vals := make([]int, 0, len(strs))
for _, str := range strs {
val, err := strconv.Atoi(str)
valInt64, err := strconv.ParseInt(str, 0, 64)
val := int(valInt64)
if err != nil && returnOnInvalid {
return nil, err
}
Expand All @@ -682,7 +684,7 @@ func (k *Key) parseInts(strs []string, addInvalid, returnOnInvalid bool) ([]int,
func (k *Key) parseInt64s(strs []string, addInvalid, returnOnInvalid bool) ([]int64, error) {
vals := make([]int64, 0, len(strs))
for _, str := range strs {
val, err := strconv.ParseInt(str, 10, 64)
val, err := strconv.ParseInt(str, 0, 64)
if err != nil && returnOnInvalid {
return nil, err
}
Expand All @@ -697,7 +699,7 @@ func (k *Key) parseInt64s(strs []string, addInvalid, returnOnInvalid bool) ([]in
func (k *Key) parseUints(strs []string, addInvalid, returnOnInvalid bool) ([]uint, error) {
vals := make([]uint, 0, len(strs))
for _, str := range strs {
val, err := strconv.ParseUint(str, 10, 0)
val, err := strconv.ParseUint(str, 0, 0)
if err != nil && returnOnInvalid {
return nil, err
}
Expand All @@ -712,7 +714,7 @@ func (k *Key) parseUints(strs []string, addInvalid, returnOnInvalid bool) ([]uin
func (k *Key) parseUint64s(strs []string, addInvalid, returnOnInvalid bool) ([]uint64, error) {
vals := make([]uint64, 0, len(strs))
for _, str := range strs {
val, err := strconv.ParseUint(str, 10, 64)
val, err := strconv.ParseUint(str, 0, 64)
if err != nil && returnOnInvalid {
return nil, err
}
Expand Down
7 changes: 7 additions & 0 deletions key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,10 @@ func TestKey_Helpers(t *testing.T) {
So(err, ShouldBeNil)
So(v7.String(), ShouldEqual, t.String())

v8, err := sec.Key("HEX_NUMBER").Int()
So(err, ShouldBeNil)
So(v8, ShouldEqual, 0x3000)

Convey("Must get values with type", func() {
So(sec.Key("STRING").MustString("404"), ShouldEqual, "str")
So(sec.Key("BOOL").MustBool(), ShouldBeTrue)
Expand All @@ -225,6 +229,7 @@ func TestKey_Helpers(t *testing.T) {
So(sec.Key("UINT").MustUint(), ShouldEqual, 3)
So(sec.Key("UINT").MustUint64(), ShouldEqual, 3)
So(sec.Key("TIME").MustTime().String(), ShouldEqual, t.String())
So(sec.Key("HEX_NUMBER").MustInt(), ShouldEqual, 0x3000)

dur, err := time.ParseDuration("2h45m")
So(err, ShouldBeNil)
Expand All @@ -238,6 +243,7 @@ func TestKey_Helpers(t *testing.T) {
So(sec.Key("INT64_404").MustInt64(15), ShouldEqual, 15)
So(sec.Key("UINT_404").MustUint(6), ShouldEqual, 6)
So(sec.Key("UINT64_404").MustUint64(6), ShouldEqual, 6)
So(sec.Key("HEX_NUMBER_404").MustInt(0x3001), ShouldEqual, 0x3001)

t, err := time.Parse(time.RFC3339, "2014-01-01T20:17:05Z")
So(err, ShouldBeNil)
Expand All @@ -255,6 +261,7 @@ func TestKey_Helpers(t *testing.T) {
So(sec.Key("UINT64_404").String(), ShouldEqual, "6")
So(sec.Key("TIME_404").String(), ShouldEqual, "2014-01-01T20:17:05Z")
So(sec.Key("DURATION_404").String(), ShouldEqual, "2h45m0s")
So(sec.Key("HEX_NUMBER_404").String(), ShouldEqual, "12289")
})
})
})
Expand Down
1 change: 1 addition & 0 deletions testdata/TestFile_WriteTo.golden
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ INT = 10
TIME = 2015-01-01T20:17:05Z
DURATION = 2h45m
UINT = 3
HEX_NUMBER = 0x3000

[array]
STRINGS = en, zh, de
Expand Down
1 change: 1 addition & 0 deletions testdata/full.ini
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ INT = 10
TIME = 2015-01-01T20:17:05Z
DURATION = 2h45m
UINT = 3
HEX_NUMBER = 0x3000

[array]
STRINGS = en, zh, de
Expand Down

0 comments on commit f841668

Please sign in to comment.