author | Mikael Berthe <mikael@lilotux.net> |
Sat, 04 Feb 2023 12:58:35 +0100 | |
changeset 265 | 05c40b36d3b2 |
parent 260 | 445e01aede7e |
permissions | -rw-r--r-- |
260 | 1 |
package toml |
2 |
||
3 |
import ( |
|
4 |
"github.com/pelletier/go-toml/v2/internal/danger" |
|
5 |
"github.com/pelletier/go-toml/v2/internal/tracker" |
|
265
05c40b36d3b2
Bump to version 3.0.0-dev, using madon v3
Mikael Berthe <mikael@lilotux.net>
parents:
260
diff
changeset
|
6 |
"github.com/pelletier/go-toml/v2/unstable" |
260 | 7 |
) |
8 |
||
9 |
type strict struct { |
|
10 |
Enabled bool |
|
11 |
||
12 |
// Tracks the current key being processed. |
|
13 |
key tracker.KeyTracker |
|
14 |
||
265
05c40b36d3b2
Bump to version 3.0.0-dev, using madon v3
Mikael Berthe <mikael@lilotux.net>
parents:
260
diff
changeset
|
15 |
missing []unstable.ParserError |
260 | 16 |
} |
17 |
||
265
05c40b36d3b2
Bump to version 3.0.0-dev, using madon v3
Mikael Berthe <mikael@lilotux.net>
parents:
260
diff
changeset
|
18 |
func (s *strict) EnterTable(node *unstable.Node) { |
260 | 19 |
if !s.Enabled { |
20 |
return |
|
21 |
} |
|
22 |
||
23 |
s.key.UpdateTable(node) |
|
24 |
} |
|
25 |
||
265
05c40b36d3b2
Bump to version 3.0.0-dev, using madon v3
Mikael Berthe <mikael@lilotux.net>
parents:
260
diff
changeset
|
26 |
func (s *strict) EnterArrayTable(node *unstable.Node) { |
260 | 27 |
if !s.Enabled { |
28 |
return |
|
29 |
} |
|
30 |
||
31 |
s.key.UpdateArrayTable(node) |
|
32 |
} |
|
33 |
||
265
05c40b36d3b2
Bump to version 3.0.0-dev, using madon v3
Mikael Berthe <mikael@lilotux.net>
parents:
260
diff
changeset
|
34 |
func (s *strict) EnterKeyValue(node *unstable.Node) { |
260 | 35 |
if !s.Enabled { |
36 |
return |
|
37 |
} |
|
38 |
||
39 |
s.key.Push(node) |
|
40 |
} |
|
41 |
||
265
05c40b36d3b2
Bump to version 3.0.0-dev, using madon v3
Mikael Berthe <mikael@lilotux.net>
parents:
260
diff
changeset
|
42 |
func (s *strict) ExitKeyValue(node *unstable.Node) { |
260 | 43 |
if !s.Enabled { |
44 |
return |
|
45 |
} |
|
46 |
||
47 |
s.key.Pop(node) |
|
48 |
} |
|
49 |
||
265
05c40b36d3b2
Bump to version 3.0.0-dev, using madon v3
Mikael Berthe <mikael@lilotux.net>
parents:
260
diff
changeset
|
50 |
func (s *strict) MissingTable(node *unstable.Node) { |
260 | 51 |
if !s.Enabled { |
52 |
return |
|
53 |
} |
|
54 |
||
265
05c40b36d3b2
Bump to version 3.0.0-dev, using madon v3
Mikael Berthe <mikael@lilotux.net>
parents:
260
diff
changeset
|
55 |
s.missing = append(s.missing, unstable.ParserError{ |
05c40b36d3b2
Bump to version 3.0.0-dev, using madon v3
Mikael Berthe <mikael@lilotux.net>
parents:
260
diff
changeset
|
56 |
Highlight: keyLocation(node), |
05c40b36d3b2
Bump to version 3.0.0-dev, using madon v3
Mikael Berthe <mikael@lilotux.net>
parents:
260
diff
changeset
|
57 |
Message: "missing table", |
05c40b36d3b2
Bump to version 3.0.0-dev, using madon v3
Mikael Berthe <mikael@lilotux.net>
parents:
260
diff
changeset
|
58 |
Key: s.key.Key(), |
260 | 59 |
}) |
60 |
} |
|
61 |
||
265
05c40b36d3b2
Bump to version 3.0.0-dev, using madon v3
Mikael Berthe <mikael@lilotux.net>
parents:
260
diff
changeset
|
62 |
func (s *strict) MissingField(node *unstable.Node) { |
260 | 63 |
if !s.Enabled { |
64 |
return |
|
65 |
} |
|
66 |
||
265
05c40b36d3b2
Bump to version 3.0.0-dev, using madon v3
Mikael Berthe <mikael@lilotux.net>
parents:
260
diff
changeset
|
67 |
s.missing = append(s.missing, unstable.ParserError{ |
05c40b36d3b2
Bump to version 3.0.0-dev, using madon v3
Mikael Berthe <mikael@lilotux.net>
parents:
260
diff
changeset
|
68 |
Highlight: keyLocation(node), |
05c40b36d3b2
Bump to version 3.0.0-dev, using madon v3
Mikael Berthe <mikael@lilotux.net>
parents:
260
diff
changeset
|
69 |
Message: "missing field", |
05c40b36d3b2
Bump to version 3.0.0-dev, using madon v3
Mikael Berthe <mikael@lilotux.net>
parents:
260
diff
changeset
|
70 |
Key: s.key.Key(), |
260 | 71 |
}) |
72 |
} |
|
73 |
||
74 |
func (s *strict) Error(doc []byte) error { |
|
75 |
if !s.Enabled || len(s.missing) == 0 { |
|
76 |
return nil |
|
77 |
} |
|
78 |
||
79 |
err := &StrictMissingError{ |
|
80 |
Errors: make([]DecodeError, 0, len(s.missing)), |
|
81 |
} |
|
82 |
||
83 |
for _, derr := range s.missing { |
|
84 |
derr := derr |
|
85 |
err.Errors = append(err.Errors, *wrapDecodeError(doc, &derr)) |
|
86 |
} |
|
87 |
||
88 |
return err |
|
89 |
} |
|
90 |
||
265
05c40b36d3b2
Bump to version 3.0.0-dev, using madon v3
Mikael Berthe <mikael@lilotux.net>
parents:
260
diff
changeset
|
91 |
func keyLocation(node *unstable.Node) []byte { |
260 | 92 |
k := node.Key() |
93 |
||
94 |
hasOne := k.Next() |
|
95 |
if !hasOne { |
|
96 |
panic("should not be called with empty key") |
|
97 |
} |
|
98 |
||
99 |
start := k.Node().Data |
|
100 |
end := k.Node().Data |
|
101 |
||
102 |
for k.Next() { |
|
103 |
end = k.Node().Data |
|
104 |
} |
|
105 |
||
106 |
return danger.BytesRange(start, end) |
|
107 |
} |