Added GetParsedDuration()/MustGetParsedDuration()
diff --git a/README.md b/README.md index 462004f..30fdaea 100644 --- a/README.md +++ b/README.md
@@ -53,6 +53,11 @@ History ------- +v1.5.1, 08 Dec 2014 +------------------- + * Added GetParsedDuration() and MustGetParsedDuration() for values specified compatible with + [time.ParseDuration()](http://golang.org/pkg/time/#ParseDuration). + v1.5.0, 18 Nov 2014 ------------------- * Added support for single and multi-line comments (reading, writing and updating)
diff --git a/properties.go b/properties.go index b6e0d59..03abec2 100644 --- a/properties.go +++ b/properties.go
@@ -180,9 +180,9 @@ // ---------------------------------------------------------------------------- -// 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. +// GetDuration parses the expanded value as an time.Duration (in ns) if the +// key exists. If key does not exist or the value cannot be parsed the default +// value is returned. In almost all cases you want to use GetParsedDuration(). func (p *Properties) GetDuration(key string, def time.Duration) time.Duration { v, err := p.getInt64(key) if err != nil { @@ -191,8 +191,9 @@ 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. +// MustGetDuration parses the expanded value as an time.Duration (in ns) if +// the key exists. If key does not exist or the value cannot be parsed the +// function panics. In almost all cases you want to use MustGetParsedDuration(). func (p *Properties) MustGetDuration(key string) time.Duration { v, err := p.getInt64(key) if err != nil { @@ -203,6 +204,37 @@ // ---------------------------------------------------------------------------- +// GetParsedDuration parses the expanded value with time.ParseDuration() if the key exists. +// If key does not exist or the value cannot be parsed the default +// value is returned. +func (p *Properties) GetParsedDuration(key string, def time.Duration) time.Duration { + s, ok := p.Get(key) + if !ok { + return def + } + v, err := time.ParseDuration(s) + if err != nil { + return def + } + return v +} + +// MustGetParsedDuration parses the expanded value with time.ParseDuration() if the key exists. +// If key does not exist or the value cannot be parsed the function panics. +func (p *Properties) MustGetParsedDuration(key string) time.Duration { + s, ok := p.Get(key) + if !ok { + ErrorHandler(invalidKeyError(key)) + } + v, err := time.ParseDuration(s) + if err != nil { + ErrorHandler(err) + } + return 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 fee9de8..6f1c5c1 100644 --- a/properties_test.go +++ b/properties_test.go
@@ -242,6 +242,30 @@ // ---------------------------------------------------------------------------- +var parsedDurationTests = []struct { + input, key string + def, value time.Duration +}{ + // valid values + {"key = -1ns", "key", 999, -1*time.Nanosecond}, + {"key = 300ms", "key", 999, 300*time.Millisecond}, + {"key = 5s", "key", 999, 5*time.Second}, + {"key = 3h", "key", 999, 3*time.Hour}, + {"key = 2h45m", "key", 999, 2*time.Hour+45*time.Minute}, + + // invalid values + {"key = 0xff", "key", 999, 999}, + {"key = 1.0", "key", 999, 999}, + {"key = a", "key", 999, 999}, + {"key = 1", "key", 999, 999}, + {"key = 0", "key", 999, 0}, + + // non existent key + {"key = 1", "key2", 999, 999}, +} + +// ---------------------------------------------------------------------------- + var floatTests = []struct { input, key string def, value float64 @@ -458,6 +482,24 @@ c.Assert(func() { p.MustGetDuration("invalid") }, PanicMatches, "unknown property: invalid") } +func (s *TestSuite) TestGetParsedDuration(c *C) { + for _, test := range parsedDurationTests { + p, err := parse(test.input) + c.Assert(err, IsNil) + c.Assert(p.Len(), Equals, 1) + c.Assert(p.GetParsedDuration(test.key, test.def), Equals, test.value) + } +} + +func (s *TestSuite) TestMustGetParsedDuration(c *C) { + input := "key = 123ms\nkey2 = ghi" + p, err := parse(input) + c.Assert(err, IsNil) + c.Assert(p.MustGetParsedDuration("key"), Equals, 123*time.Millisecond) + c.Assert(func() { p.MustGetParsedDuration("key2") }, PanicMatches, "time: invalid duration ghi") + c.Assert(func() { p.MustGetParsedDuration("invalid") }, PanicMatches, "unknown property: invalid") +} + func (s *TestSuite) TestGetFloat64(c *C) { for _, test := range floatTests { p, err := parse(test.input)