Add support for Uint types

This commit is contained in:
Brian Shore
2015-11-04 23:52:40 +00:00
parent aea5e9fd2e
commit 18cc772ced
4 changed files with 85 additions and 3 deletions
+52
View File
@@ -197,6 +197,17 @@ func (k *Key) Int64() (int64, error) {
return strconv.ParseInt(k.String(), 10, 64)
}
// Uint returns uint type valued.
func (k *Key) Uint() (uint, error) {
u, e := strconv.ParseUint(k.String(), 10, 64)
return uint(u), e
}
// Uint64 returns uint64 type value.
func (k *Key) Uint64() (uint64, error) {
return strconv.ParseUint(k.String(), 10, 64)
}
// Duration returns time.Duration type value.
func (k *Key) Duration() (time.Duration, error) {
return time.ParseDuration(k.String())
@@ -261,6 +272,26 @@ func (k *Key) MustInt64(defaultVal ...int64) int64 {
return val
}
// MustUint always returns value without error,
// it returns 0 if error occurs.
func (k *Key) MustUint(defaultVal ...uint) uint {
val, err := k.Uint()
if len(defaultVal) > 0 && err != nil {
return defaultVal[0]
}
return val
}
// MustUint64 always returns value without error,
// it returns 0 if error occurs.
func (k *Key) MustUint64(defaultVal ...uint64) uint64 {
val, err := k.Uint64()
if len(defaultVal) > 0 && err != nil {
return defaultVal[0]
}
return val
}
// MustDuration always returns value without error,
// it returns zero value if error occurs.
func (k *Key) MustDuration(defaultVal ...time.Duration) time.Duration {
@@ -443,6 +474,27 @@ func (k *Key) Int64s(delim string) []int64 {
return vals
}
// Uints returns list of uint devide by given delimiter.
func (k *Key) Uints(delim string) []uint {
strs := k.Strings(delim)
vals := make([]uint, len(strs))
for i := range strs {
u, _ := strconv.ParseUint(strs[i], 10, 64)
vals[i] = uint(u)
}
return vals
}
// Uint64s returns list of uint64 devide by given delimiter.
func (k *Key) Uint64s(delim string) []uint64 {
strs := k.Strings(delim)
vals := make([]uint64, len(strs))
for i := range strs {
vals[i], _ = strconv.ParseUint(strs[i], 10, 64)
}
return vals
}
// TimesFormat parses with given format and returns list of time.Time devide by given delimiter.
func (k *Key) TimesFormat(format, delim string) []time.Time {
strs := k.Strings(delim)
+15 -3
View File
@@ -67,11 +67,13 @@ FLOAT64 = 1.25
INT = 10
TIME = 2015-01-01T20:17:05Z
DURATION = 2h45m
UINT = 3
[array]
STRINGS = en, zh, de
FLOAT64S = 1.1, 2.2, 3.3
INTS = 1, 2, 3
UINTS = 1, 2, 3
TIMES = 2015-01-01T20:17:05Z,2015-01-01T20:17:05Z,2015-01-01T20:17:05Z
[note]
@@ -336,11 +338,21 @@ func Test_Values(t *testing.T) {
So(vals3[i], ShouldEqual, v)
}
vals4 := sec.Key("UINTS").Uints(",")
for i, v := range []uint{1, 2, 3} {
So(vals4[i], ShouldEqual, v)
}
vals5 := sec.Key("UINTS").Uint64s(",")
for i, v := range []uint64{1, 2, 3} {
So(vals5[i], ShouldEqual, v)
}
t, err := time.Parse(time.RFC3339, "2015-01-01T20:17:05Z")
So(err, ShouldBeNil)
vals4 := sec.Key("TIMES").Times(",")
vals6 := sec.Key("TIMES").Times(",")
for i, v := range []time.Time{t, t, t} {
So(vals4[i].String(), ShouldEqual, v.String())
So(vals6[i].String(), ShouldEqual, v.String())
}
})
@@ -355,7 +367,7 @@ func Test_Values(t *testing.T) {
})
Convey("Get key strings", func() {
So(strings.Join(cfg.Section("types").KeyStrings(), ","), ShouldEqual, "STRING,BOOL,BOOL_FALSE,FLOAT64,INT,TIME,DURATION")
So(strings.Join(cfg.Section("types").KeyStrings(), ","), ShouldEqual, "STRING,BOOL,BOOL_FALSE,FLOAT64,INT,TIME,DURATION,UINT")
})
Convey("Delete a key", func() {
+15
View File
@@ -104,6 +104,20 @@ func setWithProperType(t reflect.Type, key *Key, field reflect.Value, delim stri
return nil
}
field.SetInt(intVal)
// byte is an alias for uint8, so supporting uint8 breaks support for byte
case reflect.Uint, reflect.Uint16, reflect.Uint32, reflect.Uint64:
durationVal, err := key.Duration()
if err == nil {
field.Set(reflect.ValueOf(durationVal))
return nil
}
uintVal, err := key.Uint64()
if err != nil {
return nil
}
field.SetUint(uintVal)
case reflect.Float64:
floatVal, err := key.Float64()
if err != nil {
@@ -231,6 +245,7 @@ func reflectWithProperType(t reflect.Type, key *Key, field reflect.Value, delim
key.SetValue(field.String())
case reflect.Bool,
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.Float64,
reflectTime:
key.SetValue(fmt.Sprint(field))
+3
View File
@@ -43,6 +43,7 @@ type testStruct struct {
Others testNested
*testEmbeded `ini:"grade"`
Unused int `ini:"-"`
Unsigned uint
}
const _CONF_DATA_STRUCT = `
@@ -52,6 +53,7 @@ Male = true
Money = 1.25
Born = 1993-10-07T20:17:05Z
Duration = 2h45m
Unsigned = 3
[Others]
Cities = HangZhou|Boston
@@ -116,6 +118,7 @@ func Test_Struct(t *testing.T) {
So(ts.Age, ShouldEqual, 21)
So(ts.Male, ShouldBeTrue)
So(ts.Money, ShouldEqual, 1.25)
So(ts.Unsigned, ShouldEqual, 3)
t, err := time.Parse(time.RFC3339, "1993-10-07T20:17:05Z")
So(err, ShouldBeNil)