| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package codec |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
import ( |
| 194 |
"encoding" |
| 195 |
"encoding/binary" |
| 196 |
"errors" |
| 197 |
"fmt" |
| 198 |
"io" |
| 199 |
"math" |
| 200 |
"reflect" |
| 201 |
"runtime" |
| 202 |
"sort" |
| 203 |
"strconv" |
| 204 |
"strings" |
| 205 |
"sync" |
| 206 |
"sync/atomic" |
| 207 |
"time" |
| 208 |
"unicode/utf8" |
| 209 |
) |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
const debugging = false |
| 221 |
|
| 222 |
const ( |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
containerLenUnknown = -1 |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
containerLenNil = math.MinInt32 |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
handleBytesWithinKArray = true |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
supportMarshalInterfaces = true |
| 243 |
|
| 244 |
|
| 245 |
bytesFreeListNoCache = false |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
cacheLineSize = 64 |
| 250 |
|
| 251 |
wordSizeBits = 32 << (^uint(0) >> 63) |
| 252 |
wordSize = wordSizeBits / 8 |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
skipFastpathTypeSwitchInDirectCall = false |
| 259 |
) |
| 260 |
|
| 261 |
const cpu32Bit = ^uint(0)>>32 == 0 |
| 262 |
|
| 263 |
type rkind byte |
| 264 |
|
| 265 |
const ( |
| 266 |
rkindPtr = rkind(reflect.Ptr) |
| 267 |
rkindString = rkind(reflect.String) |
| 268 |
rkindChan = rkind(reflect.Chan) |
| 269 |
) |
| 270 |
|
| 271 |
type mapKeyFastKind uint8 |
| 272 |
|
| 273 |
const ( |
| 274 |
mapKeyFastKind32 = iota + 1 |
| 275 |
mapKeyFastKind32ptr |
| 276 |
mapKeyFastKind64 |
| 277 |
mapKeyFastKind64ptr |
| 278 |
mapKeyFastKindStr |
| 279 |
) |
| 280 |
|
| 281 |
var ( |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
handleInitMu sync.Mutex |
| 286 |
|
| 287 |
must mustHdl |
| 288 |
halt panicHdl |
| 289 |
|
| 290 |
digitCharBitset bitset256 |
| 291 |
numCharBitset bitset256 |
| 292 |
whitespaceCharBitset bitset256 |
| 293 |
asciiAlphaNumBitset bitset256 |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
refBitset bitset32 |
| 304 |
|
| 305 |
|
| 306 |
isnilBitset bitset32 |
| 307 |
|
| 308 |
|
| 309 |
numBoolBitset bitset32 |
| 310 |
|
| 311 |
|
| 312 |
numBoolStrSliceBitset bitset32 |
| 313 |
|
| 314 |
|
| 315 |
scalarBitset bitset32 |
| 316 |
|
| 317 |
mapKeyFastKindVals [32]mapKeyFastKind |
| 318 |
|
| 319 |
|
| 320 |
codecgen bool |
| 321 |
|
| 322 |
oneByteArr [1]byte |
| 323 |
zeroByteSlice = oneByteArr[:0:0] |
| 324 |
|
| 325 |
eofReader devNullReader |
| 326 |
) |
| 327 |
|
| 328 |
var ( |
| 329 |
errMapTypeNotMapKind = errors.New("MapType MUST be of Map Kind") |
| 330 |
errSliceTypeNotSliceKind = errors.New("SliceType MUST be of Slice Kind") |
| 331 |
|
| 332 |
errExtFnWriteExtUnsupported = errors.New("BytesExt.WriteExt is not supported") |
| 333 |
errExtFnReadExtUnsupported = errors.New("BytesExt.ReadExt is not supported") |
| 334 |
errExtFnConvertExtUnsupported = errors.New("InterfaceExt.ConvertExt is not supported") |
| 335 |
errExtFnUpdateExtUnsupported = errors.New("InterfaceExt.UpdateExt is not supported") |
| 336 |
|
| 337 |
errPanicUndefined = errors.New("panic: undefined error") |
| 338 |
|
| 339 |
errHandleInited = errors.New("cannot modify initialized Handle") |
| 340 |
|
| 341 |
errNoFormatHandle = errors.New("no handle (cannot identify format)") |
| 342 |
) |
| 343 |
|
| 344 |
var pool4tiload = sync.Pool{ |
| 345 |
New: func() interface{} { |
| 346 |
return &typeInfoLoad{ |
| 347 |
etypes: make([]uintptr, 0, 4), |
| 348 |
sfis: make([]structFieldInfo, 0, 4), |
| 349 |
sfiNames: make(map[string]uint16, 4), |
| 350 |
} |
| 351 |
}, |
| 352 |
} |
| 353 |
|
| 354 |
func init() { |
| 355 |
xx := func(f mapKeyFastKind, k ...reflect.Kind) { |
| 356 |
for _, v := range k { |
| 357 |
mapKeyFastKindVals[byte(v)&31] = f |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
var f mapKeyFastKind |
| 362 |
|
| 363 |
f = mapKeyFastKind64 |
| 364 |
if wordSizeBits == 32 { |
| 365 |
f = mapKeyFastKind32 |
| 366 |
} |
| 367 |
xx(f, reflect.Int, reflect.Uint, reflect.Uintptr) |
| 368 |
|
| 369 |
f = mapKeyFastKind64ptr |
| 370 |
if wordSizeBits == 32 { |
| 371 |
f = mapKeyFastKind32ptr |
| 372 |
} |
| 373 |
xx(f, reflect.Ptr) |
| 374 |
|
| 375 |
xx(mapKeyFastKindStr, reflect.String) |
| 376 |
xx(mapKeyFastKind32, reflect.Uint32, reflect.Int32, reflect.Float32) |
| 377 |
xx(mapKeyFastKind64, reflect.Uint64, reflect.Int64, reflect.Float64) |
| 378 |
|
| 379 |
numBoolBitset. |
| 380 |
set(byte(reflect.Bool)). |
| 381 |
set(byte(reflect.Int)). |
| 382 |
set(byte(reflect.Int8)). |
| 383 |
set(byte(reflect.Int16)). |
| 384 |
set(byte(reflect.Int32)). |
| 385 |
set(byte(reflect.Int64)). |
| 386 |
set(byte(reflect.Uint)). |
| 387 |
set(byte(reflect.Uint8)). |
| 388 |
set(byte(reflect.Uint16)). |
| 389 |
set(byte(reflect.Uint32)). |
| 390 |
set(byte(reflect.Uint64)). |
| 391 |
set(byte(reflect.Uintptr)). |
| 392 |
set(byte(reflect.Float32)). |
| 393 |
set(byte(reflect.Float64)). |
| 394 |
set(byte(reflect.Complex64)). |
| 395 |
set(byte(reflect.Complex128)) |
| 396 |
|
| 397 |
numBoolStrSliceBitset = numBoolBitset |
| 398 |
|
| 399 |
numBoolStrSliceBitset. |
| 400 |
set(byte(reflect.String)). |
| 401 |
set(byte(reflect.Slice)) |
| 402 |
|
| 403 |
scalarBitset = numBoolBitset |
| 404 |
|
| 405 |
scalarBitset. |
| 406 |
set(byte(reflect.String)) |
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
refBitset. |
| 411 |
set(byte(reflect.Map)). |
| 412 |
set(byte(reflect.Ptr)). |
| 413 |
set(byte(reflect.Func)). |
| 414 |
set(byte(reflect.Chan)). |
| 415 |
set(byte(reflect.UnsafePointer)) |
| 416 |
|
| 417 |
isnilBitset = refBitset |
| 418 |
|
| 419 |
isnilBitset. |
| 420 |
set(byte(reflect.Interface)). |
| 421 |
set(byte(reflect.Slice)) |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
for i := byte(0); i <= utf8.RuneSelf; i++ { |
| 429 |
if (i >= '0' && i <= '9') || (i >= 'a' && i <= 'z') || (i >= 'A' && i <= 'Z') { |
| 430 |
asciiAlphaNumBitset.set(i) |
| 431 |
} |
| 432 |
switch i { |
| 433 |
case ' ', '\t', '\r', '\n': |
| 434 |
whitespaceCharBitset.set(i) |
| 435 |
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9': |
| 436 |
digitCharBitset.set(i) |
| 437 |
numCharBitset.set(i) |
| 438 |
case '.', '+', '-': |
| 439 |
numCharBitset.set(i) |
| 440 |
case 'e', 'E': |
| 441 |
numCharBitset.set(i) |
| 442 |
} |
| 443 |
} |
| 444 |
} |
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
type driverStateManager interface { |
| 451 |
resetState() |
| 452 |
captureState() interface{} |
| 453 |
restoreState(state interface{}) |
| 454 |
} |
| 455 |
|
| 456 |
type bdAndBdread struct { |
| 457 |
bdRead bool |
| 458 |
bd byte |
| 459 |
} |
| 460 |
|
| 461 |
func (x bdAndBdread) captureState() interface{} { return x } |
| 462 |
func (x *bdAndBdread) resetState() { x.bd, x.bdRead = 0, false } |
| 463 |
func (x *bdAndBdread) reset() { x.resetState() } |
| 464 |
func (x *bdAndBdread) restoreState(v interface{}) { *x = v.(bdAndBdread) } |
| 465 |
|
| 466 |
type clsErr struct { |
| 467 |
err error |
| 468 |
closed bool |
| 469 |
} |
| 470 |
|
| 471 |
type charEncoding uint8 |
| 472 |
|
| 473 |
const ( |
| 474 |
_ charEncoding = iota |
| 475 |
cUTF8 |
| 476 |
cUTF16LE |
| 477 |
cUTF16BE |
| 478 |
cUTF32LE |
| 479 |
cUTF32BE |
| 480 |
|
| 481 |
cRAW charEncoding = 255 |
| 482 |
) |
| 483 |
|
| 484 |
|
| 485 |
type valueType uint8 |
| 486 |
|
| 487 |
const ( |
| 488 |
valueTypeUnset valueType = iota |
| 489 |
valueTypeNil |
| 490 |
valueTypeInt |
| 491 |
valueTypeUint |
| 492 |
valueTypeFloat |
| 493 |
valueTypeBool |
| 494 |
valueTypeString |
| 495 |
valueTypeSymbol |
| 496 |
valueTypeBytes |
| 497 |
valueTypeMap |
| 498 |
valueTypeArray |
| 499 |
valueTypeTime |
| 500 |
valueTypeExt |
| 501 |
|
| 502 |
|
| 503 |
) |
| 504 |
|
| 505 |
var valueTypeStrings = [...]string{ |
| 506 |
"Unset", |
| 507 |
"Nil", |
| 508 |
"Int", |
| 509 |
"Uint", |
| 510 |
"Float", |
| 511 |
"Bool", |
| 512 |
"String", |
| 513 |
"Symbol", |
| 514 |
"Bytes", |
| 515 |
"Map", |
| 516 |
"Array", |
| 517 |
"Timestamp", |
| 518 |
"Ext", |
| 519 |
} |
| 520 |
|
| 521 |
func (x valueType) String() string { |
| 522 |
if int(x) < len(valueTypeStrings) { |
| 523 |
return valueTypeStrings[x] |
| 524 |
} |
| 525 |
return strconv.FormatInt(int64(x), 10) |
| 526 |
} |
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
type containerState uint8 |
| 531 |
|
| 532 |
const ( |
| 533 |
_ containerState = iota |
| 534 |
|
| 535 |
containerMapStart |
| 536 |
containerMapKey |
| 537 |
containerMapValue |
| 538 |
containerMapEnd |
| 539 |
containerArrayStart |
| 540 |
containerArrayElem |
| 541 |
containerArrayEnd |
| 542 |
) |
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
const rgetMaxRecursion = 2 |
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
type fauxUnion struct { |
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
u uint64 |
| 564 |
i int64 |
| 565 |
f float64 |
| 566 |
l []byte |
| 567 |
s string |
| 568 |
|
| 569 |
|
| 570 |
t time.Time |
| 571 |
b bool |
| 572 |
|
| 573 |
|
| 574 |
v valueType |
| 575 |
} |
| 576 |
|
| 577 |
|
| 578 |
type typeInfoLoad struct { |
| 579 |
etypes []uintptr |
| 580 |
sfis []structFieldInfo |
| 581 |
sfiNames map[string]uint16 |
| 582 |
} |
| 583 |
|
| 584 |
func (x *typeInfoLoad) reset() { |
| 585 |
x.etypes = x.etypes[:0] |
| 586 |
x.sfis = x.sfis[:0] |
| 587 |
for k := range x.sfiNames { |
| 588 |
delete(x.sfiNames, k) |
| 589 |
} |
| 590 |
} |
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
type jsonMarshaler interface { |
| 596 |
MarshalJSON() ([]byte, error) |
| 597 |
} |
| 598 |
type jsonUnmarshaler interface { |
| 599 |
UnmarshalJSON([]byte) error |
| 600 |
} |
| 601 |
|
| 602 |
type isZeroer interface { |
| 603 |
IsZero() bool |
| 604 |
} |
| 605 |
|
| 606 |
type isCodecEmptyer interface { |
| 607 |
IsCodecEmpty() bool |
| 608 |
} |
| 609 |
|
| 610 |
type codecError struct { |
| 611 |
err error |
| 612 |
name string |
| 613 |
pos int |
| 614 |
encode bool |
| 615 |
} |
| 616 |
|
| 617 |
func (e *codecError) Cause() error { |
| 618 |
return e.err |
| 619 |
} |
| 620 |
|
| 621 |
func (e *codecError) Unwrap() error { |
| 622 |
return e.err |
| 623 |
} |
| 624 |
|
| 625 |
func (e *codecError) Error() string { |
| 626 |
if e.encode { |
| 627 |
return fmt.Sprintf("%s encode error: %v", e.name, e.err) |
| 628 |
} |
| 629 |
return fmt.Sprintf("%s decode error [pos %d]: %v", e.name, e.pos, e.err) |
| 630 |
} |
| 631 |
|
| 632 |
func wrapCodecErr(in error, name string, numbytesread int, encode bool) (out error) { |
| 633 |
x, ok := in.(*codecError) |
| 634 |
if ok && x.pos == numbytesread && x.name == name && x.encode == encode { |
| 635 |
return in |
| 636 |
} |
| 637 |
return &codecError{in, name, numbytesread, encode} |
| 638 |
} |
| 639 |
|
| 640 |
var ( |
| 641 |
bigen bigenHelper |
| 642 |
|
| 643 |
bigenstd = binary.BigEndian |
| 644 |
|
| 645 |
structInfoFieldName = "_struct" |
| 646 |
|
| 647 |
mapStrIntfTyp = reflect.TypeOf(map[string]interface{}(nil)) |
| 648 |
mapIntfIntfTyp = reflect.TypeOf(map[interface{}]interface{}(nil)) |
| 649 |
intfSliceTyp = reflect.TypeOf([]interface{}(nil)) |
| 650 |
intfTyp = intfSliceTyp.Elem() |
| 651 |
|
| 652 |
reflectValTyp = reflect.TypeOf((*reflect.Value)(nil)).Elem() |
| 653 |
|
| 654 |
stringTyp = reflect.TypeOf("") |
| 655 |
timeTyp = reflect.TypeOf(time.Time{}) |
| 656 |
rawExtTyp = reflect.TypeOf(RawExt{}) |
| 657 |
rawTyp = reflect.TypeOf(Raw{}) |
| 658 |
uintptrTyp = reflect.TypeOf(uintptr(0)) |
| 659 |
uint8Typ = reflect.TypeOf(uint8(0)) |
| 660 |
uint8SliceTyp = reflect.TypeOf([]uint8(nil)) |
| 661 |
uintTyp = reflect.TypeOf(uint(0)) |
| 662 |
intTyp = reflect.TypeOf(int(0)) |
| 663 |
|
| 664 |
mapBySliceTyp = reflect.TypeOf((*MapBySlice)(nil)).Elem() |
| 665 |
|
| 666 |
binaryMarshalerTyp = reflect.TypeOf((*encoding.BinaryMarshaler)(nil)).Elem() |
| 667 |
binaryUnmarshalerTyp = reflect.TypeOf((*encoding.BinaryUnmarshaler)(nil)).Elem() |
| 668 |
|
| 669 |
textMarshalerTyp = reflect.TypeOf((*encoding.TextMarshaler)(nil)).Elem() |
| 670 |
textUnmarshalerTyp = reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem() |
| 671 |
|
| 672 |
jsonMarshalerTyp = reflect.TypeOf((*jsonMarshaler)(nil)).Elem() |
| 673 |
jsonUnmarshalerTyp = reflect.TypeOf((*jsonUnmarshaler)(nil)).Elem() |
| 674 |
|
| 675 |
selferTyp = reflect.TypeOf((*Selfer)(nil)).Elem() |
| 676 |
missingFielderTyp = reflect.TypeOf((*MissingFielder)(nil)).Elem() |
| 677 |
iszeroTyp = reflect.TypeOf((*isZeroer)(nil)).Elem() |
| 678 |
isCodecEmptyerTyp = reflect.TypeOf((*isCodecEmptyer)(nil)).Elem() |
| 679 |
isSelferViaCodecgenerTyp = reflect.TypeOf((*isSelferViaCodecgener)(nil)).Elem() |
| 680 |
|
| 681 |
uint8TypId = rt2id(uint8Typ) |
| 682 |
uint8SliceTypId = rt2id(uint8SliceTyp) |
| 683 |
rawExtTypId = rt2id(rawExtTyp) |
| 684 |
rawTypId = rt2id(rawTyp) |
| 685 |
intfTypId = rt2id(intfTyp) |
| 686 |
timeTypId = rt2id(timeTyp) |
| 687 |
stringTypId = rt2id(stringTyp) |
| 688 |
|
| 689 |
mapStrIntfTypId = rt2id(mapStrIntfTyp) |
| 690 |
mapIntfIntfTypId = rt2id(mapIntfIntfTyp) |
| 691 |
intfSliceTypId = rt2id(intfSliceTyp) |
| 692 |
|
| 693 |
|
| 694 |
intBitsize = uint8(intTyp.Bits()) |
| 695 |
uintBitsize = uint8(uintTyp.Bits()) |
| 696 |
|
| 697 |
|
| 698 |
bsAll0xff = []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff} |
| 699 |
|
| 700 |
chkOvf checkOverflow |
| 701 |
) |
| 702 |
|
| 703 |
var defTypeInfos = NewTypeInfos([]string{"codec", "json"}) |
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
var SelfExt = &extFailWrapper{} |
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
type Selfer interface { |
| 732 |
CodecEncodeSelf(*Encoder) |
| 733 |
CodecDecodeSelf(*Decoder) |
| 734 |
} |
| 735 |
|
| 736 |
type isSelferViaCodecgener interface { |
| 737 |
codecSelferViaCodecgen() |
| 738 |
} |
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
type MissingFielder interface { |
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
CodecMissingField(field []byte, value interface{}) bool |
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
CodecMissingFields() map[string]interface{} |
| 759 |
} |
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
type MapBySlice interface { |
| 783 |
MapBySlice() |
| 784 |
} |
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
type basicHandleRuntimeState struct { |
| 794 |
|
| 795 |
|
| 796 |
rtidFns atomicRtidFnSlice |
| 797 |
rtidFnsNoExt atomicRtidFnSlice |
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
extHandle |
| 804 |
|
| 805 |
intf2impls |
| 806 |
|
| 807 |
mu sync.Mutex |
| 808 |
|
| 809 |
jsonHandle bool |
| 810 |
binaryHandle bool |
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
timeBuiltin bool |
| 816 |
_ bool |
| 817 |
} |
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
type BasicHandle struct { |
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
TypeInfos *TypeInfos |
| 830 |
|
| 831 |
*basicHandleRuntimeState |
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
DecodeOptions |
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
EncodeOptions |
| 840 |
|
| 841 |
RPCOptions |
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
TimeNotBuiltin bool |
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
ExplicitRelease bool |
| 881 |
|
| 882 |
|
| 883 |
inited uint32 |
| 884 |
|
| 885 |
} |
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
func initHandle(hh Handle) { |
| 891 |
x := hh.getBasicHandle() |
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
if atomic.LoadUint32(&x.inited) == 0 { |
| 906 |
x.initHandle(hh) |
| 907 |
} |
| 908 |
} |
| 909 |
|
| 910 |
func (x *BasicHandle) basicInit() { |
| 911 |
x.rtidFns.store(nil) |
| 912 |
x.rtidFnsNoExt.store(nil) |
| 913 |
x.timeBuiltin = !x.TimeNotBuiltin |
| 914 |
} |
| 915 |
|
| 916 |
func (x *BasicHandle) init() {} |
| 917 |
|
| 918 |
func (x *BasicHandle) isInited() bool { |
| 919 |
return atomic.LoadUint32(&x.inited) != 0 |
| 920 |
} |
| 921 |
|
| 922 |
|
| 923 |
func (x *BasicHandle) clearInited() { |
| 924 |
atomic.StoreUint32(&x.inited, 0) |
| 925 |
} |
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
func (x *basicHandleRuntimeState) TimeBuiltin() bool { |
| 930 |
return x.timeBuiltin |
| 931 |
} |
| 932 |
|
| 933 |
func (x *basicHandleRuntimeState) isJs() bool { |
| 934 |
return x.jsonHandle |
| 935 |
} |
| 936 |
|
| 937 |
func (x *basicHandleRuntimeState) isBe() bool { |
| 938 |
return x.binaryHandle |
| 939 |
} |
| 940 |
|
| 941 |
func (x *basicHandleRuntimeState) setExt(rt reflect.Type, tag uint64, ext Ext) (err error) { |
| 942 |
rk := rt.Kind() |
| 943 |
for rk == reflect.Ptr { |
| 944 |
rt = rt.Elem() |
| 945 |
rk = rt.Kind() |
| 946 |
} |
| 947 |
|
| 948 |
if rt.PkgPath() == "" || rk == reflect.Interface { |
| 949 |
return fmt.Errorf("codec.Handle.SetExt: Takes named type, not a pointer or interface: %v", rt) |
| 950 |
} |
| 951 |
|
| 952 |
rtid := rt2id(rt) |
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
switch rtid { |
| 957 |
case rawTypId, rawExtTypId: |
| 958 |
return |
| 959 |
case timeTypId: |
| 960 |
if x.timeBuiltin { |
| 961 |
return |
| 962 |
} |
| 963 |
} |
| 964 |
|
| 965 |
for i := range x.extHandle { |
| 966 |
v := &x.extHandle[i] |
| 967 |
if v.rtid == rtid { |
| 968 |
v.tag, v.ext = tag, ext |
| 969 |
return |
| 970 |
} |
| 971 |
} |
| 972 |
rtidptr := rt2id(reflect.PtrTo(rt)) |
| 973 |
x.extHandle = append(x.extHandle, extTypeTagFn{rtid, rtidptr, rt, tag, ext}) |
| 974 |
return |
| 975 |
} |
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
func (x *BasicHandle) initHandle(hh Handle) { |
| 982 |
handleInitMu.Lock() |
| 983 |
defer handleInitMu.Unlock() |
| 984 |
if x.inited == 0 { |
| 985 |
if x.basicHandleRuntimeState == nil { |
| 986 |
x.basicHandleRuntimeState = new(basicHandleRuntimeState) |
| 987 |
} |
| 988 |
x.jsonHandle = hh.isJson() |
| 989 |
x.binaryHandle = hh.isBinary() |
| 990 |
|
| 991 |
if x.MapType != nil && x.MapType.Kind() != reflect.Map { |
| 992 |
halt.onerror(errMapTypeNotMapKind) |
| 993 |
} |
| 994 |
if x.SliceType != nil && x.SliceType.Kind() != reflect.Slice { |
| 995 |
halt.onerror(errSliceTypeNotSliceKind) |
| 996 |
} |
| 997 |
x.basicInit() |
| 998 |
hh.init() |
| 999 |
atomic.StoreUint32(&x.inited, 1) |
| 1000 |
} |
| 1001 |
} |
| 1002 |
|
| 1003 |
func (x *BasicHandle) getBasicHandle() *BasicHandle { |
| 1004 |
return x |
| 1005 |
} |
| 1006 |
|
| 1007 |
func (x *BasicHandle) typeInfos() *TypeInfos { |
| 1008 |
if x.TypeInfos != nil { |
| 1009 |
return x.TypeInfos |
| 1010 |
} |
| 1011 |
return defTypeInfos |
| 1012 |
} |
| 1013 |
|
| 1014 |
func (x *BasicHandle) getTypeInfo(rtid uintptr, rt reflect.Type) (pti *typeInfo) { |
| 1015 |
return x.typeInfos().get(rtid, rt) |
| 1016 |
} |
| 1017 |
|
| 1018 |
func findRtidFn(s []codecRtidFn, rtid uintptr) (i uint, fn *codecFn) { |
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
|
| 1023 |
var h uint |
| 1024 |
var j = uint(len(s)) |
| 1025 |
LOOP: |
| 1026 |
if i < j { |
| 1027 |
h = (i + j) >> 1 |
| 1028 |
if s[h].rtid < rtid { |
| 1029 |
i = h + 1 |
| 1030 |
} else { |
| 1031 |
j = h |
| 1032 |
} |
| 1033 |
goto LOOP |
| 1034 |
} |
| 1035 |
if i < uint(len(s)) && s[i].rtid == rtid { |
| 1036 |
fn = s[i].fn |
| 1037 |
} |
| 1038 |
return |
| 1039 |
} |
| 1040 |
|
| 1041 |
func (x *BasicHandle) fn(rt reflect.Type) (fn *codecFn) { |
| 1042 |
return x.fnVia(rt, x.typeInfos(), &x.rtidFns, x.CheckCircularRef, true) |
| 1043 |
} |
| 1044 |
|
| 1045 |
func (x *BasicHandle) fnNoExt(rt reflect.Type) (fn *codecFn) { |
| 1046 |
return x.fnVia(rt, x.typeInfos(), &x.rtidFnsNoExt, x.CheckCircularRef, false) |
| 1047 |
} |
| 1048 |
|
| 1049 |
func (x *basicHandleRuntimeState) fnVia(rt reflect.Type, tinfos *TypeInfos, fs *atomicRtidFnSlice, checkCircularRef, checkExt bool) (fn *codecFn) { |
| 1050 |
rtid := rt2id(rt) |
| 1051 |
sp := fs.load() |
| 1052 |
if sp != nil { |
| 1053 |
if _, fn = findRtidFn(sp, rtid); fn != nil { |
| 1054 |
return |
| 1055 |
} |
| 1056 |
} |
| 1057 |
|
| 1058 |
fn = x.fnLoad(rt, rtid, tinfos, checkCircularRef, checkExt) |
| 1059 |
x.mu.Lock() |
| 1060 |
sp = fs.load() |
| 1061 |
|
| 1062 |
|
| 1063 |
if sp == nil { |
| 1064 |
sp = []codecRtidFn{{rtid, fn}} |
| 1065 |
fs.store(sp) |
| 1066 |
} else { |
| 1067 |
idx, fn2 := findRtidFn(sp, rtid) |
| 1068 |
if fn2 == nil { |
| 1069 |
sp2 := make([]codecRtidFn, len(sp)+1) |
| 1070 |
copy(sp2[idx+1:], sp[idx:]) |
| 1071 |
copy(sp2, sp[:idx]) |
| 1072 |
sp2[idx] = codecRtidFn{rtid, fn} |
| 1073 |
fs.store(sp2) |
| 1074 |
} |
| 1075 |
} |
| 1076 |
x.mu.Unlock() |
| 1077 |
return |
| 1078 |
} |
| 1079 |
|
| 1080 |
func fnloadFastpathUnderlying(ti *typeInfo) (f *fastpathE, u reflect.Type) { |
| 1081 |
var rtid uintptr |
| 1082 |
var idx int |
| 1083 |
rtid = rt2id(ti.fastpathUnderlying) |
| 1084 |
idx = fastpathAvIndex(rtid) |
| 1085 |
if idx == -1 { |
| 1086 |
return |
| 1087 |
} |
| 1088 |
f = &fastpathAv[idx] |
| 1089 |
if uint8(reflect.Array) == ti.kind { |
| 1090 |
u = reflectArrayOf(ti.rt.Len(), ti.elem) |
| 1091 |
} else { |
| 1092 |
u = f.rt |
| 1093 |
} |
| 1094 |
return |
| 1095 |
} |
| 1096 |
|
| 1097 |
func (x *basicHandleRuntimeState) fnLoad(rt reflect.Type, rtid uintptr, tinfos *TypeInfos, checkCircularRef, checkExt bool) (fn *codecFn) { |
| 1098 |
fn = new(codecFn) |
| 1099 |
fi := &(fn.i) |
| 1100 |
ti := tinfos.get(rtid, rt) |
| 1101 |
fi.ti = ti |
| 1102 |
rk := reflect.Kind(ti.kind) |
| 1103 |
|
| 1104 |
|
| 1105 |
|
| 1106 |
|
| 1107 |
|
| 1108 |
fi.addrDf = true |
| 1109 |
|
| 1110 |
|
| 1111 |
if rtid == timeTypId && x.timeBuiltin { |
| 1112 |
fn.fe = (*Encoder).kTime |
| 1113 |
fn.fd = (*Decoder).kTime |
| 1114 |
} else if rtid == rawTypId { |
| 1115 |
fn.fe = (*Encoder).raw |
| 1116 |
fn.fd = (*Decoder).raw |
| 1117 |
} else if rtid == rawExtTypId { |
| 1118 |
fn.fe = (*Encoder).rawExt |
| 1119 |
fn.fd = (*Decoder).rawExt |
| 1120 |
fi.addrD = true |
| 1121 |
fi.addrE = true |
| 1122 |
} else if xfFn := x.getExt(rtid, checkExt); xfFn != nil { |
| 1123 |
fi.xfTag, fi.xfFn = xfFn.tag, xfFn.ext |
| 1124 |
fn.fe = (*Encoder).ext |
| 1125 |
fn.fd = (*Decoder).ext |
| 1126 |
fi.addrD = true |
| 1127 |
if rk == reflect.Struct || rk == reflect.Array { |
| 1128 |
fi.addrE = true |
| 1129 |
} |
| 1130 |
} else if (ti.flagSelfer || ti.flagSelferPtr) && |
| 1131 |
!(checkCircularRef && ti.flagSelferViaCodecgen && ti.kind == byte(reflect.Struct)) { |
| 1132 |
|
| 1133 |
fn.fe = (*Encoder).selferMarshal |
| 1134 |
fn.fd = (*Decoder).selferUnmarshal |
| 1135 |
fi.addrD = ti.flagSelferPtr |
| 1136 |
fi.addrE = ti.flagSelferPtr |
| 1137 |
} else if supportMarshalInterfaces && x.isBe() && |
| 1138 |
(ti.flagBinaryMarshaler || ti.flagBinaryMarshalerPtr) && |
| 1139 |
(ti.flagBinaryUnmarshaler || ti.flagBinaryUnmarshalerPtr) { |
| 1140 |
fn.fe = (*Encoder).binaryMarshal |
| 1141 |
fn.fd = (*Decoder).binaryUnmarshal |
| 1142 |
fi.addrD = ti.flagBinaryUnmarshalerPtr |
| 1143 |
fi.addrE = ti.flagBinaryMarshalerPtr |
| 1144 |
} else if supportMarshalInterfaces && !x.isBe() && x.isJs() && |
| 1145 |
(ti.flagJsonMarshaler || ti.flagJsonMarshalerPtr) && |
| 1146 |
(ti.flagJsonUnmarshaler || ti.flagJsonUnmarshalerPtr) { |
| 1147 |
|
| 1148 |
fn.fe = (*Encoder).jsonMarshal |
| 1149 |
fn.fd = (*Decoder).jsonUnmarshal |
| 1150 |
fi.addrD = ti.flagJsonUnmarshalerPtr |
| 1151 |
fi.addrE = ti.flagJsonMarshalerPtr |
| 1152 |
} else if supportMarshalInterfaces && !x.isBe() && |
| 1153 |
(ti.flagTextMarshaler || ti.flagTextMarshalerPtr) && |
| 1154 |
(ti.flagTextUnmarshaler || ti.flagTextUnmarshalerPtr) { |
| 1155 |
fn.fe = (*Encoder).textMarshal |
| 1156 |
fn.fd = (*Decoder).textUnmarshal |
| 1157 |
fi.addrD = ti.flagTextUnmarshalerPtr |
| 1158 |
fi.addrE = ti.flagTextMarshalerPtr |
| 1159 |
} else { |
| 1160 |
if fastpathEnabled && (rk == reflect.Map || rk == reflect.Slice || rk == reflect.Array) { |
| 1161 |
|
| 1162 |
|
| 1163 |
|
| 1164 |
|
| 1165 |
|
| 1166 |
|
| 1167 |
|
| 1168 |
|
| 1169 |
|
| 1170 |
|
| 1171 |
|
| 1172 |
var rtid2 uintptr |
| 1173 |
if !ti.flagHasPkgPath { |
| 1174 |
rtid2 = rtid |
| 1175 |
if rk == reflect.Array { |
| 1176 |
rtid2 = rt2id(ti.key) |
| 1177 |
} |
| 1178 |
if idx := fastpathAvIndex(rtid2); idx != -1 { |
| 1179 |
fn.fe = fastpathAv[idx].encfn |
| 1180 |
fn.fd = fastpathAv[idx].decfn |
| 1181 |
fi.addrD = true |
| 1182 |
fi.addrDf = false |
| 1183 |
if rk == reflect.Array { |
| 1184 |
fi.addrD = false |
| 1185 |
} |
| 1186 |
} |
| 1187 |
} else { |
| 1188 |
|
| 1189 |
xfe, xrt := fnloadFastpathUnderlying(ti) |
| 1190 |
if xfe != nil { |
| 1191 |
xfnf := xfe.encfn |
| 1192 |
xfnf2 := xfe.decfn |
| 1193 |
if rk == reflect.Array { |
| 1194 |
fi.addrD = false |
| 1195 |
fn.fd = func(d *Decoder, xf *codecFnInfo, xrv reflect.Value) { |
| 1196 |
xfnf2(d, xf, rvConvert(xrv, xrt)) |
| 1197 |
} |
| 1198 |
} else { |
| 1199 |
fi.addrD = true |
| 1200 |
fi.addrDf = false |
| 1201 |
xptr2rt := reflect.PtrTo(xrt) |
| 1202 |
fn.fd = func(d *Decoder, xf *codecFnInfo, xrv reflect.Value) { |
| 1203 |
if xrv.Kind() == reflect.Ptr { |
| 1204 |
xfnf2(d, xf, rvConvert(xrv, xptr2rt)) |
| 1205 |
} else { |
| 1206 |
xfnf2(d, xf, rvConvert(xrv, xrt)) |
| 1207 |
} |
| 1208 |
} |
| 1209 |
} |
| 1210 |
fn.fe = func(e *Encoder, xf *codecFnInfo, xrv reflect.Value) { |
| 1211 |
xfnf(e, xf, rvConvert(xrv, xrt)) |
| 1212 |
} |
| 1213 |
} |
| 1214 |
} |
| 1215 |
} |
| 1216 |
if fn.fe == nil && fn.fd == nil { |
| 1217 |
switch rk { |
| 1218 |
case reflect.Bool: |
| 1219 |
fn.fe = (*Encoder).kBool |
| 1220 |
fn.fd = (*Decoder).kBool |
| 1221 |
case reflect.String: |
| 1222 |
|
| 1223 |
|
| 1224 |
|
| 1225 |
|
| 1226 |
|
| 1227 |
|
| 1228 |
|
| 1229 |
|
| 1230 |
|
| 1231 |
|
| 1232 |
fn.fe = (*Encoder).kString |
| 1233 |
fn.fd = (*Decoder).kString |
| 1234 |
case reflect.Int: |
| 1235 |
fn.fd = (*Decoder).kInt |
| 1236 |
fn.fe = (*Encoder).kInt |
| 1237 |
case reflect.Int8: |
| 1238 |
fn.fe = (*Encoder).kInt8 |
| 1239 |
fn.fd = (*Decoder).kInt8 |
| 1240 |
case reflect.Int16: |
| 1241 |
fn.fe = (*Encoder).kInt16 |
| 1242 |
fn.fd = (*Decoder).kInt16 |
| 1243 |
case reflect.Int32: |
| 1244 |
fn.fe = (*Encoder).kInt32 |
| 1245 |
fn.fd = (*Decoder).kInt32 |
| 1246 |
case reflect.Int64: |
| 1247 |
fn.fe = (*Encoder).kInt64 |
| 1248 |
fn.fd = (*Decoder).kInt64 |
| 1249 |
case reflect.Uint: |
| 1250 |
fn.fd = (*Decoder).kUint |
| 1251 |
fn.fe = (*Encoder).kUint |
| 1252 |
case reflect.Uint8: |
| 1253 |
fn.fe = (*Encoder).kUint8 |
| 1254 |
fn.fd = (*Decoder).kUint8 |
| 1255 |
case reflect.Uint16: |
| 1256 |
fn.fe = (*Encoder).kUint16 |
| 1257 |
fn.fd = (*Decoder).kUint16 |
| 1258 |
case reflect.Uint32: |
| 1259 |
fn.fe = (*Encoder).kUint32 |
| 1260 |
fn.fd = (*Decoder).kUint32 |
| 1261 |
case reflect.Uint64: |
| 1262 |
fn.fe = (*Encoder).kUint64 |
| 1263 |
fn.fd = (*Decoder).kUint64 |
| 1264 |
case reflect.Uintptr: |
| 1265 |
fn.fe = (*Encoder).kUintptr |
| 1266 |
fn.fd = (*Decoder).kUintptr |
| 1267 |
case reflect.Float32: |
| 1268 |
fn.fe = (*Encoder).kFloat32 |
| 1269 |
fn.fd = (*Decoder).kFloat32 |
| 1270 |
case reflect.Float64: |
| 1271 |
fn.fe = (*Encoder).kFloat64 |
| 1272 |
fn.fd = (*Decoder).kFloat64 |
| 1273 |
case reflect.Complex64: |
| 1274 |
fn.fe = (*Encoder).kComplex64 |
| 1275 |
fn.fd = (*Decoder).kComplex64 |
| 1276 |
case reflect.Complex128: |
| 1277 |
fn.fe = (*Encoder).kComplex128 |
| 1278 |
fn.fd = (*Decoder).kComplex128 |
| 1279 |
case reflect.Chan: |
| 1280 |
fn.fe = (*Encoder).kChan |
| 1281 |
fn.fd = (*Decoder).kChan |
| 1282 |
case reflect.Slice: |
| 1283 |
fn.fe = (*Encoder).kSlice |
| 1284 |
fn.fd = (*Decoder).kSlice |
| 1285 |
case reflect.Array: |
| 1286 |
fi.addrD = false |
| 1287 |
fn.fe = (*Encoder).kArray |
| 1288 |
fn.fd = (*Decoder).kArray |
| 1289 |
case reflect.Struct: |
| 1290 |
if ti.anyOmitEmpty || |
| 1291 |
ti.flagMissingFielder || |
| 1292 |
ti.flagMissingFielderPtr { |
| 1293 |
fn.fe = (*Encoder).kStruct |
| 1294 |
} else { |
| 1295 |
fn.fe = (*Encoder).kStructNoOmitempty |
| 1296 |
} |
| 1297 |
fn.fd = (*Decoder).kStruct |
| 1298 |
case reflect.Map: |
| 1299 |
fn.fe = (*Encoder).kMap |
| 1300 |
fn.fd = (*Decoder).kMap |
| 1301 |
case reflect.Interface: |
| 1302 |
|
| 1303 |
fn.fd = (*Decoder).kInterface |
| 1304 |
fn.fe = (*Encoder).kErr |
| 1305 |
default: |
| 1306 |
|
| 1307 |
fn.fe = (*Encoder).kErr |
| 1308 |
fn.fd = (*Decoder).kErr |
| 1309 |
} |
| 1310 |
} |
| 1311 |
} |
| 1312 |
return |
| 1313 |
} |
| 1314 |
|
| 1315 |
|
| 1316 |
|
| 1317 |
|
| 1318 |
|
| 1319 |
|
| 1320 |
|
| 1321 |
|
| 1322 |
|
| 1323 |
|
| 1324 |
|
| 1325 |
|
| 1326 |
|
| 1327 |
|
| 1328 |
|
| 1329 |
|
| 1330 |
type Handle interface { |
| 1331 |
Name() string |
| 1332 |
getBasicHandle() *BasicHandle |
| 1333 |
newEncDriver() encDriver |
| 1334 |
newDecDriver() decDriver |
| 1335 |
isBinary() bool |
| 1336 |
isJson() bool |
| 1337 |
|
| 1338 |
desc(bd byte) string |
| 1339 |
|
| 1340 |
init() |
| 1341 |
} |
| 1342 |
|
| 1343 |
|
| 1344 |
|
| 1345 |
|
| 1346 |
|
| 1347 |
type Raw []byte |
| 1348 |
|
| 1349 |
|
| 1350 |
|
| 1351 |
|
| 1352 |
|
| 1353 |
|
| 1354 |
|
| 1355 |
type RawExt struct { |
| 1356 |
Tag uint64 |
| 1357 |
|
| 1358 |
|
| 1359 |
Data []byte |
| 1360 |
|
| 1361 |
|
| 1362 |
|
| 1363 |
Value interface{} |
| 1364 |
} |
| 1365 |
|
| 1366 |
func (re *RawExt) setData(xbs []byte, zerocopy bool) { |
| 1367 |
if zerocopy { |
| 1368 |
re.Data = xbs |
| 1369 |
} else { |
| 1370 |
re.Data = append(re.Data[:0], xbs...) |
| 1371 |
} |
| 1372 |
} |
| 1373 |
|
| 1374 |
|
| 1375 |
|
| 1376 |
type BytesExt interface { |
| 1377 |
|
| 1378 |
|
| 1379 |
|
| 1380 |
WriteExt(v interface{}) []byte |
| 1381 |
|
| 1382 |
|
| 1383 |
|
| 1384 |
|
| 1385 |
ReadExt(dst interface{}, src []byte) |
| 1386 |
} |
| 1387 |
|
| 1388 |
|
| 1389 |
|
| 1390 |
|
| 1391 |
|
| 1392 |
type InterfaceExt interface { |
| 1393 |
|
| 1394 |
|
| 1395 |
|
| 1396 |
|
| 1397 |
ConvertExt(v interface{}) interface{} |
| 1398 |
|
| 1399 |
|
| 1400 |
|
| 1401 |
|
| 1402 |
|
| 1403 |
UpdateExt(dst interface{}, src interface{}) |
| 1404 |
} |
| 1405 |
|
| 1406 |
|
| 1407 |
type Ext interface { |
| 1408 |
BytesExt |
| 1409 |
InterfaceExt |
| 1410 |
} |
| 1411 |
|
| 1412 |
|
| 1413 |
type addExtWrapper struct { |
| 1414 |
encFn func(reflect.Value) ([]byte, error) |
| 1415 |
decFn func(reflect.Value, []byte) error |
| 1416 |
} |
| 1417 |
|
| 1418 |
func (x addExtWrapper) WriteExt(v interface{}) []byte { |
| 1419 |
bs, err := x.encFn(reflect.ValueOf(v)) |
| 1420 |
halt.onerror(err) |
| 1421 |
return bs |
| 1422 |
} |
| 1423 |
|
| 1424 |
func (x addExtWrapper) ReadExt(v interface{}, bs []byte) { |
| 1425 |
halt.onerror(x.decFn(reflect.ValueOf(v), bs)) |
| 1426 |
} |
| 1427 |
|
| 1428 |
func (x addExtWrapper) ConvertExt(v interface{}) interface{} { |
| 1429 |
return x.WriteExt(v) |
| 1430 |
} |
| 1431 |
|
| 1432 |
func (x addExtWrapper) UpdateExt(dest interface{}, v interface{}) { |
| 1433 |
x.ReadExt(dest, v.([]byte)) |
| 1434 |
} |
| 1435 |
|
| 1436 |
type bytesExtFailer struct{} |
| 1437 |
|
| 1438 |
func (bytesExtFailer) WriteExt(v interface{}) []byte { |
| 1439 |
halt.onerror(errExtFnWriteExtUnsupported) |
| 1440 |
return nil |
| 1441 |
} |
| 1442 |
func (bytesExtFailer) ReadExt(v interface{}, bs []byte) { |
| 1443 |
halt.onerror(errExtFnReadExtUnsupported) |
| 1444 |
} |
| 1445 |
|
| 1446 |
type interfaceExtFailer struct{} |
| 1447 |
|
| 1448 |
func (interfaceExtFailer) ConvertExt(v interface{}) interface{} { |
| 1449 |
halt.onerror(errExtFnConvertExtUnsupported) |
| 1450 |
return nil |
| 1451 |
} |
| 1452 |
func (interfaceExtFailer) UpdateExt(dest interface{}, v interface{}) { |
| 1453 |
halt.onerror(errExtFnUpdateExtUnsupported) |
| 1454 |
} |
| 1455 |
|
| 1456 |
type bytesExtWrapper struct { |
| 1457 |
interfaceExtFailer |
| 1458 |
BytesExt |
| 1459 |
} |
| 1460 |
|
| 1461 |
type interfaceExtWrapper struct { |
| 1462 |
bytesExtFailer |
| 1463 |
InterfaceExt |
| 1464 |
} |
| 1465 |
|
| 1466 |
type extFailWrapper struct { |
| 1467 |
bytesExtFailer |
| 1468 |
interfaceExtFailer |
| 1469 |
} |
| 1470 |
|
| 1471 |
type binaryEncodingType struct{} |
| 1472 |
|
| 1473 |
func (binaryEncodingType) isBinary() bool { return true } |
| 1474 |
func (binaryEncodingType) isJson() bool { return false } |
| 1475 |
|
| 1476 |
type textEncodingType struct{} |
| 1477 |
|
| 1478 |
func (textEncodingType) isBinary() bool { return false } |
| 1479 |
func (textEncodingType) isJson() bool { return false } |
| 1480 |
|
| 1481 |
type notJsonType struct{} |
| 1482 |
|
| 1483 |
func (notJsonType) isJson() bool { return false } |
| 1484 |
|
| 1485 |
|
| 1486 |
|
| 1487 |
|
| 1488 |
type noBuiltInTypes struct{} |
| 1489 |
|
| 1490 |
func (noBuiltInTypes) EncodeBuiltin(rt uintptr, v interface{}) {} |
| 1491 |
func (noBuiltInTypes) DecodeBuiltin(rt uintptr, v interface{}) {} |
| 1492 |
|
| 1493 |
|
| 1494 |
|
| 1495 |
|
| 1496 |
|
| 1497 |
|
| 1498 |
|
| 1499 |
|
| 1500 |
|
| 1501 |
|
| 1502 |
|
| 1503 |
|
| 1504 |
|
| 1505 |
type bigenHelper struct{} |
| 1506 |
|
| 1507 |
func (z bigenHelper) PutUint16(v uint16) (b [2]byte) { |
| 1508 |
return [...]byte{ |
| 1509 |
byte(v >> 8), |
| 1510 |
byte(v), |
| 1511 |
} |
| 1512 |
} |
| 1513 |
|
| 1514 |
func (z bigenHelper) PutUint32(v uint32) (b [4]byte) { |
| 1515 |
return [...]byte{ |
| 1516 |
byte(v >> 24), |
| 1517 |
byte(v >> 16), |
| 1518 |
byte(v >> 8), |
| 1519 |
byte(v), |
| 1520 |
} |
| 1521 |
} |
| 1522 |
|
| 1523 |
func (z bigenHelper) PutUint64(v uint64) (b [8]byte) { |
| 1524 |
return [...]byte{ |
| 1525 |
byte(v >> 56), |
| 1526 |
byte(v >> 48), |
| 1527 |
byte(v >> 40), |
| 1528 |
byte(v >> 32), |
| 1529 |
byte(v >> 24), |
| 1530 |
byte(v >> 16), |
| 1531 |
byte(v >> 8), |
| 1532 |
byte(v), |
| 1533 |
} |
| 1534 |
} |
| 1535 |
|
| 1536 |
func (z bigenHelper) Uint16(b [2]byte) (v uint16) { |
| 1537 |
return uint16(b[1]) | |
| 1538 |
uint16(b[0])<<8 |
| 1539 |
} |
| 1540 |
|
| 1541 |
func (z bigenHelper) Uint32(b [4]byte) (v uint32) { |
| 1542 |
return uint32(b[3]) | |
| 1543 |
uint32(b[2])<<8 | |
| 1544 |
uint32(b[1])<<16 | |
| 1545 |
uint32(b[0])<<24 |
| 1546 |
} |
| 1547 |
|
| 1548 |
func (z bigenHelper) Uint64(b [8]byte) (v uint64) { |
| 1549 |
return uint64(b[7]) | |
| 1550 |
uint64(b[6])<<8 | |
| 1551 |
uint64(b[5])<<16 | |
| 1552 |
uint64(b[4])<<24 | |
| 1553 |
uint64(b[3])<<32 | |
| 1554 |
uint64(b[2])<<40 | |
| 1555 |
uint64(b[1])<<48 | |
| 1556 |
uint64(b[0])<<56 |
| 1557 |
} |
| 1558 |
|
| 1559 |
func (z bigenHelper) writeUint16(w *encWr, v uint16) { |
| 1560 |
x := z.PutUint16(v) |
| 1561 |
w.writen2(x[0], x[1]) |
| 1562 |
} |
| 1563 |
|
| 1564 |
func (z bigenHelper) writeUint32(w *encWr, v uint32) { |
| 1565 |
w.writen4(z.PutUint32(v)) |
| 1566 |
} |
| 1567 |
|
| 1568 |
func (z bigenHelper) writeUint64(w *encWr, v uint64) { |
| 1569 |
w.writen8(z.PutUint64(v)) |
| 1570 |
} |
| 1571 |
|
| 1572 |
type extTypeTagFn struct { |
| 1573 |
rtid uintptr |
| 1574 |
rtidptr uintptr |
| 1575 |
rt reflect.Type |
| 1576 |
tag uint64 |
| 1577 |
ext Ext |
| 1578 |
} |
| 1579 |
|
| 1580 |
type extHandle []extTypeTagFn |
| 1581 |
|
| 1582 |
|
| 1583 |
|
| 1584 |
|
| 1585 |
|
| 1586 |
func (x *BasicHandle) AddExt(rt reflect.Type, tag byte, |
| 1587 |
encfn func(reflect.Value) ([]byte, error), |
| 1588 |
decfn func(reflect.Value, []byte) error) (err error) { |
| 1589 |
if encfn == nil || decfn == nil { |
| 1590 |
return x.SetExt(rt, uint64(tag), nil) |
| 1591 |
} |
| 1592 |
return x.SetExt(rt, uint64(tag), addExtWrapper{encfn, decfn}) |
| 1593 |
} |
| 1594 |
|
| 1595 |
|
| 1596 |
|
| 1597 |
|
| 1598 |
|
| 1599 |
|
| 1600 |
|
| 1601 |
func (x *BasicHandle) SetExt(rt reflect.Type, tag uint64, ext Ext) (err error) { |
| 1602 |
if x.isInited() { |
| 1603 |
return errHandleInited |
| 1604 |
} |
| 1605 |
if x.basicHandleRuntimeState == nil { |
| 1606 |
x.basicHandleRuntimeState = new(basicHandleRuntimeState) |
| 1607 |
} |
| 1608 |
return x.basicHandleRuntimeState.setExt(rt, tag, ext) |
| 1609 |
} |
| 1610 |
|
| 1611 |
func (o extHandle) getExtForI(x interface{}) (v *extTypeTagFn) { |
| 1612 |
if len(o) > 0 { |
| 1613 |
v = o.getExt(i2rtid(x), true) |
| 1614 |
} |
| 1615 |
return |
| 1616 |
} |
| 1617 |
|
| 1618 |
func (o extHandle) getExt(rtid uintptr, check bool) (v *extTypeTagFn) { |
| 1619 |
if !check { |
| 1620 |
return |
| 1621 |
} |
| 1622 |
for i := range o { |
| 1623 |
v = &o[i] |
| 1624 |
if v.rtid == rtid || v.rtidptr == rtid { |
| 1625 |
return |
| 1626 |
} |
| 1627 |
} |
| 1628 |
return nil |
| 1629 |
} |
| 1630 |
|
| 1631 |
func (o extHandle) getExtForTag(tag uint64) (v *extTypeTagFn) { |
| 1632 |
for i := range o { |
| 1633 |
v = &o[i] |
| 1634 |
if v.tag == tag { |
| 1635 |
return |
| 1636 |
} |
| 1637 |
} |
| 1638 |
return nil |
| 1639 |
} |
| 1640 |
|
| 1641 |
type intf2impl struct { |
| 1642 |
rtid uintptr |
| 1643 |
impl reflect.Type |
| 1644 |
} |
| 1645 |
|
| 1646 |
type intf2impls []intf2impl |
| 1647 |
|
| 1648 |
|
| 1649 |
|
| 1650 |
|
| 1651 |
|
| 1652 |
|
| 1653 |
|
| 1654 |
func (o *intf2impls) Intf2Impl(intf, impl reflect.Type) (err error) { |
| 1655 |
if impl != nil && !impl.Implements(intf) { |
| 1656 |
return fmt.Errorf("Intf2Impl: %v does not implement %v", impl, intf) |
| 1657 |
} |
| 1658 |
rtid := rt2id(intf) |
| 1659 |
o2 := *o |
| 1660 |
for i := range o2 { |
| 1661 |
v := &o2[i] |
| 1662 |
if v.rtid == rtid { |
| 1663 |
v.impl = impl |
| 1664 |
return |
| 1665 |
} |
| 1666 |
} |
| 1667 |
*o = append(o2, intf2impl{rtid, impl}) |
| 1668 |
return |
| 1669 |
} |
| 1670 |
|
| 1671 |
func (o intf2impls) intf2impl(rtid uintptr) (rv reflect.Value) { |
| 1672 |
for i := range o { |
| 1673 |
v := &o[i] |
| 1674 |
if v.rtid == rtid { |
| 1675 |
if v.impl == nil { |
| 1676 |
return |
| 1677 |
} |
| 1678 |
vkind := v.impl.Kind() |
| 1679 |
if vkind == reflect.Ptr { |
| 1680 |
return reflect.New(v.impl.Elem()) |
| 1681 |
} |
| 1682 |
return rvZeroAddrK(v.impl, vkind) |
| 1683 |
} |
| 1684 |
} |
| 1685 |
return |
| 1686 |
} |
| 1687 |
|
| 1688 |
|
| 1689 |
|
| 1690 |
|
| 1691 |
|
| 1692 |
|
| 1693 |
type structFieldInfoPathNode struct { |
| 1694 |
parent *structFieldInfoPathNode |
| 1695 |
|
| 1696 |
offset uint16 |
| 1697 |
index uint16 |
| 1698 |
kind uint8 |
| 1699 |
numderef uint8 |
| 1700 |
|
| 1701 |
|
| 1702 |
|
| 1703 |
|
| 1704 |
encNameAsciiAlphaNum bool |
| 1705 |
omitEmpty bool |
| 1706 |
|
| 1707 |
typ reflect.Type |
| 1708 |
} |
| 1709 |
|
| 1710 |
|
| 1711 |
func (path *structFieldInfoPathNode) depth() (d int) { |
| 1712 |
TOP: |
| 1713 |
if path != nil { |
| 1714 |
d++ |
| 1715 |
path = path.parent |
| 1716 |
goto TOP |
| 1717 |
} |
| 1718 |
return |
| 1719 |
} |
| 1720 |
|
| 1721 |
|
| 1722 |
func (path *structFieldInfoPathNode) field(v reflect.Value) (rv2 reflect.Value) { |
| 1723 |
if parent := path.parent; parent != nil { |
| 1724 |
v = parent.field(v) |
| 1725 |
for j, k := uint8(0), parent.numderef; j < k; j++ { |
| 1726 |
if rvIsNil(v) { |
| 1727 |
return |
| 1728 |
} |
| 1729 |
v = v.Elem() |
| 1730 |
} |
| 1731 |
} |
| 1732 |
return path.rvField(v) |
| 1733 |
} |
| 1734 |
|
| 1735 |
|
| 1736 |
|
| 1737 |
func (path *structFieldInfoPathNode) fieldAlloc(v reflect.Value) (rv2 reflect.Value) { |
| 1738 |
if parent := path.parent; parent != nil { |
| 1739 |
v = parent.fieldAlloc(v) |
| 1740 |
for j, k := uint8(0), parent.numderef; j < k; j++ { |
| 1741 |
if rvIsNil(v) { |
| 1742 |
rvSetDirect(v, reflect.New(rvType(v).Elem())) |
| 1743 |
} |
| 1744 |
v = v.Elem() |
| 1745 |
} |
| 1746 |
} |
| 1747 |
return path.rvField(v) |
| 1748 |
} |
| 1749 |
|
| 1750 |
type structFieldInfo struct { |
| 1751 |
encName string |
| 1752 |
|
| 1753 |
|
| 1754 |
|
| 1755 |
|
| 1756 |
|
| 1757 |
|
| 1758 |
|
| 1759 |
|
| 1760 |
path structFieldInfoPathNode |
| 1761 |
} |
| 1762 |
|
| 1763 |
func parseStructInfo(stag string) (toArray, omitEmpty bool, keytype valueType) { |
| 1764 |
keytype = valueTypeString |
| 1765 |
if stag == "" { |
| 1766 |
return |
| 1767 |
} |
| 1768 |
ss := strings.Split(stag, ",") |
| 1769 |
if len(ss) < 2 { |
| 1770 |
return |
| 1771 |
} |
| 1772 |
for _, s := range ss[1:] { |
| 1773 |
switch s { |
| 1774 |
case "omitempty": |
| 1775 |
omitEmpty = true |
| 1776 |
case "toarray": |
| 1777 |
toArray = true |
| 1778 |
case "int": |
| 1779 |
keytype = valueTypeInt |
| 1780 |
case "uint": |
| 1781 |
keytype = valueTypeUint |
| 1782 |
case "float": |
| 1783 |
keytype = valueTypeFloat |
| 1784 |
|
| 1785 |
|
| 1786 |
case "string": |
| 1787 |
keytype = valueTypeString |
| 1788 |
} |
| 1789 |
} |
| 1790 |
return |
| 1791 |
} |
| 1792 |
|
| 1793 |
func (si *structFieldInfo) parseTag(stag string) { |
| 1794 |
if stag == "" { |
| 1795 |
return |
| 1796 |
} |
| 1797 |
for i, s := range strings.Split(stag, ",") { |
| 1798 |
if i == 0 { |
| 1799 |
if s != "" { |
| 1800 |
si.encName = s |
| 1801 |
} |
| 1802 |
} else { |
| 1803 |
switch s { |
| 1804 |
case "omitempty": |
| 1805 |
si.path.omitEmpty = true |
| 1806 |
} |
| 1807 |
} |
| 1808 |
} |
| 1809 |
} |
| 1810 |
|
| 1811 |
type sfiSortedByEncName []*structFieldInfo |
| 1812 |
|
| 1813 |
func (p sfiSortedByEncName) Len() int { return len(p) } |
| 1814 |
func (p sfiSortedByEncName) Swap(i, j int) { p[uint(i)], p[uint(j)] = p[uint(j)], p[uint(i)] } |
| 1815 |
func (p sfiSortedByEncName) Less(i, j int) bool { return p[uint(i)].encName < p[uint(j)].encName } |
| 1816 |
|
| 1817 |
|
| 1818 |
|
| 1819 |
type typeInfo4Container struct { |
| 1820 |
elem reflect.Type |
| 1821 |
|
| 1822 |
|
| 1823 |
|
| 1824 |
|
| 1825 |
key reflect.Type |
| 1826 |
|
| 1827 |
|
| 1828 |
|
| 1829 |
|
| 1830 |
|
| 1831 |
fastpathUnderlying reflect.Type |
| 1832 |
|
| 1833 |
tikey *typeInfo |
| 1834 |
tielem *typeInfo |
| 1835 |
} |
| 1836 |
|
| 1837 |
|
| 1838 |
|
| 1839 |
|
| 1840 |
|
| 1841 |
|
| 1842 |
|
| 1843 |
|
| 1844 |
|
| 1845 |
|
| 1846 |
type typeInfo struct { |
| 1847 |
rt reflect.Type |
| 1848 |
ptr reflect.Type |
| 1849 |
|
| 1850 |
|
| 1851 |
|
| 1852 |
rtid uintptr |
| 1853 |
|
| 1854 |
numMeth uint16 |
| 1855 |
kind uint8 |
| 1856 |
chandir uint8 |
| 1857 |
|
| 1858 |
anyOmitEmpty bool |
| 1859 |
toArray bool |
| 1860 |
keyType valueType |
| 1861 |
mbs bool |
| 1862 |
|
| 1863 |
sfi4Name map[string]*structFieldInfo |
| 1864 |
|
| 1865 |
*typeInfo4Container |
| 1866 |
|
| 1867 |
|
| 1868 |
|
| 1869 |
size, keysize, elemsize uint32 |
| 1870 |
|
| 1871 |
keykind, elemkind uint8 |
| 1872 |
|
| 1873 |
flagHasPkgPath bool |
| 1874 |
flagComparable bool |
| 1875 |
flagCanTransient bool |
| 1876 |
|
| 1877 |
flagMarshalInterface bool |
| 1878 |
flagSelferViaCodecgen bool |
| 1879 |
|
| 1880 |
|
| 1881 |
flagIsZeroer bool |
| 1882 |
flagIsZeroerPtr bool |
| 1883 |
|
| 1884 |
flagIsCodecEmptyer bool |
| 1885 |
flagIsCodecEmptyerPtr bool |
| 1886 |
|
| 1887 |
flagBinaryMarshaler bool |
| 1888 |
flagBinaryMarshalerPtr bool |
| 1889 |
|
| 1890 |
flagBinaryUnmarshaler bool |
| 1891 |
flagBinaryUnmarshalerPtr bool |
| 1892 |
|
| 1893 |
flagTextMarshaler bool |
| 1894 |
flagTextMarshalerPtr bool |
| 1895 |
|
| 1896 |
flagTextUnmarshaler bool |
| 1897 |
flagTextUnmarshalerPtr bool |
| 1898 |
|
| 1899 |
flagJsonMarshaler bool |
| 1900 |
flagJsonMarshalerPtr bool |
| 1901 |
|
| 1902 |
flagJsonUnmarshaler bool |
| 1903 |
flagJsonUnmarshalerPtr bool |
| 1904 |
|
| 1905 |
flagSelfer bool |
| 1906 |
flagSelferPtr bool |
| 1907 |
|
| 1908 |
flagMissingFielder bool |
| 1909 |
flagMissingFielderPtr bool |
| 1910 |
|
| 1911 |
infoFieldOmitempty bool |
| 1912 |
|
| 1913 |
sfi structFieldInfos |
| 1914 |
} |
| 1915 |
|
| 1916 |
func (ti *typeInfo) siForEncName(name []byte) (si *structFieldInfo) { |
| 1917 |
return ti.sfi4Name[string(name)] |
| 1918 |
} |
| 1919 |
|
| 1920 |
func (ti *typeInfo) resolve(x []structFieldInfo, ss map[string]uint16) (n int) { |
| 1921 |
n = len(x) |
| 1922 |
|
| 1923 |
for i := range x { |
| 1924 |
ui := uint16(i) |
| 1925 |
xn := x[i].encName |
| 1926 |
j, ok := ss[xn] |
| 1927 |
if ok { |
| 1928 |
i2clear := ui |
| 1929 |
if x[i].path.depth() < x[j].path.depth() { |
| 1930 |
ss[xn] = ui |
| 1931 |
i2clear = j |
| 1932 |
} |
| 1933 |
if x[i2clear].encName != "" { |
| 1934 |
x[i2clear].encName = "" |
| 1935 |
n-- |
| 1936 |
} |
| 1937 |
} else { |
| 1938 |
ss[xn] = ui |
| 1939 |
} |
| 1940 |
} |
| 1941 |
|
| 1942 |
return |
| 1943 |
} |
| 1944 |
|
| 1945 |
func (ti *typeInfo) init(x []structFieldInfo, n int) { |
| 1946 |
var anyOmitEmpty bool |
| 1947 |
|
| 1948 |
|
| 1949 |
m := make(map[string]*structFieldInfo, n) |
| 1950 |
w := make([]structFieldInfo, n) |
| 1951 |
y := make([]*structFieldInfo, n+n) |
| 1952 |
z := y[n:] |
| 1953 |
y = y[:n] |
| 1954 |
n = 0 |
| 1955 |
for i := range x { |
| 1956 |
if x[i].encName == "" { |
| 1957 |
continue |
| 1958 |
} |
| 1959 |
if !anyOmitEmpty && x[i].path.omitEmpty { |
| 1960 |
anyOmitEmpty = true |
| 1961 |
} |
| 1962 |
w[n] = x[i] |
| 1963 |
y[n] = &w[n] |
| 1964 |
m[x[i].encName] = &w[n] |
| 1965 |
n++ |
| 1966 |
} |
| 1967 |
if n != len(y) { |
| 1968 |
halt.errorf("failure reading struct %v - expecting %d of %d valid fields, got %d", ti.rt, len(y), len(x), n) |
| 1969 |
} |
| 1970 |
|
| 1971 |
copy(z, y) |
| 1972 |
sort.Sort(sfiSortedByEncName(z)) |
| 1973 |
|
| 1974 |
ti.anyOmitEmpty = anyOmitEmpty |
| 1975 |
ti.sfi.load(y, z) |
| 1976 |
ti.sfi4Name = m |
| 1977 |
} |
| 1978 |
|
| 1979 |
|
| 1980 |
|
| 1981 |
|
| 1982 |
|
| 1983 |
|
| 1984 |
|
| 1985 |
|
| 1986 |
|
| 1987 |
|
| 1988 |
|
| 1989 |
|
| 1990 |
|
| 1991 |
|
| 1992 |
func transientBitsetFlags() *bitset32 { |
| 1993 |
if transientValueHasStringSlice { |
| 1994 |
return &numBoolStrSliceBitset |
| 1995 |
} |
| 1996 |
return &numBoolBitset |
| 1997 |
} |
| 1998 |
|
| 1999 |
func isCanTransient(t reflect.Type, k reflect.Kind) (v bool) { |
| 2000 |
var bs = transientBitsetFlags() |
| 2001 |
if bs.isset(byte(k)) { |
| 2002 |
v = true |
| 2003 |
} else if k == reflect.Slice { |
| 2004 |
elem := t.Elem() |
| 2005 |
v = numBoolBitset.isset(byte(elem.Kind())) |
| 2006 |
} else if k == reflect.Array { |
| 2007 |
elem := t.Elem() |
| 2008 |
v = isCanTransient(elem, elem.Kind()) |
| 2009 |
} else if k == reflect.Struct { |
| 2010 |
v = true |
| 2011 |
for j, jlen := 0, t.NumField(); j < jlen; j++ { |
| 2012 |
f := t.Field(j) |
| 2013 |
if !isCanTransient(f.Type, f.Type.Kind()) { |
| 2014 |
v = false |
| 2015 |
return |
| 2016 |
} |
| 2017 |
} |
| 2018 |
} else { |
| 2019 |
v = false |
| 2020 |
} |
| 2021 |
return |
| 2022 |
} |
| 2023 |
|
| 2024 |
func (ti *typeInfo) doSetFlagCanTransient() { |
| 2025 |
if transientSizeMax > 0 { |
| 2026 |
ti.flagCanTransient = ti.size <= transientSizeMax |
| 2027 |
} else { |
| 2028 |
ti.flagCanTransient = true |
| 2029 |
} |
| 2030 |
if ti.flagCanTransient { |
| 2031 |
if !transientBitsetFlags().isset(ti.kind) { |
| 2032 |
ti.flagCanTransient = isCanTransient(ti.rt, reflect.Kind(ti.kind)) |
| 2033 |
} |
| 2034 |
} |
| 2035 |
} |
| 2036 |
|
| 2037 |
type rtid2ti struct { |
| 2038 |
rtid uintptr |
| 2039 |
ti *typeInfo |
| 2040 |
} |
| 2041 |
|
| 2042 |
|
| 2043 |
|
| 2044 |
|
| 2045 |
|
| 2046 |
type TypeInfos struct { |
| 2047 |
infos atomicTypeInfoSlice |
| 2048 |
mu sync.Mutex |
| 2049 |
_ uint64 |
| 2050 |
tags []string |
| 2051 |
_ uint64 |
| 2052 |
} |
| 2053 |
|
| 2054 |
|
| 2055 |
|
| 2056 |
|
| 2057 |
|
| 2058 |
func NewTypeInfos(tags []string) *TypeInfos { |
| 2059 |
return &TypeInfos{tags: tags} |
| 2060 |
} |
| 2061 |
|
| 2062 |
func (x *TypeInfos) structTag(t reflect.StructTag) (s string) { |
| 2063 |
|
| 2064 |
|
| 2065 |
for _, x := range x.tags { |
| 2066 |
s = t.Get(x) |
| 2067 |
if s != "" { |
| 2068 |
return s |
| 2069 |
} |
| 2070 |
} |
| 2071 |
return |
| 2072 |
} |
| 2073 |
|
| 2074 |
func findTypeInfo(s []rtid2ti, rtid uintptr) (i uint, ti *typeInfo) { |
| 2075 |
|
| 2076 |
|
| 2077 |
|
| 2078 |
var h uint |
| 2079 |
var j = uint(len(s)) |
| 2080 |
LOOP: |
| 2081 |
if i < j { |
| 2082 |
h = (i + j) >> 1 |
| 2083 |
if s[h].rtid < rtid { |
| 2084 |
i = h + 1 |
| 2085 |
} else { |
| 2086 |
j = h |
| 2087 |
} |
| 2088 |
goto LOOP |
| 2089 |
} |
| 2090 |
if i < uint(len(s)) && s[i].rtid == rtid { |
| 2091 |
ti = s[i].ti |
| 2092 |
} |
| 2093 |
return |
| 2094 |
} |
| 2095 |
|
| 2096 |
func (x *TypeInfos) get(rtid uintptr, rt reflect.Type) (pti *typeInfo) { |
| 2097 |
if pti = x.find(rtid); pti == nil { |
| 2098 |
pti = x.load(rt) |
| 2099 |
} |
| 2100 |
return |
| 2101 |
} |
| 2102 |
|
| 2103 |
func (x *TypeInfos) find(rtid uintptr) (pti *typeInfo) { |
| 2104 |
sp := x.infos.load() |
| 2105 |
if sp != nil { |
| 2106 |
_, pti = findTypeInfo(sp, rtid) |
| 2107 |
} |
| 2108 |
return |
| 2109 |
} |
| 2110 |
|
| 2111 |
func (x *TypeInfos) load(rt reflect.Type) (pti *typeInfo) { |
| 2112 |
rk := rt.Kind() |
| 2113 |
|
| 2114 |
if rk == reflect.Ptr { |
| 2115 |
halt.errorf("invalid kind passed to TypeInfos.get: %v - %v", rk, rt) |
| 2116 |
} |
| 2117 |
|
| 2118 |
rtid := rt2id(rt) |
| 2119 |
|
| 2120 |
|
| 2121 |
|
| 2122 |
ti := typeInfo{ |
| 2123 |
rt: rt, |
| 2124 |
ptr: reflect.PtrTo(rt), |
| 2125 |
rtid: rtid, |
| 2126 |
kind: uint8(rk), |
| 2127 |
size: uint32(rt.Size()), |
| 2128 |
numMeth: uint16(rt.NumMethod()), |
| 2129 |
keyType: valueTypeString, |
| 2130 |
|
| 2131 |
|
| 2132 |
flagHasPkgPath: rt.PkgPath() != "", |
| 2133 |
} |
| 2134 |
|
| 2135 |
|
| 2136 |
bset := func(when bool, b *bool) { |
| 2137 |
if when { |
| 2138 |
*b = true |
| 2139 |
} |
| 2140 |
} |
| 2141 |
|
| 2142 |
var b1, b2 bool |
| 2143 |
|
| 2144 |
b1, b2 = implIntf(rt, binaryMarshalerTyp) |
| 2145 |
bset(b1, &ti.flagBinaryMarshaler) |
| 2146 |
bset(b2, &ti.flagBinaryMarshalerPtr) |
| 2147 |
b1, b2 = implIntf(rt, binaryUnmarshalerTyp) |
| 2148 |
bset(b1, &ti.flagBinaryUnmarshaler) |
| 2149 |
bset(b2, &ti.flagBinaryUnmarshalerPtr) |
| 2150 |
b1, b2 = implIntf(rt, textMarshalerTyp) |
| 2151 |
bset(b1, &ti.flagTextMarshaler) |
| 2152 |
bset(b2, &ti.flagTextMarshalerPtr) |
| 2153 |
b1, b2 = implIntf(rt, textUnmarshalerTyp) |
| 2154 |
bset(b1, &ti.flagTextUnmarshaler) |
| 2155 |
bset(b2, &ti.flagTextUnmarshalerPtr) |
| 2156 |
b1, b2 = implIntf(rt, jsonMarshalerTyp) |
| 2157 |
bset(b1, &ti.flagJsonMarshaler) |
| 2158 |
bset(b2, &ti.flagJsonMarshalerPtr) |
| 2159 |
b1, b2 = implIntf(rt, jsonUnmarshalerTyp) |
| 2160 |
bset(b1, &ti.flagJsonUnmarshaler) |
| 2161 |
bset(b2, &ti.flagJsonUnmarshalerPtr) |
| 2162 |
b1, b2 = implIntf(rt, selferTyp) |
| 2163 |
bset(b1, &ti.flagSelfer) |
| 2164 |
bset(b2, &ti.flagSelferPtr) |
| 2165 |
b1, b2 = implIntf(rt, missingFielderTyp) |
| 2166 |
bset(b1, &ti.flagMissingFielder) |
| 2167 |
bset(b2, &ti.flagMissingFielderPtr) |
| 2168 |
b1, b2 = implIntf(rt, iszeroTyp) |
| 2169 |
bset(b1, &ti.flagIsZeroer) |
| 2170 |
bset(b2, &ti.flagIsZeroerPtr) |
| 2171 |
b1, b2 = implIntf(rt, isCodecEmptyerTyp) |
| 2172 |
bset(b1, &ti.flagIsCodecEmptyer) |
| 2173 |
bset(b2, &ti.flagIsCodecEmptyerPtr) |
| 2174 |
|
| 2175 |
b1, b2 = implIntf(rt, isSelferViaCodecgenerTyp) |
| 2176 |
ti.flagSelferViaCodecgen = b1 || b2 |
| 2177 |
|
| 2178 |
ti.flagMarshalInterface = ti.flagSelfer || ti.flagSelferPtr || |
| 2179 |
ti.flagSelferViaCodecgen || |
| 2180 |
ti.flagBinaryMarshaler || ti.flagBinaryMarshalerPtr || |
| 2181 |
ti.flagBinaryUnmarshaler || ti.flagBinaryUnmarshalerPtr || |
| 2182 |
ti.flagTextMarshaler || ti.flagTextMarshalerPtr || |
| 2183 |
ti.flagTextUnmarshaler || ti.flagTextUnmarshalerPtr || |
| 2184 |
ti.flagJsonMarshaler || ti.flagJsonMarshalerPtr || |
| 2185 |
ti.flagJsonUnmarshaler || ti.flagJsonUnmarshalerPtr |
| 2186 |
|
| 2187 |
b1 = rt.Comparable() |
| 2188 |
|
| 2189 |
ti.flagComparable = b1 |
| 2190 |
|
| 2191 |
ti.doSetFlagCanTransient() |
| 2192 |
|
| 2193 |
var tt reflect.Type |
| 2194 |
switch rk { |
| 2195 |
case reflect.Struct: |
| 2196 |
var omitEmpty bool |
| 2197 |
if f, ok := rt.FieldByName(structInfoFieldName); ok { |
| 2198 |
ti.toArray, omitEmpty, ti.keyType = parseStructInfo(x.structTag(f.Tag)) |
| 2199 |
ti.infoFieldOmitempty = omitEmpty |
| 2200 |
} else { |
| 2201 |
ti.keyType = valueTypeString |
| 2202 |
} |
| 2203 |
pp, pi := &pool4tiload, pool4tiload.Get() |
| 2204 |
pv := pi.(*typeInfoLoad) |
| 2205 |
pv.reset() |
| 2206 |
pv.etypes = append(pv.etypes, ti.rtid) |
| 2207 |
x.rget(rt, rtid, nil, pv, omitEmpty) |
| 2208 |
n := ti.resolve(pv.sfis, pv.sfiNames) |
| 2209 |
ti.init(pv.sfis, n) |
| 2210 |
pp.Put(pi) |
| 2211 |
case reflect.Map: |
| 2212 |
ti.typeInfo4Container = new(typeInfo4Container) |
| 2213 |
ti.elem = rt.Elem() |
| 2214 |
for tt = ti.elem; tt.Kind() == reflect.Ptr; tt = tt.Elem() { |
| 2215 |
} |
| 2216 |
ti.tielem = x.get(rt2id(tt), tt) |
| 2217 |
ti.elemkind = uint8(ti.elem.Kind()) |
| 2218 |
ti.elemsize = uint32(ti.elem.Size()) |
| 2219 |
ti.key = rt.Key() |
| 2220 |
for tt = ti.key; tt.Kind() == reflect.Ptr; tt = tt.Elem() { |
| 2221 |
} |
| 2222 |
ti.tikey = x.get(rt2id(tt), tt) |
| 2223 |
ti.keykind = uint8(ti.key.Kind()) |
| 2224 |
ti.keysize = uint32(ti.key.Size()) |
| 2225 |
if ti.flagHasPkgPath { |
| 2226 |
ti.fastpathUnderlying = reflect.MapOf(ti.key, ti.elem) |
| 2227 |
} |
| 2228 |
case reflect.Slice: |
| 2229 |
ti.typeInfo4Container = new(typeInfo4Container) |
| 2230 |
ti.mbs, b2 = implIntf(rt, mapBySliceTyp) |
| 2231 |
if !ti.mbs && b2 { |
| 2232 |
ti.mbs = b2 |
| 2233 |
} |
| 2234 |
ti.elem = rt.Elem() |
| 2235 |
for tt = ti.elem; tt.Kind() == reflect.Ptr; tt = tt.Elem() { |
| 2236 |
} |
| 2237 |
ti.tielem = x.get(rt2id(tt), tt) |
| 2238 |
ti.elemkind = uint8(ti.elem.Kind()) |
| 2239 |
ti.elemsize = uint32(ti.elem.Size()) |
| 2240 |
if ti.flagHasPkgPath { |
| 2241 |
ti.fastpathUnderlying = reflect.SliceOf(ti.elem) |
| 2242 |
} |
| 2243 |
case reflect.Chan: |
| 2244 |
ti.typeInfo4Container = new(typeInfo4Container) |
| 2245 |
ti.elem = rt.Elem() |
| 2246 |
for tt = ti.elem; tt.Kind() == reflect.Ptr; tt = tt.Elem() { |
| 2247 |
} |
| 2248 |
ti.tielem = x.get(rt2id(tt), tt) |
| 2249 |
ti.elemkind = uint8(ti.elem.Kind()) |
| 2250 |
ti.elemsize = uint32(ti.elem.Size()) |
| 2251 |
ti.chandir = uint8(rt.ChanDir()) |
| 2252 |
ti.key = reflect.SliceOf(ti.elem) |
| 2253 |
ti.keykind = uint8(reflect.Slice) |
| 2254 |
case reflect.Array: |
| 2255 |
ti.typeInfo4Container = new(typeInfo4Container) |
| 2256 |
ti.mbs, b2 = implIntf(rt, mapBySliceTyp) |
| 2257 |
if !ti.mbs && b2 { |
| 2258 |
ti.mbs = b2 |
| 2259 |
} |
| 2260 |
ti.elem = rt.Elem() |
| 2261 |
ti.elemkind = uint8(ti.elem.Kind()) |
| 2262 |
ti.elemsize = uint32(ti.elem.Size()) |
| 2263 |
for tt = ti.elem; tt.Kind() == reflect.Ptr; tt = tt.Elem() { |
| 2264 |
} |
| 2265 |
ti.tielem = x.get(rt2id(tt), tt) |
| 2266 |
ti.key = reflect.SliceOf(ti.elem) |
| 2267 |
ti.keykind = uint8(reflect.Slice) |
| 2268 |
ti.keysize = uint32(ti.key.Size()) |
| 2269 |
if ti.flagHasPkgPath { |
| 2270 |
ti.fastpathUnderlying = ti.key |
| 2271 |
} |
| 2272 |
|
| 2273 |
|
| 2274 |
|
| 2275 |
|
| 2276 |
|
| 2277 |
|
| 2278 |
} |
| 2279 |
|
| 2280 |
x.mu.Lock() |
| 2281 |
sp := x.infos.load() |
| 2282 |
|
| 2283 |
|
| 2284 |
if sp == nil { |
| 2285 |
pti = &ti |
| 2286 |
sp = []rtid2ti{{rtid, pti}} |
| 2287 |
x.infos.store(sp) |
| 2288 |
} else { |
| 2289 |
var idx uint |
| 2290 |
idx, pti = findTypeInfo(sp, rtid) |
| 2291 |
if pti == nil { |
| 2292 |
pti = &ti |
| 2293 |
sp2 := make([]rtid2ti, len(sp)+1) |
| 2294 |
copy(sp2[idx+1:], sp[idx:]) |
| 2295 |
copy(sp2, sp[:idx]) |
| 2296 |
sp2[idx] = rtid2ti{rtid, pti} |
| 2297 |
x.infos.store(sp2) |
| 2298 |
} |
| 2299 |
} |
| 2300 |
x.mu.Unlock() |
| 2301 |
return |
| 2302 |
} |
| 2303 |
|
| 2304 |
func (x *TypeInfos) rget(rt reflect.Type, rtid uintptr, |
| 2305 |
path *structFieldInfoPathNode, pv *typeInfoLoad, omitEmpty bool) { |
| 2306 |
|
| 2307 |
|
| 2308 |
|
| 2309 |
|
| 2310 |
|
| 2311 |
|
| 2312 |
|
| 2313 |
|
| 2314 |
flen := rt.NumField() |
| 2315 |
LOOP: |
| 2316 |
for j, jlen := uint16(0), uint16(flen); j < jlen; j++ { |
| 2317 |
f := rt.Field(int(j)) |
| 2318 |
fkind := f.Type.Kind() |
| 2319 |
|
| 2320 |
|
| 2321 |
switch fkind { |
| 2322 |
case reflect.Func, reflect.UnsafePointer: |
| 2323 |
continue LOOP |
| 2324 |
} |
| 2325 |
|
| 2326 |
isUnexported := f.PkgPath != "" |
| 2327 |
if isUnexported && !f.Anonymous { |
| 2328 |
continue |
| 2329 |
} |
| 2330 |
stag := x.structTag(f.Tag) |
| 2331 |
if stag == "-" { |
| 2332 |
continue |
| 2333 |
} |
| 2334 |
var si structFieldInfo |
| 2335 |
|
| 2336 |
var numderef uint8 = 0 |
| 2337 |
for xft := f.Type; xft.Kind() == reflect.Ptr; xft = xft.Elem() { |
| 2338 |
numderef++ |
| 2339 |
} |
| 2340 |
|
| 2341 |
var parsed bool |
| 2342 |
|
| 2343 |
|
| 2344 |
if f.Anonymous && fkind != reflect.Interface { |
| 2345 |
|
| 2346 |
ft := f.Type |
| 2347 |
isPtr := ft.Kind() == reflect.Ptr |
| 2348 |
for ft.Kind() == reflect.Ptr { |
| 2349 |
ft = ft.Elem() |
| 2350 |
} |
| 2351 |
isStruct := ft.Kind() == reflect.Struct |
| 2352 |
|
| 2353 |
|
| 2354 |
|
| 2355 |
|
| 2356 |
|
| 2357 |
if (isUnexported && !isStruct) || (!allowSetUnexportedEmbeddedPtr && isUnexported && isPtr) { |
| 2358 |
continue |
| 2359 |
} |
| 2360 |
doInline := stag == "" |
| 2361 |
if !doInline { |
| 2362 |
si.parseTag(stag) |
| 2363 |
parsed = true |
| 2364 |
doInline = si.encName == "" |
| 2365 |
} |
| 2366 |
if doInline && isStruct { |
| 2367 |
|
| 2368 |
ftid := rt2id(ft) |
| 2369 |
|
| 2370 |
|
| 2371 |
|
| 2372 |
|
| 2373 |
processIt := true |
| 2374 |
numk := 0 |
| 2375 |
for _, k := range pv.etypes { |
| 2376 |
if k == ftid { |
| 2377 |
numk++ |
| 2378 |
if numk == rgetMaxRecursion { |
| 2379 |
processIt = false |
| 2380 |
break |
| 2381 |
} |
| 2382 |
} |
| 2383 |
} |
| 2384 |
if processIt { |
| 2385 |
pv.etypes = append(pv.etypes, ftid) |
| 2386 |
path2 := &structFieldInfoPathNode{ |
| 2387 |
parent: path, |
| 2388 |
typ: f.Type, |
| 2389 |
offset: uint16(f.Offset), |
| 2390 |
index: j, |
| 2391 |
kind: uint8(fkind), |
| 2392 |
numderef: numderef, |
| 2393 |
} |
| 2394 |
x.rget(ft, ftid, path2, pv, omitEmpty) |
| 2395 |
} |
| 2396 |
continue |
| 2397 |
} |
| 2398 |
} |
| 2399 |
|
| 2400 |
|
| 2401 |
if isUnexported || f.Name == "" { |
| 2402 |
continue |
| 2403 |
} |
| 2404 |
|
| 2405 |
si.path = structFieldInfoPathNode{ |
| 2406 |
parent: path, |
| 2407 |
typ: f.Type, |
| 2408 |
offset: uint16(f.Offset), |
| 2409 |
index: j, |
| 2410 |
kind: uint8(fkind), |
| 2411 |
numderef: numderef, |
| 2412 |
|
| 2413 |
encNameAsciiAlphaNum: true, |
| 2414 |
|
| 2415 |
omitEmpty: si.path.omitEmpty, |
| 2416 |
} |
| 2417 |
|
| 2418 |
if !parsed { |
| 2419 |
si.encName = f.Name |
| 2420 |
si.parseTag(stag) |
| 2421 |
parsed = true |
| 2422 |
} else if si.encName == "" { |
| 2423 |
si.encName = f.Name |
| 2424 |
} |
| 2425 |
|
| 2426 |
|
| 2427 |
|
| 2428 |
if omitEmpty { |
| 2429 |
si.path.omitEmpty = true |
| 2430 |
} |
| 2431 |
|
| 2432 |
for i := len(si.encName) - 1; i >= 0; i-- { |
| 2433 |
if !asciiAlphaNumBitset.isset(si.encName[i]) { |
| 2434 |
si.path.encNameAsciiAlphaNum = false |
| 2435 |
break |
| 2436 |
} |
| 2437 |
} |
| 2438 |
|
| 2439 |
pv.sfis = append(pv.sfis, si) |
| 2440 |
} |
| 2441 |
} |
| 2442 |
|
| 2443 |
func implIntf(rt, iTyp reflect.Type) (base bool, indir bool) { |
| 2444 |
|
| 2445 |
|
| 2446 |
|
| 2447 |
|
| 2448 |
|
| 2449 |
|
| 2450 |
|
| 2451 |
|
| 2452 |
|
| 2453 |
base = rt.Implements(iTyp) |
| 2454 |
if base { |
| 2455 |
indir = true |
| 2456 |
} else { |
| 2457 |
indir = reflect.PtrTo(rt).Implements(iTyp) |
| 2458 |
} |
| 2459 |
return |
| 2460 |
} |
| 2461 |
|
| 2462 |
func isSliceBoundsError(s string) bool { |
| 2463 |
return strings.Contains(s, "index out of range") || |
| 2464 |
strings.Contains(s, "slice bounds out of range") |
| 2465 |
} |
| 2466 |
|
| 2467 |
func sprintf(format string, v ...interface{}) string { |
| 2468 |
return fmt.Sprintf(format, v...) |
| 2469 |
} |
| 2470 |
|
| 2471 |
func panicValToErr(h errDecorator, v interface{}, err *error) { |
| 2472 |
if v == *err { |
| 2473 |
return |
| 2474 |
} |
| 2475 |
switch xerr := v.(type) { |
| 2476 |
case nil: |
| 2477 |
case runtime.Error: |
| 2478 |
d, dok := h.(*Decoder) |
| 2479 |
if dok && d.bytes && isSliceBoundsError(xerr.Error()) { |
| 2480 |
*err = io.EOF |
| 2481 |
} else { |
| 2482 |
h.wrapErr(xerr, err) |
| 2483 |
} |
| 2484 |
case error: |
| 2485 |
switch xerr { |
| 2486 |
case nil: |
| 2487 |
case io.EOF, io.ErrUnexpectedEOF, errEncoderNotInitialized, errDecoderNotInitialized: |
| 2488 |
|
| 2489 |
*err = xerr |
| 2490 |
default: |
| 2491 |
h.wrapErr(xerr, err) |
| 2492 |
} |
| 2493 |
default: |
| 2494 |
|
| 2495 |
h.wrapErr(fmt.Errorf("%v", v), err) |
| 2496 |
} |
| 2497 |
} |
| 2498 |
|
| 2499 |
func usableByteSlice(bs []byte, slen int) (out []byte, changed bool) { |
| 2500 |
const maxCap = 1024 * 1024 * 64 |
| 2501 |
const skipMaxCap = false |
| 2502 |
if slen <= 0 { |
| 2503 |
return []byte{}, true |
| 2504 |
} |
| 2505 |
if slen <= cap(bs) { |
| 2506 |
return bs[:slen], false |
| 2507 |
} |
| 2508 |
|
| 2509 |
if skipMaxCap || slen <= maxCap { |
| 2510 |
return make([]byte, slen), true |
| 2511 |
} |
| 2512 |
return make([]byte, maxCap), true |
| 2513 |
} |
| 2514 |
|
| 2515 |
func mapKeyFastKindFor(k reflect.Kind) mapKeyFastKind { |
| 2516 |
return mapKeyFastKindVals[k&31] |
| 2517 |
} |
| 2518 |
|
| 2519 |
|
| 2520 |
|
| 2521 |
type codecFnInfo struct { |
| 2522 |
ti *typeInfo |
| 2523 |
xfFn Ext |
| 2524 |
xfTag uint64 |
| 2525 |
addrD bool |
| 2526 |
addrDf bool |
| 2527 |
addrE bool |
| 2528 |
|
| 2529 |
} |
| 2530 |
|
| 2531 |
|
| 2532 |
|
| 2533 |
|
| 2534 |
|
| 2535 |
type codecFn struct { |
| 2536 |
i codecFnInfo |
| 2537 |
fe func(*Encoder, *codecFnInfo, reflect.Value) |
| 2538 |
fd func(*Decoder, *codecFnInfo, reflect.Value) |
| 2539 |
|
| 2540 |
} |
| 2541 |
|
| 2542 |
type codecRtidFn struct { |
| 2543 |
rtid uintptr |
| 2544 |
fn *codecFn |
| 2545 |
} |
| 2546 |
|
| 2547 |
func makeExt(ext interface{}) Ext { |
| 2548 |
switch t := ext.(type) { |
| 2549 |
case Ext: |
| 2550 |
return t |
| 2551 |
case BytesExt: |
| 2552 |
return &bytesExtWrapper{BytesExt: t} |
| 2553 |
case InterfaceExt: |
| 2554 |
return &interfaceExtWrapper{InterfaceExt: t} |
| 2555 |
} |
| 2556 |
return &extFailWrapper{} |
| 2557 |
} |
| 2558 |
|
| 2559 |
func baseRV(v interface{}) (rv reflect.Value) { |
| 2560 |
|
| 2561 |
for rv = reflect.ValueOf(v); rv.Kind() == reflect.Ptr; rv = rv.Elem() { |
| 2562 |
} |
| 2563 |
return |
| 2564 |
} |
| 2565 |
|
| 2566 |
|
| 2567 |
|
| 2568 |
|
| 2569 |
|
| 2570 |
|
| 2571 |
|
| 2572 |
|
| 2573 |
type checkOverflow struct{} |
| 2574 |
|
| 2575 |
func (checkOverflow) Float32(v float64) (overflow bool) { |
| 2576 |
if v < 0 { |
| 2577 |
v = -v |
| 2578 |
} |
| 2579 |
return math.MaxFloat32 < v && v <= math.MaxFloat64 |
| 2580 |
} |
| 2581 |
func (checkOverflow) Uint(v uint64, bitsize uint8) (overflow bool) { |
| 2582 |
if v != 0 && v != (v<<(64-bitsize))>>(64-bitsize) { |
| 2583 |
overflow = true |
| 2584 |
} |
| 2585 |
return |
| 2586 |
} |
| 2587 |
func (checkOverflow) Int(v int64, bitsize uint8) (overflow bool) { |
| 2588 |
if v != 0 && v != (v<<(64-bitsize))>>(64-bitsize) { |
| 2589 |
overflow = true |
| 2590 |
} |
| 2591 |
return |
| 2592 |
} |
| 2593 |
|
| 2594 |
func (checkOverflow) Uint2Int(v uint64, neg bool) (overflow bool) { |
| 2595 |
return (neg && v > 1<<63) || (!neg && v >= 1<<63) |
| 2596 |
} |
| 2597 |
|
| 2598 |
func (checkOverflow) SignedInt(v uint64) (overflow bool) { |
| 2599 |
|
| 2600 |
|
| 2601 |
|
| 2602 |
|
| 2603 |
|
| 2604 |
|
| 2605 |
|
| 2606 |
|
| 2607 |
|
| 2608 |
|
| 2609 |
|
| 2610 |
|
| 2611 |
|
| 2612 |
|
| 2613 |
|
| 2614 |
overflow = (v>>63) != 0 && v&0x7fffffffffffffff > math.MaxInt64-1 |
| 2615 |
|
| 2616 |
return |
| 2617 |
} |
| 2618 |
|
| 2619 |
func (x checkOverflow) Float32V(v float64) float64 { |
| 2620 |
if x.Float32(v) { |
| 2621 |
halt.errorf("float32 overflow: %v", v) |
| 2622 |
} |
| 2623 |
return v |
| 2624 |
} |
| 2625 |
func (x checkOverflow) UintV(v uint64, bitsize uint8) uint64 { |
| 2626 |
if x.Uint(v, bitsize) { |
| 2627 |
halt.errorf("uint64 overflow: %v", v) |
| 2628 |
} |
| 2629 |
return v |
| 2630 |
} |
| 2631 |
func (x checkOverflow) IntV(v int64, bitsize uint8) int64 { |
| 2632 |
if x.Int(v, bitsize) { |
| 2633 |
halt.errorf("int64 overflow: %v", v) |
| 2634 |
} |
| 2635 |
return v |
| 2636 |
} |
| 2637 |
func (x checkOverflow) SignedIntV(v uint64) int64 { |
| 2638 |
if x.SignedInt(v) { |
| 2639 |
halt.errorf("uint64 to int64 overflow: %v", v) |
| 2640 |
} |
| 2641 |
return int64(v) |
| 2642 |
} |
| 2643 |
|
| 2644 |
|
| 2645 |
|
| 2646 |
func isNaN64(f float64) bool { return f != f } |
| 2647 |
|
| 2648 |
func isWhitespaceChar(v byte) bool { |
| 2649 |
|
| 2650 |
|
| 2651 |
return v < 33 |
| 2652 |
|
| 2653 |
|
| 2654 |
|
| 2655 |
|
| 2656 |
} |
| 2657 |
|
| 2658 |
func isNumberChar(v byte) bool { |
| 2659 |
|
| 2660 |
|
| 2661 |
return numCharBitset.isset(v) |
| 2662 |
|
| 2663 |
|
| 2664 |
} |
| 2665 |
|
| 2666 |
|
| 2667 |
|
| 2668 |
type ioFlusher interface { |
| 2669 |
Flush() error |
| 2670 |
} |
| 2671 |
|
| 2672 |
type ioBuffered interface { |
| 2673 |
Buffered() int |
| 2674 |
} |
| 2675 |
|
| 2676 |
|
| 2677 |
|
| 2678 |
type sfiRv struct { |
| 2679 |
v *structFieldInfo |
| 2680 |
r reflect.Value |
| 2681 |
} |
| 2682 |
|
| 2683 |
|
| 2684 |
|
| 2685 |
|
| 2686 |
|
| 2687 |
|
| 2688 |
|
| 2689 |
|
| 2690 |
|
| 2691 |
|
| 2692 |
|
| 2693 |
|
| 2694 |
|
| 2695 |
|
| 2696 |
|
| 2697 |
|
| 2698 |
|
| 2699 |
|
| 2700 |
|
| 2701 |
|
| 2702 |
|
| 2703 |
|
| 2704 |
|
| 2705 |
type bitset32 [32]bool |
| 2706 |
|
| 2707 |
func (x *bitset32) set(pos byte) *bitset32 { |
| 2708 |
x[pos&31] = true |
| 2709 |
return x |
| 2710 |
} |
| 2711 |
func (x *bitset32) isset(pos byte) bool { |
| 2712 |
return x[pos&31] |
| 2713 |
} |
| 2714 |
|
| 2715 |
type bitset256 [256]bool |
| 2716 |
|
| 2717 |
func (x *bitset256) set(pos byte) *bitset256 { |
| 2718 |
x[pos] = true |
| 2719 |
return x |
| 2720 |
} |
| 2721 |
func (x *bitset256) isset(pos byte) bool { |
| 2722 |
return x[pos] |
| 2723 |
} |
| 2724 |
|
| 2725 |
|
| 2726 |
|
| 2727 |
type panicHdl struct{} |
| 2728 |
|
| 2729 |
|
| 2730 |
func (panicHdl) onerror(err error) { |
| 2731 |
if err != nil { |
| 2732 |
panic(err) |
| 2733 |
} |
| 2734 |
} |
| 2735 |
|
| 2736 |
|
| 2737 |
|
| 2738 |
|
| 2739 |
|
| 2740 |
|
| 2741 |
|
| 2742 |
func (panicHdl) errorf(format string, params ...interface{}) { |
| 2743 |
if format == "" { |
| 2744 |
panic(errPanicUndefined) |
| 2745 |
} |
| 2746 |
if len(params) == 0 { |
| 2747 |
panic(errors.New(format)) |
| 2748 |
} |
| 2749 |
panic(fmt.Errorf(format, params...)) |
| 2750 |
} |
| 2751 |
|
| 2752 |
|
| 2753 |
|
| 2754 |
type errDecorator interface { |
| 2755 |
wrapErr(in error, out *error) |
| 2756 |
} |
| 2757 |
|
| 2758 |
type errDecoratorDef struct{} |
| 2759 |
|
| 2760 |
func (errDecoratorDef) wrapErr(v error, e *error) { *e = v } |
| 2761 |
|
| 2762 |
|
| 2763 |
|
| 2764 |
type mustHdl struct{} |
| 2765 |
|
| 2766 |
func (mustHdl) String(s string, err error) string { |
| 2767 |
halt.onerror(err) |
| 2768 |
return s |
| 2769 |
} |
| 2770 |
func (mustHdl) Int(s int64, err error) int64 { |
| 2771 |
halt.onerror(err) |
| 2772 |
return s |
| 2773 |
} |
| 2774 |
func (mustHdl) Uint(s uint64, err error) uint64 { |
| 2775 |
halt.onerror(err) |
| 2776 |
return s |
| 2777 |
} |
| 2778 |
func (mustHdl) Float(s float64, err error) float64 { |
| 2779 |
halt.onerror(err) |
| 2780 |
return s |
| 2781 |
} |
| 2782 |
|
| 2783 |
|
| 2784 |
|
| 2785 |
func freelistCapacity(length int) (capacity int) { |
| 2786 |
for capacity = 8; capacity <= length; capacity *= 2 { |
| 2787 |
} |
| 2788 |
return |
| 2789 |
} |
| 2790 |
|
| 2791 |
|
| 2792 |
|
| 2793 |
|
| 2794 |
|
| 2795 |
|
| 2796 |
|
| 2797 |
|
| 2798 |
|
| 2799 |
|
| 2800 |
|
| 2801 |
|
| 2802 |
|
| 2803 |
|
| 2804 |
|
| 2805 |
|
| 2806 |
|
| 2807 |
|
| 2808 |
|
| 2809 |
|
| 2810 |
|
| 2811 |
|
| 2812 |
|
| 2813 |
|
| 2814 |
|
| 2815 |
type bytesFreelist [][]byte |
| 2816 |
|
| 2817 |
|
| 2818 |
|
| 2819 |
func (x *bytesFreelist) peek(length int, pop bool) (out []byte) { |
| 2820 |
if bytesFreeListNoCache { |
| 2821 |
return make([]byte, 0, freelistCapacity(length)) |
| 2822 |
} |
| 2823 |
y := *x |
| 2824 |
if len(y) > 0 { |
| 2825 |
out = y[len(y)-1] |
| 2826 |
} |
| 2827 |
|
| 2828 |
const minLenBytes = 64 |
| 2829 |
if length < minLenBytes { |
| 2830 |
length = minLenBytes |
| 2831 |
} |
| 2832 |
if cap(out) < length { |
| 2833 |
out = make([]byte, 0, freelistCapacity(length)) |
| 2834 |
y = append(y, out) |
| 2835 |
*x = y |
| 2836 |
} |
| 2837 |
if pop && len(y) > 0 { |
| 2838 |
y = y[:len(y)-1] |
| 2839 |
*x = y |
| 2840 |
} |
| 2841 |
return |
| 2842 |
} |
| 2843 |
|
| 2844 |
|
| 2845 |
|
| 2846 |
func (x *bytesFreelist) get(length int) (out []byte) { |
| 2847 |
if bytesFreeListNoCache { |
| 2848 |
return make([]byte, 0, freelistCapacity(length)) |
| 2849 |
} |
| 2850 |
y := *x |
| 2851 |
|
| 2852 |
|
| 2853 |
for i := 0; i < len(y); i++ { |
| 2854 |
v := y[i] |
| 2855 |
if cap(v) >= length { |
| 2856 |
|
| 2857 |
copy(y[i:], y[i+1:]) |
| 2858 |
*x = y[:len(y)-1] |
| 2859 |
return v |
| 2860 |
} |
| 2861 |
} |
| 2862 |
return make([]byte, 0, freelistCapacity(length)) |
| 2863 |
} |
| 2864 |
|
| 2865 |
func (x *bytesFreelist) put(v []byte) { |
| 2866 |
if bytesFreeListNoCache || cap(v) == 0 { |
| 2867 |
return |
| 2868 |
} |
| 2869 |
if len(v) != 0 { |
| 2870 |
v = v[:0] |
| 2871 |
} |
| 2872 |
|
| 2873 |
y := append(*x, v) |
| 2874 |
*x = y |
| 2875 |
|
| 2876 |
|
| 2877 |
for i := 0; i < len(y)-1; i++ { |
| 2878 |
z := y[i] |
| 2879 |
if cap(z) > cap(v) { |
| 2880 |
copy(y[i+1:], y[i:]) |
| 2881 |
y[i] = v |
| 2882 |
return |
| 2883 |
} |
| 2884 |
} |
| 2885 |
} |
| 2886 |
|
| 2887 |
func (x *bytesFreelist) check(v []byte, length int) (out []byte) { |
| 2888 |
|
| 2889 |
if cap(v) >= length { |
| 2890 |
return v[:0] |
| 2891 |
} |
| 2892 |
return x.checkPutGet(v, length) |
| 2893 |
} |
| 2894 |
|
| 2895 |
func (x *bytesFreelist) checkPutGet(v []byte, length int) []byte { |
| 2896 |
|
| 2897 |
const useSeparateCalls = false |
| 2898 |
|
| 2899 |
if useSeparateCalls { |
| 2900 |
x.put(v) |
| 2901 |
return x.get(length) |
| 2902 |
} |
| 2903 |
|
| 2904 |
if bytesFreeListNoCache { |
| 2905 |
return make([]byte, 0, freelistCapacity(length)) |
| 2906 |
} |
| 2907 |
|
| 2908 |
|
| 2909 |
y := *x |
| 2910 |
var put = cap(v) == 0 |
| 2911 |
if !put { |
| 2912 |
y = append(y, v) |
| 2913 |
*x = y |
| 2914 |
} |
| 2915 |
for i := 0; i < len(y); i++ { |
| 2916 |
z := y[i] |
| 2917 |
if put { |
| 2918 |
if cap(z) >= length { |
| 2919 |
copy(y[i:], y[i+1:]) |
| 2920 |
y = y[:len(y)-1] |
| 2921 |
*x = y |
| 2922 |
return z |
| 2923 |
} |
| 2924 |
} else { |
| 2925 |
if cap(z) > cap(v) { |
| 2926 |
copy(y[i+1:], y[i:]) |
| 2927 |
y[i] = v |
| 2928 |
put = true |
| 2929 |
} |
| 2930 |
} |
| 2931 |
} |
| 2932 |
return make([]byte, 0, freelistCapacity(length)) |
| 2933 |
} |
| 2934 |
|
| 2935 |
|
| 2936 |
|
| 2937 |
|
| 2938 |
|
| 2939 |
|
| 2940 |
|
| 2941 |
|
| 2942 |
|
| 2943 |
|
| 2944 |
|
| 2945 |
|
| 2946 |
type sfiRvFreelist [][]sfiRv |
| 2947 |
|
| 2948 |
func (x *sfiRvFreelist) get(length int) (out []sfiRv) { |
| 2949 |
y := *x |
| 2950 |
|
| 2951 |
|
| 2952 |
|
| 2953 |
for i := 0; i < len(y); i++ { |
| 2954 |
v := y[i] |
| 2955 |
if cap(v) >= length { |
| 2956 |
|
| 2957 |
copy(y[i:], y[i+1:]) |
| 2958 |
*x = y[:len(y)-1] |
| 2959 |
return v |
| 2960 |
} |
| 2961 |
} |
| 2962 |
return make([]sfiRv, 0, freelistCapacity(length)) |
| 2963 |
} |
| 2964 |
|
| 2965 |
func (x *sfiRvFreelist) put(v []sfiRv) { |
| 2966 |
if len(v) != 0 { |
| 2967 |
v = v[:0] |
| 2968 |
} |
| 2969 |
|
| 2970 |
y := append(*x, v) |
| 2971 |
*x = y |
| 2972 |
|
| 2973 |
|
| 2974 |
for i := 0; i < len(y)-1; i++ { |
| 2975 |
z := y[i] |
| 2976 |
if cap(z) > cap(v) { |
| 2977 |
copy(y[i+1:], y[i:]) |
| 2978 |
y[i] = v |
| 2979 |
return |
| 2980 |
} |
| 2981 |
} |
| 2982 |
} |
| 2983 |
|
| 2984 |
|
| 2985 |
|
| 2986 |
|
| 2987 |
|
| 2988 |
|
| 2989 |
|
| 2990 |
|
| 2991 |
const ( |
| 2992 |
internMaxStrLen = 16 |
| 2993 |
internCap = 64 * 2 |
| 2994 |
) |
| 2995 |
|
| 2996 |
type internerMap map[string]string |
| 2997 |
|
| 2998 |
func (x *internerMap) init() { |
| 2999 |
*x = make(map[string]string, internCap) |
| 3000 |
} |
| 3001 |
|
| 3002 |
func (x internerMap) string(v []byte) (s string) { |
| 3003 |
s, ok := x[string(v)] |
| 3004 |
if !ok { |
| 3005 |
s = string(v) |
| 3006 |
x[s] = s |
| 3007 |
} |
| 3008 |
return |
| 3009 |
} |