diff --git a/internal/toml-test/tests/valid/table/sub-sub-subtable.json b/internal/toml-test/tests/valid/table/sub-sub-subtable.json new file mode 100644 index 00000000..eaafc8cc --- /dev/null +++ b/internal/toml-test/tests/valid/table/sub-sub-subtable.json @@ -0,0 +1,16 @@ +{ + "x": { + "y": { + "z": { + "a": { + "type": "integer", + "value": "1" + }, + "b": { + "type": "integer", + "value": "2" + } + } + } + } +} diff --git a/internal/toml-test/tests/valid/table/sub-sub-subtable.toml b/internal/toml-test/tests/valid/table/sub-sub-subtable.toml new file mode 100644 index 00000000..dc8d7015 --- /dev/null +++ b/internal/toml-test/tests/valid/table/sub-sub-subtable.toml @@ -0,0 +1,3 @@ +[x.y.z] +a = 1 +b = 2 diff --git a/parse.go b/parse.go index 3f2c090c..385a7c05 100644 --- a/parse.go +++ b/parse.go @@ -442,7 +442,7 @@ func (p *parser) valueInlineTable(it item, parentIsArray bool) (any, tomlType) { outerKey = p.currentKey ) - p.context = append(p.context, p.currentKey) + p.context = p.context.add(p.currentKey) prevContext := p.context p.currentKey = "" @@ -603,7 +603,7 @@ func (p *parser) addContext(key Key, array bool) { } else { p.setValue(key.last(), make(map[string]any)) } - p.context = append(p.context, key.last()) + p.context = p.context.add(key.last()) } // setValue sets the given key to the given value in the current context. @@ -614,10 +614,10 @@ func (p *parser) setValue(key string, value any) { tmpHash any ok bool hash = p.mapping - keyContext = make(Key, 0, len(p.context)+1) + keyContext = make(Key, len(p.context)+1) ) - for _, k := range p.context { - keyContext = append(keyContext, k) + for i, k := range p.context { + keyContext[i] = k if tmpHash, ok = hash[k]; !ok { p.bug("Context for key '%s' has not been established.", keyContext) } @@ -632,7 +632,7 @@ func (p *parser) setValue(key string, value any) { p.panicf("Key '%s' has already been defined.", keyContext) } } - keyContext = append(keyContext, key) + keyContext[len(p.context)] = key if _, ok := hash[key]; ok { // Normally redefining keys isn't allowed, but the key could have been @@ -667,8 +667,8 @@ func (p *parser) setValue(key string, value any) { // Note that if `key` is empty, then the type given will be applied to the // current context (which is either a table or an array of tables). func (p *parser) setType(key string, typ tomlType, pos Position) { - keyContext := make(Key, 0, len(p.context)+1) - keyContext = append(keyContext, p.context...) + keyContext := make(Key, len(p.context), len(p.context)+1) + copy(keyContext, p.context) if len(key) > 0 { // allow type setting for hashes keyContext = append(keyContext, key) }