Skip to content

Commit

Permalink
Decoder: prevent duplicates of inline tables (#667)
Browse files Browse the repository at this point in the history
* seen: prevent duplicates of inline tables

* Provide clearer error message for redefined keys

For example:

``
toml: key b is already defined
```
  • Loading branch information
pelletier committed Nov 10, 2021
1 parent 2dbd29a commit 4dff8ea
Show file tree
Hide file tree
Showing 2 changed files with 346 additions and 343 deletions.
29 changes: 14 additions & 15 deletions internal/tracker/seen.go
Expand Up @@ -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 {
Expand Down

0 comments on commit 4dff8ea

Please sign in to comment.