Added MustGet methods for all types
diff --git a/properties.go b/properties.go
index 289e6a4..7382520 100644
--- a/properties.go
+++ b/properties.go
@@ -34,7 +34,8 @@
}
}
-// Get returns the expanded value for the given key if exists. Otherwise, ok is false.
+// Get returns the expanded value for the given key if exists.
+// Otherwise, ok is false.
func (p *Properties) Get(key string) (value string, ok bool) {
v, ok := p.m[key]
if !ok {
@@ -53,60 +54,153 @@
return expanded, true
}
+// MustGet returns the expanded value for the given key if exists.
+// Otherwise, it panics.
+func (p *Properties) MustGet(key string) string {
+ if v, ok := p.Get(key); ok {
+ return v
+ }
+ panic(invalidKeyError(key))
+}
+
+// ----------------------------------------------------------------------------
+
// GetBool checks if the expanded value is one of '1', 'yes',
// 'true' or 'on' if the key exists. The comparison is case-insensitive.
// If the key does not exist the default value is returned.
func (p *Properties) GetBool(key string, def bool) bool {
+ v, err := p.getBool(key)
+ if err != nil {
+ return def
+ }
+ return v
+}
+
+// MustGetBool checks if the expanded value is one of '1', 'yes',
+// 'true' or 'on' if the key exists. The comparison is case-insensitive.
+// If the key does not exist the function panics.
+func (p *Properties) MustGetBool(key string) bool {
+ v, err := p.getBool(key)
+ if err != nil {
+ panic(err)
+ }
+ return v
+}
+
+func (p *Properties) getBool(key string) (value bool, err error) {
if v, ok := p.Get(key); ok {
v = strings.ToLower(v)
- return v == "1" || v == "true" || v == "yes" || v == "on"
+ return v == "1" || v == "true" || v == "yes" || v == "on", nil
}
- return def
+ return false, invalidKeyError(key)
}
+// ----------------------------------------------------------------------------
+
// 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.
func (p *Properties) GetFloat64(key string, def float64) float64 {
- if v, ok := p.Get(key); ok {
- n, err := strconv.ParseFloat(v, 64)
- if err != nil {
- return def
- }
- return n
+ v, err := p.getFloat64(key)
+ if err != nil {
+ return def
}
- return def
+ return v
}
-// GetInt64 parses the expanded value as an int if the key exists.
+// GetFloat64 parses the expanded value as a float64 if the key exists.
+// If key does not exist or the value cannot be parsed the function panics.
+func (p *Properties) MustGetFloat64(key string) float64 {
+ v, err := p.getFloat64(key)
+ if err != nil {
+ panic(err)
+ }
+ return v
+}
+
+func (p *Properties) getFloat64(key string) (value float64, err error) {
+ if v, ok := p.Get(key); ok {
+ value, err = strconv.ParseFloat(v, 64)
+ if err != nil {
+ return 0, err
+ }
+ return value, nil
+ }
+ return 0, invalidKeyError(key)
+}
+
+// ----------------------------------------------------------------------------
+
+// GetInt64 parses the expanded value as an int64 if the key exists.
// If key does not exist or the value cannot be parsed the default
// value is returned.
func (p *Properties) GetInt64(key string, def int64) int64 {
- if v, ok := p.Get(key); ok {
- n, err := strconv.ParseInt(v, 10, 64)
- if err != nil {
- return def
- }
- return n
+ v, err := p.getInt64(key)
+ if err != nil {
+ return def
}
- return def
+ return v
}
+// MustGetInt64 parses the expanded value as an int if the key exists.
+// If key does not exist or the value cannot be parsed the function panics.
+func (p *Properties) MustGetInt64(key string) int64 {
+ v, err := p.getInt64(key)
+ if err != nil {
+ panic(err)
+ }
+ return v
+}
+
+func (p *Properties) getInt64(key string) (value int64, err error) {
+ if v, ok := p.Get(key); ok {
+ value, err = strconv.ParseInt(v, 10, 64)
+ if err != nil {
+ return 0, err
+ }
+ return value, nil
+ }
+ return 0, invalidKeyError(key)
+}
+
+// ----------------------------------------------------------------------------
+
// GetUint64 parses the expanded value as an uint64 if the key exists.
// If key does not exist or the value cannot be parsed the default
// value is returned.
func (p *Properties) GetUint64(key string, def uint64) uint64 {
- if v, ok := p.Get(key); ok {
- n, err := strconv.ParseUint(v, 10, 64)
- if err != nil {
- return def
- }
- return n
+ v, err := p.getUint64(key)
+ if err != nil {
+ return def
}
- return def
+ return v
}
-// GetString returns the expanded value for the given key if exists or the default value otherwise.
+// MustGetUint64 parses the expanded value as an int if the key exists.
+// If key does not exist or the value cannot be parsed the function panics.
+func (p *Properties) MustGetUint64(key string) uint64 {
+ v, err := p.getUint64(key)
+ if err != nil {
+ panic(err)
+ }
+ return v
+}
+
+func (p *Properties) getUint64(key string) (value uint64, err error) {
+ if v, ok := p.Get(key); ok {
+ value, err = strconv.ParseUint(v, 10, 64)
+ if err != nil {
+ return 0, err
+ }
+ return value, nil
+ }
+ return 0, invalidKeyError(key)
+}
+
+// ----------------------------------------------------------------------------
+
+// GetString returns the expanded value for the given key if exists or
+// the default value otherwise.
func (p *Properties) GetString(key, def string) string {
if v, ok := p.Get(key); ok {
return v
@@ -114,6 +208,17 @@
return def
}
+// MustGetString returns the expanded value for the given key if exists or
+// panics otherwise.
+func (p *Properties) MustGetString(key string) string {
+ if v, ok := p.Get(key); ok {
+ return v
+ }
+ panic(invalidKeyError(key))
+}
+
+// ----------------------------------------------------------------------------
+
// Len returns the number of keys.
func (p *Properties) Len() int {
return len(p.m)
@@ -273,3 +378,7 @@
return string(r)
}
}
+
+func invalidKeyError(key string) error {
+ return fmt.Errorf("invalid key: %s", key)
+}
diff --git a/properties_test.go b/properties_test.go
index 2b2c715..3d2a413 100644
--- a/properties_test.go
+++ b/properties_test.go
@@ -266,6 +266,14 @@
}
}
+func (l *TestSuite) TestMustGet(c *C) {
+ input := "key = value\nkey2 = ghi"
+ p, err := parse(input)
+ c.Assert(err, IsNil)
+ c.Assert(p.MustGet("key"), Equals, "value")
+ c.Assert(func() { p.MustGet("invalid") }, PanicMatches, "invalid key: invalid")
+}
+
func (l *TestSuite) TestGetBool(c *C) {
for _, test := range boolTests {
p, err := parse(test.input)
@@ -275,6 +283,14 @@
}
}
+func (l *TestSuite) TestMustGetBool(c *C) {
+ input := "key = true\nkey2 = ghi"
+ p, err := parse(input)
+ c.Assert(err, IsNil)
+ c.Assert(p.MustGetBool("key"), Equals, true)
+ c.Assert(func() { p.MustGetBool("invalid") }, PanicMatches, "invalid key: invalid")
+}
+
func (l *TestSuite) TestGetFloat64(c *C) {
for _, test := range floatTests {
p, err := parse(test.input)
@@ -284,6 +300,15 @@
}
}
+func (l *TestSuite) TestMustGetFloat64(c *C) {
+ input := "key = 123\nkey2 = ghi"
+ p, err := parse(input)
+ c.Assert(err, IsNil)
+ c.Assert(p.MustGetFloat64("key"), Equals, float64(123))
+ c.Assert(func() { p.MustGetFloat64("key2") }, PanicMatches, "strconv.ParseFloat: parsing.*")
+ c.Assert(func() { p.MustGetFloat64("invalid") }, PanicMatches, "invalid key: invalid")
+}
+
func (l *TestSuite) TestGetInt64(c *C) {
for _, test := range intTests {
p, err := parse(test.input)
@@ -293,6 +318,15 @@
}
}
+func (l *TestSuite) TestMustGetInt64(c *C) {
+ input := "key = 123\nkey2 = ghi"
+ p, err := parse(input)
+ c.Assert(err, IsNil)
+ c.Assert(p.MustGetInt64("key"), Equals, int64(123))
+ c.Assert(func() { p.MustGetInt64("key2") }, PanicMatches, "strconv.ParseInt: parsing.*")
+ c.Assert(func() { p.MustGetInt64("invalid") }, PanicMatches, "invalid key: invalid")
+}
+
func (l *TestSuite) TestGetUint64(c *C) {
for _, test := range uintTests {
p, err := parse(test.input)
@@ -302,6 +336,15 @@
}
}
+func (l *TestSuite) TestMustGetUint64(c *C) {
+ input := "key = 123\nkey2 = ghi"
+ p, err := parse(input)
+ c.Assert(err, IsNil)
+ c.Assert(p.MustGetUint64("key"), Equals, uint64(123))
+ c.Assert(func() { p.MustGetUint64("key2") }, PanicMatches, "strconv.ParseUint: parsing.*")
+ c.Assert(func() { p.MustGetUint64("invalid") }, PanicMatches, "invalid key: invalid")
+}
+
func (l *TestSuite) TestGetString(c *C) {
for _, test := range stringTests {
p, err := parse(test.input)
@@ -311,6 +354,14 @@
}
}
+func (l *TestSuite) TestMustGetString(c *C) {
+ input := `key = value`
+ p, err := parse(input)
+ c.Assert(err, IsNil)
+ c.Assert(p.MustGetString("key"), Equals, "value")
+ c.Assert(func() { p.MustGetString("invalid") }, PanicMatches, "invalid key: invalid")
+}
+
func (l *TestSuite) TestWrite(c *C) {
for _, test := range writeTests {
input, output, enc := test[0], test[1], test[2]