Issue #5: Support disabling expansion
diff --git a/README.md b/README.md index d05d86b..ae63343 100644 --- a/README.md +++ b/README.md
@@ -53,6 +53,10 @@ History ------- +v1.5.4, 23 Jun 2015 +------------------- + * [Issue #5](https://github.com/magiconair/properties/issues/5): Allow disabling of property expansion [DisableExpansion](http://godoc.org/github.com/magiconair/properties#Properties.DisableExpansion). When property expansion is disabled Properties become a simple key/value store and don't check for circular references. + v1.5.3, 02 Jun 2015 ------------------- * [Issue #4](https://github.com/magiconair/properties/issues/4): Maintain key order in [Filter()](http://godoc.org/github.com/magiconair/properties#Properties.Filter), [FilterPrefix()](http://godoc.org/github.com/magiconair/properties#Properties.FilterPrefix) and [FilterRegexp()](http://godoc.org/github.com/magiconair/properties#Properties.FilterRegexp)
diff --git a/properties.go b/properties.go index edcaccc..6201f8b 100644 --- a/properties.go +++ b/properties.go
@@ -47,6 +47,12 @@ Prefix string Postfix string + // DisableExpansion controls the expansion of properties on Get() + // and the check for circular references on Set(). When set to + // true Properties behaves like a simple key/value store and does + // not check for circular references on Get() or on Set(). + DisableExpansion bool + // Stores the key/value pairs m map[string]string @@ -73,6 +79,9 @@ // Otherwise, ok is false. func (p *Properties) Get(key string) (value string, ok bool) { v, ok := p.m[key] + if p.DisableExpansion { + return v, ok + } if !ok { return "", false } @@ -470,6 +479,13 @@ return "", false, nil } + // if expansion is disabled we allow circular references + if p.DisableExpansion { + prev, ok = p.Get(key) + p.m[key] = value + return prev, ok, nil + } + // to check for a circular reference we temporarily need // to set the new value. If there is an error then revert // to the previous state. Only if all tests are successful
diff --git a/properties_test.go b/properties_test.go index 36ff293..6ae0b42 100644 --- a/properties_test.go +++ b/properties_test.go
@@ -442,6 +442,21 @@ } } +func (s *TestSuite) TestDisableExpansion(c *C) { + input := "key=value\nkey2=${key}" + p, err := parse(input) + p.DisableExpansion = true + c.Assert(err, IsNil) + c.Assert(p.MustGet("key"), Equals, "value") + c.Assert(p.MustGet("key2"), Equals, "${key}") + + // with expansion disabled we can introduce circular references + p.Set("keyA", "${keyB}") + p.Set("keyB", "${keyA}") + c.Assert(p.MustGet("keyA"), Equals, "${keyB}") + c.Assert(p.MustGet("keyB"), Equals, "${keyA}") +} + func (s *TestSuite) TestMustGet(c *C) { input := "key = value\nkey2 = ghi" p, err := parse(input)