2022-04-25 21:50:10 +00:00
|
|
|
package toml
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/pelletier/go-toml/v2/internal/danger"
|
|
|
|
"github.com/pelletier/go-toml/v2/internal/tracker"
|
2023-01-28 21:57:53 +00:00
|
|
|
"github.com/pelletier/go-toml/v2/unstable"
|
2022-04-25 21:50:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type strict struct {
|
|
|
|
Enabled bool
|
|
|
|
|
|
|
|
// Tracks the current key being processed.
|
|
|
|
key tracker.KeyTracker
|
|
|
|
|
2023-01-28 21:57:53 +00:00
|
|
|
missing []unstable.ParserError
|
2022-04-25 21:50:10 +00:00
|
|
|
}
|
|
|
|
|
2023-01-28 21:57:53 +00:00
|
|
|
func (s *strict) EnterTable(node *unstable.Node) {
|
2022-04-25 21:50:10 +00:00
|
|
|
if !s.Enabled {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s.key.UpdateTable(node)
|
|
|
|
}
|
|
|
|
|
2023-01-28 21:57:53 +00:00
|
|
|
func (s *strict) EnterArrayTable(node *unstable.Node) {
|
2022-04-25 21:50:10 +00:00
|
|
|
if !s.Enabled {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s.key.UpdateArrayTable(node)
|
|
|
|
}
|
|
|
|
|
2023-01-28 21:57:53 +00:00
|
|
|
func (s *strict) EnterKeyValue(node *unstable.Node) {
|
2022-04-25 21:50:10 +00:00
|
|
|
if !s.Enabled {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s.key.Push(node)
|
|
|
|
}
|
|
|
|
|
2023-01-28 21:57:53 +00:00
|
|
|
func (s *strict) ExitKeyValue(node *unstable.Node) {
|
2022-04-25 21:50:10 +00:00
|
|
|
if !s.Enabled {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s.key.Pop(node)
|
|
|
|
}
|
|
|
|
|
2023-01-28 21:57:53 +00:00
|
|
|
func (s *strict) MissingTable(node *unstable.Node) {
|
2022-04-25 21:50:10 +00:00
|
|
|
if !s.Enabled {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-28 21:57:53 +00:00
|
|
|
s.missing = append(s.missing, unstable.ParserError{
|
|
|
|
Highlight: keyLocation(node),
|
|
|
|
Message: "missing table",
|
|
|
|
Key: s.key.Key(),
|
2022-04-25 21:50:10 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-01-28 21:57:53 +00:00
|
|
|
func (s *strict) MissingField(node *unstable.Node) {
|
2022-04-25 21:50:10 +00:00
|
|
|
if !s.Enabled {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-28 21:57:53 +00:00
|
|
|
s.missing = append(s.missing, unstable.ParserError{
|
|
|
|
Highlight: keyLocation(node),
|
|
|
|
Message: "missing field",
|
|
|
|
Key: s.key.Key(),
|
2022-04-25 21:50:10 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *strict) Error(doc []byte) error {
|
|
|
|
if !s.Enabled || len(s.missing) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err := &StrictMissingError{
|
|
|
|
Errors: make([]DecodeError, 0, len(s.missing)),
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, derr := range s.missing {
|
|
|
|
derr := derr
|
|
|
|
err.Errors = append(err.Errors, *wrapDecodeError(doc, &derr))
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-28 21:57:53 +00:00
|
|
|
func keyLocation(node *unstable.Node) []byte {
|
2022-04-25 21:50:10 +00:00
|
|
|
k := node.Key()
|
|
|
|
|
|
|
|
hasOne := k.Next()
|
|
|
|
if !hasOne {
|
|
|
|
panic("should not be called with empty key")
|
|
|
|
}
|
|
|
|
|
|
|
|
start := k.Node().Data
|
|
|
|
end := k.Node().Data
|
|
|
|
|
|
|
|
for k.Next() {
|
|
|
|
end = k.Node().Data
|
|
|
|
}
|
|
|
|
|
|
|
|
return danger.BytesRange(start, end)
|
|
|
|
}
|