khenaidoo | ffe076b | 2019-01-15 16:08:08 -0500 | [diff] [blame^] | 1 | // Copyright (c) 2012-2018 Ugorji Nwoke. All rights reserved. |
| 2 | // Use of this source code is governed by a MIT license found in the LICENSE file. |
| 3 | |
| 4 | package codec |
| 5 | |
| 6 | import ( |
| 7 | "encoding" |
| 8 | "errors" |
| 9 | "fmt" |
| 10 | "io" |
| 11 | "reflect" |
| 12 | "strconv" |
| 13 | "sync" |
| 14 | "time" |
| 15 | ) |
| 16 | |
| 17 | // Some tagging information for error messages. |
| 18 | const ( |
| 19 | msgBadDesc = "unrecognized descriptor byte" |
| 20 | msgDecCannotExpandArr = "cannot expand go array from %v to stream length: %v" |
| 21 | ) |
| 22 | |
| 23 | const decDefSliceCap = 8 |
| 24 | const decDefChanCap = 64 // should be large, as cap cannot be expanded |
| 25 | const decScratchByteArrayLen = cacheLineSize - 8 |
| 26 | |
| 27 | var ( |
| 28 | errstrOnlyMapOrArrayCanDecodeIntoStruct = "only encoded map or array can be decoded into a struct" |
| 29 | errstrCannotDecodeIntoNil = "cannot decode into nil" |
| 30 | |
| 31 | errmsgExpandSliceOverflow = "expand slice: slice overflow" |
| 32 | errmsgExpandSliceCannotChange = "expand slice: cannot change" |
| 33 | |
| 34 | errDecoderNotInitialized = errors.New("Decoder not initialized") |
| 35 | |
| 36 | errDecUnreadByteNothingToRead = errors.New("cannot unread - nothing has been read") |
| 37 | errDecUnreadByteLastByteNotRead = errors.New("cannot unread - last byte has not been read") |
| 38 | errDecUnreadByteUnknown = errors.New("cannot unread - reason unknown") |
| 39 | ) |
| 40 | |
| 41 | // decReader abstracts the reading source, allowing implementations that can |
| 42 | // read from an io.Reader or directly off a byte slice with zero-copying. |
| 43 | type decReader interface { |
| 44 | unreadn1() |
| 45 | |
| 46 | // readx will use the implementation scratch buffer if possible i.e. n < len(scratchbuf), OR |
| 47 | // just return a view of the []byte being decoded from. |
| 48 | // Ensure you call detachZeroCopyBytes later if this needs to be sent outside codec control. |
| 49 | readx(n int) []byte |
| 50 | readb([]byte) |
| 51 | readn1() uint8 |
| 52 | numread() int // number of bytes read |
| 53 | track() |
| 54 | stopTrack() []byte |
| 55 | |
| 56 | // skip will skip any byte that matches, and return the first non-matching byte |
| 57 | skip(accept *bitset256) (token byte) |
| 58 | // readTo will read any byte that matches, stopping once no-longer matching. |
| 59 | readTo(in []byte, accept *bitset256) (out []byte) |
| 60 | // readUntil will read, only stopping once it matches the 'stop' byte. |
| 61 | readUntil(in []byte, stop byte) (out []byte) |
| 62 | } |
| 63 | |
| 64 | type decDriver interface { |
| 65 | // this will check if the next token is a break. |
| 66 | CheckBreak() bool |
| 67 | // Note: TryDecodeAsNil should be careful not to share any temporary []byte with |
| 68 | // the rest of the decDriver. This is because sometimes, we optimize by holding onto |
| 69 | // a transient []byte, and ensuring the only other call we make to the decDriver |
| 70 | // during that time is maybe a TryDecodeAsNil() call. |
| 71 | TryDecodeAsNil() bool |
| 72 | // vt is one of: Bytes, String, Nil, Slice or Map. Return unSet if not known. |
| 73 | ContainerType() (vt valueType) |
| 74 | // IsBuiltinType(rt uintptr) bool |
| 75 | |
| 76 | // DecodeNaked will decode primitives (number, bool, string, []byte) and RawExt. |
| 77 | // For maps and arrays, it will not do the decoding in-band, but will signal |
| 78 | // the decoder, so that is done later, by setting the decNaked.valueType field. |
| 79 | // |
| 80 | // Note: Numbers are decoded as int64, uint64, float64 only (no smaller sized number types). |
| 81 | // for extensions, DecodeNaked must read the tag and the []byte if it exists. |
| 82 | // if the []byte is not read, then kInterfaceNaked will treat it as a Handle |
| 83 | // that stores the subsequent value in-band, and complete reading the RawExt. |
| 84 | // |
| 85 | // extensions should also use readx to decode them, for efficiency. |
| 86 | // kInterface will extract the detached byte slice if it has to pass it outside its realm. |
| 87 | DecodeNaked() |
| 88 | |
| 89 | // Deprecated: use DecodeInt64 and DecodeUint64 instead |
| 90 | // DecodeInt(bitsize uint8) (i int64) |
| 91 | // DecodeUint(bitsize uint8) (ui uint64) |
| 92 | |
| 93 | DecodeInt64() (i int64) |
| 94 | DecodeUint64() (ui uint64) |
| 95 | |
| 96 | DecodeFloat64() (f float64) |
| 97 | DecodeBool() (b bool) |
| 98 | // DecodeString can also decode symbols. |
| 99 | // It looks redundant as DecodeBytes is available. |
| 100 | // However, some codecs (e.g. binc) support symbols and can |
| 101 | // return a pre-stored string value, meaning that it can bypass |
| 102 | // the cost of []byte->string conversion. |
| 103 | DecodeString() (s string) |
| 104 | DecodeStringAsBytes() (v []byte) |
| 105 | |
| 106 | // DecodeBytes may be called directly, without going through reflection. |
| 107 | // Consequently, it must be designed to handle possible nil. |
| 108 | DecodeBytes(bs []byte, zerocopy bool) (bsOut []byte) |
| 109 | // DecodeBytes(bs []byte, isstring, zerocopy bool) (bsOut []byte) |
| 110 | |
| 111 | // decodeExt will decode into a *RawExt or into an extension. |
| 112 | DecodeExt(v interface{}, xtag uint64, ext Ext) (realxtag uint64) |
| 113 | // decodeExt(verifyTag bool, tag byte) (xtag byte, xbs []byte) |
| 114 | |
| 115 | DecodeTime() (t time.Time) |
| 116 | |
| 117 | ReadArrayStart() int |
| 118 | ReadArrayElem() |
| 119 | ReadArrayEnd() |
| 120 | ReadMapStart() int |
| 121 | ReadMapElemKey() |
| 122 | ReadMapElemValue() |
| 123 | ReadMapEnd() |
| 124 | |
| 125 | reset() |
| 126 | uncacheRead() |
| 127 | } |
| 128 | |
| 129 | type decDriverNoopContainerReader struct{} |
| 130 | |
| 131 | func (x decDriverNoopContainerReader) ReadArrayStart() (v int) { return } |
| 132 | func (x decDriverNoopContainerReader) ReadArrayElem() {} |
| 133 | func (x decDriverNoopContainerReader) ReadArrayEnd() {} |
| 134 | func (x decDriverNoopContainerReader) ReadMapStart() (v int) { return } |
| 135 | func (x decDriverNoopContainerReader) ReadMapElemKey() {} |
| 136 | func (x decDriverNoopContainerReader) ReadMapElemValue() {} |
| 137 | func (x decDriverNoopContainerReader) ReadMapEnd() {} |
| 138 | func (x decDriverNoopContainerReader) CheckBreak() (v bool) { return } |
| 139 | |
| 140 | // func (x decNoSeparator) uncacheRead() {} |
| 141 | |
| 142 | // DecodeOptions captures configuration options during decode. |
| 143 | type DecodeOptions struct { |
| 144 | // MapType specifies type to use during schema-less decoding of a map in the stream. |
| 145 | // If nil (unset), we default to map[string]interface{} iff json handle and MapStringAsKey=true, |
| 146 | // else map[interface{}]interface{}. |
| 147 | MapType reflect.Type |
| 148 | |
| 149 | // SliceType specifies type to use during schema-less decoding of an array in the stream. |
| 150 | // If nil (unset), we default to []interface{} for all formats. |
| 151 | SliceType reflect.Type |
| 152 | |
| 153 | // MaxInitLen defines the maxinum initial length that we "make" a collection |
| 154 | // (string, slice, map, chan). If 0 or negative, we default to a sensible value |
| 155 | // based on the size of an element in the collection. |
| 156 | // |
| 157 | // For example, when decoding, a stream may say that it has 2^64 elements. |
| 158 | // We should not auto-matically provision a slice of that size, to prevent Out-Of-Memory crash. |
| 159 | // Instead, we provision up to MaxInitLen, fill that up, and start appending after that. |
| 160 | MaxInitLen int |
| 161 | |
| 162 | // ReaderBufferSize is the size of the buffer used when reading. |
| 163 | // |
| 164 | // if > 0, we use a smart buffer internally for performance purposes. |
| 165 | ReaderBufferSize int |
| 166 | |
| 167 | // If ErrorIfNoField, return an error when decoding a map |
| 168 | // from a codec stream into a struct, and no matching struct field is found. |
| 169 | ErrorIfNoField bool |
| 170 | |
| 171 | // If ErrorIfNoArrayExpand, return an error when decoding a slice/array that cannot be expanded. |
| 172 | // For example, the stream contains an array of 8 items, but you are decoding into a [4]T array, |
| 173 | // or you are decoding into a slice of length 4 which is non-addressable (and so cannot be set). |
| 174 | ErrorIfNoArrayExpand bool |
| 175 | |
| 176 | // If SignedInteger, use the int64 during schema-less decoding of unsigned values (not uint64). |
| 177 | SignedInteger bool |
| 178 | |
| 179 | // MapValueReset controls how we decode into a map value. |
| 180 | // |
| 181 | // By default, we MAY retrieve the mapping for a key, and then decode into that. |
| 182 | // However, especially with big maps, that retrieval may be expensive and unnecessary |
| 183 | // if the stream already contains all that is necessary to recreate the value. |
| 184 | // |
| 185 | // If true, we will never retrieve the previous mapping, |
| 186 | // but rather decode into a new value and set that in the map. |
| 187 | // |
| 188 | // If false, we will retrieve the previous mapping if necessary e.g. |
| 189 | // the previous mapping is a pointer, or is a struct or array with pre-set state, |
| 190 | // or is an interface. |
| 191 | MapValueReset bool |
| 192 | |
| 193 | // SliceElementReset: on decoding a slice, reset the element to a zero value first. |
| 194 | // |
| 195 | // concern: if the slice already contained some garbage, we will decode into that garbage. |
| 196 | SliceElementReset bool |
| 197 | |
| 198 | // InterfaceReset controls how we decode into an interface. |
| 199 | // |
| 200 | // By default, when we see a field that is an interface{...}, |
| 201 | // or a map with interface{...} value, we will attempt decoding into the |
| 202 | // "contained" value. |
| 203 | // |
| 204 | // However, this prevents us from reading a string into an interface{} |
| 205 | // that formerly contained a number. |
| 206 | // |
| 207 | // If true, we will decode into a new "blank" value, and set that in the interface. |
| 208 | // If false, we will decode into whatever is contained in the interface. |
| 209 | InterfaceReset bool |
| 210 | |
| 211 | // InternString controls interning of strings during decoding. |
| 212 | // |
| 213 | // Some handles, e.g. json, typically will read map keys as strings. |
| 214 | // If the set of keys are finite, it may help reduce allocation to |
| 215 | // look them up from a map (than to allocate them afresh). |
| 216 | // |
| 217 | // Note: Handles will be smart when using the intern functionality. |
| 218 | // Every string should not be interned. |
| 219 | // An excellent use-case for interning is struct field names, |
| 220 | // or map keys where key type is string. |
| 221 | InternString bool |
| 222 | |
| 223 | // PreferArrayOverSlice controls whether to decode to an array or a slice. |
| 224 | // |
| 225 | // This only impacts decoding into a nil interface{}. |
| 226 | // Consequently, it has no effect on codecgen. |
| 227 | // |
| 228 | // *Note*: This only applies if using go1.5 and above, |
| 229 | // as it requires reflect.ArrayOf support which was absent before go1.5. |
| 230 | PreferArrayOverSlice bool |
| 231 | |
| 232 | // DeleteOnNilMapValue controls how to decode a nil value in the stream. |
| 233 | // |
| 234 | // If true, we will delete the mapping of the key. |
| 235 | // Else, just set the mapping to the zero value of the type. |
| 236 | DeleteOnNilMapValue bool |
| 237 | } |
| 238 | |
| 239 | // ------------------------------------ |
| 240 | |
| 241 | type bufioDecReader struct { |
| 242 | buf []byte |
| 243 | r io.Reader |
| 244 | |
| 245 | c int // cursor |
| 246 | n int // num read |
| 247 | err error |
| 248 | |
| 249 | tr []byte |
| 250 | trb bool |
| 251 | b [4]byte |
| 252 | } |
| 253 | |
| 254 | func (z *bufioDecReader) reset(r io.Reader) { |
| 255 | z.r, z.c, z.n, z.err, z.trb = r, 0, 0, nil, false |
| 256 | if z.tr != nil { |
| 257 | z.tr = z.tr[:0] |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | func (z *bufioDecReader) Read(p []byte) (n int, err error) { |
| 262 | if z.err != nil { |
| 263 | return 0, z.err |
| 264 | } |
| 265 | p0 := p |
| 266 | n = copy(p, z.buf[z.c:]) |
| 267 | z.c += n |
| 268 | if z.c == len(z.buf) { |
| 269 | z.c = 0 |
| 270 | } |
| 271 | z.n += n |
| 272 | if len(p) == n { |
| 273 | if z.c == 0 { |
| 274 | z.buf = z.buf[:1] |
| 275 | z.buf[0] = p[len(p)-1] |
| 276 | z.c = 1 |
| 277 | } |
| 278 | if z.trb { |
| 279 | z.tr = append(z.tr, p0[:n]...) |
| 280 | } |
| 281 | return |
| 282 | } |
| 283 | p = p[n:] |
| 284 | var n2 int |
| 285 | // if we are here, then z.buf is all read |
| 286 | if len(p) > len(z.buf) { |
| 287 | n2, err = decReadFull(z.r, p) |
| 288 | n += n2 |
| 289 | z.n += n2 |
| 290 | z.err = err |
| 291 | // don't return EOF if some bytes were read. keep for next time. |
| 292 | if n > 0 && err == io.EOF { |
| 293 | err = nil |
| 294 | } |
| 295 | // always keep last byte in z.buf |
| 296 | z.buf = z.buf[:1] |
| 297 | z.buf[0] = p[len(p)-1] |
| 298 | z.c = 1 |
| 299 | if z.trb { |
| 300 | z.tr = append(z.tr, p0[:n]...) |
| 301 | } |
| 302 | return |
| 303 | } |
| 304 | // z.c is now 0, and len(p) <= len(z.buf) |
| 305 | for len(p) > 0 && z.err == nil { |
| 306 | // println("len(p) loop starting ... ") |
| 307 | z.c = 0 |
| 308 | z.buf = z.buf[0:cap(z.buf)] |
| 309 | n2, err = z.r.Read(z.buf) |
| 310 | if n2 > 0 { |
| 311 | if err == io.EOF { |
| 312 | err = nil |
| 313 | } |
| 314 | z.buf = z.buf[:n2] |
| 315 | n2 = copy(p, z.buf) |
| 316 | z.c = n2 |
| 317 | n += n2 |
| 318 | z.n += n2 |
| 319 | p = p[n2:] |
| 320 | } |
| 321 | z.err = err |
| 322 | // println("... len(p) loop done") |
| 323 | } |
| 324 | if z.c == 0 { |
| 325 | z.buf = z.buf[:1] |
| 326 | z.buf[0] = p[len(p)-1] |
| 327 | z.c = 1 |
| 328 | } |
| 329 | if z.trb { |
| 330 | z.tr = append(z.tr, p0[:n]...) |
| 331 | } |
| 332 | return |
| 333 | } |
| 334 | |
| 335 | func (z *bufioDecReader) ReadByte() (b byte, err error) { |
| 336 | z.b[0] = 0 |
| 337 | _, err = z.Read(z.b[:1]) |
| 338 | b = z.b[0] |
| 339 | return |
| 340 | } |
| 341 | |
| 342 | func (z *bufioDecReader) UnreadByte() (err error) { |
| 343 | if z.err != nil { |
| 344 | return z.err |
| 345 | } |
| 346 | if z.c > 0 { |
| 347 | z.c-- |
| 348 | z.n-- |
| 349 | if z.trb { |
| 350 | z.tr = z.tr[:len(z.tr)-1] |
| 351 | } |
| 352 | return |
| 353 | } |
| 354 | return errDecUnreadByteNothingToRead |
| 355 | } |
| 356 | |
| 357 | func (z *bufioDecReader) numread() int { |
| 358 | return z.n |
| 359 | } |
| 360 | |
| 361 | func (z *bufioDecReader) readx(n int) (bs []byte) { |
| 362 | if n <= 0 || z.err != nil { |
| 363 | return |
| 364 | } |
| 365 | if z.c+n <= len(z.buf) { |
| 366 | bs = z.buf[z.c : z.c+n] |
| 367 | z.n += n |
| 368 | z.c += n |
| 369 | if z.trb { |
| 370 | z.tr = append(z.tr, bs...) |
| 371 | } |
| 372 | return |
| 373 | } |
| 374 | bs = make([]byte, n) |
| 375 | _, err := z.Read(bs) |
| 376 | if err != nil { |
| 377 | panic(err) |
| 378 | } |
| 379 | return |
| 380 | } |
| 381 | |
| 382 | func (z *bufioDecReader) readb(bs []byte) { |
| 383 | _, err := z.Read(bs) |
| 384 | if err != nil { |
| 385 | panic(err) |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | // func (z *bufioDecReader) readn1eof() (b uint8, eof bool) { |
| 390 | // b, err := z.ReadByte() |
| 391 | // if err != nil { |
| 392 | // if err == io.EOF { |
| 393 | // eof = true |
| 394 | // } else { |
| 395 | // panic(err) |
| 396 | // } |
| 397 | // } |
| 398 | // return |
| 399 | // } |
| 400 | |
| 401 | func (z *bufioDecReader) readn1() (b uint8) { |
| 402 | b, err := z.ReadByte() |
| 403 | if err != nil { |
| 404 | panic(err) |
| 405 | } |
| 406 | return |
| 407 | } |
| 408 | |
| 409 | func (z *bufioDecReader) search(in []byte, accept *bitset256, stop, flag uint8) (token byte, out []byte) { |
| 410 | // flag: 1 (skip), 2 (readTo), 4 (readUntil) |
| 411 | if flag == 4 { |
| 412 | for i := z.c; i < len(z.buf); i++ { |
| 413 | if z.buf[i] == stop { |
| 414 | token = z.buf[i] |
| 415 | z.n = z.n + (i - z.c) - 1 |
| 416 | i++ |
| 417 | out = z.buf[z.c:i] |
| 418 | if z.trb { |
| 419 | z.tr = append(z.tr, z.buf[z.c:i]...) |
| 420 | } |
| 421 | z.c = i |
| 422 | return |
| 423 | } |
| 424 | } |
| 425 | } else { |
| 426 | for i := z.c; i < len(z.buf); i++ { |
| 427 | if !accept.isset(z.buf[i]) { |
| 428 | token = z.buf[i] |
| 429 | z.n = z.n + (i - z.c) - 1 |
| 430 | if flag == 1 { |
| 431 | i++ |
| 432 | } else { |
| 433 | out = z.buf[z.c:i] |
| 434 | } |
| 435 | if z.trb { |
| 436 | z.tr = append(z.tr, z.buf[z.c:i]...) |
| 437 | } |
| 438 | z.c = i |
| 439 | return |
| 440 | } |
| 441 | } |
| 442 | } |
| 443 | z.n += len(z.buf) - z.c |
| 444 | if flag != 1 { |
| 445 | out = append(in, z.buf[z.c:]...) |
| 446 | } |
| 447 | if z.trb { |
| 448 | z.tr = append(z.tr, z.buf[z.c:]...) |
| 449 | } |
| 450 | var n2 int |
| 451 | if z.err != nil { |
| 452 | return |
| 453 | } |
| 454 | for { |
| 455 | z.c = 0 |
| 456 | z.buf = z.buf[0:cap(z.buf)] |
| 457 | n2, z.err = z.r.Read(z.buf) |
| 458 | if n2 > 0 && z.err != nil { |
| 459 | z.err = nil |
| 460 | } |
| 461 | z.buf = z.buf[:n2] |
| 462 | if flag == 4 { |
| 463 | for i := 0; i < n2; i++ { |
| 464 | if z.buf[i] == stop { |
| 465 | token = z.buf[i] |
| 466 | z.n += i - 1 |
| 467 | i++ |
| 468 | out = append(out, z.buf[z.c:i]...) |
| 469 | if z.trb { |
| 470 | z.tr = append(z.tr, z.buf[z.c:i]...) |
| 471 | } |
| 472 | z.c = i |
| 473 | return |
| 474 | } |
| 475 | } |
| 476 | } else { |
| 477 | for i := 0; i < n2; i++ { |
| 478 | if !accept.isset(z.buf[i]) { |
| 479 | token = z.buf[i] |
| 480 | z.n += i - 1 |
| 481 | if flag == 1 { |
| 482 | i++ |
| 483 | } |
| 484 | if flag != 1 { |
| 485 | out = append(out, z.buf[z.c:i]...) |
| 486 | } |
| 487 | if z.trb { |
| 488 | z.tr = append(z.tr, z.buf[z.c:i]...) |
| 489 | } |
| 490 | z.c = i |
| 491 | return |
| 492 | } |
| 493 | } |
| 494 | } |
| 495 | if flag != 1 { |
| 496 | out = append(out, z.buf[:n2]...) |
| 497 | } |
| 498 | z.n += n2 |
| 499 | if z.err != nil { |
| 500 | return |
| 501 | } |
| 502 | if z.trb { |
| 503 | z.tr = append(z.tr, z.buf[:n2]...) |
| 504 | } |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | func (z *bufioDecReader) skip(accept *bitset256) (token byte) { |
| 509 | token, _ = z.search(nil, accept, 0, 1) |
| 510 | return |
| 511 | } |
| 512 | |
| 513 | func (z *bufioDecReader) readTo(in []byte, accept *bitset256) (out []byte) { |
| 514 | _, out = z.search(in, accept, 0, 2) |
| 515 | return |
| 516 | } |
| 517 | |
| 518 | func (z *bufioDecReader) readUntil(in []byte, stop byte) (out []byte) { |
| 519 | _, out = z.search(in, nil, stop, 4) |
| 520 | return |
| 521 | } |
| 522 | |
| 523 | func (z *bufioDecReader) unreadn1() { |
| 524 | err := z.UnreadByte() |
| 525 | if err != nil { |
| 526 | panic(err) |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | func (z *bufioDecReader) track() { |
| 531 | if z.tr != nil { |
| 532 | z.tr = z.tr[:0] |
| 533 | } |
| 534 | z.trb = true |
| 535 | } |
| 536 | |
| 537 | func (z *bufioDecReader) stopTrack() (bs []byte) { |
| 538 | z.trb = false |
| 539 | return z.tr |
| 540 | } |
| 541 | |
| 542 | // ioDecReader is a decReader that reads off an io.Reader. |
| 543 | // |
| 544 | // It also has a fallback implementation of ByteScanner if needed. |
| 545 | type ioDecReader struct { |
| 546 | r io.Reader // the reader passed in |
| 547 | |
| 548 | rr io.Reader |
| 549 | br io.ByteScanner |
| 550 | |
| 551 | l byte // last byte |
| 552 | ls byte // last byte status. 0: init-canDoNothing, 1: canRead, 2: canUnread |
| 553 | trb bool // tracking bytes turned on |
| 554 | _ bool |
| 555 | b [4]byte // tiny buffer for reading single bytes |
| 556 | |
| 557 | x [scratchByteArrayLen]byte // for: get struct field name, swallow valueTypeBytes, etc |
| 558 | n int // num read |
| 559 | tr []byte // tracking bytes read |
| 560 | } |
| 561 | |
| 562 | func (z *ioDecReader) reset(r io.Reader) { |
| 563 | z.r = r |
| 564 | z.rr = r |
| 565 | z.l, z.ls, z.n, z.trb = 0, 0, 0, false |
| 566 | if z.tr != nil { |
| 567 | z.tr = z.tr[:0] |
| 568 | } |
| 569 | var ok bool |
| 570 | if z.br, ok = r.(io.ByteScanner); !ok { |
| 571 | z.br = z |
| 572 | z.rr = z |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | func (z *ioDecReader) Read(p []byte) (n int, err error) { |
| 577 | if len(p) == 0 { |
| 578 | return |
| 579 | } |
| 580 | var firstByte bool |
| 581 | if z.ls == 1 { |
| 582 | z.ls = 2 |
| 583 | p[0] = z.l |
| 584 | if len(p) == 1 { |
| 585 | n = 1 |
| 586 | return |
| 587 | } |
| 588 | firstByte = true |
| 589 | p = p[1:] |
| 590 | } |
| 591 | n, err = z.r.Read(p) |
| 592 | if n > 0 { |
| 593 | if err == io.EOF && n == len(p) { |
| 594 | err = nil // read was successful, so postpone EOF (till next time) |
| 595 | } |
| 596 | z.l = p[n-1] |
| 597 | z.ls = 2 |
| 598 | } |
| 599 | if firstByte { |
| 600 | n++ |
| 601 | } |
| 602 | return |
| 603 | } |
| 604 | |
| 605 | func (z *ioDecReader) ReadByte() (c byte, err error) { |
| 606 | n, err := z.Read(z.b[:1]) |
| 607 | if n == 1 { |
| 608 | c = z.b[0] |
| 609 | if err == io.EOF { |
| 610 | err = nil // read was successful, so postpone EOF (till next time) |
| 611 | } |
| 612 | } |
| 613 | return |
| 614 | } |
| 615 | |
| 616 | func (z *ioDecReader) UnreadByte() (err error) { |
| 617 | switch z.ls { |
| 618 | case 2: |
| 619 | z.ls = 1 |
| 620 | case 0: |
| 621 | err = errDecUnreadByteNothingToRead |
| 622 | case 1: |
| 623 | err = errDecUnreadByteLastByteNotRead |
| 624 | default: |
| 625 | err = errDecUnreadByteUnknown |
| 626 | } |
| 627 | return |
| 628 | } |
| 629 | |
| 630 | func (z *ioDecReader) numread() int { |
| 631 | return z.n |
| 632 | } |
| 633 | |
| 634 | func (z *ioDecReader) readx(n int) (bs []byte) { |
| 635 | if n <= 0 { |
| 636 | return |
| 637 | } |
| 638 | if n < len(z.x) { |
| 639 | bs = z.x[:n] |
| 640 | } else { |
| 641 | bs = make([]byte, n) |
| 642 | } |
| 643 | if _, err := decReadFull(z.rr, bs); err != nil { |
| 644 | panic(err) |
| 645 | } |
| 646 | z.n += len(bs) |
| 647 | if z.trb { |
| 648 | z.tr = append(z.tr, bs...) |
| 649 | } |
| 650 | return |
| 651 | } |
| 652 | |
| 653 | func (z *ioDecReader) readb(bs []byte) { |
| 654 | // if len(bs) == 0 { |
| 655 | // return |
| 656 | // } |
| 657 | if _, err := decReadFull(z.rr, bs); err != nil { |
| 658 | panic(err) |
| 659 | } |
| 660 | z.n += len(bs) |
| 661 | if z.trb { |
| 662 | z.tr = append(z.tr, bs...) |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | func (z *ioDecReader) readn1eof() (b uint8, eof bool) { |
| 667 | b, err := z.br.ReadByte() |
| 668 | if err == nil { |
| 669 | z.n++ |
| 670 | if z.trb { |
| 671 | z.tr = append(z.tr, b) |
| 672 | } |
| 673 | } else if err == io.EOF { |
| 674 | eof = true |
| 675 | } else { |
| 676 | panic(err) |
| 677 | } |
| 678 | return |
| 679 | } |
| 680 | |
| 681 | func (z *ioDecReader) readn1() (b uint8) { |
| 682 | var err error |
| 683 | if b, err = z.br.ReadByte(); err == nil { |
| 684 | z.n++ |
| 685 | if z.trb { |
| 686 | z.tr = append(z.tr, b) |
| 687 | } |
| 688 | return |
| 689 | } |
| 690 | panic(err) |
| 691 | } |
| 692 | |
| 693 | func (z *ioDecReader) skip(accept *bitset256) (token byte) { |
| 694 | for { |
| 695 | var eof bool |
| 696 | token, eof = z.readn1eof() |
| 697 | if eof { |
| 698 | return |
| 699 | } |
| 700 | if accept.isset(token) { |
| 701 | continue |
| 702 | } |
| 703 | return |
| 704 | } |
| 705 | } |
| 706 | |
| 707 | func (z *ioDecReader) readTo(in []byte, accept *bitset256) (out []byte) { |
| 708 | out = in |
| 709 | for { |
| 710 | token, eof := z.readn1eof() |
| 711 | if eof { |
| 712 | return |
| 713 | } |
| 714 | if accept.isset(token) { |
| 715 | out = append(out, token) |
| 716 | } else { |
| 717 | z.unreadn1() |
| 718 | return |
| 719 | } |
| 720 | } |
| 721 | } |
| 722 | |
| 723 | func (z *ioDecReader) readUntil(in []byte, stop byte) (out []byte) { |
| 724 | out = in |
| 725 | for { |
| 726 | token, eof := z.readn1eof() |
| 727 | if eof { |
| 728 | panic(io.EOF) |
| 729 | } |
| 730 | out = append(out, token) |
| 731 | if token == stop { |
| 732 | return |
| 733 | } |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | func (z *ioDecReader) unreadn1() { |
| 738 | err := z.br.UnreadByte() |
| 739 | if err != nil { |
| 740 | panic(err) |
| 741 | } |
| 742 | z.n-- |
| 743 | if z.trb { |
| 744 | if l := len(z.tr) - 1; l >= 0 { |
| 745 | z.tr = z.tr[:l] |
| 746 | } |
| 747 | } |
| 748 | } |
| 749 | |
| 750 | func (z *ioDecReader) track() { |
| 751 | if z.tr != nil { |
| 752 | z.tr = z.tr[:0] |
| 753 | } |
| 754 | z.trb = true |
| 755 | } |
| 756 | |
| 757 | func (z *ioDecReader) stopTrack() (bs []byte) { |
| 758 | z.trb = false |
| 759 | return z.tr |
| 760 | } |
| 761 | |
| 762 | // ------------------------------------ |
| 763 | |
| 764 | var errBytesDecReaderCannotUnread = errors.New("cannot unread last byte read") |
| 765 | |
| 766 | // bytesDecReader is a decReader that reads off a byte slice with zero copying |
| 767 | type bytesDecReader struct { |
| 768 | b []byte // data |
| 769 | c int // cursor |
| 770 | a int // available |
| 771 | t int // track start |
| 772 | } |
| 773 | |
| 774 | func (z *bytesDecReader) reset(in []byte) { |
| 775 | z.b = in |
| 776 | z.a = len(in) |
| 777 | z.c = 0 |
| 778 | z.t = 0 |
| 779 | } |
| 780 | |
| 781 | func (z *bytesDecReader) numread() int { |
| 782 | return z.c |
| 783 | } |
| 784 | |
| 785 | func (z *bytesDecReader) unreadn1() { |
| 786 | if z.c == 0 || len(z.b) == 0 { |
| 787 | panic(errBytesDecReaderCannotUnread) |
| 788 | } |
| 789 | z.c-- |
| 790 | z.a++ |
| 791 | return |
| 792 | } |
| 793 | |
| 794 | func (z *bytesDecReader) readx(n int) (bs []byte) { |
| 795 | // slicing from a non-constant start position is more expensive, |
| 796 | // as more computation is required to decipher the pointer start position. |
| 797 | // However, we do it only once, and it's better than reslicing both z.b and return value. |
| 798 | |
| 799 | if n <= 0 { |
| 800 | } else if z.a == 0 { |
| 801 | panic(io.EOF) |
| 802 | } else if n > z.a { |
| 803 | panic(io.ErrUnexpectedEOF) |
| 804 | } else { |
| 805 | c0 := z.c |
| 806 | z.c = c0 + n |
| 807 | z.a = z.a - n |
| 808 | bs = z.b[c0:z.c] |
| 809 | } |
| 810 | return |
| 811 | } |
| 812 | |
| 813 | func (z *bytesDecReader) readb(bs []byte) { |
| 814 | copy(bs, z.readx(len(bs))) |
| 815 | } |
| 816 | |
| 817 | func (z *bytesDecReader) readn1() (v uint8) { |
| 818 | if z.a == 0 { |
| 819 | panic(io.EOF) |
| 820 | } |
| 821 | v = z.b[z.c] |
| 822 | z.c++ |
| 823 | z.a-- |
| 824 | return |
| 825 | } |
| 826 | |
| 827 | // func (z *bytesDecReader) readn1eof() (v uint8, eof bool) { |
| 828 | // if z.a == 0 { |
| 829 | // eof = true |
| 830 | // return |
| 831 | // } |
| 832 | // v = z.b[z.c] |
| 833 | // z.c++ |
| 834 | // z.a-- |
| 835 | // return |
| 836 | // } |
| 837 | |
| 838 | func (z *bytesDecReader) skip(accept *bitset256) (token byte) { |
| 839 | if z.a == 0 { |
| 840 | return |
| 841 | } |
| 842 | blen := len(z.b) |
| 843 | for i := z.c; i < blen; i++ { |
| 844 | if !accept.isset(z.b[i]) { |
| 845 | token = z.b[i] |
| 846 | i++ |
| 847 | z.a -= (i - z.c) |
| 848 | z.c = i |
| 849 | return |
| 850 | } |
| 851 | } |
| 852 | z.a, z.c = 0, blen |
| 853 | return |
| 854 | } |
| 855 | |
| 856 | func (z *bytesDecReader) readTo(_ []byte, accept *bitset256) (out []byte) { |
| 857 | if z.a == 0 { |
| 858 | return |
| 859 | } |
| 860 | blen := len(z.b) |
| 861 | for i := z.c; i < blen; i++ { |
| 862 | if !accept.isset(z.b[i]) { |
| 863 | out = z.b[z.c:i] |
| 864 | z.a -= (i - z.c) |
| 865 | z.c = i |
| 866 | return |
| 867 | } |
| 868 | } |
| 869 | out = z.b[z.c:] |
| 870 | z.a, z.c = 0, blen |
| 871 | return |
| 872 | } |
| 873 | |
| 874 | func (z *bytesDecReader) readUntil(_ []byte, stop byte) (out []byte) { |
| 875 | if z.a == 0 { |
| 876 | panic(io.EOF) |
| 877 | } |
| 878 | blen := len(z.b) |
| 879 | for i := z.c; i < blen; i++ { |
| 880 | if z.b[i] == stop { |
| 881 | i++ |
| 882 | out = z.b[z.c:i] |
| 883 | z.a -= (i - z.c) |
| 884 | z.c = i |
| 885 | return |
| 886 | } |
| 887 | } |
| 888 | z.a, z.c = 0, blen |
| 889 | panic(io.EOF) |
| 890 | } |
| 891 | |
| 892 | func (z *bytesDecReader) track() { |
| 893 | z.t = z.c |
| 894 | } |
| 895 | |
| 896 | func (z *bytesDecReader) stopTrack() (bs []byte) { |
| 897 | return z.b[z.t:z.c] |
| 898 | } |
| 899 | |
| 900 | // ---------------------------------------- |
| 901 | |
| 902 | // func (d *Decoder) builtin(f *codecFnInfo, rv reflect.Value) { |
| 903 | // d.d.DecodeBuiltin(f.ti.rtid, rv2i(rv)) |
| 904 | // } |
| 905 | |
| 906 | func (d *Decoder) rawExt(f *codecFnInfo, rv reflect.Value) { |
| 907 | d.d.DecodeExt(rv2i(rv), 0, nil) |
| 908 | } |
| 909 | |
| 910 | func (d *Decoder) ext(f *codecFnInfo, rv reflect.Value) { |
| 911 | d.d.DecodeExt(rv2i(rv), f.xfTag, f.xfFn) |
| 912 | } |
| 913 | |
| 914 | func (d *Decoder) selferUnmarshal(f *codecFnInfo, rv reflect.Value) { |
| 915 | rv2i(rv).(Selfer).CodecDecodeSelf(d) |
| 916 | } |
| 917 | |
| 918 | func (d *Decoder) binaryUnmarshal(f *codecFnInfo, rv reflect.Value) { |
| 919 | bm := rv2i(rv).(encoding.BinaryUnmarshaler) |
| 920 | xbs := d.d.DecodeBytes(nil, true) |
| 921 | if fnerr := bm.UnmarshalBinary(xbs); fnerr != nil { |
| 922 | panic(fnerr) |
| 923 | } |
| 924 | } |
| 925 | |
| 926 | func (d *Decoder) textUnmarshal(f *codecFnInfo, rv reflect.Value) { |
| 927 | tm := rv2i(rv).(encoding.TextUnmarshaler) |
| 928 | fnerr := tm.UnmarshalText(d.d.DecodeStringAsBytes()) |
| 929 | if fnerr != nil { |
| 930 | panic(fnerr) |
| 931 | } |
| 932 | } |
| 933 | |
| 934 | func (d *Decoder) jsonUnmarshal(f *codecFnInfo, rv reflect.Value) { |
| 935 | tm := rv2i(rv).(jsonUnmarshaler) |
| 936 | // bs := d.d.DecodeBytes(d.b[:], true, true) |
| 937 | // grab the bytes to be read, as UnmarshalJSON needs the full JSON so as to unmarshal it itself. |
| 938 | fnerr := tm.UnmarshalJSON(d.nextValueBytes()) |
| 939 | if fnerr != nil { |
| 940 | panic(fnerr) |
| 941 | } |
| 942 | } |
| 943 | |
| 944 | func (d *Decoder) kErr(f *codecFnInfo, rv reflect.Value) { |
| 945 | d.errorf("no decoding function defined for kind %v", rv.Kind()) |
| 946 | } |
| 947 | |
| 948 | // var kIntfCtr uint64 |
| 949 | |
| 950 | func (d *Decoder) kInterfaceNaked(f *codecFnInfo) (rvn reflect.Value) { |
| 951 | // nil interface: |
| 952 | // use some hieristics to decode it appropriately |
| 953 | // based on the detected next value in the stream. |
| 954 | n := d.naked() |
| 955 | d.d.DecodeNaked() |
| 956 | if n.v == valueTypeNil { |
| 957 | return |
| 958 | } |
| 959 | // We cannot decode non-nil stream value into nil interface with methods (e.g. io.Reader). |
| 960 | if f.ti.numMeth > 0 { |
| 961 | d.errorf("cannot decode non-nil codec value into nil %v (%v methods)", f.ti.rt, f.ti.numMeth) |
| 962 | return |
| 963 | } |
| 964 | // var useRvn bool |
| 965 | switch n.v { |
| 966 | case valueTypeMap: |
| 967 | // if json, default to a map type with string keys |
| 968 | mtid := d.mtid |
| 969 | if mtid == 0 { |
| 970 | if d.jsms { |
| 971 | mtid = mapStrIntfTypId |
| 972 | } else { |
| 973 | mtid = mapIntfIntfTypId |
| 974 | } |
| 975 | } |
| 976 | if mtid == mapIntfIntfTypId { |
| 977 | n.initContainers() |
| 978 | if n.lm < arrayCacheLen { |
| 979 | n.ma[n.lm] = nil |
| 980 | rvn = n.rma[n.lm] |
| 981 | n.lm++ |
| 982 | d.decode(&n.ma[n.lm-1]) |
| 983 | n.lm-- |
| 984 | } else { |
| 985 | var v2 map[interface{}]interface{} |
| 986 | d.decode(&v2) |
| 987 | rvn = reflect.ValueOf(&v2).Elem() |
| 988 | } |
| 989 | } else if mtid == mapStrIntfTypId { // for json performance |
| 990 | n.initContainers() |
| 991 | if n.ln < arrayCacheLen { |
| 992 | n.na[n.ln] = nil |
| 993 | rvn = n.rna[n.ln] |
| 994 | n.ln++ |
| 995 | d.decode(&n.na[n.ln-1]) |
| 996 | n.ln-- |
| 997 | } else { |
| 998 | var v2 map[string]interface{} |
| 999 | d.decode(&v2) |
| 1000 | rvn = reflect.ValueOf(&v2).Elem() |
| 1001 | } |
| 1002 | } else { |
| 1003 | if d.mtr { |
| 1004 | rvn = reflect.New(d.h.MapType) |
| 1005 | d.decode(rv2i(rvn)) |
| 1006 | rvn = rvn.Elem() |
| 1007 | } else { |
| 1008 | rvn = reflect.New(d.h.MapType).Elem() |
| 1009 | d.decodeValue(rvn, nil, true) |
| 1010 | } |
| 1011 | } |
| 1012 | case valueTypeArray: |
| 1013 | if d.stid == 0 || d.stid == intfSliceTypId { |
| 1014 | n.initContainers() |
| 1015 | if n.ls < arrayCacheLen { |
| 1016 | n.sa[n.ls] = nil |
| 1017 | rvn = n.rsa[n.ls] |
| 1018 | n.ls++ |
| 1019 | d.decode(&n.sa[n.ls-1]) |
| 1020 | n.ls-- |
| 1021 | } else { |
| 1022 | var v2 []interface{} |
| 1023 | d.decode(&v2) |
| 1024 | rvn = reflect.ValueOf(&v2).Elem() |
| 1025 | } |
| 1026 | if reflectArrayOfSupported && d.stid == 0 && d.h.PreferArrayOverSlice { |
| 1027 | rvn2 := reflect.New(reflectArrayOf(rvn.Len(), intfTyp)).Elem() |
| 1028 | reflect.Copy(rvn2, rvn) |
| 1029 | rvn = rvn2 |
| 1030 | } |
| 1031 | } else { |
| 1032 | if d.str { |
| 1033 | rvn = reflect.New(d.h.SliceType) |
| 1034 | d.decode(rv2i(rvn)) |
| 1035 | rvn = rvn.Elem() |
| 1036 | } else { |
| 1037 | rvn = reflect.New(d.h.SliceType).Elem() |
| 1038 | d.decodeValue(rvn, nil, true) |
| 1039 | } |
| 1040 | } |
| 1041 | case valueTypeExt: |
| 1042 | var v interface{} |
| 1043 | tag, bytes := n.u, n.l // calling decode below might taint the values |
| 1044 | if bytes == nil { |
| 1045 | n.initContainers() |
| 1046 | if n.li < arrayCacheLen { |
| 1047 | n.ia[n.li] = nil |
| 1048 | n.li++ |
| 1049 | d.decode(&n.ia[n.li-1]) |
| 1050 | // v = *(&n.ia[l]) |
| 1051 | n.li-- |
| 1052 | v = n.ia[n.li] |
| 1053 | n.ia[n.li] = nil |
| 1054 | } else { |
| 1055 | d.decode(&v) |
| 1056 | } |
| 1057 | } |
| 1058 | bfn := d.h.getExtForTag(tag) |
| 1059 | if bfn == nil { |
| 1060 | var re RawExt |
| 1061 | re.Tag = tag |
| 1062 | re.Data = detachZeroCopyBytes(d.bytes, nil, bytes) |
| 1063 | re.Value = v |
| 1064 | rvn = reflect.ValueOf(&re).Elem() |
| 1065 | } else { |
| 1066 | rvnA := reflect.New(bfn.rt) |
| 1067 | if bytes != nil { |
| 1068 | bfn.ext.ReadExt(rv2i(rvnA), bytes) |
| 1069 | } else { |
| 1070 | bfn.ext.UpdateExt(rv2i(rvnA), v) |
| 1071 | } |
| 1072 | rvn = rvnA.Elem() |
| 1073 | } |
| 1074 | case valueTypeNil: |
| 1075 | // no-op |
| 1076 | case valueTypeInt: |
| 1077 | rvn = n.ri |
| 1078 | case valueTypeUint: |
| 1079 | rvn = n.ru |
| 1080 | case valueTypeFloat: |
| 1081 | rvn = n.rf |
| 1082 | case valueTypeBool: |
| 1083 | rvn = n.rb |
| 1084 | case valueTypeString, valueTypeSymbol: |
| 1085 | rvn = n.rs |
| 1086 | case valueTypeBytes: |
| 1087 | rvn = n.rl |
| 1088 | case valueTypeTime: |
| 1089 | rvn = n.rt |
| 1090 | default: |
| 1091 | panicv.errorf("kInterfaceNaked: unexpected valueType: %d", n.v) |
| 1092 | } |
| 1093 | return |
| 1094 | } |
| 1095 | |
| 1096 | func (d *Decoder) kInterface(f *codecFnInfo, rv reflect.Value) { |
| 1097 | // Note: |
| 1098 | // A consequence of how kInterface works, is that |
| 1099 | // if an interface already contains something, we try |
| 1100 | // to decode into what was there before. |
| 1101 | // We do not replace with a generic value (as got from decodeNaked). |
| 1102 | |
| 1103 | // every interface passed here MUST be settable. |
| 1104 | var rvn reflect.Value |
| 1105 | if rv.IsNil() || d.h.InterfaceReset { |
| 1106 | // check if mapping to a type: if so, initialize it and move on |
| 1107 | rvn = d.h.intf2impl(f.ti.rtid) |
| 1108 | if rvn.IsValid() { |
| 1109 | rv.Set(rvn) |
| 1110 | } else { |
| 1111 | rvn = d.kInterfaceNaked(f) |
| 1112 | if rvn.IsValid() { |
| 1113 | rv.Set(rvn) |
| 1114 | } else if d.h.InterfaceReset { |
| 1115 | // reset to zero value based on current type in there. |
| 1116 | rv.Set(reflect.Zero(rv.Elem().Type())) |
| 1117 | } |
| 1118 | return |
| 1119 | } |
| 1120 | } else { |
| 1121 | // now we have a non-nil interface value, meaning it contains a type |
| 1122 | rvn = rv.Elem() |
| 1123 | } |
| 1124 | if d.d.TryDecodeAsNil() { |
| 1125 | rv.Set(reflect.Zero(rvn.Type())) |
| 1126 | return |
| 1127 | } |
| 1128 | |
| 1129 | // Note: interface{} is settable, but underlying type may not be. |
| 1130 | // Consequently, we MAY have to create a decodable value out of the underlying value, |
| 1131 | // decode into it, and reset the interface itself. |
| 1132 | // fmt.Printf(">>>> kInterface: rvn type: %v, rv type: %v\n", rvn.Type(), rv.Type()) |
| 1133 | |
| 1134 | rvn2, canDecode := isDecodeable(rvn) |
| 1135 | if canDecode { |
| 1136 | d.decodeValue(rvn2, nil, true) |
| 1137 | return |
| 1138 | } |
| 1139 | |
| 1140 | rvn2 = reflect.New(rvn.Type()).Elem() |
| 1141 | rvn2.Set(rvn) |
| 1142 | d.decodeValue(rvn2, nil, true) |
| 1143 | rv.Set(rvn2) |
| 1144 | } |
| 1145 | |
| 1146 | func decStructFieldKey(dd decDriver, keyType valueType, b *[decScratchByteArrayLen]byte) (rvkencname []byte) { |
| 1147 | // use if-else-if, not switch (which compiles to binary-search) |
| 1148 | // since keyType is typically valueTypeString, branch prediction is pretty good. |
| 1149 | |
| 1150 | if keyType == valueTypeString { |
| 1151 | rvkencname = dd.DecodeStringAsBytes() |
| 1152 | } else if keyType == valueTypeInt { |
| 1153 | rvkencname = strconv.AppendInt(b[:0], dd.DecodeInt64(), 10) |
| 1154 | } else if keyType == valueTypeUint { |
| 1155 | rvkencname = strconv.AppendUint(b[:0], dd.DecodeUint64(), 10) |
| 1156 | } else if keyType == valueTypeFloat { |
| 1157 | rvkencname = strconv.AppendFloat(b[:0], dd.DecodeFloat64(), 'f', -1, 64) |
| 1158 | } else { |
| 1159 | rvkencname = dd.DecodeStringAsBytes() |
| 1160 | } |
| 1161 | return rvkencname |
| 1162 | } |
| 1163 | |
| 1164 | func (d *Decoder) kStruct(f *codecFnInfo, rv reflect.Value) { |
| 1165 | fti := f.ti |
| 1166 | dd := d.d |
| 1167 | elemsep := d.esep |
| 1168 | sfn := structFieldNode{v: rv, update: true} |
| 1169 | ctyp := dd.ContainerType() |
| 1170 | if ctyp == valueTypeMap { |
| 1171 | containerLen := dd.ReadMapStart() |
| 1172 | if containerLen == 0 { |
| 1173 | dd.ReadMapEnd() |
| 1174 | return |
| 1175 | } |
| 1176 | tisfi := fti.sfiSort |
| 1177 | hasLen := containerLen >= 0 |
| 1178 | |
| 1179 | var rvkencname []byte |
| 1180 | for j := 0; (hasLen && j < containerLen) || !(hasLen || dd.CheckBreak()); j++ { |
| 1181 | if elemsep { |
| 1182 | dd.ReadMapElemKey() |
| 1183 | } |
| 1184 | rvkencname = decStructFieldKey(dd, fti.keyType, &d.b) |
| 1185 | if elemsep { |
| 1186 | dd.ReadMapElemValue() |
| 1187 | } |
| 1188 | if k := fti.indexForEncName(rvkencname); k > -1 { |
| 1189 | si := tisfi[k] |
| 1190 | if dd.TryDecodeAsNil() { |
| 1191 | si.setToZeroValue(rv) |
| 1192 | } else { |
| 1193 | d.decodeValue(sfn.field(si), nil, true) |
| 1194 | } |
| 1195 | } else { |
| 1196 | d.structFieldNotFound(-1, stringView(rvkencname)) |
| 1197 | } |
| 1198 | // keepAlive4StringView(rvkencnameB) // not needed, as reference is outside loop |
| 1199 | } |
| 1200 | dd.ReadMapEnd() |
| 1201 | } else if ctyp == valueTypeArray { |
| 1202 | containerLen := dd.ReadArrayStart() |
| 1203 | if containerLen == 0 { |
| 1204 | dd.ReadArrayEnd() |
| 1205 | return |
| 1206 | } |
| 1207 | // Not much gain from doing it two ways for array. |
| 1208 | // Arrays are not used as much for structs. |
| 1209 | hasLen := containerLen >= 0 |
| 1210 | for j, si := range fti.sfiSrc { |
| 1211 | if (hasLen && j == containerLen) || (!hasLen && dd.CheckBreak()) { |
| 1212 | break |
| 1213 | } |
| 1214 | if elemsep { |
| 1215 | dd.ReadArrayElem() |
| 1216 | } |
| 1217 | if dd.TryDecodeAsNil() { |
| 1218 | si.setToZeroValue(rv) |
| 1219 | } else { |
| 1220 | d.decodeValue(sfn.field(si), nil, true) |
| 1221 | } |
| 1222 | } |
| 1223 | if containerLen > len(fti.sfiSrc) { |
| 1224 | // read remaining values and throw away |
| 1225 | for j := len(fti.sfiSrc); j < containerLen; j++ { |
| 1226 | if elemsep { |
| 1227 | dd.ReadArrayElem() |
| 1228 | } |
| 1229 | d.structFieldNotFound(j, "") |
| 1230 | } |
| 1231 | } |
| 1232 | dd.ReadArrayEnd() |
| 1233 | } else { |
| 1234 | d.errorstr(errstrOnlyMapOrArrayCanDecodeIntoStruct) |
| 1235 | return |
| 1236 | } |
| 1237 | } |
| 1238 | |
| 1239 | func (d *Decoder) kSlice(f *codecFnInfo, rv reflect.Value) { |
| 1240 | // A slice can be set from a map or array in stream. |
| 1241 | // This way, the order can be kept (as order is lost with map). |
| 1242 | ti := f.ti |
| 1243 | if f.seq == seqTypeChan && ti.chandir&uint8(reflect.SendDir) == 0 { |
| 1244 | d.errorf("receive-only channel cannot be decoded") |
| 1245 | } |
| 1246 | dd := d.d |
| 1247 | rtelem0 := ti.elem |
| 1248 | ctyp := dd.ContainerType() |
| 1249 | if ctyp == valueTypeBytes || ctyp == valueTypeString { |
| 1250 | // you can only decode bytes or string in the stream into a slice or array of bytes |
| 1251 | if !(ti.rtid == uint8SliceTypId || rtelem0.Kind() == reflect.Uint8) { |
| 1252 | d.errorf("bytes/string in stream must decode into slice/array of bytes, not %v", ti.rt) |
| 1253 | } |
| 1254 | if f.seq == seqTypeChan { |
| 1255 | bs2 := dd.DecodeBytes(nil, true) |
| 1256 | irv := rv2i(rv) |
| 1257 | ch, ok := irv.(chan<- byte) |
| 1258 | if !ok { |
| 1259 | ch = irv.(chan byte) |
| 1260 | } |
| 1261 | for _, b := range bs2 { |
| 1262 | ch <- b |
| 1263 | } |
| 1264 | } else { |
| 1265 | rvbs := rv.Bytes() |
| 1266 | bs2 := dd.DecodeBytes(rvbs, false) |
| 1267 | // if rvbs == nil && bs2 != nil || rvbs != nil && bs2 == nil || len(bs2) != len(rvbs) { |
| 1268 | if !(len(bs2) > 0 && len(bs2) == len(rvbs) && &bs2[0] == &rvbs[0]) { |
| 1269 | if rv.CanSet() { |
| 1270 | rv.SetBytes(bs2) |
| 1271 | } else if len(rvbs) > 0 && len(bs2) > 0 { |
| 1272 | copy(rvbs, bs2) |
| 1273 | } |
| 1274 | } |
| 1275 | } |
| 1276 | return |
| 1277 | } |
| 1278 | |
| 1279 | // array := f.seq == seqTypeChan |
| 1280 | |
| 1281 | slh, containerLenS := d.decSliceHelperStart() // only expects valueType(Array|Map) |
| 1282 | |
| 1283 | // an array can never return a nil slice. so no need to check f.array here. |
| 1284 | if containerLenS == 0 { |
| 1285 | if rv.CanSet() { |
| 1286 | if f.seq == seqTypeSlice { |
| 1287 | if rv.IsNil() { |
| 1288 | rv.Set(reflect.MakeSlice(ti.rt, 0, 0)) |
| 1289 | } else { |
| 1290 | rv.SetLen(0) |
| 1291 | } |
| 1292 | } else if f.seq == seqTypeChan { |
| 1293 | if rv.IsNil() { |
| 1294 | rv.Set(reflect.MakeChan(ti.rt, 0)) |
| 1295 | } |
| 1296 | } |
| 1297 | } |
| 1298 | slh.End() |
| 1299 | return |
| 1300 | } |
| 1301 | |
| 1302 | rtelem0Size := int(rtelem0.Size()) |
| 1303 | rtElem0Kind := rtelem0.Kind() |
| 1304 | rtelem0Mut := !isImmutableKind(rtElem0Kind) |
| 1305 | rtelem := rtelem0 |
| 1306 | rtelemkind := rtelem.Kind() |
| 1307 | for rtelemkind == reflect.Ptr { |
| 1308 | rtelem = rtelem.Elem() |
| 1309 | rtelemkind = rtelem.Kind() |
| 1310 | } |
| 1311 | |
| 1312 | var fn *codecFn |
| 1313 | |
| 1314 | var rvCanset = rv.CanSet() |
| 1315 | var rvChanged bool |
| 1316 | var rv0 = rv |
| 1317 | var rv9 reflect.Value |
| 1318 | |
| 1319 | rvlen := rv.Len() |
| 1320 | rvcap := rv.Cap() |
| 1321 | hasLen := containerLenS > 0 |
| 1322 | if hasLen && f.seq == seqTypeSlice { |
| 1323 | if containerLenS > rvcap { |
| 1324 | oldRvlenGtZero := rvlen > 0 |
| 1325 | rvlen = decInferLen(containerLenS, d.h.MaxInitLen, int(rtelem0.Size())) |
| 1326 | if rvlen <= rvcap { |
| 1327 | if rvCanset { |
| 1328 | rv.SetLen(rvlen) |
| 1329 | } |
| 1330 | } else if rvCanset { |
| 1331 | rv = reflect.MakeSlice(ti.rt, rvlen, rvlen) |
| 1332 | rvcap = rvlen |
| 1333 | rvChanged = true |
| 1334 | } else { |
| 1335 | d.errorf("cannot decode into non-settable slice") |
| 1336 | } |
| 1337 | if rvChanged && oldRvlenGtZero && !isImmutableKind(rtelem0.Kind()) { |
| 1338 | reflect.Copy(rv, rv0) // only copy up to length NOT cap i.e. rv0.Slice(0, rvcap) |
| 1339 | } |
| 1340 | } else if containerLenS != rvlen { |
| 1341 | rvlen = containerLenS |
| 1342 | if rvCanset { |
| 1343 | rv.SetLen(rvlen) |
| 1344 | } |
| 1345 | // else { |
| 1346 | // rv = rv.Slice(0, rvlen) |
| 1347 | // rvChanged = true |
| 1348 | // d.errorf("cannot decode into non-settable slice") |
| 1349 | // } |
| 1350 | } |
| 1351 | } |
| 1352 | |
| 1353 | // consider creating new element once, and just decoding into it. |
| 1354 | var rtelem0Zero reflect.Value |
| 1355 | var rtelem0ZeroValid bool |
| 1356 | var decodeAsNil bool |
| 1357 | var j int |
| 1358 | d.cfer() |
| 1359 | for ; (hasLen && j < containerLenS) || !(hasLen || dd.CheckBreak()); j++ { |
| 1360 | if j == 0 && (f.seq == seqTypeSlice || f.seq == seqTypeChan) && rv.IsNil() { |
| 1361 | if hasLen { |
| 1362 | rvlen = decInferLen(containerLenS, d.h.MaxInitLen, rtelem0Size) |
| 1363 | } else if f.seq == seqTypeSlice { |
| 1364 | rvlen = decDefSliceCap |
| 1365 | } else { |
| 1366 | rvlen = decDefChanCap |
| 1367 | } |
| 1368 | if rvCanset { |
| 1369 | if f.seq == seqTypeSlice { |
| 1370 | rv = reflect.MakeSlice(ti.rt, rvlen, rvlen) |
| 1371 | rvChanged = true |
| 1372 | } else { // chan |
| 1373 | // xdebugf(">>>>>> haslen = %v, make chan of type '%v' with length: %v", hasLen, ti.rt, rvlen) |
| 1374 | rv = reflect.MakeChan(ti.rt, rvlen) |
| 1375 | rvChanged = true |
| 1376 | } |
| 1377 | } else { |
| 1378 | d.errorf("cannot decode into non-settable slice") |
| 1379 | } |
| 1380 | } |
| 1381 | slh.ElemContainerState(j) |
| 1382 | decodeAsNil = dd.TryDecodeAsNil() |
| 1383 | if f.seq == seqTypeChan { |
| 1384 | if decodeAsNil { |
| 1385 | rv.Send(reflect.Zero(rtelem0)) |
| 1386 | continue |
| 1387 | } |
| 1388 | if rtelem0Mut || !rv9.IsValid() { // || (rtElem0Kind == reflect.Ptr && rv9.IsNil()) { |
| 1389 | rv9 = reflect.New(rtelem0).Elem() |
| 1390 | } |
| 1391 | if fn == nil { |
| 1392 | fn = d.cf.get(rtelem, true, true) |
| 1393 | } |
| 1394 | d.decodeValue(rv9, fn, true) |
| 1395 | // xdebugf(">>>> rv9 sent on %v during decode: %v, with len=%v, cap=%v", rv.Type(), rv9, rv.Len(), rv.Cap()) |
| 1396 | rv.Send(rv9) |
| 1397 | } else { |
| 1398 | // if indefinite, etc, then expand the slice if necessary |
| 1399 | var decodeIntoBlank bool |
| 1400 | if j >= rvlen { |
| 1401 | if f.seq == seqTypeArray { |
| 1402 | d.arrayCannotExpand(rvlen, j+1) |
| 1403 | decodeIntoBlank = true |
| 1404 | } else { // if f.seq == seqTypeSlice |
| 1405 | // rv = reflect.Append(rv, reflect.Zero(rtelem0)) // append logic + varargs |
| 1406 | var rvcap2 int |
| 1407 | var rvErrmsg2 string |
| 1408 | rv9, rvcap2, rvChanged, rvErrmsg2 = |
| 1409 | expandSliceRV(rv, ti.rt, rvCanset, rtelem0Size, 1, rvlen, rvcap) |
| 1410 | if rvErrmsg2 != "" { |
| 1411 | d.errorf(rvErrmsg2) |
| 1412 | } |
| 1413 | rvlen++ |
| 1414 | if rvChanged { |
| 1415 | rv = rv9 |
| 1416 | rvcap = rvcap2 |
| 1417 | } |
| 1418 | } |
| 1419 | } |
| 1420 | if decodeIntoBlank { |
| 1421 | if !decodeAsNil { |
| 1422 | d.swallow() |
| 1423 | } |
| 1424 | } else { |
| 1425 | rv9 = rv.Index(j) |
| 1426 | if d.h.SliceElementReset || decodeAsNil { |
| 1427 | if !rtelem0ZeroValid { |
| 1428 | rtelem0ZeroValid = true |
| 1429 | rtelem0Zero = reflect.Zero(rtelem0) |
| 1430 | } |
| 1431 | rv9.Set(rtelem0Zero) |
| 1432 | } |
| 1433 | if decodeAsNil { |
| 1434 | continue |
| 1435 | } |
| 1436 | |
| 1437 | if fn == nil { |
| 1438 | fn = d.cf.get(rtelem, true, true) |
| 1439 | } |
| 1440 | d.decodeValue(rv9, fn, true) |
| 1441 | } |
| 1442 | } |
| 1443 | } |
| 1444 | if f.seq == seqTypeSlice { |
| 1445 | if j < rvlen { |
| 1446 | if rv.CanSet() { |
| 1447 | rv.SetLen(j) |
| 1448 | } else if rvCanset { |
| 1449 | rv = rv.Slice(0, j) |
| 1450 | rvChanged = true |
| 1451 | } // else { d.errorf("kSlice: cannot change non-settable slice") } |
| 1452 | rvlen = j |
| 1453 | } else if j == 0 && rv.IsNil() { |
| 1454 | if rvCanset { |
| 1455 | rv = reflect.MakeSlice(ti.rt, 0, 0) |
| 1456 | rvChanged = true |
| 1457 | } // else { d.errorf("kSlice: cannot change non-settable slice") } |
| 1458 | } |
| 1459 | } |
| 1460 | slh.End() |
| 1461 | |
| 1462 | if rvChanged { // infers rvCanset=true, so it can be reset |
| 1463 | rv0.Set(rv) |
| 1464 | } |
| 1465 | } |
| 1466 | |
| 1467 | // func (d *Decoder) kArray(f *codecFnInfo, rv reflect.Value) { |
| 1468 | // // d.decodeValueFn(rv.Slice(0, rv.Len())) |
| 1469 | // f.kSlice(rv.Slice(0, rv.Len())) |
| 1470 | // } |
| 1471 | |
| 1472 | func (d *Decoder) kMap(f *codecFnInfo, rv reflect.Value) { |
| 1473 | dd := d.d |
| 1474 | containerLen := dd.ReadMapStart() |
| 1475 | elemsep := d.esep |
| 1476 | ti := f.ti |
| 1477 | if rv.IsNil() { |
| 1478 | rv.Set(makeMapReflect(ti.rt, containerLen)) |
| 1479 | } |
| 1480 | |
| 1481 | if containerLen == 0 { |
| 1482 | dd.ReadMapEnd() |
| 1483 | return |
| 1484 | } |
| 1485 | |
| 1486 | ktype, vtype := ti.key, ti.elem |
| 1487 | ktypeId := rt2id(ktype) |
| 1488 | vtypeKind := vtype.Kind() |
| 1489 | |
| 1490 | var keyFn, valFn *codecFn |
| 1491 | var ktypeLo, vtypeLo reflect.Type |
| 1492 | |
| 1493 | for ktypeLo = ktype; ktypeLo.Kind() == reflect.Ptr; ktypeLo = ktypeLo.Elem() { |
| 1494 | } |
| 1495 | |
| 1496 | for vtypeLo = vtype; vtypeLo.Kind() == reflect.Ptr; vtypeLo = vtypeLo.Elem() { |
| 1497 | } |
| 1498 | |
| 1499 | var mapGet, mapSet bool |
| 1500 | rvvImmut := isImmutableKind(vtypeKind) |
| 1501 | if !d.h.MapValueReset { |
| 1502 | // if pointer, mapGet = true |
| 1503 | // if interface, mapGet = true if !DecodeNakedAlways (else false) |
| 1504 | // if builtin, mapGet = false |
| 1505 | // else mapGet = true |
| 1506 | if vtypeKind == reflect.Ptr { |
| 1507 | mapGet = true |
| 1508 | } else if vtypeKind == reflect.Interface { |
| 1509 | if !d.h.InterfaceReset { |
| 1510 | mapGet = true |
| 1511 | } |
| 1512 | } else if !rvvImmut { |
| 1513 | mapGet = true |
| 1514 | } |
| 1515 | } |
| 1516 | |
| 1517 | var rvk, rvkp, rvv, rvz reflect.Value |
| 1518 | rvkMut := !isImmutableKind(ktype.Kind()) // if ktype is immutable, then re-use the same rvk. |
| 1519 | ktypeIsString := ktypeId == stringTypId |
| 1520 | ktypeIsIntf := ktypeId == intfTypId |
| 1521 | hasLen := containerLen > 0 |
| 1522 | var kstrbs []byte |
| 1523 | d.cfer() |
| 1524 | for j := 0; (hasLen && j < containerLen) || !(hasLen || dd.CheckBreak()); j++ { |
| 1525 | if rvkMut || !rvkp.IsValid() { |
| 1526 | rvkp = reflect.New(ktype) |
| 1527 | rvk = rvkp.Elem() |
| 1528 | } |
| 1529 | if elemsep { |
| 1530 | dd.ReadMapElemKey() |
| 1531 | } |
| 1532 | if false && dd.TryDecodeAsNil() { // nil cannot be a map key, so disregard this block |
| 1533 | // Previously, if a nil key, we just ignored the mapped value and continued. |
| 1534 | // However, that makes the result of encoding and then decoding map[intf]intf{nil:nil} |
| 1535 | // to be an empty map. |
| 1536 | // Instead, we treat a nil key as the zero value of the type. |
| 1537 | rvk.Set(reflect.Zero(ktype)) |
| 1538 | } else if ktypeIsString { |
| 1539 | kstrbs = dd.DecodeStringAsBytes() |
| 1540 | rvk.SetString(stringView(kstrbs)) |
| 1541 | // NOTE: if doing an insert, you MUST use a real string (not stringview) |
| 1542 | } else { |
| 1543 | if keyFn == nil { |
| 1544 | keyFn = d.cf.get(ktypeLo, true, true) |
| 1545 | } |
| 1546 | d.decodeValue(rvk, keyFn, true) |
| 1547 | } |
| 1548 | // special case if a byte array. |
| 1549 | if ktypeIsIntf { |
| 1550 | if rvk2 := rvk.Elem(); rvk2.IsValid() { |
| 1551 | if rvk2.Type() == uint8SliceTyp { |
| 1552 | rvk = reflect.ValueOf(d.string(rvk2.Bytes())) |
| 1553 | } else { |
| 1554 | rvk = rvk2 |
| 1555 | } |
| 1556 | } |
| 1557 | } |
| 1558 | |
| 1559 | if elemsep { |
| 1560 | dd.ReadMapElemValue() |
| 1561 | } |
| 1562 | |
| 1563 | // Brittle, but OK per TryDecodeAsNil() contract. |
| 1564 | // i.e. TryDecodeAsNil never shares slices with other decDriver procedures |
| 1565 | if dd.TryDecodeAsNil() { |
| 1566 | if ktypeIsString { |
| 1567 | rvk.SetString(d.string(kstrbs)) |
| 1568 | } |
| 1569 | if d.h.DeleteOnNilMapValue { |
| 1570 | rv.SetMapIndex(rvk, reflect.Value{}) |
| 1571 | } else { |
| 1572 | rv.SetMapIndex(rvk, reflect.Zero(vtype)) |
| 1573 | } |
| 1574 | continue |
| 1575 | } |
| 1576 | |
| 1577 | mapSet = true // set to false if u do a get, and its a non-nil pointer |
| 1578 | if mapGet { |
| 1579 | // mapGet true only in case where kind=Ptr|Interface or kind is otherwise mutable. |
| 1580 | rvv = rv.MapIndex(rvk) |
| 1581 | if !rvv.IsValid() { |
| 1582 | rvv = reflect.New(vtype).Elem() |
| 1583 | } else if vtypeKind == reflect.Ptr { |
| 1584 | if rvv.IsNil() { |
| 1585 | rvv = reflect.New(vtype).Elem() |
| 1586 | } else { |
| 1587 | mapSet = false |
| 1588 | } |
| 1589 | } else if vtypeKind == reflect.Interface { |
| 1590 | // not addressable, and thus not settable. |
| 1591 | // e MUST create a settable/addressable variant |
| 1592 | rvv2 := reflect.New(rvv.Type()).Elem() |
| 1593 | if !rvv.IsNil() { |
| 1594 | rvv2.Set(rvv) |
| 1595 | } |
| 1596 | rvv = rvv2 |
| 1597 | } |
| 1598 | // else it is ~mutable, and we can just decode into it directly |
| 1599 | } else if rvvImmut { |
| 1600 | if !rvz.IsValid() { |
| 1601 | rvz = reflect.New(vtype).Elem() |
| 1602 | } |
| 1603 | rvv = rvz |
| 1604 | } else { |
| 1605 | rvv = reflect.New(vtype).Elem() |
| 1606 | } |
| 1607 | |
| 1608 | // We MUST be done with the stringview of the key, before decoding the value |
| 1609 | // so that we don't bastardize the reused byte array. |
| 1610 | if mapSet && ktypeIsString { |
| 1611 | rvk.SetString(d.string(kstrbs)) |
| 1612 | } |
| 1613 | if valFn == nil { |
| 1614 | valFn = d.cf.get(vtypeLo, true, true) |
| 1615 | } |
| 1616 | d.decodeValue(rvv, valFn, true) |
| 1617 | // d.decodeValueFn(rvv, valFn) |
| 1618 | if mapSet { |
| 1619 | rv.SetMapIndex(rvk, rvv) |
| 1620 | } |
| 1621 | // if ktypeIsString { |
| 1622 | // // keepAlive4StringView(kstrbs) // not needed, as reference is outside loop |
| 1623 | // } |
| 1624 | } |
| 1625 | |
| 1626 | dd.ReadMapEnd() |
| 1627 | } |
| 1628 | |
| 1629 | // decNaked is used to keep track of the primitives decoded. |
| 1630 | // Without it, we would have to decode each primitive and wrap it |
| 1631 | // in an interface{}, causing an allocation. |
| 1632 | // In this model, the primitives are decoded in a "pseudo-atomic" fashion, |
| 1633 | // so we can rest assured that no other decoding happens while these |
| 1634 | // primitives are being decoded. |
| 1635 | // |
| 1636 | // maps and arrays are not handled by this mechanism. |
| 1637 | // However, RawExt is, and we accommodate for extensions that decode |
| 1638 | // RawExt from DecodeNaked, but need to decode the value subsequently. |
| 1639 | // kInterfaceNaked and swallow, which call DecodeNaked, handle this caveat. |
| 1640 | // |
| 1641 | // However, decNaked also keeps some arrays of default maps and slices |
| 1642 | // used in DecodeNaked. This way, we can get a pointer to it |
| 1643 | // without causing a new heap allocation. |
| 1644 | // |
| 1645 | // kInterfaceNaked will ensure that there is no allocation for the common |
| 1646 | // uses. |
| 1647 | |
| 1648 | type decNakedContainers struct { |
| 1649 | // array/stacks for reducing allocation |
| 1650 | // keep arrays at the bottom? Chance is that they are not used much. |
| 1651 | ia [arrayCacheLen]interface{} |
| 1652 | ma [arrayCacheLen]map[interface{}]interface{} |
| 1653 | na [arrayCacheLen]map[string]interface{} |
| 1654 | sa [arrayCacheLen][]interface{} |
| 1655 | |
| 1656 | // ria [arrayCacheLen]reflect.Value // not needed, as we decode directly into &ia[n] |
| 1657 | rma, rna, rsa [arrayCacheLen]reflect.Value // reflect.Value mapping to above |
| 1658 | } |
| 1659 | |
| 1660 | func (n *decNakedContainers) init() { |
| 1661 | for i := 0; i < arrayCacheLen; i++ { |
| 1662 | // n.ria[i] = reflect.ValueOf(&(n.ia[i])).Elem() |
| 1663 | n.rma[i] = reflect.ValueOf(&(n.ma[i])).Elem() |
| 1664 | n.rna[i] = reflect.ValueOf(&(n.na[i])).Elem() |
| 1665 | n.rsa[i] = reflect.ValueOf(&(n.sa[i])).Elem() |
| 1666 | } |
| 1667 | } |
| 1668 | |
| 1669 | type decNaked struct { |
| 1670 | // r RawExt // used for RawExt, uint, []byte. |
| 1671 | |
| 1672 | // primitives below |
| 1673 | u uint64 |
| 1674 | i int64 |
| 1675 | f float64 |
| 1676 | l []byte |
| 1677 | s string |
| 1678 | |
| 1679 | // ---- cpu cache line boundary? |
| 1680 | t time.Time |
| 1681 | b bool |
| 1682 | |
| 1683 | // state |
| 1684 | v valueType |
| 1685 | li, lm, ln, ls int8 |
| 1686 | inited bool |
| 1687 | |
| 1688 | *decNakedContainers |
| 1689 | |
| 1690 | ru, ri, rf, rl, rs, rb, rt reflect.Value // mapping to the primitives above |
| 1691 | |
| 1692 | // _ [6]uint64 // padding // no padding - rt goes into next cache line |
| 1693 | } |
| 1694 | |
| 1695 | func (n *decNaked) init() { |
| 1696 | if n.inited { |
| 1697 | return |
| 1698 | } |
| 1699 | n.ru = reflect.ValueOf(&n.u).Elem() |
| 1700 | n.ri = reflect.ValueOf(&n.i).Elem() |
| 1701 | n.rf = reflect.ValueOf(&n.f).Elem() |
| 1702 | n.rl = reflect.ValueOf(&n.l).Elem() |
| 1703 | n.rs = reflect.ValueOf(&n.s).Elem() |
| 1704 | n.rt = reflect.ValueOf(&n.t).Elem() |
| 1705 | n.rb = reflect.ValueOf(&n.b).Elem() |
| 1706 | |
| 1707 | n.inited = true |
| 1708 | // n.rr[] = reflect.ValueOf(&n.) |
| 1709 | } |
| 1710 | |
| 1711 | func (n *decNaked) initContainers() { |
| 1712 | if n.decNakedContainers == nil { |
| 1713 | n.decNakedContainers = new(decNakedContainers) |
| 1714 | n.decNakedContainers.init() |
| 1715 | } |
| 1716 | } |
| 1717 | |
| 1718 | func (n *decNaked) reset() { |
| 1719 | if n == nil { |
| 1720 | return |
| 1721 | } |
| 1722 | n.li, n.lm, n.ln, n.ls = 0, 0, 0, 0 |
| 1723 | } |
| 1724 | |
| 1725 | type rtid2rv struct { |
| 1726 | rtid uintptr |
| 1727 | rv reflect.Value |
| 1728 | } |
| 1729 | |
| 1730 | // -------------- |
| 1731 | |
| 1732 | type decReaderSwitch struct { |
| 1733 | rb bytesDecReader |
| 1734 | // ---- cpu cache line boundary? |
| 1735 | ri *ioDecReader |
| 1736 | mtr, str bool // whether maptype or slicetype are known types |
| 1737 | |
| 1738 | be bool // is binary encoding |
| 1739 | bytes bool // is bytes reader |
| 1740 | js bool // is json handle |
| 1741 | jsms bool // is json handle, and MapKeyAsString |
| 1742 | esep bool // has elem separators |
| 1743 | } |
| 1744 | |
| 1745 | // TODO: Uncomment after mid-stack inlining enabled in go 1.11 |
| 1746 | // |
| 1747 | // func (z *decReaderSwitch) unreadn1() { |
| 1748 | // if z.bytes { |
| 1749 | // z.rb.unreadn1() |
| 1750 | // } else { |
| 1751 | // z.ri.unreadn1() |
| 1752 | // } |
| 1753 | // } |
| 1754 | // func (z *decReaderSwitch) readx(n int) []byte { |
| 1755 | // if z.bytes { |
| 1756 | // return z.rb.readx(n) |
| 1757 | // } |
| 1758 | // return z.ri.readx(n) |
| 1759 | // } |
| 1760 | // func (z *decReaderSwitch) readb(s []byte) { |
| 1761 | // if z.bytes { |
| 1762 | // z.rb.readb(s) |
| 1763 | // } else { |
| 1764 | // z.ri.readb(s) |
| 1765 | // } |
| 1766 | // } |
| 1767 | // func (z *decReaderSwitch) readn1() uint8 { |
| 1768 | // if z.bytes { |
| 1769 | // return z.rb.readn1() |
| 1770 | // } |
| 1771 | // return z.ri.readn1() |
| 1772 | // } |
| 1773 | // func (z *decReaderSwitch) numread() int { |
| 1774 | // if z.bytes { |
| 1775 | // return z.rb.numread() |
| 1776 | // } |
| 1777 | // return z.ri.numread() |
| 1778 | // } |
| 1779 | // func (z *decReaderSwitch) track() { |
| 1780 | // if z.bytes { |
| 1781 | // z.rb.track() |
| 1782 | // } else { |
| 1783 | // z.ri.track() |
| 1784 | // } |
| 1785 | // } |
| 1786 | // func (z *decReaderSwitch) stopTrack() []byte { |
| 1787 | // if z.bytes { |
| 1788 | // return z.rb.stopTrack() |
| 1789 | // } |
| 1790 | // return z.ri.stopTrack() |
| 1791 | // } |
| 1792 | // func (z *decReaderSwitch) skip(accept *bitset256) (token byte) { |
| 1793 | // if z.bytes { |
| 1794 | // return z.rb.skip(accept) |
| 1795 | // } |
| 1796 | // return z.ri.skip(accept) |
| 1797 | // } |
| 1798 | // func (z *decReaderSwitch) readTo(in []byte, accept *bitset256) (out []byte) { |
| 1799 | // if z.bytes { |
| 1800 | // return z.rb.readTo(in, accept) |
| 1801 | // } |
| 1802 | // return z.ri.readTo(in, accept) |
| 1803 | // } |
| 1804 | // func (z *decReaderSwitch) readUntil(in []byte, stop byte) (out []byte) { |
| 1805 | // if z.bytes { |
| 1806 | // return z.rb.readUntil(in, stop) |
| 1807 | // } |
| 1808 | // return z.ri.readUntil(in, stop) |
| 1809 | // } |
| 1810 | |
| 1811 | // A Decoder reads and decodes an object from an input stream in the codec format. |
| 1812 | type Decoder struct { |
| 1813 | panicHdl |
| 1814 | // hopefully, reduce derefencing cost by laying the decReader inside the Decoder. |
| 1815 | // Try to put things that go together to fit within a cache line (8 words). |
| 1816 | |
| 1817 | d decDriver |
| 1818 | // NOTE: Decoder shouldn't call it's read methods, |
| 1819 | // as the handler MAY need to do some coordination. |
| 1820 | r decReader |
| 1821 | h *BasicHandle |
| 1822 | bi *bufioDecReader |
| 1823 | // cache the mapTypeId and sliceTypeId for faster comparisons |
| 1824 | mtid uintptr |
| 1825 | stid uintptr |
| 1826 | |
| 1827 | // ---- cpu cache line boundary? |
| 1828 | decReaderSwitch |
| 1829 | |
| 1830 | // ---- cpu cache line boundary? |
| 1831 | codecFnPooler |
| 1832 | // cr containerStateRecv |
| 1833 | n *decNaked |
| 1834 | nsp *sync.Pool |
| 1835 | err error |
| 1836 | |
| 1837 | // ---- cpu cache line boundary? |
| 1838 | b [decScratchByteArrayLen]byte // scratch buffer, used by Decoder and xxxEncDrivers |
| 1839 | is map[string]string // used for interning strings |
| 1840 | |
| 1841 | // padding - false sharing help // modify 232 if Decoder struct changes. |
| 1842 | // _ [cacheLineSize - 232%cacheLineSize]byte |
| 1843 | } |
| 1844 | |
| 1845 | // NewDecoder returns a Decoder for decoding a stream of bytes from an io.Reader. |
| 1846 | // |
| 1847 | // For efficiency, Users are encouraged to pass in a memory buffered reader |
| 1848 | // (eg bufio.Reader, bytes.Buffer). |
| 1849 | func NewDecoder(r io.Reader, h Handle) *Decoder { |
| 1850 | d := newDecoder(h) |
| 1851 | d.Reset(r) |
| 1852 | return d |
| 1853 | } |
| 1854 | |
| 1855 | // NewDecoderBytes returns a Decoder which efficiently decodes directly |
| 1856 | // from a byte slice with zero copying. |
| 1857 | func NewDecoderBytes(in []byte, h Handle) *Decoder { |
| 1858 | d := newDecoder(h) |
| 1859 | d.ResetBytes(in) |
| 1860 | return d |
| 1861 | } |
| 1862 | |
| 1863 | var defaultDecNaked decNaked |
| 1864 | |
| 1865 | func newDecoder(h Handle) *Decoder { |
| 1866 | d := &Decoder{h: h.getBasicHandle(), err: errDecoderNotInitialized} |
| 1867 | d.hh = h |
| 1868 | d.be = h.isBinary() |
| 1869 | // NOTE: do not initialize d.n here. It is lazily initialized in d.naked() |
| 1870 | var jh *JsonHandle |
| 1871 | jh, d.js = h.(*JsonHandle) |
| 1872 | if d.js { |
| 1873 | d.jsms = jh.MapKeyAsString |
| 1874 | } |
| 1875 | d.esep = d.hh.hasElemSeparators() |
| 1876 | if d.h.InternString { |
| 1877 | d.is = make(map[string]string, 32) |
| 1878 | } |
| 1879 | d.d = h.newDecDriver(d) |
| 1880 | // d.cr, _ = d.d.(containerStateRecv) |
| 1881 | return d |
| 1882 | } |
| 1883 | |
| 1884 | func (d *Decoder) resetCommon() { |
| 1885 | d.n.reset() |
| 1886 | d.d.reset() |
| 1887 | d.err = nil |
| 1888 | // reset all things which were cached from the Handle, but could change |
| 1889 | d.mtid, d.stid = 0, 0 |
| 1890 | d.mtr, d.str = false, false |
| 1891 | if d.h.MapType != nil { |
| 1892 | d.mtid = rt2id(d.h.MapType) |
| 1893 | d.mtr = fastpathAV.index(d.mtid) != -1 |
| 1894 | } |
| 1895 | if d.h.SliceType != nil { |
| 1896 | d.stid = rt2id(d.h.SliceType) |
| 1897 | d.str = fastpathAV.index(d.stid) != -1 |
| 1898 | } |
| 1899 | } |
| 1900 | |
| 1901 | // Reset the Decoder with a new Reader to decode from, |
| 1902 | // clearing all state from last run(s). |
| 1903 | func (d *Decoder) Reset(r io.Reader) { |
| 1904 | if r == nil { |
| 1905 | return |
| 1906 | } |
| 1907 | if d.bi == nil { |
| 1908 | d.bi = new(bufioDecReader) |
| 1909 | } |
| 1910 | d.bytes = false |
| 1911 | if d.h.ReaderBufferSize > 0 { |
| 1912 | d.bi.buf = make([]byte, 0, d.h.ReaderBufferSize) |
| 1913 | d.bi.reset(r) |
| 1914 | d.r = d.bi |
| 1915 | } else { |
| 1916 | // d.ri.x = &d.b |
| 1917 | // d.s = d.sa[:0] |
| 1918 | if d.ri == nil { |
| 1919 | d.ri = new(ioDecReader) |
| 1920 | } |
| 1921 | d.ri.reset(r) |
| 1922 | d.r = d.ri |
| 1923 | } |
| 1924 | d.resetCommon() |
| 1925 | } |
| 1926 | |
| 1927 | // ResetBytes resets the Decoder with a new []byte to decode from, |
| 1928 | // clearing all state from last run(s). |
| 1929 | func (d *Decoder) ResetBytes(in []byte) { |
| 1930 | if in == nil { |
| 1931 | return |
| 1932 | } |
| 1933 | d.bytes = true |
| 1934 | d.rb.reset(in) |
| 1935 | d.r = &d.rb |
| 1936 | d.resetCommon() |
| 1937 | } |
| 1938 | |
| 1939 | // naked must be called before each call to .DecodeNaked, |
| 1940 | // as they will use it. |
| 1941 | func (d *Decoder) naked() *decNaked { |
| 1942 | if d.n == nil { |
| 1943 | // consider one of: |
| 1944 | // - get from sync.Pool (if GC is frequent, there's no value here) |
| 1945 | // - new alloc (safest. only init'ed if it a naked decode will be done) |
| 1946 | // - field in Decoder (makes the Decoder struct very big) |
| 1947 | // To support using a decoder where a DecodeNaked is not needed, |
| 1948 | // we prefer #1 or #2. |
| 1949 | // d.n = new(decNaked) // &d.nv // new(decNaked) // grab from a sync.Pool |
| 1950 | // d.n.init() |
| 1951 | var v interface{} |
| 1952 | d.nsp, v = pool.decNaked() |
| 1953 | d.n = v.(*decNaked) |
| 1954 | } |
| 1955 | return d.n |
| 1956 | } |
| 1957 | |
| 1958 | // Decode decodes the stream from reader and stores the result in the |
| 1959 | // value pointed to by v. v cannot be a nil pointer. v can also be |
| 1960 | // a reflect.Value of a pointer. |
| 1961 | // |
| 1962 | // Note that a pointer to a nil interface is not a nil pointer. |
| 1963 | // If you do not know what type of stream it is, pass in a pointer to a nil interface. |
| 1964 | // We will decode and store a value in that nil interface. |
| 1965 | // |
| 1966 | // Sample usages: |
| 1967 | // // Decoding into a non-nil typed value |
| 1968 | // var f float32 |
| 1969 | // err = codec.NewDecoder(r, handle).Decode(&f) |
| 1970 | // |
| 1971 | // // Decoding into nil interface |
| 1972 | // var v interface{} |
| 1973 | // dec := codec.NewDecoder(r, handle) |
| 1974 | // err = dec.Decode(&v) |
| 1975 | // |
| 1976 | // When decoding into a nil interface{}, we will decode into an appropriate value based |
| 1977 | // on the contents of the stream: |
| 1978 | // - Numbers are decoded as float64, int64 or uint64. |
| 1979 | // - Other values are decoded appropriately depending on the type: |
| 1980 | // bool, string, []byte, time.Time, etc |
| 1981 | // - Extensions are decoded as RawExt (if no ext function registered for the tag) |
| 1982 | // Configurations exist on the Handle to override defaults |
| 1983 | // (e.g. for MapType, SliceType and how to decode raw bytes). |
| 1984 | // |
| 1985 | // When decoding into a non-nil interface{} value, the mode of encoding is based on the |
| 1986 | // type of the value. When a value is seen: |
| 1987 | // - If an extension is registered for it, call that extension function |
| 1988 | // - If it implements BinaryUnmarshaler, call its UnmarshalBinary(data []byte) error |
| 1989 | // - Else decode it based on its reflect.Kind |
| 1990 | // |
| 1991 | // There are some special rules when decoding into containers (slice/array/map/struct). |
| 1992 | // Decode will typically use the stream contents to UPDATE the container. |
| 1993 | // - A map can be decoded from a stream map, by updating matching keys. |
| 1994 | // - A slice can be decoded from a stream array, |
| 1995 | // by updating the first n elements, where n is length of the stream. |
| 1996 | // - A slice can be decoded from a stream map, by decoding as if |
| 1997 | // it contains a sequence of key-value pairs. |
| 1998 | // - A struct can be decoded from a stream map, by updating matching fields. |
| 1999 | // - A struct can be decoded from a stream array, |
| 2000 | // by updating fields as they occur in the struct (by index). |
| 2001 | // |
| 2002 | // When decoding a stream map or array with length of 0 into a nil map or slice, |
| 2003 | // we reset the destination map or slice to a zero-length value. |
| 2004 | // |
| 2005 | // However, when decoding a stream nil, we reset the destination container |
| 2006 | // to its "zero" value (e.g. nil for slice/map, etc). |
| 2007 | // |
| 2008 | // Note: we allow nil values in the stream anywhere except for map keys. |
| 2009 | // A nil value in the encoded stream where a map key is expected is treated as an error. |
| 2010 | func (d *Decoder) Decode(v interface{}) (err error) { |
| 2011 | defer d.deferred(&err) |
| 2012 | d.MustDecode(v) |
| 2013 | return |
| 2014 | } |
| 2015 | |
| 2016 | // MustDecode is like Decode, but panics if unable to Decode. |
| 2017 | // This provides insight to the code location that triggered the error. |
| 2018 | func (d *Decoder) MustDecode(v interface{}) { |
| 2019 | // TODO: Top-level: ensure that v is a pointer and not nil. |
| 2020 | if d.err != nil { |
| 2021 | panic(d.err) |
| 2022 | } |
| 2023 | if d.d.TryDecodeAsNil() { |
| 2024 | setZero(v) |
| 2025 | } else { |
| 2026 | d.decode(v) |
| 2027 | } |
| 2028 | d.alwaysAtEnd() |
| 2029 | // xprintf(">>>>>>>> >>>>>>>> num decFns: %v\n", d.cf.sn) |
| 2030 | } |
| 2031 | |
| 2032 | func (d *Decoder) deferred(err1 *error) { |
| 2033 | d.alwaysAtEnd() |
| 2034 | if recoverPanicToErr { |
| 2035 | if x := recover(); x != nil { |
| 2036 | panicValToErr(d, x, err1) |
| 2037 | panicValToErr(d, x, &d.err) |
| 2038 | } |
| 2039 | } |
| 2040 | } |
| 2041 | |
| 2042 | func (d *Decoder) alwaysAtEnd() { |
| 2043 | if d.n != nil { |
| 2044 | // if n != nil, then nsp != nil (they are always set together) |
| 2045 | d.nsp.Put(d.n) |
| 2046 | d.n, d.nsp = nil, nil |
| 2047 | } |
| 2048 | d.codecFnPooler.alwaysAtEnd() |
| 2049 | } |
| 2050 | |
| 2051 | // // this is not a smart swallow, as it allocates objects and does unnecessary work. |
| 2052 | // func (d *Decoder) swallowViaHammer() { |
| 2053 | // var blank interface{} |
| 2054 | // d.decodeValueNoFn(reflect.ValueOf(&blank).Elem()) |
| 2055 | // } |
| 2056 | |
| 2057 | func (d *Decoder) swallow() { |
| 2058 | // smarter decode that just swallows the content |
| 2059 | dd := d.d |
| 2060 | if dd.TryDecodeAsNil() { |
| 2061 | return |
| 2062 | } |
| 2063 | elemsep := d.esep |
| 2064 | switch dd.ContainerType() { |
| 2065 | case valueTypeMap: |
| 2066 | containerLen := dd.ReadMapStart() |
| 2067 | hasLen := containerLen >= 0 |
| 2068 | for j := 0; (hasLen && j < containerLen) || !(hasLen || dd.CheckBreak()); j++ { |
| 2069 | // if clenGtEqualZero {if j >= containerLen {break} } else if dd.CheckBreak() {break} |
| 2070 | if elemsep { |
| 2071 | dd.ReadMapElemKey() |
| 2072 | } |
| 2073 | d.swallow() |
| 2074 | if elemsep { |
| 2075 | dd.ReadMapElemValue() |
| 2076 | } |
| 2077 | d.swallow() |
| 2078 | } |
| 2079 | dd.ReadMapEnd() |
| 2080 | case valueTypeArray: |
| 2081 | containerLen := dd.ReadArrayStart() |
| 2082 | hasLen := containerLen >= 0 |
| 2083 | for j := 0; (hasLen && j < containerLen) || !(hasLen || dd.CheckBreak()); j++ { |
| 2084 | if elemsep { |
| 2085 | dd.ReadArrayElem() |
| 2086 | } |
| 2087 | d.swallow() |
| 2088 | } |
| 2089 | dd.ReadArrayEnd() |
| 2090 | case valueTypeBytes: |
| 2091 | dd.DecodeBytes(d.b[:], true) |
| 2092 | case valueTypeString: |
| 2093 | dd.DecodeStringAsBytes() |
| 2094 | default: |
| 2095 | // these are all primitives, which we can get from decodeNaked |
| 2096 | // if RawExt using Value, complete the processing. |
| 2097 | n := d.naked() |
| 2098 | dd.DecodeNaked() |
| 2099 | if n.v == valueTypeExt && n.l == nil { |
| 2100 | n.initContainers() |
| 2101 | if n.li < arrayCacheLen { |
| 2102 | n.ia[n.li] = nil |
| 2103 | n.li++ |
| 2104 | d.decode(&n.ia[n.li-1]) |
| 2105 | n.ia[n.li-1] = nil |
| 2106 | n.li-- |
| 2107 | } else { |
| 2108 | var v2 interface{} |
| 2109 | d.decode(&v2) |
| 2110 | } |
| 2111 | } |
| 2112 | } |
| 2113 | } |
| 2114 | |
| 2115 | func setZero(iv interface{}) { |
| 2116 | if iv == nil || definitelyNil(iv) { |
| 2117 | return |
| 2118 | } |
| 2119 | var canDecode bool |
| 2120 | switch v := iv.(type) { |
| 2121 | case *string: |
| 2122 | *v = "" |
| 2123 | case *bool: |
| 2124 | *v = false |
| 2125 | case *int: |
| 2126 | *v = 0 |
| 2127 | case *int8: |
| 2128 | *v = 0 |
| 2129 | case *int16: |
| 2130 | *v = 0 |
| 2131 | case *int32: |
| 2132 | *v = 0 |
| 2133 | case *int64: |
| 2134 | *v = 0 |
| 2135 | case *uint: |
| 2136 | *v = 0 |
| 2137 | case *uint8: |
| 2138 | *v = 0 |
| 2139 | case *uint16: |
| 2140 | *v = 0 |
| 2141 | case *uint32: |
| 2142 | *v = 0 |
| 2143 | case *uint64: |
| 2144 | *v = 0 |
| 2145 | case *float32: |
| 2146 | *v = 0 |
| 2147 | case *float64: |
| 2148 | *v = 0 |
| 2149 | case *[]uint8: |
| 2150 | *v = nil |
| 2151 | case *Raw: |
| 2152 | *v = nil |
| 2153 | case *time.Time: |
| 2154 | *v = time.Time{} |
| 2155 | case reflect.Value: |
| 2156 | if v, canDecode = isDecodeable(v); canDecode && v.CanSet() { |
| 2157 | v.Set(reflect.Zero(v.Type())) |
| 2158 | } // TODO: else drain if chan, clear if map, set all to nil if slice??? |
| 2159 | default: |
| 2160 | if !fastpathDecodeSetZeroTypeSwitch(iv) { |
| 2161 | v := reflect.ValueOf(iv) |
| 2162 | if v, canDecode = isDecodeable(v); canDecode && v.CanSet() { |
| 2163 | v.Set(reflect.Zero(v.Type())) |
| 2164 | } // TODO: else drain if chan, clear if map, set all to nil if slice??? |
| 2165 | } |
| 2166 | } |
| 2167 | } |
| 2168 | |
| 2169 | func (d *Decoder) decode(iv interface{}) { |
| 2170 | // check nil and interfaces explicitly, |
| 2171 | // so that type switches just have a run of constant non-interface types. |
| 2172 | if iv == nil { |
| 2173 | d.errorstr(errstrCannotDecodeIntoNil) |
| 2174 | return |
| 2175 | } |
| 2176 | if v, ok := iv.(Selfer); ok { |
| 2177 | v.CodecDecodeSelf(d) |
| 2178 | return |
| 2179 | } |
| 2180 | |
| 2181 | switch v := iv.(type) { |
| 2182 | // case nil: |
| 2183 | // case Selfer: |
| 2184 | |
| 2185 | case reflect.Value: |
| 2186 | v = d.ensureDecodeable(v) |
| 2187 | d.decodeValue(v, nil, true) |
| 2188 | |
| 2189 | case *string: |
| 2190 | *v = d.d.DecodeString() |
| 2191 | case *bool: |
| 2192 | *v = d.d.DecodeBool() |
| 2193 | case *int: |
| 2194 | *v = int(chkOvf.IntV(d.d.DecodeInt64(), intBitsize)) |
| 2195 | case *int8: |
| 2196 | *v = int8(chkOvf.IntV(d.d.DecodeInt64(), 8)) |
| 2197 | case *int16: |
| 2198 | *v = int16(chkOvf.IntV(d.d.DecodeInt64(), 16)) |
| 2199 | case *int32: |
| 2200 | *v = int32(chkOvf.IntV(d.d.DecodeInt64(), 32)) |
| 2201 | case *int64: |
| 2202 | *v = d.d.DecodeInt64() |
| 2203 | case *uint: |
| 2204 | *v = uint(chkOvf.UintV(d.d.DecodeUint64(), uintBitsize)) |
| 2205 | case *uint8: |
| 2206 | *v = uint8(chkOvf.UintV(d.d.DecodeUint64(), 8)) |
| 2207 | case *uint16: |
| 2208 | *v = uint16(chkOvf.UintV(d.d.DecodeUint64(), 16)) |
| 2209 | case *uint32: |
| 2210 | *v = uint32(chkOvf.UintV(d.d.DecodeUint64(), 32)) |
| 2211 | case *uint64: |
| 2212 | *v = d.d.DecodeUint64() |
| 2213 | case *float32: |
| 2214 | f64 := d.d.DecodeFloat64() |
| 2215 | if chkOvf.Float32(f64) { |
| 2216 | d.errorf("float32 overflow: %v", f64) |
| 2217 | } |
| 2218 | *v = float32(f64) |
| 2219 | case *float64: |
| 2220 | *v = d.d.DecodeFloat64() |
| 2221 | case *[]uint8: |
| 2222 | *v = d.d.DecodeBytes(*v, false) |
| 2223 | case []uint8: |
| 2224 | b := d.d.DecodeBytes(v, false) |
| 2225 | if !(len(b) > 0 && len(b) == len(v) && &b[0] == &v[0]) { |
| 2226 | copy(v, b) |
| 2227 | } |
| 2228 | case *time.Time: |
| 2229 | *v = d.d.DecodeTime() |
| 2230 | case *Raw: |
| 2231 | *v = d.rawBytes() |
| 2232 | |
| 2233 | case *interface{}: |
| 2234 | d.decodeValue(reflect.ValueOf(iv).Elem(), nil, true) |
| 2235 | // d.decodeValueNotNil(reflect.ValueOf(iv).Elem()) |
| 2236 | |
| 2237 | default: |
| 2238 | if !fastpathDecodeTypeSwitch(iv, d) { |
| 2239 | v := reflect.ValueOf(iv) |
| 2240 | v = d.ensureDecodeable(v) |
| 2241 | d.decodeValue(v, nil, false) |
| 2242 | // d.decodeValueFallback(v) |
| 2243 | } |
| 2244 | } |
| 2245 | } |
| 2246 | |
| 2247 | func (d *Decoder) decodeValue(rv reflect.Value, fn *codecFn, chkAll bool) { |
| 2248 | // If stream is not containing a nil value, then we can deref to the base |
| 2249 | // non-pointer value, and decode into that. |
| 2250 | var rvp reflect.Value |
| 2251 | var rvpValid bool |
| 2252 | if rv.Kind() == reflect.Ptr { |
| 2253 | rvpValid = true |
| 2254 | for { |
| 2255 | if rv.IsNil() { |
| 2256 | rv.Set(reflect.New(rv.Type().Elem())) |
| 2257 | } |
| 2258 | rvp = rv |
| 2259 | rv = rv.Elem() |
| 2260 | if rv.Kind() != reflect.Ptr { |
| 2261 | break |
| 2262 | } |
| 2263 | } |
| 2264 | } |
| 2265 | |
| 2266 | if fn == nil { |
| 2267 | // always pass checkCodecSelfer=true, in case T or ****T is passed, where *T is a Selfer |
| 2268 | fn = d.cfer().get(rv.Type(), chkAll, true) // chkAll, chkAll) |
| 2269 | } |
| 2270 | if fn.i.addrD { |
| 2271 | if rvpValid { |
| 2272 | fn.fd(d, &fn.i, rvp) |
| 2273 | } else if rv.CanAddr() { |
| 2274 | fn.fd(d, &fn.i, rv.Addr()) |
| 2275 | } else if !fn.i.addrF { |
| 2276 | fn.fd(d, &fn.i, rv) |
| 2277 | } else { |
| 2278 | d.errorf("cannot decode into a non-pointer value") |
| 2279 | } |
| 2280 | } else { |
| 2281 | fn.fd(d, &fn.i, rv) |
| 2282 | } |
| 2283 | // return rv |
| 2284 | } |
| 2285 | |
| 2286 | func (d *Decoder) structFieldNotFound(index int, rvkencname string) { |
| 2287 | // NOTE: rvkencname may be a stringView, so don't pass it to another function. |
| 2288 | if d.h.ErrorIfNoField { |
| 2289 | if index >= 0 { |
| 2290 | d.errorf("no matching struct field found when decoding stream array at index %v", index) |
| 2291 | return |
| 2292 | } else if rvkencname != "" { |
| 2293 | d.errorf("no matching struct field found when decoding stream map with key " + rvkencname) |
| 2294 | return |
| 2295 | } |
| 2296 | } |
| 2297 | d.swallow() |
| 2298 | } |
| 2299 | |
| 2300 | func (d *Decoder) arrayCannotExpand(sliceLen, streamLen int) { |
| 2301 | if d.h.ErrorIfNoArrayExpand { |
| 2302 | d.errorf("cannot expand array len during decode from %v to %v", sliceLen, streamLen) |
| 2303 | } |
| 2304 | } |
| 2305 | |
| 2306 | func isDecodeable(rv reflect.Value) (rv2 reflect.Value, canDecode bool) { |
| 2307 | switch rv.Kind() { |
| 2308 | case reflect.Array: |
| 2309 | return rv, true |
| 2310 | case reflect.Ptr: |
| 2311 | if !rv.IsNil() { |
| 2312 | return rv.Elem(), true |
| 2313 | } |
| 2314 | case reflect.Slice, reflect.Chan, reflect.Map: |
| 2315 | if !rv.IsNil() { |
| 2316 | return rv, true |
| 2317 | } |
| 2318 | } |
| 2319 | return |
| 2320 | } |
| 2321 | |
| 2322 | func (d *Decoder) ensureDecodeable(rv reflect.Value) (rv2 reflect.Value) { |
| 2323 | // decode can take any reflect.Value that is a inherently addressable i.e. |
| 2324 | // - array |
| 2325 | // - non-nil chan (we will SEND to it) |
| 2326 | // - non-nil slice (we will set its elements) |
| 2327 | // - non-nil map (we will put into it) |
| 2328 | // - non-nil pointer (we can "update" it) |
| 2329 | rv2, canDecode := isDecodeable(rv) |
| 2330 | if canDecode { |
| 2331 | return |
| 2332 | } |
| 2333 | if !rv.IsValid() { |
| 2334 | d.errorstr(errstrCannotDecodeIntoNil) |
| 2335 | return |
| 2336 | } |
| 2337 | if !rv.CanInterface() { |
| 2338 | d.errorf("cannot decode into a value without an interface: %v", rv) |
| 2339 | return |
| 2340 | } |
| 2341 | rvi := rv2i(rv) |
| 2342 | rvk := rv.Kind() |
| 2343 | d.errorf("cannot decode into value of kind: %v, type: %T, %v", rvk, rvi, rvi) |
| 2344 | return |
| 2345 | } |
| 2346 | |
| 2347 | // Possibly get an interned version of a string |
| 2348 | // |
| 2349 | // This should mostly be used for map keys, where the key type is string. |
| 2350 | // This is because keys of a map/struct are typically reused across many objects. |
| 2351 | func (d *Decoder) string(v []byte) (s string) { |
| 2352 | if d.is == nil { |
| 2353 | return string(v) // don't return stringView, as we need a real string here. |
| 2354 | } |
| 2355 | s, ok := d.is[string(v)] // no allocation here, per go implementation |
| 2356 | if !ok { |
| 2357 | s = string(v) // new allocation here |
| 2358 | d.is[s] = s |
| 2359 | } |
| 2360 | return s |
| 2361 | } |
| 2362 | |
| 2363 | // nextValueBytes returns the next value in the stream as a set of bytes. |
| 2364 | func (d *Decoder) nextValueBytes() (bs []byte) { |
| 2365 | d.d.uncacheRead() |
| 2366 | d.r.track() |
| 2367 | d.swallow() |
| 2368 | bs = d.r.stopTrack() |
| 2369 | return |
| 2370 | } |
| 2371 | |
| 2372 | func (d *Decoder) rawBytes() []byte { |
| 2373 | // ensure that this is not a view into the bytes |
| 2374 | // i.e. make new copy always. |
| 2375 | bs := d.nextValueBytes() |
| 2376 | bs2 := make([]byte, len(bs)) |
| 2377 | copy(bs2, bs) |
| 2378 | return bs2 |
| 2379 | } |
| 2380 | |
| 2381 | func (d *Decoder) wrapErrstr(v interface{}, err *error) { |
| 2382 | *err = fmt.Errorf("%s decode error [pos %d]: %v", d.hh.Name(), d.r.numread(), v) |
| 2383 | } |
| 2384 | |
| 2385 | // -------------------------------------------------- |
| 2386 | |
| 2387 | // decSliceHelper assists when decoding into a slice, from a map or an array in the stream. |
| 2388 | // A slice can be set from a map or array in stream. This supports the MapBySlice interface. |
| 2389 | type decSliceHelper struct { |
| 2390 | d *Decoder |
| 2391 | // ct valueType |
| 2392 | array bool |
| 2393 | } |
| 2394 | |
| 2395 | func (d *Decoder) decSliceHelperStart() (x decSliceHelper, clen int) { |
| 2396 | dd := d.d |
| 2397 | ctyp := dd.ContainerType() |
| 2398 | switch ctyp { |
| 2399 | case valueTypeArray: |
| 2400 | x.array = true |
| 2401 | clen = dd.ReadArrayStart() |
| 2402 | case valueTypeMap: |
| 2403 | clen = dd.ReadMapStart() * 2 |
| 2404 | default: |
| 2405 | d.errorf("only encoded map or array can be decoded into a slice (%d)", ctyp) |
| 2406 | } |
| 2407 | // x.ct = ctyp |
| 2408 | x.d = d |
| 2409 | return |
| 2410 | } |
| 2411 | |
| 2412 | func (x decSliceHelper) End() { |
| 2413 | if x.array { |
| 2414 | x.d.d.ReadArrayEnd() |
| 2415 | } else { |
| 2416 | x.d.d.ReadMapEnd() |
| 2417 | } |
| 2418 | } |
| 2419 | |
| 2420 | func (x decSliceHelper) ElemContainerState(index int) { |
| 2421 | if x.array { |
| 2422 | x.d.d.ReadArrayElem() |
| 2423 | } else if index%2 == 0 { |
| 2424 | x.d.d.ReadMapElemKey() |
| 2425 | } else { |
| 2426 | x.d.d.ReadMapElemValue() |
| 2427 | } |
| 2428 | } |
| 2429 | |
| 2430 | func decByteSlice(r decReader, clen, maxInitLen int, bs []byte) (bsOut []byte) { |
| 2431 | if clen == 0 { |
| 2432 | return zeroByteSlice |
| 2433 | } |
| 2434 | if len(bs) == clen { |
| 2435 | bsOut = bs |
| 2436 | r.readb(bsOut) |
| 2437 | } else if cap(bs) >= clen { |
| 2438 | bsOut = bs[:clen] |
| 2439 | r.readb(bsOut) |
| 2440 | } else { |
| 2441 | // bsOut = make([]byte, clen) |
| 2442 | len2 := decInferLen(clen, maxInitLen, 1) |
| 2443 | bsOut = make([]byte, len2) |
| 2444 | r.readb(bsOut) |
| 2445 | for len2 < clen { |
| 2446 | len3 := decInferLen(clen-len2, maxInitLen, 1) |
| 2447 | bs3 := bsOut |
| 2448 | bsOut = make([]byte, len2+len3) |
| 2449 | copy(bsOut, bs3) |
| 2450 | r.readb(bsOut[len2:]) |
| 2451 | len2 += len3 |
| 2452 | } |
| 2453 | } |
| 2454 | return |
| 2455 | } |
| 2456 | |
| 2457 | func detachZeroCopyBytes(isBytesReader bool, dest []byte, in []byte) (out []byte) { |
| 2458 | if xlen := len(in); xlen > 0 { |
| 2459 | if isBytesReader || xlen <= scratchByteArrayLen { |
| 2460 | if cap(dest) >= xlen { |
| 2461 | out = dest[:xlen] |
| 2462 | } else { |
| 2463 | out = make([]byte, xlen) |
| 2464 | } |
| 2465 | copy(out, in) |
| 2466 | return |
| 2467 | } |
| 2468 | } |
| 2469 | return in |
| 2470 | } |
| 2471 | |
| 2472 | // decInferLen will infer a sensible length, given the following: |
| 2473 | // - clen: length wanted. |
| 2474 | // - maxlen: max length to be returned. |
| 2475 | // if <= 0, it is unset, and we infer it based on the unit size |
| 2476 | // - unit: number of bytes for each element of the collection |
| 2477 | func decInferLen(clen, maxlen, unit int) (rvlen int) { |
| 2478 | // handle when maxlen is not set i.e. <= 0 |
| 2479 | if clen <= 0 { |
| 2480 | return |
| 2481 | } |
| 2482 | if unit == 0 { |
| 2483 | return clen |
| 2484 | } |
| 2485 | if maxlen <= 0 { |
| 2486 | // no maxlen defined. Use maximum of 256K memory, with a floor of 4K items. |
| 2487 | // maxlen = 256 * 1024 / unit |
| 2488 | // if maxlen < (4 * 1024) { |
| 2489 | // maxlen = 4 * 1024 |
| 2490 | // } |
| 2491 | if unit < (256 / 4) { |
| 2492 | maxlen = 256 * 1024 / unit |
| 2493 | } else { |
| 2494 | maxlen = 4 * 1024 |
| 2495 | } |
| 2496 | } |
| 2497 | if clen > maxlen { |
| 2498 | rvlen = maxlen |
| 2499 | } else { |
| 2500 | rvlen = clen |
| 2501 | } |
| 2502 | return |
| 2503 | } |
| 2504 | |
| 2505 | func expandSliceRV(s reflect.Value, st reflect.Type, canChange bool, stElemSize, num, slen, scap int) ( |
| 2506 | s2 reflect.Value, scap2 int, changed bool, err string) { |
| 2507 | l1 := slen + num // new slice length |
| 2508 | if l1 < slen { |
| 2509 | err = errmsgExpandSliceOverflow |
| 2510 | return |
| 2511 | } |
| 2512 | if l1 <= scap { |
| 2513 | if s.CanSet() { |
| 2514 | s.SetLen(l1) |
| 2515 | } else if canChange { |
| 2516 | s2 = s.Slice(0, l1) |
| 2517 | scap2 = scap |
| 2518 | changed = true |
| 2519 | } else { |
| 2520 | err = errmsgExpandSliceCannotChange |
| 2521 | return |
| 2522 | } |
| 2523 | return |
| 2524 | } |
| 2525 | if !canChange { |
| 2526 | err = errmsgExpandSliceCannotChange |
| 2527 | return |
| 2528 | } |
| 2529 | scap2 = growCap(scap, stElemSize, num) |
| 2530 | s2 = reflect.MakeSlice(st, l1, scap2) |
| 2531 | changed = true |
| 2532 | reflect.Copy(s2, s) |
| 2533 | return |
| 2534 | } |
| 2535 | |
| 2536 | func decReadFull(r io.Reader, bs []byte) (n int, err error) { |
| 2537 | var nn int |
| 2538 | for n < len(bs) && err == nil { |
| 2539 | nn, err = r.Read(bs[n:]) |
| 2540 | if nn > 0 { |
| 2541 | if err == io.EOF { |
| 2542 | // leave EOF for next time |
| 2543 | err = nil |
| 2544 | } |
| 2545 | n += nn |
| 2546 | } |
| 2547 | } |
| 2548 | |
| 2549 | // do not do this - it serves no purpose |
| 2550 | // if n != len(bs) && err == io.EOF { err = io.ErrUnexpectedEOF } |
| 2551 | return |
| 2552 | } |