diff --git a/cmd/neofs-node/config/calls.go b/cmd/neofs-node/config/calls.go index 56ffd4371d..4f15351df7 100644 --- a/cmd/neofs-node/config/calls.go +++ b/cmd/neofs-node/config/calls.go @@ -8,9 +8,16 @@ import ( // // Returns nil if subsection if missing. func (x *Config) Sub(name string) *Config { + // copy path in order to prevent consequent violations + ln := len(x.path) + + path := make([]string, ln, ln+1) + + copy(path, x.path) + return &Config{ v: x.v, - path: append(x.path, name), + path: append(path, name), } } diff --git a/cmd/neofs-node/config/calls_test.go b/cmd/neofs-node/config/calls_test.go index 6408447c21..52425ab704 100644 --- a/cmd/neofs-node/config/calls_test.go +++ b/cmd/neofs-node/config/calls_test.go @@ -42,3 +42,21 @@ func TestConfigEnv(t *testing.T) { require.Equal(t, value, c.Sub(section).Value(name)) } + +func TestConfig_SubValue(t *testing.T) { + configtest.ForEachFileType("test/config", func(c *config.Config) { + c = c. + Sub("section"). + Sub("sub"). + Sub("sub") + + // get subsection 1 + sub := c.Sub("sub1") + + // get subsection 2 + c.Sub("sub2") + + // sub should not be corrupted + require.Equal(t, "val1", sub.Value("key")) + }) +} diff --git a/cmd/neofs-node/config/test/config.json b/cmd/neofs-node/config/test/config.json index 8332e3e845..e2d00fcab0 100644 --- a/cmd/neofs-node/config/test/config.json +++ b/cmd/neofs-node/config/test/config.json @@ -1,7 +1,18 @@ { "value": "some value", + "section": { - "any": "thing" + "any": "thing", + "sub": { + "sub": { + "sub1": { + "key": "val1" + }, + "sub2": { + "key": "val2" + } + } + } }, "string_slice": { diff --git a/cmd/neofs-node/config/test/config.yaml b/cmd/neofs-node/config/test/config.yaml index 1b5f6c07d7..6ee4aca0f2 100644 --- a/cmd/neofs-node/config/test/config.yaml +++ b/cmd/neofs-node/config/test/config.yaml @@ -3,6 +3,14 @@ value: some value section: any: thing + sub: + sub: + sub1: + key: val1 + + sub2: + key: val2 + string_slice: empty: []