summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/pelletier/go-toml/v2/errors.go
blob: 5e6635c3e44a3bd639e92a421a12fe18a7201fc5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package toml

import (
	"fmt"
	"strconv"
	"strings"

	"github.com/pelletier/go-toml/v2/internal/danger"
)

// DecodeError represents an error encountered during the parsing or decoding
// of a TOML document.
//
// In addition to the error message, it contains the position in the document
// where it happened, as well as a human-readable representation that shows
// where the error occurred in the document.
type DecodeError struct {
	message string
	line    int
	column  int
	key     Key

	human string
}

// StrictMissingError occurs in a TOML document that does not have a
// corresponding field in the target value. It contains all the missing fields
// in Errors.
//
// Emitted by Decoder when DisallowUnknownFields() was called.
type StrictMissingError struct {
	// One error per field that could not be found.
	Errors []DecodeError
}

// Error returns the canonical string for this error.
func (s *StrictMissingError) Error() string {
	return "strict mode: fields in the document are missing in the target struct"
}

// String returns a human readable description of all errors.
func (s *StrictMissingError) String() string {
	var buf strings.Builder

	for i, e := range s.Errors {
		if i > 0 {
			buf.WriteString("\n---\n")
		}

		buf.WriteString(e.String())
	}

	return buf.String()
}

type Key []string

// internal version of DecodeError that is used as the base to create a
// DecodeError with full context.
type decodeError struct {
	highlight []byte
	message   string
	key       Key // optional
}

func (de *decodeError) Error() string {
	return de.message
}

func newDecodeError(highlight []byte, format string, args ...interface{}) error {
	return &decodeError{
		highlight: highlight,
		message:   fmt.Errorf(format, args...).Error(),
	}
}

// Error returns the error message contained in the DecodeError.
func (e *DecodeError) Error() string {
	return "toml: " + e.message
}

// String returns the human-readable contextualized error. This string is multi-line.
func (e *DecodeError) String() string {
	return e.human
}

// Position returns the (line, column) pair indicating where the error
// occurred in the document. Positions are 1-indexed.
func (e *DecodeError) Position() (row int, column int) {
	return e.line, e.column
}

// Key that was being processed when the error occurred. The key is present only
// if this DecodeError is part of a StrictMissingError.
func (e *DecodeError) Key() Key {
	return e.key
}

// decodeErrorFromHighlight creates a DecodeError referencing a highlighted
// range of bytes from document.
//
// highlight needs to be a sub-slice of document, or this function panics.
//
// The function copies all bytes used in DecodeError, so that document and
// highlight can be freely deallocated.
//nolint:funlen
func wrapDecodeError(document []byte, de *decodeError) *DecodeError {
	offset := danger.SubsliceOffset(document, de.highlight)

	errMessage := de.Error()
	errLine, errColumn := positionAtEnd(document[:offset])
	before, after := linesOfContext(document, de.highlight, offset, 3)

	var buf strings.Builder

	maxLine := errLine + len(after) - 1
	lineColumnWidth := len(strconv.Itoa(maxLine))

	// Write the lines of context strictly before the error.
	for i := len(before) - 1; i > 0; i-- {
		line := errLine - i
		buf.WriteString(formatLineNumber(line, lineColumnWidth))
		buf.WriteString("|")

		if len(before[i]) > 0 {
			buf.WriteString(" ")
			buf.Write(before[i])
		}

		buf.WriteRune('\n')
	}

	// Write the document line that contains the error.

	buf.WriteString(formatLineNumber(errLine, lineColumnWidth))
	buf.WriteString("| ")

	if len(before) > 0 {
		buf.Write(before[0])
	}

	buf.Write(de.highlight)

	if len(after) > 0 {
		buf.Write(after[0])
	}

	buf.WriteRune('\n')

	// Write the line with the error message itself (so it does not have a line
	// number).

	buf.WriteString(strings.Repeat(" ", lineColumnWidth))
	buf.WriteString("| ")

	if len(before) > 0 {
		buf.WriteString(strings.Repeat(" ", len(before[0])))
	}

	buf.WriteString(strings.Repeat("~", len(de.highlight)))

	if len(errMessage) > 0 {
		buf.WriteString(" ")
		buf.WriteString(errMessage)
	}

	// Write the lines of context strictly after the error.

	for i := 1; i < len(after); i++ {
		buf.WriteRune('\n')
		line := errLine + i
		buf.WriteString(formatLineNumber(line, lineColumnWidth))
		buf.WriteString("|")

		if len(after[i]) > 0 {
			buf.WriteString(" ")
			buf.Write(after[i])
		}
	}

	return &DecodeError{
		message: errMessage,
		line:    errLine,
		column:  errColumn,
		key:     de.key,
		human:   buf.String(),
	}
}

func formatLineNumber(line int, width int) string {
	format := "%" + strconv.Itoa(width) + "d"

	return fmt.Sprintf(format, line)
}

func linesOfContext(document []byte, highlight []byte, offset int, linesAround int) ([][]byte, [][]byte) {
	return beforeLines(document, offset, linesAround), afterLines(document, highlight, offset, linesAround)
}

func beforeLines(document []byte, offset int, linesAround int) [][]byte {
	var beforeLines [][]byte

	// Walk the document backward from the highlight to find previous lines
	// of context.
	rest := document[:offset]
backward:
	for o := len(rest) - 1; o >= 0 && len(beforeLines) <= linesAround && len(rest) > 0; {
		switch {
		case rest[o] == '\n':
			// handle individual lines
			beforeLines = append(beforeLines, rest[o+1:])
			rest = rest[:o]
			o = len(rest) - 1
		case o == 0:
			// add the first line only if it's non-empty
			beforeLines = append(beforeLines, rest)

			break backward
		default:
			o--
		}
	}

	return beforeLines
}

func afterLines(document []byte, highlight []byte, offset int, linesAround int) [][]byte {
	var afterLines [][]byte

	// Walk the document forward from the highlight to find the following
	// lines of context.
	rest := document[offset+len(highlight):]
forward:
	for o := 0; o < len(rest) && len(afterLines) <= linesAround; {
		switch {
		case rest[o] == '\n':
			// handle individual lines
			afterLines = append(afterLines, rest[:o])
			rest = rest[o+1:]
			o = 0

		case o == len(rest)-1:
			// add last line only if it's non-empty
			afterLines = append(afterLines, rest)

			break forward
		default:
			o++
		}
	}

	return afterLines
}

func positionAtEnd(b []byte) (row int, column int) {
	row = 1
	column = 1

	for _, c := range b {
		if c == '\n' {
			row++
			column = 1
		} else {
			column++
		}
	}

	return
}