Decoder: prevent duplicates of inline tables (#667)

* seen: prevent duplicates of inline tables

* Provide clearer error message for redefined keys

For example:

``
toml: key b is already defined
```
This commit is contained in:
Thomas Pelletier
2021-11-10 10:04:43 -05:00
committed by GitHub
parent 2dbd29a565
commit 4dff8eaa4d
2 changed files with 353 additions and 350 deletions
+14 -15
View File
@@ -216,35 +216,34 @@ func (s *SeenTracker) checkKeyValue(parentIdx int, node *ast.Node) error {
idx := s.find(parentIdx, k)
if idx >= 0 {
if s.entries[idx].kind != tableKind {
return fmt.Errorf("toml: expected %s to be a table, not a %s", string(k), s.entries[idx].kind)
}
if s.entries[idx].explicit {
if idx < 0 {
idx = s.create(parentIdx, k, tableKind, false)
} else {
entry := s.entries[idx]
if it.IsLast() {
return fmt.Errorf("toml: key %s is already defined", string(k))
} else if entry.kind != tableKind {
return fmt.Errorf("toml: expected %s to be a table, not a %s", string(k), entry.kind)
} else if entry.explicit {
return fmt.Errorf("toml: cannot redefine table %s that has already been explicitly defined", string(k))
}
} else {
idx = s.create(parentIdx, k, tableKind, false)
}
parentIdx = idx
}
kind := valueKind
var err error
s.entries[parentIdx].kind = valueKind
value := node.Value()
switch value.Kind {
case ast.InlineTable:
kind = tableKind
err = s.checkInlineTable(parentIdx, value)
return s.checkInlineTable(parentIdx, value)
case ast.Array:
err = s.checkArray(parentIdx, value)
return s.checkArray(parentIdx, value)
}
s.entries[parentIdx].kind = kind
return err
return nil
}
func (s *SeenTracker) checkArray(parentIdx int, node *ast.Node) error {