@@ -202,11 +202,28 @@ func (d *Decoder) nextRootNode(tnode *toml.Node) error {
202
202
case toml .Table :
203
203
// Tables always begin a new line.
204
204
key , keyElems := d .decodeKey ("" , tnode .Key ())
205
+
206
+ // Check if this table is a subtable of an existing array element
207
+ array := d .findArrayPrefix (key )
208
+ var actualKey string
209
+ if array != nil { // [last_array.new_table]
210
+ if array .rkey == key {
211
+ return d .nodeErrf (tnode .Child (), "cannot redeclare table array %q as a table" , key )
212
+ }
213
+ // For subtables within array elements, we need to use the current array element's key
214
+ // to avoid false duplicate key errors between different array elements
215
+ arrayElementKey := array .rkey + "." + strconv .Itoa (len (array .list .Elts )- 1 )
216
+ subKey := key [len (array .rkey )+ 1 :] // Remove the array prefix and dot
217
+ actualKey = arrayElementKey + "." + subKey
218
+ } else {
219
+ actualKey = key
220
+ }
221
+
205
222
// All table keys must be unique, including for the top-level table.
206
- if d .seenTableKeys [key ] {
223
+ if d .seenTableKeys [actualKey ] {
207
224
return d .nodeErrf (tnode .Child (), "duplicate key: %s" , key )
208
225
}
209
- d .seenTableKeys [key ] = true
226
+ d .seenTableKeys [actualKey ] = true
210
227
211
228
// We want a multi-line struct with curly braces,
212
229
// just like TOML's tables are on multiple lines.
@@ -215,11 +232,7 @@ func (d *Decoder) nextRootNode(tnode *toml.Node) error {
215
232
Lbrace : token .NoPos .WithRel (token .Blank ),
216
233
Rbrace : token .NoPos .WithRel (token .Newline ),
217
234
}
218
- array := d .findArrayPrefix (key )
219
235
if array != nil { // [last_array.new_table]
220
- if array .rkey == key {
221
- return d .nodeErrf (tnode .Child (), "cannot redeclare table array %q as a table" , key )
222
- }
223
236
subKeyElems := keyElems [array .level :]
224
237
topField , leafField := d .inlineFields (subKeyElems , token .Newline )
225
238
array .lastTable .Elts = append (array .lastTable .Elts , topField )
@@ -229,7 +242,7 @@ func (d *Decoder) nextRootNode(tnode *toml.Node) error {
229
242
d .topFile .Elts = append (d .topFile .Elts , topField )
230
243
leafField .Value = d .currentTable
231
244
}
232
- d .currentTableKey = key
245
+ d .currentTableKey = actualKey
233
246
234
247
case toml .ArrayTable :
235
248
// Table array elements always begin a new line.
0 commit comments