1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944
|
// Copyright (c) 2012-2015 Ugorji Nwoke. All rights reserved.
// Use of this source code is governed by a MIT license found in the LICENSE file.
package codec
// Contains code shared by both encode and decode.
// Some shared ideas around encoding/decoding
// ------------------------------------------
//
// If an interface{} is passed, we first do a type assertion to see if it is
// a primitive type or a map/slice of primitive types, and use a fastpath to handle it.
//
// If we start with a reflect.Value, we are already in reflect.Value land and
// will try to grab the function for the underlying Type and directly call that function.
// This is more performant than calling reflect.Value.Interface().
//
// This still helps us bypass many layers of reflection, and give best performance.
//
// Containers
// ------------
// Containers in the stream are either associative arrays (key-value pairs) or
// regular arrays (indexed by incrementing integers).
//
// Some streams support indefinite-length containers, and use a breaking
// byte-sequence to denote that the container has come to an end.
//
// Some streams also are text-based, and use explicit separators to denote the
// end/beginning of different values.
//
// During encode, we use a high-level condition to determine how to iterate through
// the container. That decision is based on whether the container is text-based (with
// separators) or binary (without separators). If binary, we do not even call the
// encoding of separators.
//
// During decode, we use a different high-level condition to determine how to iterate
// through the containers. That decision is based on whether the stream contained
// a length prefix, or if it used explicit breaks. If length-prefixed, we assume that
// it has to be binary, and we do not even try to read separators.
//
// Philosophy
// ------------
// On decode, this codec will update containers appropriately:
// - If struct, update fields from stream into fields of struct.
// If field in stream not found in struct, handle appropriately (based on option).
// If a struct field has no corresponding value in the stream, leave it AS IS.
// If nil in stream, set value to nil/zero value.
// - If map, update map from stream.
// If the stream value is NIL, set the map to nil.
// - if slice, try to update up to length of array in stream.
// if container len is less than stream array length,
// and container cannot be expanded, handled (based on option).
// This means you can decode 4-element stream array into 1-element array.
//
// ------------------------------------
// On encode, user can specify omitEmpty. This means that the value will be omitted
// if the zero value. The problem may occur during decode, where omitted values do not affect
// the value being decoded into. This means that if decoding into a struct with an
// int field with current value=5, and the field is omitted in the stream, then after
// decoding, the value will still be 5 (not 0).
// omitEmpty only works if you guarantee that you always decode into zero-values.
//
// ------------------------------------
// We could have truncated a map to remove keys not available in the stream,
// or set values in the struct which are not in the stream to their zero values.
// We decided against it because there is no efficient way to do it.
// We may introduce it as an option later.
// However, that will require enabling it for both runtime and code generation modes.
//
// To support truncate, we need to do 2 passes over the container:
// map
// - first collect all keys (e.g. in k1)
// - for each key in stream, mark k1 that the key should not be removed
// - after updating map, do second pass and call delete for all keys in k1 which are not marked
// struct:
// - for each field, track the *typeInfo s1
// - iterate through all s1, and for each one not marked, set value to zero
// - this involves checking the possible anonymous fields which are nil ptrs.
// too much work.
//
// ------------------------------------------
// Error Handling is done within the library using panic.
//
// This way, the code doesn't have to keep checking if an error has happened,
// and we don't have to keep sending the error value along with each call
// or storing it in the En|Decoder and checking it constantly along the way.
//
// The disadvantage is that small functions which use panics cannot be inlined.
// The code accounts for that by only using panics behind an interface;
// since interface calls cannot be inlined, this is irrelevant.
//
// We considered storing the error is En|Decoder.
// - once it has its err field set, it cannot be used again.
// - panicing will be optional, controlled by const flag.
// - code should always check error first and return early.
// We eventually decided against it as it makes the code clumsier to always
// check for these error conditions.
import (
"bytes"
"encoding"
"encoding/binary"
"errors"
"fmt"
"math"
"os"
"reflect"
"sort"
"strconv"
"strings"
"sync"
"time"
)
const (
scratchByteArrayLen = 32
// initCollectionCap = 16 // 32 is defensive. 16 is preferred.
// Support encoding.(Binary|Text)(Unm|M)arshaler.
// This constant flag will enable or disable it.
supportMarshalInterfaces = true
// for debugging, set this to false, to catch panic traces.
// Note that this will always cause rpc tests to fail, since they need io.EOF sent via panic.
recoverPanicToErr = true
// arrayCacheLen is the length of the cache used in encoder or decoder for
// allowing zero-alloc initialization.
arrayCacheLen = 8
// We tried an optimization, where we detect if a type is one of the known types
// we optimized for (e.g. int, []uint64, etc).
//
// However, we notice some worse performance when using this optimization.
// So we hide it behind a flag, to turn on if needed.
useLookupRecognizedTypes = false
// using recognized allows us to do d.decode(interface{}) instead of d.decodeValue(reflect.Value)
// when we can infer that the kind of the interface{} is one of the ones hard-coded in the
// type switch for known types or the ones defined by fast-path.
//
// However, it seems we get better performance when we don't recognize, and just let
// reflection handle it.
//
// Reasoning is as below:
// typeswitch is a binary search with a branch to a code-point.
// getdecfn is a binary search with a call to a function pointer.
//
// both are about the same.
//
// so: why prefer typeswitch?
//
// is recognized does the following:
// - lookup rtid
// - check if in sorted list
// - calls decode(type switch)
// - 1 or 2 binary search to a point in code
// - branch there
//
// vs getdecfn
// - lookup rtid
// - check in sorted list for a function pointer
// - calls it to decode using reflection (optimized)
// always set xDebug = false before releasing software
xDebug = true
)
var (
oneByteArr = [1]byte{0}
zeroByteSlice = oneByteArr[:0:0]
)
var pool pooler
func init() {
pool.init()
}
// type findCodecFnMode uint8
// const (
// findCodecFnModeMap findCodecFnMode = iota
// findCodecFnModeBinarySearch
// findCodecFnModeLinearSearch
// )
type charEncoding uint8
const (
c_RAW charEncoding = iota
c_UTF8
c_UTF16LE
c_UTF16BE
c_UTF32LE
c_UTF32BE
)
// valueType is the stream type
type valueType uint8
const (
valueTypeUnset valueType = iota
valueTypeNil
valueTypeInt
valueTypeUint
valueTypeFloat
valueTypeBool
valueTypeString
valueTypeSymbol
valueTypeBytes
valueTypeMap
valueTypeArray
valueTypeTimestamp
valueTypeExt
// valueTypeInvalid = 0xff
)
func (x valueType) String() string {
switch x {
case valueTypeNil:
return "Nil"
case valueTypeInt:
return "Int"
case valueTypeUint:
return "Uint"
case valueTypeFloat:
return "Float"
case valueTypeBool:
return "Bool"
case valueTypeString:
return "String"
case valueTypeSymbol:
return "Symbol"
case valueTypeBytes:
return "Bytes"
case valueTypeMap:
return "Map"
case valueTypeArray:
return "Array"
case valueTypeTimestamp:
return "Timestamp"
case valueTypeExt:
return "Ext"
}
return strconv.FormatInt(int64(x), 10)
}
type seqType uint8
const (
_ seqType = iota
seqTypeArray
seqTypeSlice
seqTypeChan
)
// note that containerMapStart and containerArraySend are not sent.
// This is because the ReadXXXStart and EncodeXXXStart already does these.
type containerState uint8
const (
_ containerState = iota
containerMapStart // slot left open, since Driver method already covers it
containerMapKey
containerMapValue
containerMapEnd
containerArrayStart // slot left open, since Driver methods already cover it
containerArrayElem
containerArrayEnd
)
// sfiIdx used for tracking where a (field/enc)Name is seen in a []*structFieldInfo
type sfiIdx struct {
name string
index int
}
// do not recurse if a containing type refers to an embedded type
// which refers back to its containing type (via a pointer).
// The second time this back-reference happens, break out,
// so as not to cause an infinite loop.
const rgetMaxRecursion = 2
// Anecdotally, we believe most types have <= 12 fields.
// Java's PMD rules set TooManyFields threshold to 15.
const typeInfoLoadArrayLen = 12
type typeInfoLoad struct {
fNames []string
encNames []string
etypes []uintptr
sfis []*structFieldInfo
}
type typeInfoLoadArray struct {
fNames [typeInfoLoadArrayLen]string
encNames [typeInfoLoadArrayLen]string
etypes [typeInfoLoadArrayLen]uintptr
sfis [typeInfoLoadArrayLen]*structFieldInfo
sfiidx [typeInfoLoadArrayLen]sfiIdx
}
// type containerStateRecv interface {
// sendContainerState(containerState)
// }
// mirror json.Marshaler and json.Unmarshaler here,
// so we don't import the encoding/json package
type jsonMarshaler interface {
MarshalJSON() ([]byte, error)
}
type jsonUnmarshaler interface {
UnmarshalJSON([]byte) error
}
// type byteAccepter func(byte) bool
var (
bigen = binary.BigEndian
structInfoFieldName = "_struct"
mapStrIntfTyp = reflect.TypeOf(map[string]interface{}(nil))
mapIntfIntfTyp = reflect.TypeOf(map[interface{}]interface{}(nil))
intfSliceTyp = reflect.TypeOf([]interface{}(nil))
intfTyp = intfSliceTyp.Elem()
stringTyp = reflect.TypeOf("")
timeTyp = reflect.TypeOf(time.Time{})
rawExtTyp = reflect.TypeOf(RawExt{})
rawTyp = reflect.TypeOf(Raw{})
uint8SliceTyp = reflect.TypeOf([]uint8(nil))
mapBySliceTyp = reflect.TypeOf((*MapBySlice)(nil)).Elem()
binaryMarshalerTyp = reflect.TypeOf((*encoding.BinaryMarshaler)(nil)).Elem()
binaryUnmarshalerTyp = reflect.TypeOf((*encoding.BinaryUnmarshaler)(nil)).Elem()
textMarshalerTyp = reflect.TypeOf((*encoding.TextMarshaler)(nil)).Elem()
textUnmarshalerTyp = reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem()
jsonMarshalerTyp = reflect.TypeOf((*jsonMarshaler)(nil)).Elem()
jsonUnmarshalerTyp = reflect.TypeOf((*jsonUnmarshaler)(nil)).Elem()
selferTyp = reflect.TypeOf((*Selfer)(nil)).Elem()
uint8SliceTypId = rt2id(uint8SliceTyp)
rawExtTypId = rt2id(rawExtTyp)
rawTypId = rt2id(rawTyp)
intfTypId = rt2id(intfTyp)
timeTypId = rt2id(timeTyp)
stringTypId = rt2id(stringTyp)
mapStrIntfTypId = rt2id(mapStrIntfTyp)
mapIntfIntfTypId = rt2id(mapIntfIntfTyp)
intfSliceTypId = rt2id(intfSliceTyp)
// mapBySliceTypId = rt2id(mapBySliceTyp)
intBitsize uint8 = uint8(reflect.TypeOf(int(0)).Bits())
uintBitsize uint8 = uint8(reflect.TypeOf(uint(0)).Bits())
bsAll0x00 = []byte{0, 0, 0, 0, 0, 0, 0, 0}
bsAll0xff = []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
chkOvf checkOverflow
noFieldNameToStructFieldInfoErr = errors.New("no field name passed to parseStructFieldInfo")
)
var defTypeInfos = NewTypeInfos([]string{"codec", "json"})
var immutableKindsSet = [32]bool{
// reflect.Invalid: ,
reflect.Bool: true,
reflect.Int: true,
reflect.Int8: true,
reflect.Int16: true,
reflect.Int32: true,
reflect.Int64: true,
reflect.Uint: true,
reflect.Uint8: true,
reflect.Uint16: true,
reflect.Uint32: true,
reflect.Uint64: true,
reflect.Uintptr: true,
reflect.Float32: true,
reflect.Float64: true,
reflect.Complex64: true,
reflect.Complex128: true,
// reflect.Array
// reflect.Chan
// reflect.Func: true,
// reflect.Interface
// reflect.Map
// reflect.Ptr
// reflect.Slice
reflect.String: true,
// reflect.Struct
// reflect.UnsafePointer
}
var recognizedRtids []uintptr
var recognizedRtidPtrs []uintptr
var recognizedRtidOrPtrs []uintptr
func init() {
if !useLookupRecognizedTypes {
return
}
for _, v := range [...]interface{}{
float32(0),
float64(0),
uintptr(0),
uint(0),
uint8(0),
uint16(0),
uint32(0),
uint64(0),
uintptr(0),
int(0),
int8(0),
int16(0),
int32(0),
int64(0),
bool(false),
string(""),
Raw{},
[]byte(nil),
} {
rt := reflect.TypeOf(v)
recognizedRtids = append(recognizedRtids, rt2id(rt))
recognizedRtidPtrs = append(recognizedRtidPtrs, rt2id(reflect.PtrTo(rt)))
}
}
func containsU(s []uintptr, v uintptr) bool {
// return false // TODO: REMOVE
h, i, j := 0, 0, len(s)
for i < j {
h = i + (j-i)/2
if s[h] < v {
i = h + 1
} else {
j = h
}
}
if i < len(s) && s[i] == v {
return true
}
return false
}
func isRecognizedRtid(rtid uintptr) bool {
return containsU(recognizedRtids, rtid)
}
func isRecognizedRtidPtr(rtid uintptr) bool {
return containsU(recognizedRtidPtrs, rtid)
}
func isRecognizedRtidOrPtr(rtid uintptr) bool {
return containsU(recognizedRtidOrPtrs, rtid)
}
// Selfer defines methods by which a value can encode or decode itself.
//
// Any type which implements Selfer will be able to encode or decode itself.
// Consequently, during (en|de)code, this takes precedence over
// (text|binary)(M|Unm)arshal or extension support.
type Selfer interface {
CodecEncodeSelf(*Encoder)
CodecDecodeSelf(*Decoder)
}
// MapBySlice represents a slice which should be encoded as a map in the stream.
// The slice contains a sequence of key-value pairs.
// This affords storing a map in a specific sequence in the stream.
//
// The support of MapBySlice affords the following:
// - A slice type which implements MapBySlice will be encoded as a map
// - A slice can be decoded from a map in the stream
type MapBySlice interface {
MapBySlice()
}
// WARNING: DO NOT USE DIRECTLY. EXPORTED FOR GODOC BENEFIT. WILL BE REMOVED.
//
// BasicHandle encapsulates the common options and extension functions.
type BasicHandle struct {
// TypeInfos is used to get the type info for any type.
//
// If not configured, the default TypeInfos is used, which uses struct tag keys: codec, json
TypeInfos *TypeInfos
extHandle
EncodeOptions
DecodeOptions
noBuiltInTypeChecker
}
func (x *BasicHandle) getBasicHandle() *BasicHandle {
return x
}
func (x *BasicHandle) getTypeInfo(rtid uintptr, rt reflect.Type) (pti *typeInfo) {
if x.TypeInfos == nil {
return defTypeInfos.get(rtid, rt)
}
return x.TypeInfos.get(rtid, rt)
}
// Handle is the interface for a specific encoding format.
//
// Typically, a Handle is pre-configured before first time use,
// and not modified while in use. Such a pre-configured Handle
// is safe for concurrent access.
type Handle interface {
getBasicHandle() *BasicHandle
newEncDriver(w *Encoder) encDriver
newDecDriver(r *Decoder) decDriver
isBinary() bool
hasElemSeparators() bool
IsBuiltinType(rtid uintptr) bool
}
// Raw represents raw formatted bytes.
// We "blindly" store it during encode and store the raw bytes during decode.
// Note: it is dangerous during encode, so we may gate the behaviour behind an Encode flag which must be explicitly set.
type Raw []byte
// RawExt represents raw unprocessed extension data.
// Some codecs will decode extension data as a *RawExt if there is no registered extension for the tag.
//
// Only one of Data or Value is nil. If Data is nil, then the content of the RawExt is in the Value.
type RawExt struct {
Tag uint64
// Data is the []byte which represents the raw ext. If Data is nil, ext is exposed in Value.
// Data is used by codecs (e.g. binc, msgpack, simple) which do custom serialization of the types
Data []byte
// Value represents the extension, if Data is nil.
// Value is used by codecs (e.g. cbor, json) which use the format to do custom serialization of the types.
Value interface{}
}
// BytesExt handles custom (de)serialization of types to/from []byte.
// It is used by codecs (e.g. binc, msgpack, simple) which do custom serialization of the types.
type BytesExt interface {
// WriteExt converts a value to a []byte.
//
// Note: v *may* be a pointer to the extension type, if the extension type was a struct or array.
WriteExt(v interface{}) []byte
// ReadExt updates a value from a []byte.
ReadExt(dst interface{}, src []byte)
}
// InterfaceExt handles custom (de)serialization of types to/from another interface{} value.
// The Encoder or Decoder will then handle the further (de)serialization of that known type.
//
// It is used by codecs (e.g. cbor, json) which use the format to do custom serialization of the types.
type InterfaceExt interface {
// ConvertExt converts a value into a simpler interface for easy encoding e.g. convert time.Time to int64.
//
// Note: v *may* be a pointer to the extension type, if the extension type was a struct or array.
ConvertExt(v interface{}) interface{}
// UpdateExt updates a value from a simpler interface for easy decoding e.g. convert int64 to time.Time.
UpdateExt(dst interface{}, src interface{})
}
// Ext handles custom (de)serialization of custom types / extensions.
type Ext interface {
BytesExt
InterfaceExt
}
// addExtWrapper is a wrapper implementation to support former AddExt exported method.
type addExtWrapper struct {
encFn func(reflect.Value) ([]byte, error)
decFn func(reflect.Value, []byte) error
}
func (x addExtWrapper) WriteExt(v interface{}) []byte {
bs, err := x.encFn(reflect.ValueOf(v))
if err != nil {
panic(err)
}
return bs
}
func (x addExtWrapper) ReadExt(v interface{}, bs []byte) {
if err := x.decFn(reflect.ValueOf(v), bs); err != nil {
panic(err)
}
}
func (x addExtWrapper) ConvertExt(v interface{}) interface{} {
return x.WriteExt(v)
}
func (x addExtWrapper) UpdateExt(dest interface{}, v interface{}) {
x.ReadExt(dest, v.([]byte))
}
type setExtWrapper struct {
b BytesExt
i InterfaceExt
}
func (x *setExtWrapper) WriteExt(v interface{}) []byte {
if x.b == nil {
panic("BytesExt.WriteExt is not supported")
}
return x.b.WriteExt(v)
}
func (x *setExtWrapper) ReadExt(v interface{}, bs []byte) {
if x.b == nil {
panic("BytesExt.WriteExt is not supported")
}
x.b.ReadExt(v, bs)
}
func (x *setExtWrapper) ConvertExt(v interface{}) interface{} {
if x.i == nil {
panic("InterfaceExt.ConvertExt is not supported")
}
return x.i.ConvertExt(v)
}
func (x *setExtWrapper) UpdateExt(dest interface{}, v interface{}) {
if x.i == nil {
panic("InterfaceExxt.UpdateExt is not supported")
}
x.i.UpdateExt(dest, v)
}
type binaryEncodingType struct{}
func (_ binaryEncodingType) isBinary() bool { return true }
type textEncodingType struct{}
func (_ textEncodingType) isBinary() bool { return false }
// noBuiltInTypes is embedded into many types which do not support builtins
// e.g. msgpack, simple, cbor.
type noBuiltInTypeChecker struct{}
func (_ noBuiltInTypeChecker) IsBuiltinType(rt uintptr) bool { return false }
type noBuiltInTypes struct{ noBuiltInTypeChecker }
func (_ noBuiltInTypes) EncodeBuiltin(rt uintptr, v interface{}) {}
func (_ noBuiltInTypes) DecodeBuiltin(rt uintptr, v interface{}) {}
// type noStreamingCodec struct{}
// func (_ noStreamingCodec) CheckBreak() bool { return false }
// func (_ noStreamingCodec) hasElemSeparators() bool { return false }
type noElemSeparators struct{}
func (_ noElemSeparators) hasElemSeparators() (v bool) { return }
// bigenHelper.
// Users must already slice the x completely, because we will not reslice.
type bigenHelper struct {
x []byte // must be correctly sliced to appropriate len. slicing is a cost.
w encWriter
}
func (z bigenHelper) writeUint16(v uint16) {
bigen.PutUint16(z.x, v)
z.w.writeb(z.x)
}
func (z bigenHelper) writeUint32(v uint32) {
bigen.PutUint32(z.x, v)
z.w.writeb(z.x)
}
func (z bigenHelper) writeUint64(v uint64) {
bigen.PutUint64(z.x, v)
z.w.writeb(z.x)
}
type extTypeTagFn struct {
rtid uintptr
rt reflect.Type
tag uint64
ext Ext
}
type extHandle []extTypeTagFn
// DEPRECATED: Use SetBytesExt or SetInterfaceExt on the Handle instead.
//
// AddExt registes an encode and decode function for a reflect.Type.
// AddExt internally calls SetExt.
// To deregister an Ext, call AddExt with nil encfn and/or nil decfn.
func (o *extHandle) AddExt(
rt reflect.Type, tag byte,
encfn func(reflect.Value) ([]byte, error), decfn func(reflect.Value, []byte) error,
) (err error) {
if encfn == nil || decfn == nil {
return o.SetExt(rt, uint64(tag), nil)
}
return o.SetExt(rt, uint64(tag), addExtWrapper{encfn, decfn})
}
// DEPRECATED: Use SetBytesExt or SetInterfaceExt on the Handle instead.
//
// Note that the type must be a named type, and specifically not
// a pointer or Interface. An error is returned if that is not honored.
//
// To Deregister an ext, call SetExt with nil Ext
func (o *extHandle) SetExt(rt reflect.Type, tag uint64, ext Ext) (err error) {
// o is a pointer, because we may need to initialize it
if rt.PkgPath() == "" || rt.Kind() == reflect.Interface {
err = fmt.Errorf("codec.Handle.AddExt: Takes named type, not a pointer or interface: %T",
reflect.Zero(rt).Interface())
return
}
rtid := rt2id(rt)
for _, v := range *o {
if v.rtid == rtid {
v.tag, v.ext = tag, ext
return
}
}
if *o == nil {
*o = make([]extTypeTagFn, 0, 4)
}
*o = append(*o, extTypeTagFn{rtid, rt, tag, ext})
return
}
func (o extHandle) getExt(rtid uintptr) *extTypeTagFn {
var v *extTypeTagFn
for i := range o {
v = &o[i]
if v.rtid == rtid {
return v
}
}
return nil
}
func (o extHandle) getExtForTag(tag uint64) *extTypeTagFn {
var v *extTypeTagFn
for i := range o {
v = &o[i]
if v.tag == tag {
return v
}
}
return nil
}
const maxLevelsEmbedding = 16
type structFieldInfo struct {
encName string // encode name
fieldName string // field name
is [maxLevelsEmbedding]uint16 // (recursive/embedded) field index in struct
nis uint8 // num levels of embedding. if 1, then it's not embedded.
omitEmpty bool
toArray bool // if field is _struct, is the toArray set?
}
func (si *structFieldInfo) setToZeroValue(v reflect.Value) {
if v, valid := si.field(v, false); valid {
v.Set(reflect.Zero(v.Type()))
}
}
// rv returns the field of the struct.
// If anonymous, it returns an Invalid
func (si *structFieldInfo) field(v reflect.Value, update bool) (rv2 reflect.Value, valid bool) {
// replicate FieldByIndex
for i, x := range si.is {
if uint8(i) == si.nis {
break
}
if v, valid = baseStructRv(v, update); !valid {
return
}
v = v.Field(int(x))
}
return v, true
}
func (si *structFieldInfo) fieldval(v reflect.Value, update bool) reflect.Value {
v, _ = si.field(v, update)
return v
}
func parseStructFieldInfo(fname string, stag string) *structFieldInfo {
// if fname == "" {
// panic(noFieldNameToStructFieldInfoErr)
// }
si := structFieldInfo{
encName: fname,
}
if stag != "" {
for i, s := range strings.Split(stag, ",") {
if i == 0 {
if s != "" {
si.encName = s
}
} else {
if s == "omitempty" {
si.omitEmpty = true
} else if s == "toarray" {
si.toArray = true
}
}
}
}
// si.encNameBs = []byte(si.encName)
return &si
}
type sfiSortedByEncName []*structFieldInfo
func (p sfiSortedByEncName) Len() int {
return len(p)
}
func (p sfiSortedByEncName) Less(i, j int) bool {
return p[i].encName < p[j].encName
}
func (p sfiSortedByEncName) Swap(i, j int) {
p[i], p[j] = p[j], p[i]
}
const structFieldNodeNumToCache = 4
type structFieldNodeCache struct {
rv [structFieldNodeNumToCache]reflect.Value
idx [structFieldNodeNumToCache]uint32
num uint8
}
func (x *structFieldNodeCache) get(key uint32) (fv reflect.Value, valid bool) {
// defer func() { fmt.Printf(">>>> found in cache2? %v\n", valid) }()
for i, k := range &x.idx {
if uint8(i) == x.num {
return // break
}
if key == k {
return x.rv[i], true
}
}
return
}
func (x *structFieldNodeCache) tryAdd(fv reflect.Value, key uint32) {
if x.num < structFieldNodeNumToCache {
x.rv[x.num] = fv
x.idx[x.num] = key
x.num++
return
}
}
type structFieldNode struct {
v reflect.Value
cache2 structFieldNodeCache
cache3 structFieldNodeCache
update bool
}
func (x *structFieldNode) field(si *structFieldInfo) (fv reflect.Value) {
// return si.fieldval(x.v, x.update)
// Note: we only cache if nis=2 or nis=3 i.e. up to 2 levels of embedding
// This mostly saves us time on the repeated calls to v.Elem, v.Field, etc.
var valid bool
switch si.nis {
case 1:
fv = x.v.Field(int(si.is[0]))
case 2:
if fv, valid = x.cache2.get(uint32(si.is[0])); valid {
fv = fv.Field(int(si.is[1]))
return
}
fv = x.v.Field(int(si.is[0]))
if fv, valid = baseStructRv(fv, x.update); !valid {
return
}
x.cache2.tryAdd(fv, uint32(si.is[0]))
fv = fv.Field(int(si.is[1]))
case 3:
var key uint32 = uint32(si.is[0])<<16 | uint32(si.is[1])
if fv, valid = x.cache3.get(key); valid {
fv = fv.Field(int(si.is[2]))
return
}
fv = x.v.Field(int(si.is[0]))
if fv, valid = baseStructRv(fv, x.update); !valid {
return
}
fv = fv.Field(int(si.is[1]))
if fv, valid = baseStructRv(fv, x.update); !valid {
return
}
x.cache3.tryAdd(fv, key)
fv = fv.Field(int(si.is[2]))
default:
fv, _ = si.field(x.v, x.update)
}
return
}
func baseStructRv(v reflect.Value, update bool) (v2 reflect.Value, valid bool) {
for v.Kind() == reflect.Ptr {
if v.IsNil() {
if !update {
return
}
v.Set(reflect.New(v.Type().Elem()))
}
v = v.Elem()
}
return v, true
}
// typeInfo keeps information about each type referenced in the encode/decode sequence.
//
// During an encode/decode sequence, we work as below:
// - If base is a built in type, en/decode base value
// - If base is registered as an extension, en/decode base value
// - If type is binary(M/Unm)arshaler, call Binary(M/Unm)arshal method
// - If type is text(M/Unm)arshaler, call Text(M/Unm)arshal method
// - Else decode appropriately based on the reflect.Kind
type typeInfo struct {
sfi []*structFieldInfo // sorted. Used when enc/dec struct to map.
sfip []*structFieldInfo // unsorted. Used when enc/dec struct to array.
rt reflect.Type
rtid uintptr
// rv0 reflect.Value // saved zero value, used if immutableKind
numMeth uint16 // number of methods
// baseId gives pointer to the base reflect.Type, after deferencing
// the pointers. E.g. base type of ***time.Time is time.Time.
base reflect.Type
baseId uintptr
baseIndir int8 // number of indirections to get to base
anyOmitEmpty bool
mbs bool // base type (T or *T) is a MapBySlice
bm bool // base type (T or *T) is a binaryMarshaler
bunm bool // base type (T or *T) is a binaryUnmarshaler
bmIndir int8 // number of indirections to get to binaryMarshaler type
bunmIndir int8 // number of indirections to get to binaryUnmarshaler type
tm bool // base type (T or *T) is a textMarshaler
tunm bool // base type (T or *T) is a textUnmarshaler
tmIndir int8 // number of indirections to get to textMarshaler type
tunmIndir int8 // number of indirections to get to textUnmarshaler type
jm bool // base type (T or *T) is a jsonMarshaler
junm bool // base type (T or *T) is a jsonUnmarshaler
jmIndir int8 // number of indirections to get to jsonMarshaler type
junmIndir int8 // number of indirections to get to jsonUnmarshaler type
cs bool // base type (T or *T) is a Selfer
csIndir int8 // number of indirections to get to Selfer type
toArray bool // whether this (struct) type should be encoded as an array
}
// define length beyond which we do a binary search instead of a linear search.
// From our testing, linear search seems faster than binary search up to 16-field structs.
// However, we set to 8 similar to what python does for hashtables.
const indexForEncNameBinarySearchThreshold = 8
func (ti *typeInfo) indexForEncName(name string) int {
// NOTE: name may be a stringView, so don't pass it to another function.
//tisfi := ti.sfi
sfilen := len(ti.sfi)
if sfilen < indexForEncNameBinarySearchThreshold {
for i, si := range ti.sfi {
if si.encName == name {
return i
}
}
return -1
}
// binary search. adapted from sort/search.go.
h, i, j := 0, 0, sfilen
for i < j {
h = i + (j-i)/2
if ti.sfi[h].encName < name {
i = h + 1
} else {
j = h
}
}
if i < sfilen && ti.sfi[i].encName == name {
return i
}
return -1
}
type rtid2ti struct {
rtid uintptr
ti *typeInfo
}
// TypeInfos caches typeInfo for each type on first inspection.
//
// It is configured with a set of tag keys, which are used to get
// configuration for the type.
type TypeInfos struct {
infos atomicTypeInfoSlice // formerly map[uintptr]*typeInfo, now *[]rtid2ti
mu sync.Mutex
tags []string
}
// NewTypeInfos creates a TypeInfos given a set of struct tags keys.
//
// This allows users customize the struct tag keys which contain configuration
// of their types.
func NewTypeInfos(tags []string) *TypeInfos {
return &TypeInfos{tags: tags}
}
func (x *TypeInfos) structTag(t reflect.StructTag) (s string) {
// check for tags: codec, json, in that order.
// this allows seamless support for many configured structs.
for _, x := range x.tags {
s = t.Get(x)
if s != "" {
return s
}
}
return
}
func (x *TypeInfos) find(sp *[]rtid2ti, rtid uintptr) (idx int, ti *typeInfo) {
// binary search. adapted from sort/search.go.
// if sp == nil {
// return -1, nil
// }
s := *sp
h, i, j := 0, 0, len(s)
for i < j {
h = i + (j-i)/2
if s[h].rtid < rtid {
i = h + 1
} else {
j = h
}
}
if i < len(s) && s[i].rtid == rtid {
return i, s[i].ti
}
return i, nil
}
func (x *TypeInfos) get(rtid uintptr, rt reflect.Type) (pti *typeInfo) {
sp := x.infos.load()
var idx int
if sp != nil {
idx, pti = x.find(sp, rtid)
if pti != nil {
return
}
}
// do not hold lock while computing this.
// it may lead to duplication, but that's ok.
ti := typeInfo{rt: rt, rtid: rtid}
// ti.rv0 = reflect.Zero(rt)
ti.numMeth = uint16(rt.NumMethod())
var ok bool
var indir int8
if ok, indir = implementsIntf(rt, binaryMarshalerTyp); ok {
ti.bm, ti.bmIndir = true, indir
}
if ok, indir = implementsIntf(rt, binaryUnmarshalerTyp); ok {
ti.bunm, ti.bunmIndir = true, indir
}
if ok, indir = implementsIntf(rt, textMarshalerTyp); ok {
ti.tm, ti.tmIndir = true, indir
}
if ok, indir = implementsIntf(rt, textUnmarshalerTyp); ok {
ti.tunm, ti.tunmIndir = true, indir
}
if ok, indir = implementsIntf(rt, jsonMarshalerTyp); ok {
ti.jm, ti.jmIndir = true, indir
}
if ok, indir = implementsIntf(rt, jsonUnmarshalerTyp); ok {
ti.junm, ti.junmIndir = true, indir
}
if ok, indir = implementsIntf(rt, selferTyp); ok {
ti.cs, ti.csIndir = true, indir
}
if ok, _ = implementsIntf(rt, mapBySliceTyp); ok {
ti.mbs = true
}
pt := rt
var ptIndir int8
// for ; pt.Kind() == reflect.Ptr; pt, ptIndir = pt.Elem(), ptIndir+1 { }
for pt.Kind() == reflect.Ptr {
pt = pt.Elem()
ptIndir++
}
if ptIndir == 0 {
ti.base = rt
ti.baseId = rtid
} else {
ti.base = pt
ti.baseId = rt2id(pt)
ti.baseIndir = ptIndir
}
if rt.Kind() == reflect.Struct {
var omitEmpty bool
if f, ok := rt.FieldByName(structInfoFieldName); ok {
siInfo := parseStructFieldInfo(structInfoFieldName, x.structTag(f.Tag))
ti.toArray = siInfo.toArray
omitEmpty = siInfo.omitEmpty
}
pp, pi := pool.tiLoad()
pv := pi.(*typeInfoLoadArray)
pv.etypes[0] = ti.baseId
vv := typeInfoLoad{pv.fNames[:0], pv.encNames[:0], pv.etypes[:1], pv.sfis[:0]}
x.rget(rt, rtid, omitEmpty, nil, &vv)
ti.sfip, ti.sfi, ti.anyOmitEmpty = rgetResolveSFI(vv.sfis, pv.sfiidx[:0])
pp.Put(pi)
}
// sfi = sfip
var vs []rtid2ti
x.mu.Lock()
sp = x.infos.load()
if sp == nil {
pti = &ti
vs = []rtid2ti{{rtid, pti}}
x.infos.store(&vs)
} else {
idx, pti = x.find(sp, rtid)
if pti == nil {
s := *sp
pti = &ti
vs = make([]rtid2ti, len(s)+1)
copy(vs, s[:idx])
vs[idx] = rtid2ti{rtid, pti}
copy(vs[idx+1:], s[idx:])
x.infos.store(&vs)
}
}
x.mu.Unlock()
return
}
func (x *TypeInfos) rget(rt reflect.Type, rtid uintptr, omitEmpty bool,
indexstack []uint16, pv *typeInfoLoad,
) {
// Read up fields and store how to access the value.
//
// It uses go's rules for message selectors,
// which say that the field with the shallowest depth is selected.
//
// Note: we consciously use slices, not a map, to simulate a set.
// Typically, types have < 16 fields,
// and iteration using equals is faster than maps there
flen := rt.NumField()
if flen > (1<<maxLevelsEmbedding - 1) {
panic(fmt.Errorf("codec: types with more than %v fields are not supported - has %v fields", (1<<maxLevelsEmbedding - 1), flen))
}
LOOP:
for j, jlen := uint16(0), uint16(flen); j < jlen; j++ {
f := rt.Field(int(j))
fkind := f.Type.Kind()
// skip if a func type, or is unexported, or structTag value == "-"
switch fkind {
case reflect.Func, reflect.Complex64, reflect.Complex128, reflect.UnsafePointer:
continue LOOP
}
// if r1, _ := utf8.DecodeRuneInString(f.Name);
// r1 == utf8.RuneError || !unicode.IsUpper(r1) {
if f.PkgPath != "" && !f.Anonymous { // unexported, not embedded
continue
}
stag := x.structTag(f.Tag)
if stag == "-" {
continue
}
var si *structFieldInfo
// if anonymous and no struct tag (or it's blank),
// and a struct (or pointer to struct), inline it.
if f.Anonymous && fkind != reflect.Interface {
doInline := stag == ""
if !doInline {
si = parseStructFieldInfo("", stag)
doInline = si.encName == ""
// doInline = si.isZero()
}
if doInline {
ft := f.Type
for ft.Kind() == reflect.Ptr {
ft = ft.Elem()
}
if ft.Kind() == reflect.Struct {
// if etypes contains this, don't call rget again (as fields are already seen here)
ftid := rt2id(ft)
// We cannot recurse forever, but we need to track other field depths.
// So - we break if we see a type twice (not the first time).
// This should be sufficient to handle an embedded type that refers to its
// owning type, which then refers to its embedded type.
processIt := true
numk := 0
for _, k := range pv.etypes {
if k == ftid {
numk++
if numk == rgetMaxRecursion {
processIt = false
break
}
}
}
if processIt {
pv.etypes = append(pv.etypes, ftid)
indexstack2 := make([]uint16, len(indexstack)+1)
copy(indexstack2, indexstack)
indexstack2[len(indexstack)] = j
// indexstack2 := append(append(make([]int, 0, len(indexstack)+4), indexstack...), j)
x.rget(ft, ftid, omitEmpty, indexstack2, pv)
}
continue
}
}
}
// after the anonymous dance: if an unexported field, skip
if f.PkgPath != "" { // unexported
continue
}
if f.Name == "" {
panic(noFieldNameToStructFieldInfoErr)
}
pv.fNames = append(pv.fNames, f.Name)
if si == nil {
si = parseStructFieldInfo(f.Name, stag)
} else if si.encName == "" {
si.encName = f.Name
}
si.fieldName = f.Name
pv.encNames = append(pv.encNames, si.encName)
// si.ikind = int(f.Type.Kind())
if len(indexstack) > maxLevelsEmbedding-1 {
panic(fmt.Errorf("codec: only supports up to %v depth of embedding - type has %v depth", maxLevelsEmbedding-1, len(indexstack)))
}
si.nis = uint8(len(indexstack)) + 1
copy(si.is[:], indexstack)
si.is[len(indexstack)] = j
if omitEmpty {
si.omitEmpty = true
}
pv.sfis = append(pv.sfis, si)
}
}
// resolves the struct field info got from a call to rget.
// Returns a trimmed, unsorted and sorted []*structFieldInfo.
func rgetResolveSFI(x []*structFieldInfo, pv []sfiIdx) (y, z []*structFieldInfo, anyOmitEmpty bool) {
var n int
for i, v := range x {
xn := v.encName // TODO: fieldName or encName? use encName for now.
var found bool
for j, k := range pv {
if k.name == xn {
// one of them must be reset to nil, and the index updated appropriately to the other one
if v.nis == x[k.index].nis {
} else if v.nis < x[k.index].nis {
pv[j].index = i
if x[k.index] != nil {
x[k.index] = nil
n++
}
} else {
if x[i] != nil {
x[i] = nil
n++
}
}
found = true
break
}
}
if !found {
pv = append(pv, sfiIdx{xn, i})
}
}
// remove all the nils
y = make([]*structFieldInfo, len(x)-n)
n = 0
for _, v := range x {
if v == nil {
continue
}
if !anyOmitEmpty && v.omitEmpty {
anyOmitEmpty = true
}
y[n] = v
n++
}
z = make([]*structFieldInfo, len(y))
copy(z, y)
sort.Sort(sfiSortedByEncName(z))
return
}
func xprintf(format string, a ...interface{}) {
if xDebug {
fmt.Fprintf(os.Stderr, format, a...)
}
}
func panicToErr(err *error) {
if recoverPanicToErr {
if x := recover(); x != nil {
// if false && xDebug {
// fmt.Printf("panic'ing with: %v\n", x)
// debug.PrintStack()
// }
panicValToErr(x, err)
}
}
}
func panicToErrs2(err1, err2 *error) {
if recoverPanicToErr {
if x := recover(); x != nil {
panicValToErr(x, err1)
panicValToErr(x, err2)
}
}
}
// func doPanic(tag string, format string, params ...interface{}) {
// params2 := make([]interface{}, len(params)+1)
// params2[0] = tag
// copy(params2[1:], params)
// panic(fmt.Errorf("%s: "+format, params2...))
// }
func isImmutableKind(k reflect.Kind) (v bool) {
return immutableKindsSet[k]
// return false ||
// k == reflect.Int ||
// k == reflect.Int8 ||
// k == reflect.Int16 ||
// k == reflect.Int32 ||
// k == reflect.Int64 ||
// k == reflect.Uint ||
// k == reflect.Uint8 ||
// k == reflect.Uint16 ||
// k == reflect.Uint32 ||
// k == reflect.Uint64 ||
// k == reflect.Uintptr ||
// k == reflect.Float32 ||
// k == reflect.Float64 ||
// k == reflect.Bool ||
// k == reflect.String
}
// ----
type codecFnInfo struct {
ti *typeInfo
xfFn Ext
xfTag uint64
seq seqType
addr bool
}
// codecFn encapsulates the captured variables and the encode function.
// This way, we only do some calculations one times, and pass to the
// code block that should be called (encapsulated in a function)
// instead of executing the checks every time.
type codecFn struct {
i codecFnInfo
fe func(*Encoder, *codecFnInfo, reflect.Value)
fd func(*Decoder, *codecFnInfo, reflect.Value)
}
type codecRtidFn struct {
rtid uintptr
fn codecFn
}
type codecFner struct {
hh Handle
h *BasicHandle
cs [arrayCacheLen]*[arrayCacheLen]codecRtidFn
s []*[arrayCacheLen]codecRtidFn
sn uint32
be bool
js bool
cf [arrayCacheLen]codecRtidFn
}
func (c *codecFner) reset(hh Handle) {
c.hh = hh
c.h = hh.getBasicHandle()
_, c.js = hh.(*JsonHandle)
c.be = hh.isBinary()
}
func (c *codecFner) get(rt reflect.Type, checkFastpath, checkCodecSelfer bool) (fn *codecFn) {
rtid := rt2id(rt)
var j uint32
var sn uint32 = c.sn
if sn == 0 {
c.s = c.cs[:1]
c.s[0] = &c.cf
c.cf[0].rtid = rtid
fn = &(c.cf[0].fn)
c.sn = 1
} else {
LOOP1:
for _, x := range c.s {
for i := range x {
if j == sn {
break LOOP1
}
if x[i].rtid == rtid {
fn = &(x[i].fn)
return
}
j++
}
}
sx, sy := sn/arrayCacheLen, sn%arrayCacheLen
if sy == 0 {
c.s = append(c.s, &[arrayCacheLen]codecRtidFn{})
}
c.s[sx][sy].rtid = rtid
fn = &(c.s[sx][sy].fn)
c.sn++
}
ti := c.h.getTypeInfo(rtid, rt)
fi := &(fn.i)
fi.ti = ti
if checkCodecSelfer && ti.cs {
fn.fe = (*Encoder).selferMarshal
fn.fd = (*Decoder).selferUnmarshal
} else if rtid == rawTypId {
fn.fe = (*Encoder).raw
fn.fd = (*Decoder).raw
} else if rtid == rawExtTypId {
fn.fe = (*Encoder).rawExt
fn.fd = (*Decoder).rawExt
fn.i.addr = true
} else if c.hh.IsBuiltinType(rtid) {
fn.fe = (*Encoder).builtin
fn.fd = (*Decoder).builtin
fn.i.addr = true
} else if xfFn := c.h.getExt(rtid); xfFn != nil {
fi.xfTag, fi.xfFn = xfFn.tag, xfFn.ext
fn.fe = (*Encoder).ext
fn.fd = (*Decoder).ext
fn.i.addr = true
} else if supportMarshalInterfaces && c.be && ti.bm {
fn.fe = (*Encoder).binaryMarshal
fn.fd = (*Decoder).binaryUnmarshal
} else if supportMarshalInterfaces && !c.be && c.js && ti.jm {
//If JSON, we should check JSONMarshal before textMarshal
fn.fe = (*Encoder).jsonMarshal
fn.fd = (*Decoder).jsonUnmarshal
} else if supportMarshalInterfaces && !c.be && ti.tm {
fn.fe = (*Encoder).textMarshal
fn.fd = (*Decoder).textUnmarshal
} else {
rk := rt.Kind()
if fastpathEnabled && checkFastpath && (rk == reflect.Map || rk == reflect.Slice) {
if rt.PkgPath() == "" { // un-named slice or map
if idx := fastpathAV.index(rtid); idx != -1 {
fn.fe = fastpathAV[idx].encfn
fn.fd = fastpathAV[idx].decfn
fn.i.addr = true
}
} else {
// use mapping for underlying type if there
var rtu reflect.Type
if rk == reflect.Map {
rtu = reflect.MapOf(rt.Key(), rt.Elem())
} else {
rtu = reflect.SliceOf(rt.Elem())
}
rtuid := rt2id(rtu)
if idx := fastpathAV.index(rtuid); idx != -1 {
xfnf := fastpathAV[idx].encfn
xrt := fastpathAV[idx].rt
fn.fe = func(e *Encoder, xf *codecFnInfo, xrv reflect.Value) {
xfnf(e, xf, xrv.Convert(xrt))
}
fn.i.addr = true
xfnf2 := fastpathAV[idx].decfn
fn.fd = func(d *Decoder, xf *codecFnInfo, xrv reflect.Value) {
xfnf2(d, xf, xrv.Convert(reflect.PtrTo(xrt)))
}
}
}
}
if fn.fe == nil && fn.fd == nil {
switch rk {
case reflect.Bool:
fn.fe = (*Encoder).kBool
fn.fd = (*Decoder).kBool
case reflect.String:
fn.fe = (*Encoder).kString
fn.fd = (*Decoder).kString
case reflect.Int:
fn.fd = (*Decoder).kInt
fn.fe = (*Encoder).kInt
case reflect.Int8:
fn.fe = (*Encoder).kInt
fn.fd = (*Decoder).kInt8
case reflect.Int16:
fn.fe = (*Encoder).kInt
fn.fd = (*Decoder).kInt16
case reflect.Int32:
fn.fe = (*Encoder).kInt
fn.fd = (*Decoder).kInt32
case reflect.Int64:
fn.fe = (*Encoder).kInt
fn.fd = (*Decoder).kInt64
case reflect.Uint:
fn.fd = (*Decoder).kUint
fn.fe = (*Encoder).kUint
case reflect.Uint8:
fn.fe = (*Encoder).kUint
fn.fd = (*Decoder).kUint8
case reflect.Uint16:
fn.fe = (*Encoder).kUint
fn.fd = (*Decoder).kUint16
case reflect.Uint32:
fn.fe = (*Encoder).kUint
fn.fd = (*Decoder).kUint32
case reflect.Uint64:
fn.fe = (*Encoder).kUint
fn.fd = (*Decoder).kUint64
// case reflect.Ptr:
// fn.fd = (*Decoder).kPtr
case reflect.Uintptr:
fn.fe = (*Encoder).kUint
fn.fd = (*Decoder).kUintptr
case reflect.Float32:
fn.fe = (*Encoder).kFloat32
fn.fd = (*Decoder).kFloat32
case reflect.Float64:
fn.fe = (*Encoder).kFloat64
fn.fd = (*Decoder).kFloat64
case reflect.Invalid:
fn.fe = (*Encoder).kInvalid
case reflect.Chan:
fi.seq = seqTypeChan
fn.fe = (*Encoder).kSlice
fn.fd = (*Decoder).kSlice
case reflect.Slice:
fi.seq = seqTypeSlice
fn.fe = (*Encoder).kSlice
fn.fd = (*Decoder).kSlice
case reflect.Array:
fi.seq = seqTypeArray
fn.fe = (*Encoder).kSlice
fi.addr = false
rt2 := reflect.SliceOf(rt.Elem())
fn.fd = func(d *Decoder, xf *codecFnInfo, xrv reflect.Value) {
// println(">>>>>> decoding an array ... ")
d.cf.get(rt2, true, false).fd(d, xf, xrv.Slice(0, xrv.Len()))
// println(">>>>>> decoding an array ... DONE")
}
// fn.fd = (*Decoder).kArray
case reflect.Struct:
if ti.anyOmitEmpty {
fn.fe = (*Encoder).kStruct
} else {
fn.fe = (*Encoder).kStructNoOmitempty
}
fn.fd = (*Decoder).kStruct
// reflect.Ptr and reflect.Interface are handled already by preEncodeValue
// case reflect.Ptr:
// fn.fe = (*Encoder).kPtr
// case reflect.Interface:
// fn.fe = (*Encoder).kInterface
case reflect.Map:
fn.fe = (*Encoder).kMap
fn.fd = (*Decoder).kMap
case reflect.Interface:
// encode: reflect.Interface are handled already by preEncodeValue
fn.fd = (*Decoder).kInterface
default:
fn.fe = (*Encoder).kErr
fn.fd = (*Decoder).kErr
}
}
}
return
}
// ----
// these functions must be inlinable, and not call anybody
type checkOverflow struct{}
func (_ checkOverflow) Float32(f float64) (overflow bool) {
if f < 0 {
f = -f
}
return math.MaxFloat32 < f && f <= math.MaxFloat64
}
func (_ checkOverflow) Uint(v uint64, bitsize uint8) (overflow bool) {
if bitsize == 0 || bitsize >= 64 || v == 0 {
return
}
if trunc := (v << (64 - bitsize)) >> (64 - bitsize); v != trunc {
overflow = true
}
return
}
func (_ checkOverflow) Int(v int64, bitsize uint8) (overflow bool) {
if bitsize == 0 || bitsize >= 64 || v == 0 {
return
}
if trunc := (v << (64 - bitsize)) >> (64 - bitsize); v != trunc {
overflow = true
}
return
}
func (_ checkOverflow) SignedInt(v uint64) (i int64, overflow bool) {
//e.g. -127 to 128 for int8
pos := (v >> 63) == 0
ui2 := v & 0x7fffffffffffffff
if pos {
if ui2 > math.MaxInt64 {
overflow = true
return
}
} else {
if ui2 > math.MaxInt64-1 {
overflow = true
return
}
}
i = int64(v)
return
}
// ------------------ SORT -----------------
func isNaN(f float64) bool { return f != f }
// -----------------------
type intSlice []int64
type uintSlice []uint64
type uintptrSlice []uintptr
type floatSlice []float64
type boolSlice []bool
type stringSlice []string
type bytesSlice [][]byte
func (p intSlice) Len() int { return len(p) }
func (p intSlice) Less(i, j int) bool { return p[i] < p[j] }
func (p intSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (p uintSlice) Len() int { return len(p) }
func (p uintSlice) Less(i, j int) bool { return p[i] < p[j] }
func (p uintSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (p uintptrSlice) Len() int { return len(p) }
func (p uintptrSlice) Less(i, j int) bool { return p[i] < p[j] }
func (p uintptrSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (p floatSlice) Len() int { return len(p) }
func (p floatSlice) Less(i, j int) bool {
return p[i] < p[j] || isNaN(p[i]) && !isNaN(p[j])
}
func (p floatSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (p stringSlice) Len() int { return len(p) }
func (p stringSlice) Less(i, j int) bool { return p[i] < p[j] }
func (p stringSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (p bytesSlice) Len() int { return len(p) }
func (p bytesSlice) Less(i, j int) bool { return bytes.Compare(p[i], p[j]) == -1 }
func (p bytesSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (p boolSlice) Len() int { return len(p) }
func (p boolSlice) Less(i, j int) bool { return !p[i] && p[j] }
func (p boolSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
// ---------------------
type intRv struct {
v int64
r reflect.Value
}
type intRvSlice []intRv
type uintRv struct {
v uint64
r reflect.Value
}
type uintRvSlice []uintRv
type floatRv struct {
v float64
r reflect.Value
}
type floatRvSlice []floatRv
type boolRv struct {
v bool
r reflect.Value
}
type boolRvSlice []boolRv
type stringRv struct {
v string
r reflect.Value
}
type stringRvSlice []stringRv
type bytesRv struct {
v []byte
r reflect.Value
}
type bytesRvSlice []bytesRv
func (p intRvSlice) Len() int { return len(p) }
func (p intRvSlice) Less(i, j int) bool { return p[i].v < p[j].v }
func (p intRvSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (p uintRvSlice) Len() int { return len(p) }
func (p uintRvSlice) Less(i, j int) bool { return p[i].v < p[j].v }
func (p uintRvSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (p floatRvSlice) Len() int { return len(p) }
func (p floatRvSlice) Less(i, j int) bool {
return p[i].v < p[j].v || isNaN(p[i].v) && !isNaN(p[j].v)
}
func (p floatRvSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (p stringRvSlice) Len() int { return len(p) }
func (p stringRvSlice) Less(i, j int) bool { return p[i].v < p[j].v }
func (p stringRvSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (p bytesRvSlice) Len() int { return len(p) }
func (p bytesRvSlice) Less(i, j int) bool { return bytes.Compare(p[i].v, p[j].v) == -1 }
func (p bytesRvSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (p boolRvSlice) Len() int { return len(p) }
func (p boolRvSlice) Less(i, j int) bool { return !p[i].v && p[j].v }
func (p boolRvSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
// -----------------
type bytesI struct {
v []byte
i interface{}
}
type bytesISlice []bytesI
func (p bytesISlice) Len() int { return len(p) }
func (p bytesISlice) Less(i, j int) bool { return bytes.Compare(p[i].v, p[j].v) == -1 }
func (p bytesISlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
// -----------------
type set []uintptr
func (s *set) add(v uintptr) (exists bool) {
// e.ci is always nil, or len >= 1
x := *s
if x == nil {
x = make([]uintptr, 1, 8)
x[0] = v
*s = x
return
}
// typically, length will be 1. make this perform.
if len(x) == 1 {
if j := x[0]; j == 0 {
x[0] = v
} else if j == v {
exists = true
} else {
x = append(x, v)
*s = x
}
return
}
// check if it exists
for _, j := range x {
if j == v {
exists = true
return
}
}
// try to replace a "deleted" slot
for i, j := range x {
if j == 0 {
x[i] = v
return
}
}
// if unable to replace deleted slot, just append it.
x = append(x, v)
*s = x
return
}
func (s *set) remove(v uintptr) (exists bool) {
x := *s
if len(x) == 0 {
return
}
if len(x) == 1 {
if x[0] == v {
x[0] = 0
}
return
}
for i, j := range x {
if j == v {
exists = true
x[i] = 0 // set it to 0, as way to delete it.
// copy(x[i:], x[i+1:])
// x = x[:len(x)-1]
return
}
}
return
}
// ------
// bitset types are better than [256]bool, because they permit the whole
// bitset array being on a single cache line and use less memory.
// given x > 0 and n > 0 and x is exactly 2^n, then pos/x === pos>>n AND pos%x === pos&(x-1).
// consequently, pos/32 === pos>>5, pos/16 === pos>>4, pos/8 === pos>>3, pos%8 == pos&7
type bitset256 [32]byte
func (x *bitset256) set(pos byte) {
x[pos>>3] |= (1 << (pos & 7))
}
func (x *bitset256) unset(pos byte) {
x[pos>>3] &^= (1 << (pos & 7))
}
func (x *bitset256) isset(pos byte) bool {
return x[pos>>3]&(1<<(pos&7)) != 0
}
type bitset128 [16]byte
func (x *bitset128) set(pos byte) {
x[pos>>3] |= (1 << (pos & 7))
}
func (x *bitset128) unset(pos byte) {
x[pos>>3] &^= (1 << (pos & 7))
}
func (x *bitset128) isset(pos byte) bool {
return x[pos>>3]&(1<<(pos&7)) != 0
}
// ------------
type pooler struct {
// for stringRV
strRv8, strRv16, strRv32, strRv64, strRv128 sync.Pool
// for the decNaked
dn sync.Pool
tiload sync.Pool
}
func (p *pooler) init() {
p.strRv8.New = func() interface{} { return new([8]stringRv) }
p.strRv16.New = func() interface{} { return new([16]stringRv) }
p.strRv32.New = func() interface{} { return new([32]stringRv) }
p.strRv64.New = func() interface{} { return new([64]stringRv) }
p.strRv128.New = func() interface{} { return new([128]stringRv) }
p.dn.New = func() interface{} { x := new(decNaked); x.init(); return x }
p.tiload.New = func() interface{} { return new(typeInfoLoadArray) }
}
func (p *pooler) stringRv8() (sp *sync.Pool, v interface{}) {
return &p.strRv8, p.strRv8.Get()
}
func (p *pooler) stringRv16() (sp *sync.Pool, v interface{}) {
return &p.strRv16, p.strRv16.Get()
}
func (p *pooler) stringRv32() (sp *sync.Pool, v interface{}) {
return &p.strRv32, p.strRv32.Get()
}
func (p *pooler) stringRv64() (sp *sync.Pool, v interface{}) {
return &p.strRv64, p.strRv64.Get()
}
func (p *pooler) stringRv128() (sp *sync.Pool, v interface{}) {
return &p.strRv128, p.strRv128.Get()
}
func (p *pooler) decNaked() (sp *sync.Pool, v interface{}) {
return &p.dn, p.dn.Get()
}
func (p *pooler) tiLoad() (sp *sync.Pool, v interface{}) {
return &p.tiload, p.tiload.Get()
}
|