Debian Codegraph @

internal/decoder/type_error.go

raw
1 package decoder
2
3 import (
4 "reflect"
5 "strconv"
6 )
7
8 // The type errors are the ones of encoding/json: a value which is not of the type of its destination is skipped,
9 // the decoding goes on, and the first such error is returned at the end, unless the input has a syntax error,
10 // which is returned instead. The error is recorded where it happens by its position only; the path to its value,
11 // which encoding/json reports as the Struct and the Field of the error, is computed at the end by a walk of the
12 // input from the root ( see typeErrorPath ), so that the decoding of a value which has no error keeps no path.
13
14 // float64Type is the type which the type error of a number out of the range of a float64 decoded into an
15 // interface{} reports.
16 var float64Type = reflect.TypeOf(float64(0))
17
18 // pendingTypeError is the first type error of a decoding, as it is recorded.
19 type pendingTypeError struct {
20 typ reflect.Type
21 // start and end are the positions of the value in the buffer.
22 start, end int64
23 // value is the description of the value which the error reports, as "string" or "number 1.5".
24 value string
25 // kind is the kind of the value.
26 kind jsonKind
27 // atKey is set for the error of the key of a map entry, whose value starts at start too.
28 atKey bool
29 // literal is set for an error which reports the literal of the value in its value, as "number 1.5".
30 literal bool
31 // plain is an error which is not a type error, which encoding/json returns as it is: an invalid use of the string
32 // option before Go 1.27, or the error of an unmarshal method by Go 1.27.
33 plain error
34 // err is the cause of the error, which encoding/json of Go 1.27 reports.
35 err error
36 // noContext is set for an error which is reported with no path and no offset, as encoding/json of Go 1.27
37 // reports the ones of a json.Number.
38 noContext bool
39 // atStart is set for an error whose offset is the start of the value, as encoding/json of Go 1.27 reports the
40 // ones of an interface value which is kept.
41 atStart bool
42 }
43
44 // jsonKind is the kind of a JSON value, which a decoder decodes and a type error reports.
45 type jsonKind uint8
46
47 const (
48 noValue jsonKind = iota
49 stringValue
50 numberValue
51 boolValue
52 nullValue
53 arrayValue
54 objectValue
55 )
56
57 // kindOf returns the kind of the value which starts with c, or noValue if c starts no value.
58 func kindOf(c byte) jsonKind {
59 switch c {
60 case '"':
61 return stringValue
62 case 't', 'f':
63 return boolValue
64 case 'n':
65 return nullValue
66 case '[':
67 return arrayValue
68 case '{':
69 return objectValue
70 case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
71 return numberValue
72 }
73 return noValue
74 }
75
76 // String returns the description of the kind which the type errors report.
77 func (k jsonKind) String() string {
78 switch k {
79 case stringValue:
80 return "string"
81 case boolValue:
82 return "bool"
83 case nullValue:
84 return "null"
85 case arrayValue:
86 return "array"
87 case objectValue:
88 return "object"
89 case numberValue:
90 return "number"
91 }
92 return ""
93 }
94
95 // isOtherValue reports whether c starts a JSON value of another kind than kind, which is the kind of the values
96 // of a decoder: such a value is of another type, and is a type error, as a byte which starts no value is a
97 // syntax error.
98 func isOtherValue(c byte, kind jsonKind) bool {
99 k := kindOf(c)
100 return k != noValue && k != kind
101 }
102
103 // skipTypeError records the type error of the value at cursor, which is not of typ, if it is the first one, and
104 // returns the position after the value, which is skipped. A syntax error of the value is returned instead.
105 func (ctx *RuntimeContext) skipTypeError(cursor, depth int64, typ reflect.Type) (int64, error) {
106 buf := ctx.Buf
107 cursor = skipWhiteSpace(buf, cursor)
108 end, err := skipValue(buf, cursor, depth)
109 if err != nil {
110 return 0, err
111 }
112 if ctx.typeError == nil {
113 kind := kindOf(buf[cursor])
114 ctx.typeError = &pendingTypeError{typ: typ, start: cursor, end: end, value: kind.String(), kind: kind}
115 }
116 return end, nil
117 }
118
119 // setEmbedded sets the names of the embedded fields which the field is promoted through.
120 func (d *structDecoder) setEmbedded(field *structFieldSet, names []string) {
121 if d.embedded == nil {
122 d.embedded = map[*structFieldSet][]string{}
123 }
124 d.embedded[field] = names
125 }
126
127 // numberTypeError records the type error of the number between start and end, which is not of typ because it
128 // is out of its range or not an integer: the error reports the literal of the number. It is not inlined, so that
129 // the decoders of the numbers keep the size they had.
130 //
131 //go:noinline
132 func (ctx *RuntimeContext) numberTypeError(start, end int64, typ reflect.Type) {
133 if ctx.typeError == nil {
134 ctx.typeError = &pendingTypeError{
135 typ: typ, start: start, end: end, value: "number " + string(ctx.Buf[start:end]), kind: numberValue,
136 literal: true,
137 }
138 }
139 }
140
141 // keyTypeError records the type error of the key of a map entry between start and end, the quotes of which are
142 // included, which is not a number of typ: the error reports the key.
143 func (ctx *RuntimeContext) keyTypeError(start, end int64, key []byte, typ reflect.Type) {
144 if ctx.typeError == nil {
145 ctx.typeError = &pendingTypeError{
146 typ: typ, start: start, end: end, value: "number " + string(key), kind: stringValue, atKey: true,
147 literal: true,
148 }
149 }
150 }
151
152 // HasTypeError reports whether the decoding has a type error, which TypeError returns: the type of the value
153 // is made for it only.
154 func (ctx *RuntimeContext) HasTypeError() bool {
155 return ctx.typeError != nil
156 }
157
158 // DiscardTypeError drops the type error of a decoding which failed with another error.
159 func (ctx *RuntimeContext) DiscardTypeError() {
160 ctx.typeError = nil
161 }
162
163 // TypeError returns the first type error of the decoding of the value which dec decoded from base into a value
164 // of typ, as encoding/json of the Go version reports it, or nil if the decoding had none. Its offset is relative
165 // to offsetBase.
166 func (ctx *RuntimeContext) TypeError(dec Decoder, typ reflect.Type, base, offsetBase int64) error {
167 p := ctx.typeError
168 if p == nil {
169 return nil
170 }
171 ctx.typeError = nil
172 if p.plain != nil {
173 return p.plain
174 }
175 e := newTypeError(p, typeErrorPath(dec, ctx.Buf, base, p), typ)
176 if p.atStart {
177 e.Offset = p.start
178 }
179 if e.Offset != 0 {
180 e.Offset -= offsetBase
181 }
182 return e
183 }
184
185 // typeErrorStep is a step of the path from the root to the value of a type error: a field of a struct, an
186 // element of an array or a slice, or an entry of a map.
187 type typeErrorStep struct {
188 // structName is the name of the struct type of a field.
189 structName string
190 // embedded are the names of the embedded fields which a field is promoted through.
191 embedded []string
192 // name is the name of a field, which is its key, the key of a map entry as it is decoded, or the index of an
193 // element.
194 name string
195 // inputName is the key of a field as the input has it, decoded, which encoding/json of Go 1.27 reports.
196 inputName string
197 // isField is set for a field of a struct.
198 isField bool
199 }
200
201 // typeErrorPath returns the path from the value which dec decodes at cursor to the value of the error, by the
202 // decoders of the values it goes through.
203 func typeErrorPath(dec Decoder, buf []byte, cursor int64, p *pendingTypeError) []typeErrorStep {
204 var path []typeErrorStep
205 for {
206 cursor = skipWhiteSpace(buf, cursor)
207 if cursor >= p.start {
208 return path
209 }
210 switch d := dec.(type) {
211 case *ptrDecoder:
212 dec = d.dec
213 continue
214 case *basicPtrDecoder:
215 dec = d.dec
216 continue
217 case *anonymousFieldDecoder:
218 dec = d.dec
219 continue
220 case *structDecoder:
221 step, child, next, ok := d.typeErrorChild(buf, cursor, p)
222 if !ok {
223 return path
224 }
225 path = append(path, step)
226 if next < 0 {
227 return path
228 }
229 dec, cursor = child, next
230 case *sliceDecoder:
231 i, next, ok := typeErrorElement(buf, cursor, p)
232 if !ok {
233 return path
234 }
235 path = append(path, typeErrorStep{name: strconv.Itoa(i)})
236 dec, cursor = d.valueDecoder, next
237 case *arrayDecoder:
238 i, next, ok := typeErrorElement(buf, cursor, p)
239 if !ok {
240 return path
241 }
242 path = append(path, typeErrorStep{name: strconv.Itoa(i)})
243 dec, cursor = d.valueDecoder, next
244 case *interfaceDecoder:
245 // the objects and the arrays of an interface{}, whose values are decoded by the same decoder
246 var next int64
247 var ok bool
248 switch buf[cursor] {
249 case '{':
250 var key string
251 if key, next, ok = typeErrorEntry(buf, cursor, p); ok {
252 path = append(path, typeErrorStep{name: key})
253 }
254 case '[':
255 var i int
256 if i, next, ok = typeErrorElement(buf, cursor, p); ok {
257 path = append(path, typeErrorStep{name: strconv.Itoa(i)})
258 }
259 }
260 if !ok || next < 0 || d.hasMethods {
261 return path
262 }
263 cursor = next
264 case *mapDecoder:
265 key, next, ok := typeErrorEntry(buf, cursor, p)
266 if !ok {
267 return path
268 }
269 path = append(path, typeErrorStep{name: key})
270 if next < 0 {
271 return path
272 }
273 dec, cursor = d.valueDecoder, next
274 default:
275 return path
276 }
277 }
278 }
279
280 // typeErrorChild returns the step to the field of the object at cursor whose value has the error, the decoder of
281 // the field and the position of its value, which is -1 if the error is of the key. ok is false if the error is in
282 // no field of the object.
283 func (d *structDecoder) typeErrorChild(buf []byte, cursor int64, p *pendingTypeError) (typeErrorStep, Decoder, int64, bool) {
284 if buf[cursor] != '{' {
285 return typeErrorStep{}, nil, 0, false
286 }
287 cursor++
288 for {
289 cursor = skipWhiteSpace(buf, cursor)
290 if buf[cursor] != '"' {
291 return typeErrorStep{}, nil, 0, false
292 }
293 key, info, next, ok := typeErrorKey(buf, cursor)
294 if !ok {
295 return typeErrorStep{}, nil, 0, false
296 }
297 field, _ := d.keys.lookup(key, info)
298 cursor = skipWhiteSpace(buf, next)
299 if buf[cursor] != ':' {
300 return typeErrorStep{}, nil, 0, false
301 }
302 cursor = skipWhiteSpace(buf, cursor+1)
303 end, err := skipValue(buf, cursor, 0)
304 if err != nil {
305 return typeErrorStep{}, nil, 0, false
306 }
307 if p.start >= cursor && p.start < end {
308 if field == nil {
309 return typeErrorStep{}, nil, 0, false
310 }
311 step := typeErrorStep{
312 structName: d.typeName, name: field.key, inputName: string(decodeLiteral(key, info)), isField: true,
313 }
314 if embeddedFieldNames {
315 step.embedded = d.embedded[field]
316 }
317 return step, field.dec, cursor, true
318 }
319 cursor = skipWhiteSpace(buf, end)
320 if buf[cursor] != ',' {
321 return typeErrorStep{}, nil, 0, false
322 }
323 cursor++
324 }
325 }
326
327 // typeErrorElement returns the index of the element of the array at cursor which has the error, and its position.
328 func typeErrorElement(buf []byte, cursor int64, p *pendingTypeError) (int, int64, bool) {
329 if buf[cursor] != '[' {
330 return 0, 0, false
331 }
332 cursor++
333 for i := 0; ; i++ {
334 cursor = skipWhiteSpace(buf, cursor)
335 end, err := skipValue(buf, cursor, 0)
336 if err != nil {
337 return 0, 0, false
338 }
339 if p.start >= cursor && p.start < end {
340 return i, cursor, true
341 }
342 cursor = skipWhiteSpace(buf, end)
343 if buf[cursor] != ',' {
344 return 0, 0, false
345 }
346 cursor++
347 }
348 }
349
350 // typeErrorEntry returns the key of the entry of the object at cursor which has the error, and the position of
351 // its value, which is -1 if the error is of the key.
352 func typeErrorEntry(buf []byte, cursor int64, p *pendingTypeError) (string, int64, bool) {
353 if buf[cursor] != '{' {
354 return "", 0, false
355 }
356 cursor++
357 for {
358 cursor = skipWhiteSpace(buf, cursor)
359 keyStart := cursor
360 key, info, next, ok := typeErrorKey(buf, cursor)
361 if !ok {
362 return "", 0, false
363 }
364 key = decodeLiteral(key, info)
365 if p.atKey && keyStart == p.start {
366 return string(key), -1, true
367 }
368 cursor = skipWhiteSpace(buf, next)
369 if buf[cursor] != ':' {
370 return "", 0, false
371 }
372 cursor = skipWhiteSpace(buf, cursor+1)
373 end, err := skipValue(buf, cursor, 0)
374 if err != nil {
375 return "", 0, false
376 }
377 if p.start >= cursor && p.start < end {
378 return string(key), cursor, true
379 }
380 cursor = skipWhiteSpace(buf, end)
381 if buf[cursor] != ',' {
382 return "", 0, false
383 }
384 cursor++
385 }
386 }
387
388 // typeErrorKey returns a copy of the bytes of the key at cursor as they are in buf, what they have, and the position
389 // after the key: the buffer is not written, as the decoding of a key does to its escapes.
390 func typeErrorKey(buf []byte, cursor int64) ([]byte, stringInfo, int64, bool) {
391 raw, next, info, err := skipStringDecoder.scanString(buf, cursor)
392 if next < 0 {
393 raw, next, info, err = skipStringDecoder.scanStringRest(buf, raw, -next-1, info)
394 }
395 if err != nil || raw == nil {
396 return nil, stringInfo{}, 0, false
397 }
398 return append([]byte(nil), raw...), info, next, true
399 }