key: fix panic when recursive value has missing target (#191)

This commit is contained in:
Unknwon
2019-10-17 18:13:53 -07:00
parent b19459e590
commit 2e33564b44
2 changed files with 18 additions and 1 deletions
+6 -1
View File
@@ -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'.
+12
View File
@@ -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")
})
}