@@ -196,11 +196,11 @@ func (p *parser) topLevel(item item) {
196
196
p .assertEqual (itemKeyEnd , k .typ )
197
197
198
198
/// The current key is the last part.
199
- p .currentKey = key [ len ( key ) - 1 ]
199
+ p .currentKey = key . last ()
200
200
201
201
/// All the other parts (if any) are the context; need to set each part
202
202
/// as implicit.
203
- context := key [: len ( key ) - 1 ]
203
+ context := key . parent ()
204
204
for i := range context {
205
205
p .addImplicitContext (append (p .context , context [i :i + 1 ]... ))
206
206
}
@@ -209,7 +209,8 @@ func (p *parser) topLevel(item item) {
209
209
/// Set value.
210
210
vItem := p .next ()
211
211
val , typ := p .value (vItem , false )
212
- p .set (p .currentKey , val , typ , vItem .pos )
212
+ p .setValue (p .currentKey , val )
213
+ p .setType (p .currentKey , typ , vItem .pos )
213
214
214
215
/// Remove the context we added (preserving any context from [tbl] lines).
215
216
p .context = outerContext
@@ -434,7 +435,7 @@ func (p *parser) valueArray(it item) (any, tomlType) {
434
435
435
436
func (p * parser ) valueInlineTable (it item , parentIsArray bool ) (any , tomlType ) {
436
437
var (
437
- hash = make (map [string ]any )
438
+ topHash = make (map [string ]any )
438
439
outerContext = p .context
439
440
outerKey = p .currentKey
440
441
)
@@ -462,27 +463,38 @@ func (p *parser) valueInlineTable(it item, parentIsArray bool) (any, tomlType) {
462
463
p .assertEqual (itemKeyEnd , k .typ )
463
464
464
465
/// The current key is the last part.
465
- p .currentKey = key [ len ( key ) - 1 ]
466
+ p .currentKey = key . last ()
466
467
467
468
/// All the other parts (if any) are the context; need to set each part
468
469
/// as implicit.
469
- context := key [: len ( key ) - 1 ]
470
+ context := key . parent ()
470
471
for i := range context {
471
472
p .addImplicitContext (append (p .context , context [i :i + 1 ]... ))
472
473
}
473
474
p .ordered = append (p .ordered , p .context .add (p .currentKey ))
474
475
475
476
/// Set the value.
476
477
val , typ := p .value (p .next (), false )
477
- p .set (p .currentKey , val , typ , it .pos )
478
+ p .setValue (p .currentKey , val )
479
+ p .setType (p .currentKey , typ , it .pos )
480
+
481
+ hash := topHash
482
+ for _ , c := range context {
483
+ h , ok := hash [c ]
484
+ if ! ok {
485
+ h = make (map [string ]any )
486
+ hash [c ] = h
487
+ }
488
+ hash = h .(map [string ]any )
489
+ }
478
490
hash [p .currentKey ] = val
479
491
480
492
/// Restore context.
481
493
p .context = prevContext
482
494
}
483
495
p .context = outerContext
484
496
p .currentKey = outerKey
485
- return hash , tomlHash
497
+ return topHash , tomlHash
486
498
}
487
499
488
500
// numHasLeadingZero checks if this number has leading zeroes, allowing for '0',
@@ -537,15 +549,13 @@ func numPeriodsOK(s string) bool {
537
549
// Establishing the context also makes sure that the key isn't a duplicate, and
538
550
// will create implicit hashes automatically.
539
551
func (p * parser ) addContext (key Key , array bool ) {
540
- var ok bool
541
-
542
- // Always start at the top level and drill down for our context.
552
+ /// Always start at the top level and drill down for our context.
543
553
hashContext := p .mapping
544
554
keyContext := make (Key , 0 , len (key )- 1 )
545
555
546
- // We only need implicit hashes for key[0:-1]
547
- for _ , k := range key [ 0 : len ( key ) - 1 ] {
548
- _ , ok = hashContext [k ]
556
+ /// We only need implicit hashes for the parents.
557
+ for _ , k := range key . parent () {
558
+ _ , ok : = hashContext [k ]
549
559
keyContext = append (keyContext , k )
550
560
551
561
// No key? Make an implicit hash and move on.
@@ -573,7 +583,7 @@ func (p *parser) addContext(key Key, array bool) {
573
583
if array {
574
584
// If this is the first element for this array, then allocate a new
575
585
// list of tables for it.
576
- k := key [ len ( key ) - 1 ]
586
+ k := key . last ()
577
587
if _ , ok := hashContext [k ]; ! ok {
578
588
hashContext [k ] = make ([]map [string ]any , 0 , 4 )
579
589
}
@@ -586,15 +596,9 @@ func (p *parser) addContext(key Key, array bool) {
586
596
p .panicf ("Key '%s' was already created and cannot be used as an array." , key )
587
597
}
588
598
} else {
589
- p .setValue (key [ len ( key ) - 1 ] , make (map [string ]any ))
599
+ p .setValue (key . last () , make (map [string ]any ))
590
600
}
591
- p .context = append (p .context , key [len (key )- 1 ])
592
- }
593
-
594
- // set calls setValue and setType.
595
- func (p * parser ) set (key string , val any , typ tomlType , pos Position ) {
596
- p .setValue (key , val )
597
- p .setType (key , typ , pos )
601
+ p .context = append (p .context , key .last ())
598
602
}
599
603
600
604
// setValue sets the given key to the given value in the current context.
@@ -644,9 +648,8 @@ func (p *parser) setValue(key string, value any) {
644
648
p .removeImplicit (keyContext )
645
649
return
646
650
}
647
-
648
- // Otherwise, we have a concrete key trying to override a previous
649
- // key, which is *always* wrong.
651
+ // Otherwise, we have a concrete key trying to override a previous key,
652
+ // which is *always* wrong.
650
653
p .panicf ("Key '%s' has already been defined." , keyContext )
651
654
}
652
655
0 commit comments