Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
//
Expand Down
20 changes: 15 additions & 5 deletions key.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
}
Expand Down
41 changes: 41 additions & 0 deletions key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
Loading