From eb37d3805a1366ef00a6c6df0bb689d2138386f6 Mon Sep 17 00:00:00 2001 From: Joe Chen Date: Sun, 15 Nov 2026 04:41:15 +0000 Subject: [PATCH] Resolve %(section.key)s references A reference names a key of its own section or of the default section, as before. When neither has the name, it's split at the last dot into a section and a key, so %(db.port)s reads port in [db] (#286). --- doc.go | 2 +- key.go | 20 +++++++++++++++----- key_test.go | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 6 deletions(-) diff --git a/doc.go b/doc.go index 51eba1f..c15615a 100644 --- a/doc.go +++ b/doc.go @@ -29,7 +29,7 @@ // [Key.Duration]. The generic [List], [In] and [Range] parse lists and // candidates of any [Scalar] type, and ranges of any [Ordered] one. A value // can refer to another key of its section or of the default section as -// %(name)s. +// %(name)s, or to a key of another section as %(section.key)s. // // # Structs // diff --git a/key.go b/key.go index 5eeb854..52f6018 100644 --- a/key.go +++ b/key.go @@ -207,7 +207,8 @@ type expansion struct { } // expand applies the ValueMapper and replaces %(name)s references in val -// with the expanded values of those keys. References nest at most +// with the expanded values of those keys. A name is a key of k's section +// or of the default section, or else section.key, split at the last dot. References nest at most // depthValues deep, and one expansion makes at most maxSubstitutions // substitutions producing at most maxExpandedLen bytes in total, so // reference cycles and values that double at every level stop instead of @@ -230,14 +231,23 @@ func (k *Key) expand(val string, depth int, e *expansion) string { // Take off leading '%(' and trailing ')s'. 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. + // Search in the same section, then in the default section, then, + // for a name with a dot, for the key after the last dot in the + // section before it, so key names with dots keep working. nk, err := k.s.GetKey(noption) if err != nil || k == nk { nk, _ = k.s.f.Section("").GetKey(noption) + } + if nk == nil || nk == k { + nk = nil + if i := strings.LastIndex(noption, "."); i > 0 { + if sec, err := k.s.f.GetSection(noption[:i]); err == nil { + nk, _ = sec.GetKey(noption[i+1:]) + } + } if nk == nil || nk == k { - // Stop, and return the value as it is, when the default - // section has no such key or only this key. + // Stop, and return the value as it is, when no key or + // only this key has the name. break } } diff --git a/key_test.go b/key_test.go index a0cf3ab..7c7e3b1 100644 --- a/key_test.go +++ b/key_test.go @@ -432,6 +432,47 @@ expires = %(expires)s`)) assert.Equal(t, "yes", f.Section("package").Key("expires").String()) }) + // https://github.com/go-ini/ini/issues/286 + t.Run("section.key references", func(t *testing.T) { + f, err := Load([]byte(` +host = default-host +a.b = default-dotted +[db] +host = db-host +port = 5432 +[db.replica] +port = 5433 +[app] +dsn = %(db.host)s:%(db.port)s +replica = %(db.replica.port)s +dotted = %(a.b)s +own = %(host)s +missing = %(db.user)s +self = %(app.self)s +[app2] +db.host = own-dotted-key +dsn = %(db.host)s +`)) + require.NoError(t, err) + + app := f.Section("app") + assert.Equal(t, "db-host:5432", app.Key("dsn").String()) + // The section is the name before the last dot. + assert.Equal(t, "5433", app.Key("replica").String()) + // Keys whose names have dots are found first, as before. + assert.Equal(t, "default-dotted", app.Key("dotted").String()) + assert.Equal(t, "own-dotted-key", f.Section("app2").Key("dsn").String()) + assert.Equal(t, "default-host", app.Key("own").String()) + assert.Equal(t, "%(db.user)s", app.Key("missing").String()) + assert.Equal(t, "%(app.self)s", app.Key("self").String()) + }) + + t.Run("section.key reference cycle stops", func(t *testing.T) { + f, err := Load([]byte("[x]\na = %(y.b)s\n[y]\nb = %(x.a)s\n")) + require.NoError(t, err) + assert.LessOrEqual(t, len(f.Section("x").Key("a").String()), maxExpandedLen) + }) + // https://github.com/go-ini/ini/issues/409 t.Run("reference cycles and doubling values stop", func(t *testing.T) { for _, data := range []string{