diff --git a/key.go b/key.go index 62f9146..3c19741 100644 --- a/key.go +++ b/key.go @@ -147,10 +147,15 @@ func (k *Key) transformValue(val string) string { noption := vr[2 : len(vr)-2] // Search in the same section. + // If not found or found the key itself, then search again in default section. nk, err := k.s.GetKey(noption) if err != nil || k == nk { - // Search again in default section. nk, _ = k.s.f.Section("").GetKey(noption) + if nk == nil { + // Stop when no results found in the default section, + // and returns the value as-is. + break + } } // Substitute by new value and take off leading '%(' and trailing ')s'. diff --git a/key_test.go b/key_test.go index 25b2d48..380f7fe 100644 --- a/key_test.go +++ b/key_test.go @@ -551,7 +551,19 @@ NAME = %(NAME)s expires = %(expires)s`)) So(err, ShouldBeNil) So(f, ShouldNotBeNil) + So(f.Section("package").Key("NAME").String(), ShouldEqual, "ini") So(f.Section("package").Key("expires").String(), ShouldEqual, "yes") }) + + Convey("Recursive value with no target found", t, func() { + f, err := ini.Load([]byte(` +[foo] +bar = %(missing)s +`)) + So(err, ShouldBeNil) + So(f, ShouldNotBeNil) + + So(f.Section("foo").Key("bar").String(), ShouldEqual, "%(missing)s") + }) }