Added support for time.Duration
diff --git a/properties.go b/properties.go index 195dd77..8610622 100644 --- a/properties.go +++ b/properties.go
@@ -13,6 +13,7 @@ "os" "strconv" "strings" + "time" "unicode/utf8" ) @@ -97,6 +98,29 @@ // ---------------------------------------------------------------------------- +// GetDuration parses the expanded value as an time.Duration if the key exists. +// If key does not exist or the value cannot be parsed the default +// value is returned. +func (p *Properties) GetDuration(key string, def time.Duration) time.Duration { + v, err := p.getInt64(key) + if err != nil { + return def + } + return time.Duration(v) +} + +// MustGetDuration parses the expanded value as an time.Duration if the key exists. +// If key does not exist or the value cannot be parsed the function panics. +func (p *Properties) MustGetDuration(key string) time.Duration { + v, err := p.getInt64(key) + if err != nil { + panic(err) + } + return time.Duration(v) +} + +// ---------------------------------------------------------------------------- + // GetFloat64 parses the expanded value as a float64 if the key exists. // If key does not exist or the value cannot be parsed the default // value is returned.
diff --git a/properties_test.go b/properties_test.go index 6eaabb5..2563f63 100644 --- a/properties_test.go +++ b/properties_test.go
@@ -12,6 +12,7 @@ "os" "strings" "testing" + "time" . "launchpad.net/gocheck" ) @@ -160,6 +161,29 @@ // ---------------------------------------------------------------------------- +type durationTest struct { + input, key string + def, value time.Duration +} + +var durationTests = []*durationTest{ + // valid values + &durationTest{"key = 1", "key", 999, 1}, + &durationTest{"key = 0", "key", 999, 0}, + &durationTest{"key = -1", "key", 999, -1}, + &durationTest{"key = 0123", "key", 999, 123}, + + // invalid values + &durationTest{"key = 0xff", "key", 999, 999}, + &durationTest{"key = 1.0", "key", 999, 999}, + &durationTest{"key = a", "key", 999, 999}, + + // non existent key + &durationTest{"key = 1", "key2", 999, 999}, +} + +// ---------------------------------------------------------------------------- + type floatTest struct { input, key string def, value float64 @@ -292,6 +316,24 @@ c.Assert(func() { p.MustGetBool("invalid") }, PanicMatches, "invalid key: invalid") } +func (l *TestSuite) TestGetDuration(c *C) { + for _, test := range durationTests { + p, err := parse(test.input) + c.Assert(err, IsNil) + c.Assert(p.Len(), Equals, 1) + c.Assert(p.GetDuration(test.key, test.def), Equals, test.value) + } +} + +func (l *TestSuite) TestMustGetDuration(c *C) { + input := "key = 123\nkey2 = ghi" + p, err := parse(input) + c.Assert(err, IsNil) + c.Assert(p.MustGetDuration("key"), Equals, time.Duration(123)) + c.Assert(func() { p.MustGetDuration("key2") }, PanicMatches, "strconv.ParseInt: parsing.*") + c.Assert(func() { p.MustGetDuration("invalid") }, PanicMatches, "invalid key: invalid") +} + func (l *TestSuite) TestGetFloat64(c *C) { for _, test := range floatTests { p, err := parse(test.input) @@ -425,7 +467,7 @@ func (l *TestSuite) TestPanicOn32BitIntOverflow(c *C) { is32Bit = true - var min, max int64 = math.MinInt32-1, math.MaxInt32+1 + var min, max int64 = math.MinInt32 - 1, math.MaxInt32 + 1 input := fmt.Sprintf("min=%d\nmax=%d", min, max) p, err := parse(input) c.Assert(err, IsNil) @@ -437,7 +479,7 @@ func (l *TestSuite) TestPanicOn32BitUintOverflow(c *C) { is32Bit = true - var max uint64 = math.MaxUint32+1 + var max uint64 = math.MaxUint32 + 1 input := fmt.Sprintf("max=%d", max) p, err := parse(input) c.Assert(err, IsNil)