vendor/github.com/pelletier/go-toml/v2/strict.go
changeset 265 05c40b36d3b2
parent 260 445e01aede7e
equal deleted inserted replaced
264:8f478162d991 265:05c40b36d3b2
     1 package toml
     1 package toml
     2 
     2 
     3 import (
     3 import (
     4 	"github.com/pelletier/go-toml/v2/internal/ast"
       
     5 	"github.com/pelletier/go-toml/v2/internal/danger"
     4 	"github.com/pelletier/go-toml/v2/internal/danger"
     6 	"github.com/pelletier/go-toml/v2/internal/tracker"
     5 	"github.com/pelletier/go-toml/v2/internal/tracker"
       
     6 	"github.com/pelletier/go-toml/v2/unstable"
     7 )
     7 )
     8 
     8 
     9 type strict struct {
     9 type strict struct {
    10 	Enabled bool
    10 	Enabled bool
    11 
    11 
    12 	// Tracks the current key being processed.
    12 	// Tracks the current key being processed.
    13 	key tracker.KeyTracker
    13 	key tracker.KeyTracker
    14 
    14 
    15 	missing []decodeError
    15 	missing []unstable.ParserError
    16 }
    16 }
    17 
    17 
    18 func (s *strict) EnterTable(node *ast.Node) {
    18 func (s *strict) EnterTable(node *unstable.Node) {
    19 	if !s.Enabled {
    19 	if !s.Enabled {
    20 		return
    20 		return
    21 	}
    21 	}
    22 
    22 
    23 	s.key.UpdateTable(node)
    23 	s.key.UpdateTable(node)
    24 }
    24 }
    25 
    25 
    26 func (s *strict) EnterArrayTable(node *ast.Node) {
    26 func (s *strict) EnterArrayTable(node *unstable.Node) {
    27 	if !s.Enabled {
    27 	if !s.Enabled {
    28 		return
    28 		return
    29 	}
    29 	}
    30 
    30 
    31 	s.key.UpdateArrayTable(node)
    31 	s.key.UpdateArrayTable(node)
    32 }
    32 }
    33 
    33 
    34 func (s *strict) EnterKeyValue(node *ast.Node) {
    34 func (s *strict) EnterKeyValue(node *unstable.Node) {
    35 	if !s.Enabled {
    35 	if !s.Enabled {
    36 		return
    36 		return
    37 	}
    37 	}
    38 
    38 
    39 	s.key.Push(node)
    39 	s.key.Push(node)
    40 }
    40 }
    41 
    41 
    42 func (s *strict) ExitKeyValue(node *ast.Node) {
    42 func (s *strict) ExitKeyValue(node *unstable.Node) {
    43 	if !s.Enabled {
    43 	if !s.Enabled {
    44 		return
    44 		return
    45 	}
    45 	}
    46 
    46 
    47 	s.key.Pop(node)
    47 	s.key.Pop(node)
    48 }
    48 }
    49 
    49 
    50 func (s *strict) MissingTable(node *ast.Node) {
    50 func (s *strict) MissingTable(node *unstable.Node) {
    51 	if !s.Enabled {
    51 	if !s.Enabled {
    52 		return
    52 		return
    53 	}
    53 	}
    54 
    54 
    55 	s.missing = append(s.missing, decodeError{
    55 	s.missing = append(s.missing, unstable.ParserError{
    56 		highlight: keyLocation(node),
    56 		Highlight: keyLocation(node),
    57 		message:   "missing table",
    57 		Message:   "missing table",
    58 		key:       s.key.Key(),
    58 		Key:       s.key.Key(),
    59 	})
    59 	})
    60 }
    60 }
    61 
    61 
    62 func (s *strict) MissingField(node *ast.Node) {
    62 func (s *strict) MissingField(node *unstable.Node) {
    63 	if !s.Enabled {
    63 	if !s.Enabled {
    64 		return
    64 		return
    65 	}
    65 	}
    66 
    66 
    67 	s.missing = append(s.missing, decodeError{
    67 	s.missing = append(s.missing, unstable.ParserError{
    68 		highlight: keyLocation(node),
    68 		Highlight: keyLocation(node),
    69 		message:   "missing field",
    69 		Message:   "missing field",
    70 		key:       s.key.Key(),
    70 		Key:       s.key.Key(),
    71 	})
    71 	})
    72 }
    72 }
    73 
    73 
    74 func (s *strict) Error(doc []byte) error {
    74 func (s *strict) Error(doc []byte) error {
    75 	if !s.Enabled || len(s.missing) == 0 {
    75 	if !s.Enabled || len(s.missing) == 0 {
    86 	}
    86 	}
    87 
    87 
    88 	return err
    88 	return err
    89 }
    89 }
    90 
    90 
    91 func keyLocation(node *ast.Node) []byte {
    91 func keyLocation(node *unstable.Node) []byte {
    92 	k := node.Key()
    92 	k := node.Key()
    93 
    93 
    94 	hasOne := k.Next()
    94 	hasOne := k.Next()
    95 	if !hasOne {
    95 	if !hasOne {
    96 		panic("should not be called with empty key")
    96 		panic("should not be called with empty key")