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 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334
|
/*
Copyright 2017 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package spanner
import (
"encoding/json"
"fmt"
"math"
"reflect"
"testing"
"time"
"cloud.google.com/go/civil"
"cloud.google.com/go/internal/testutil"
"github.com/golang/protobuf/proto"
proto3 "github.com/golang/protobuf/ptypes/struct"
"github.com/google/go-cmp/cmp"
sppb "google.golang.org/genproto/googleapis/spanner/v1"
)
var (
t1 = mustParseTime("2016-11-15T15:04:05.999999999Z")
// Boundaries
t2 = mustParseTime("0001-01-01T00:00:00.000000000Z")
t3 = mustParseTime("9999-12-31T23:59:59.999999999Z")
// Local timezone
t4 = time.Now()
d1 = mustParseDate("2016-11-15")
d2 = mustParseDate("1678-01-01")
)
func mustParseTime(s string) time.Time {
t, err := time.Parse(time.RFC3339Nano, s)
if err != nil {
panic(err)
}
return t
}
func mustParseDate(s string) civil.Date {
d, err := civil.ParseDate(s)
if err != nil {
panic(err)
}
return d
}
// Test encoding Values.
func TestEncodeValue(t *testing.T) {
type CustomString string
type CustomBytes []byte
type CustomInt64 int64
type CustomBool bool
type CustomFloat64 float64
type CustomTime time.Time
type CustomDate civil.Date
type CustomNullString NullString
type CustomNullInt64 NullInt64
type CustomNullBool NullBool
type CustomNullFloat64 NullFloat64
type CustomNullTime NullTime
type CustomNullDate NullDate
sValue := "abc"
var sNilPtr *string
iValue := int64(7)
var iNilPtr *int64
bValue := true
var bNilPtr *bool
fValue := 3.14
var fNilPtr *float64
tValue := t1
var tNilPtr *time.Time
dValue := d1
var dNilPtr *civil.Date
var (
tString = stringType()
tInt = intType()
tBool = boolType()
tFloat = floatType()
tBytes = bytesType()
tTime = timeType()
tDate = dateType()
)
for i, test := range []struct {
in interface{}
want *proto3.Value
wantType *sppb.Type
name string
}{
// STRING / STRING ARRAY:
{"abc", stringProto("abc"), tString, "string"},
{NullString{"abc", true}, stringProto("abc"), tString, "NullString with value"},
{NullString{"abc", false}, nullProto(), tString, "NullString with null"},
{&sValue, stringProto("abc"), tString, "*string with value"},
{sNilPtr, nullProto(), tString, "*string with null"},
{[]string(nil), nullProto(), listType(tString), "null []string"},
{[]string{"abc", "bcd"}, listProto(stringProto("abc"), stringProto("bcd")), listType(tString), "[]string"},
{[]NullString{{"abcd", true}, {"xyz", false}}, listProto(stringProto("abcd"), nullProto()), listType(tString), "[]NullString"},
{[]*string{&sValue, sNilPtr}, listProto(stringProto("abc"), nullProto()), listType(tString), "[]*string"},
// BYTES / BYTES ARRAY
{[]byte("foo"), bytesProto([]byte("foo")), tBytes, "[]byte with value"},
{[]byte(nil), nullProto(), tBytes, "null []byte"},
{[][]byte{nil, []byte("ab")}, listProto(nullProto(), bytesProto([]byte("ab"))), listType(tBytes), "[][]byte"},
{[][]byte(nil), nullProto(), listType(tBytes), "null [][]byte"},
// INT64 / INT64 ARRAY
{7, intProto(7), tInt, "int"},
{[]int(nil), nullProto(), listType(tInt), "null []int"},
{[]int{31, 127}, listProto(intProto(31), intProto(127)), listType(tInt), "[]int"},
{int64(81), intProto(81), tInt, "int64"},
{[]int64(nil), nullProto(), listType(tInt), "null []int64"},
{[]int64{33, 129}, listProto(intProto(33), intProto(129)), listType(tInt), "[]int64"},
{NullInt64{11, true}, intProto(11), tInt, "NullInt64 with value"},
{NullInt64{11, false}, nullProto(), tInt, "NullInt64 with null"},
{&iValue, intProto(7), tInt, "*int64 with value"},
{iNilPtr, nullProto(), tInt, "*int64 with null"},
{[]NullInt64{{35, true}, {131, false}}, listProto(intProto(35), nullProto()), listType(tInt), "[]NullInt64"},
{[]*int64{&iValue, iNilPtr}, listProto(intProto(7), nullProto()), listType(tInt), "[]*int64"},
// BOOL / BOOL ARRAY
{true, boolProto(true), tBool, "bool"},
{NullBool{true, true}, boolProto(true), tBool, "NullBool with value"},
{NullBool{true, false}, nullProto(), tBool, "NullBool with null"},
{&bValue, boolProto(true), tBool, "*bool with value"},
{bNilPtr, nullProto(), tBool, "*bool with null"},
{[]bool{true, false}, listProto(boolProto(true), boolProto(false)), listType(tBool), "[]bool"},
{[]NullBool{{true, true}, {true, false}}, listProto(boolProto(true), nullProto()), listType(tBool), "[]NullBool"},
{[]*bool{&bValue, bNilPtr}, listProto(boolProto(true), nullProto()), listType(tBool), "[]*bool"},
// FLOAT64 / FLOAT64 ARRAY
{3.14, floatProto(3.14), tFloat, "float"},
{NullFloat64{3.1415, true}, floatProto(3.1415), tFloat, "NullFloat64 with value"},
{NullFloat64{math.Inf(1), true}, floatProto(math.Inf(1)), tFloat, "NullFloat64 with infinity"},
{NullFloat64{3.14159, false}, nullProto(), tFloat, "NullFloat64 with null"},
{&fValue, floatProto(3.14), tFloat, "*float64 with value"},
{fNilPtr, nullProto(), tFloat, "*float64 with null"},
{[]float64(nil), nullProto(), listType(tFloat), "null []float64"},
{[]float64{3.141, 0.618, math.Inf(-1)}, listProto(floatProto(3.141), floatProto(0.618), floatProto(math.Inf(-1))), listType(tFloat), "[]float64"},
{[]NullFloat64{{3.141, true}, {0.618, false}}, listProto(floatProto(3.141), nullProto()), listType(tFloat), "[]NullFloat64"},
{[]*float64{&fValue, fNilPtr}, listProto(floatProto(3.14), nullProto()), listType(tFloat), "[]NullFloat64"},
// TIMESTAMP / TIMESTAMP ARRAY
{t1, timeProto(t1), tTime, "time"},
{NullTime{t1, true}, timeProto(t1), tTime, "NullTime with value"},
{NullTime{t1, false}, nullProto(), tTime, "NullTime with null"},
{&tValue, timeProto(t1), tTime, "*time.Time with value"},
{tNilPtr, nullProto(), tTime, "*time.Time with null"},
{[]time.Time(nil), nullProto(), listType(tTime), "null []time"},
{[]time.Time{t1, t2, t3, t4}, listProto(timeProto(t1), timeProto(t2), timeProto(t3), timeProto(t4)), listType(tTime), "[]time"},
{[]NullTime{{t1, true}, {t1, false}}, listProto(timeProto(t1), nullProto()), listType(tTime), "[]NullTime"},
{[]*time.Time{&tValue, tNilPtr}, listProto(timeProto(t1), nullProto()), listType(tTime), "[]*time.Time"},
// DATE / DATE ARRAY
{d1, dateProto(d1), tDate, "date"},
{NullDate{d1, true}, dateProto(d1), tDate, "NullDate with value"},
{NullDate{civil.Date{}, false}, nullProto(), tDate, "NullDate with null"},
{&dValue, dateProto(d1), tDate, "*civil.Date with value"},
{dNilPtr, nullProto(), tDate, "*civil.Date with null"},
{[]civil.Date(nil), nullProto(), listType(tDate), "null []date"},
{[]civil.Date{d1, d2}, listProto(dateProto(d1), dateProto(d2)), listType(tDate), "[]date"},
{[]NullDate{{d1, true}, {civil.Date{}, false}}, listProto(dateProto(d1), nullProto()), listType(tDate), "[]NullDate"},
{[]*civil.Date{&dValue, dNilPtr}, listProto(dateProto(d1), nullProto()), listType(tDate), "[]*civil.Date"},
// GenericColumnValue
{GenericColumnValue{tString, stringProto("abc")}, stringProto("abc"), tString, "GenericColumnValue with value"},
{GenericColumnValue{tString, nullProto()}, nullProto(), tString, "GenericColumnValue with null"},
// not actually valid (stringProto inside int list), but demonstrates pass-through.
{
GenericColumnValue{
Type: listType(tInt),
Value: listProto(intProto(5), nullProto(), stringProto("bcd")),
},
listProto(intProto(5), nullProto(), stringProto("bcd")),
listType(tInt),
"pass-through",
},
// placeholder
{CommitTimestamp, stringProto(commitTimestampPlaceholderString), tTime, "CommitTimestampPlaceholder"},
// CUSTOM STRING / CUSTOM STRING ARRAY
{CustomString("abc"), stringProto("abc"), tString, "CustomString"},
{CustomNullString{"abc", true}, stringProto("abc"), tString, "CustomNullString with value"},
{CustomNullString{"abc", false}, nullProto(), tString, "CustomNullString with null"},
{[]CustomString(nil), nullProto(), listType(tString), "null []CustomString"},
{[]CustomString{"abc", "bcd"}, listProto(stringProto("abc"), stringProto("bcd")), listType(tString), "[]CustomString"},
{[]CustomNullString(nil), nullProto(), listType(tString), "null []NullCustomString"},
{[]CustomNullString{{"abcd", true}, {"xyz", false}}, listProto(stringProto("abcd"), nullProto()), listType(tString), "[]NullCustomString"},
// CUSTOM BYTES / CUSTOM BYTES ARRAY
{CustomBytes("foo"), bytesProto([]byte("foo")), tBytes, "CustomBytes with value"},
{CustomBytes(nil), nullProto(), tBytes, "null CustomBytes"},
{[]CustomBytes{nil, CustomBytes("ab")}, listProto(nullProto(), bytesProto([]byte("ab"))), listType(tBytes), "[]CustomBytes"},
{[]CustomBytes(nil), nullProto(), listType(tBytes), "null []CustomBytes"},
// CUSTOM INT64 / CUSTOM INT64 ARRAY
{CustomInt64(81), intProto(81), tInt, "CustomInt64"},
{[]CustomInt64(nil), nullProto(), listType(tInt), "null []CustomInt64"},
{[]CustomInt64{33, 129}, listProto(intProto(33), intProto(129)), listType(tInt), "[]CustomInt64"},
{CustomNullInt64{11, true}, intProto(11), tInt, "CustomNullInt64 with value"},
{CustomNullInt64{11, false}, nullProto(), tInt, "CustomNullInt64 with null"},
{[]CustomNullInt64(nil), nullProto(), listType(tInt), "null []CustomNullInt64"},
{[]CustomNullInt64{{35, true}, {131, false}}, listProto(intProto(35), nullProto()), listType(tInt), "[]CustomNullInt64"},
// CUSTOM BOOL / CUSTOM BOOL ARRAY
{CustomBool(true), boolProto(true), tBool, "CustomBool"},
{CustomNullBool{true, true}, boolProto(true), tBool, "CustomNullBool with value"},
{CustomNullBool{true, false}, nullProto(), tBool, "CustomNullBool with null"},
{[]CustomBool{true, false}, listProto(boolProto(true), boolProto(false)), listType(tBool), "[]CustomBool"},
{[]CustomNullBool{{true, true}, {true, false}}, listProto(boolProto(true), nullProto()), listType(tBool), "[]CustomNullBool"},
// FLOAT64 / FLOAT64 ARRAY
{CustomFloat64(3.14), floatProto(3.14), tFloat, "CustomFloat64"},
{CustomNullFloat64{3.1415, true}, floatProto(3.1415), tFloat, "CustomNullFloat64 with value"},
{CustomNullFloat64{math.Inf(1), true}, floatProto(math.Inf(1)), tFloat, "CustomNullFloat64 with infinity"},
{CustomNullFloat64{3.14159, false}, nullProto(), tFloat, "CustomNullFloat64 with null"},
{[]CustomFloat64(nil), nullProto(), listType(tFloat), "null []CustomFloat64"},
{[]CustomFloat64{3.141, 0.618, CustomFloat64(math.Inf(-1))}, listProto(floatProto(3.141), floatProto(0.618), floatProto(math.Inf(-1))), listType(tFloat), "[]CustomFloat64"},
{[]CustomNullFloat64(nil), nullProto(), listType(tFloat), "null []CustomNullFloat64"},
{[]CustomNullFloat64{{3.141, true}, {0.618, false}}, listProto(floatProto(3.141), nullProto()), listType(tFloat), "[]CustomNullFloat64"},
// CUSTOM TIMESTAMP / CUSTOM TIMESTAMP ARRAY
{CustomTime(t1), timeProto(t1), tTime, "CustomTime"},
{CustomNullTime{t1, true}, timeProto(t1), tTime, "CustomNullTime with value"},
{CustomNullTime{t1, false}, nullProto(), tTime, "CustomNullTime with null"},
{[]CustomTime(nil), nullProto(), listType(tTime), "null []CustomTime"},
{[]CustomTime{CustomTime(t1), CustomTime(t2), CustomTime(t3), CustomTime(t4)}, listProto(timeProto(t1), timeProto(t2), timeProto(t3), timeProto(t4)), listType(tTime), "[]CustomTime"},
{[]CustomNullTime(nil), nullProto(), listType(tTime), "null []CustomNullTime"},
{[]CustomNullTime{{t1, true}, {t1, false}}, listProto(timeProto(t1), nullProto()), listType(tTime), "[]CustomNullTime"},
// CUSTOM DATE / CUSTOM DATE ARRAY
{CustomDate(d1), dateProto(d1), tDate, "CustomDate"},
{CustomNullDate{d1, true}, dateProto(d1), tDate, "CustomNullDate with value"},
{CustomNullDate{civil.Date{}, false}, nullProto(), tDate, "CustomNullDate with null"},
{[]CustomDate(nil), nullProto(), listType(tDate), "null []CustomDate"},
{[]CustomDate{CustomDate(d1), CustomDate(d2)}, listProto(dateProto(d1), dateProto(d2)), listType(tDate), "[]CustomDate"},
{[]CustomNullDate(nil), nullProto(), listType(tDate), "null []CustomNullDate"},
{[]CustomNullDate{{d1, true}, {civil.Date{}, false}}, listProto(dateProto(d1), nullProto()), listType(tDate), "[]NullDate"},
} {
got, gotType, err := encodeValue(test.in)
if err != nil {
t.Fatalf("#%d (%s): got error during encoding: %v, want nil", i, test.name, err)
}
if !testEqual(got, test.want) {
t.Errorf("#%d (%s): got encode result: %v, want %v", i, test.name, got, test.want)
}
if !testEqual(gotType, test.wantType) {
t.Errorf("#%d (%s): got encode type: %v, want %v", i, test.name, gotType, test.wantType)
}
}
}
type encodeTest struct {
desc string
in interface{}
want *proto3.Value
wantType *sppb.Type
}
func checkStructEncoding(desc string, got *proto3.Value, gotType *sppb.Type,
want *proto3.Value, wantType *sppb.Type, t *testing.T) {
if !testEqual(got, want) {
t.Errorf("Test %s: got encode result: %v, want %v", desc, got, want)
}
if !testEqual(gotType, wantType) {
t.Errorf("Test %s: got encode type: %v, want %v", desc, gotType, wantType)
}
}
// Testcase code
func encodeStructValue(test encodeTest, t *testing.T) {
got, gotType, err := encodeValue(test.in)
if err != nil {
t.Fatalf("Test %s: got error during encoding: %v, want nil", test.desc, err)
}
checkStructEncoding(test.desc, got, gotType, test.want, test.wantType, t)
}
func TestEncodeStructValuePointers(t *testing.T) {
type structf struct {
F int `spanner:"ff2"`
}
nestedStructProto := structType(mkField("ff2", intType()))
type testType struct {
Stringf string
Structf *structf
ArrStructf []*structf
}
testTypeProto := structType(
mkField("Stringf", stringType()),
mkField("Structf", nestedStructProto),
mkField("ArrStructf", listType(nestedStructProto)))
for _, test := range []encodeTest{
{
"Pointer to Go struct with pointers-to-(array)-struct fields.",
&testType{"hello", &structf{50}, []*structf{{30}, {40}}},
listProto(
stringProto("hello"),
listProto(intProto(50)),
listProto(
listProto(intProto(30)),
listProto(intProto(40)))),
testTypeProto,
},
{
"Nil pointer to Go struct representing a NULL struct value.",
(*testType)(nil),
nullProto(),
testTypeProto,
},
{
"Slice of pointers to Go structs with NULL and non-NULL elements.",
[]*testType{
(*testType)(nil),
{"hello", nil, []*structf{nil, {40}}},
{"world", &structf{70}, nil},
},
listProto(
nullProto(),
listProto(
stringProto("hello"),
nullProto(),
listProto(nullProto(), listProto(intProto(40)))),
listProto(
stringProto("world"),
listProto(intProto(70)),
nullProto())),
listType(testTypeProto),
},
{
"Nil slice of pointers to structs representing a NULL array of structs.",
[]*testType(nil),
nullProto(),
listType(testTypeProto),
},
{
"Empty slice of pointers to structs representing an empty array of structs.",
[]*testType{},
listProto(),
listType(testTypeProto),
},
} {
encodeStructValue(test, t)
}
}
func TestEncodeStructValueErrors(t *testing.T) {
type Embedded struct {
A int
}
type embedded struct {
B bool
}
x := 0
for _, test := range []struct {
desc string
in interface{}
wantErr error
}{
{
"Unsupported embedded fields.",
struct{ Embedded }{Embedded{10}},
errUnsupportedEmbeddedStructFields("Embedded"),
},
{
"Unsupported pointer to embedded fields.",
struct{ *Embedded }{&Embedded{10}},
errUnsupportedEmbeddedStructFields("Embedded"),
},
{
"Unsupported embedded + unexported fields.",
struct {
int
*bool
embedded
}{10, nil, embedded{false}},
errUnsupportedEmbeddedStructFields("int"),
},
{
"Unsupported type.",
(**struct{})(nil),
errEncoderUnsupportedType((**struct{})(nil)),
},
{
"Unsupported type.",
3,
errEncoderUnsupportedType(3),
},
{
"Unsupported type.",
&x,
errEncoderUnsupportedType(&x),
},
} {
_, _, got := encodeStruct(test.in)
if got == nil || !testEqual(test.wantErr, got) {
t.Errorf("Test: %s, expected error %v during decoding, got %v", test.desc, test.wantErr, got)
}
}
}
func TestEncodeStructValueArrayStructFields(t *testing.T) {
type structf struct {
Intff int
}
structfType := structType(mkField("Intff", intType()))
for _, test := range []encodeTest{
{
"Unnamed array-of-struct-typed field.",
struct {
Intf int
ArrStructf []structf `spanner:""`
}{10, []structf{{1}, {2}}},
listProto(
intProto(10),
listProto(
listProto(intProto(1)),
listProto(intProto(2)))),
structType(
mkField("Intf", intType()),
mkField("", listType(structfType))),
},
{
"Null array-of-struct-typed field.",
struct {
Intf int
ArrStructf []structf
}{10, []structf(nil)},
listProto(intProto(10), nullProto()),
structType(
mkField("Intf", intType()),
mkField("ArrStructf", listType(structfType))),
},
{
"Array-of-struct-typed field representing empty array.",
struct {
Intf int
ArrStructf []structf
}{10, []structf{}},
listProto(intProto(10), listProto([]*proto3.Value{}...)),
structType(
mkField("Intf", intType()),
mkField("ArrStructf", listType(structfType))),
},
{
"Array-of-struct-typed field with nullable struct elements.",
struct {
Intf int
ArrStructf []*structf
}{
10,
[]*structf{(*structf)(nil), {1}},
},
listProto(
intProto(10),
listProto(
nullProto(),
listProto(intProto(1)))),
structType(
mkField("Intf", intType()),
mkField("ArrStructf", listType(structfType))),
},
} {
encodeStructValue(test, t)
}
}
func TestEncodeStructValueStructFields(t *testing.T) {
type structf struct {
Intff int
}
structfType := structType(mkField("Intff", intType()))
for _, test := range []encodeTest{
{
"Named struct-type field.",
struct {
Intf int
Structf structf
}{10, structf{10}},
listProto(intProto(10), listProto(intProto(10))),
structType(
mkField("Intf", intType()),
mkField("Structf", structfType)),
},
{
"Unnamed struct-type field.",
struct {
Intf int
Structf structf `spanner:""`
}{10, structf{10}},
listProto(intProto(10), listProto(intProto(10))),
structType(
mkField("Intf", intType()),
mkField("", structfType)),
},
{
"Duplicate struct-typed field.",
struct {
Structf1 structf `spanner:""`
Structf2 structf `spanner:""`
}{structf{10}, structf{20}},
listProto(listProto(intProto(10)), listProto(intProto(20))),
structType(
mkField("", structfType),
mkField("", structfType)),
},
{
"Null struct-typed field.",
struct {
Intf int
Structf *structf
}{10, nil},
listProto(intProto(10), nullProto()),
structType(
mkField("Intf", intType()),
mkField("Structf", structfType)),
},
{
"Empty struct-typed field.",
struct {
Intf int
Structf struct{}
}{10, struct{}{}},
listProto(intProto(10), listProto([]*proto3.Value{}...)),
structType(
mkField("Intf", intType()),
mkField("Structf", structType([]*sppb.StructType_Field{}...))),
},
} {
encodeStructValue(test, t)
}
}
func TestEncodeStructValueFieldNames(t *testing.T) {
type embedded struct {
B bool
}
for _, test := range []encodeTest{
{
"Duplicate fields.",
struct {
Field1 int `spanner:"field"`
DupField1 int `spanner:"field"`
}{10, 20},
listProto(intProto(10), intProto(20)),
structType(
mkField("field", intType()),
mkField("field", intType())),
},
{
"Duplicate Fields (different types).",
struct {
IntField int `spanner:"field"`
StringField string `spanner:"field"`
}{10, "abc"},
listProto(intProto(10), stringProto("abc")),
structType(
mkField("field", intType()),
mkField("field", stringType())),
},
{
"Duplicate unnamed fields.",
struct {
Dup int `spanner:""`
Dup1 int `spanner:""`
}{10, 20},
listProto(intProto(10), intProto(20)),
structType(
mkField("", intType()),
mkField("", intType())),
},
{
"Named and unnamed fields.",
struct {
Field string
Field1 int `spanner:""`
Field2 string `spanner:"field"`
}{"abc", 10, "def"},
listProto(stringProto("abc"), intProto(10), stringProto("def")),
structType(
mkField("Field", stringType()),
mkField("", intType()),
mkField("field", stringType())),
},
{
"Ignored unexported fields.",
struct {
Field int
field bool
Field1 string `spanner:"field"`
}{10, false, "abc"},
listProto(intProto(10), stringProto("abc")),
structType(
mkField("Field", intType()),
mkField("field", stringType())),
},
{
"Ignored unexported struct/slice fields.",
struct {
a []*embedded
b []embedded
c embedded
d *embedded
Field1 string `spanner:"field"`
}{nil, nil, embedded{}, nil, "def"},
listProto(stringProto("def")),
structType(
mkField("field", stringType())),
},
} {
encodeStructValue(test, t)
}
}
func TestEncodeStructValueBasicFields(t *testing.T) {
type CustomString string
type CustomBytes []byte
type CustomInt64 int64
type CustomBool bool
type CustomFloat64 float64
type CustomTime time.Time
type CustomDate civil.Date
type CustomNullString NullString
type CustomNullInt64 NullInt64
type CustomNullBool NullBool
type CustomNullFloat64 NullFloat64
type CustomNullTime NullTime
type CustomNullDate NullDate
sValue := "abc"
iValue := int64(300)
bValue := false
fValue := 3.45
tValue := t1
dValue := d1
StructTypeProto := structType(
mkField("Stringf", stringType()),
mkField("Intf", intType()),
mkField("Boolf", boolType()),
mkField("Floatf", floatType()),
mkField("Bytef", bytesType()),
mkField("Timef", timeType()),
mkField("Datef", dateType()))
for _, test := range []encodeTest{
{
"Basic types.",
struct {
Stringf string
Intf int
Boolf bool
Floatf float64
Bytef []byte
Timef time.Time
Datef civil.Date
}{"abc", 300, false, 3.45, []byte("foo"), t1, d1},
listProto(
stringProto("abc"),
intProto(300),
boolProto(false),
floatProto(3.45),
bytesProto([]byte("foo")),
timeProto(t1),
dateProto(d1)),
StructTypeProto,
},
{
"Pointers to basic types.",
struct {
Stringf *string
Intf *int64
Boolf *bool
Floatf *float64
Bytef []byte
Timef *time.Time
Datef *civil.Date
}{&sValue, &iValue, &bValue, &fValue, []byte("foo"), &tValue, &dValue},
listProto(
stringProto("abc"),
intProto(300),
boolProto(false),
floatProto(3.45),
bytesProto([]byte("foo")),
timeProto(t1),
dateProto(d1)),
StructTypeProto,
},
{
"Pointers to basic types with null values.",
struct {
Stringf *string
Intf *int64
Boolf *bool
Floatf *float64
Bytef []byte
Timef *time.Time
Datef *civil.Date
}{nil, nil, nil, nil, nil, nil, nil},
listProto(
nullProto(),
nullProto(),
nullProto(),
nullProto(),
nullProto(),
nullProto(),
nullProto()),
StructTypeProto,
},
{
"Basic custom types.",
struct {
Stringf CustomString
Intf CustomInt64
Boolf CustomBool
Floatf CustomFloat64
Bytef CustomBytes
Timef CustomTime
Datef CustomDate
}{"abc", 300, false, 3.45, []byte("foo"), CustomTime(t1), CustomDate(d1)},
listProto(
stringProto("abc"),
intProto(300),
boolProto(false),
floatProto(3.45),
bytesProto([]byte("foo")),
timeProto(t1),
dateProto(d1)),
StructTypeProto,
},
{
"Basic types null values.",
struct {
Stringf NullString
Intf NullInt64
Boolf NullBool
Floatf NullFloat64
Bytef []byte
Timef NullTime
Datef NullDate
}{
NullString{"abc", false},
NullInt64{4, false},
NullBool{false, false},
NullFloat64{5.6, false},
nil,
NullTime{t1, false},
NullDate{d1, false},
},
listProto(
nullProto(),
nullProto(),
nullProto(),
nullProto(),
nullProto(),
nullProto(),
nullProto()),
StructTypeProto,
},
{
"Basic custom types null values.",
struct {
Stringf CustomNullString
Intf CustomNullInt64
Boolf CustomNullBool
Floatf CustomNullFloat64
Bytef CustomBytes
Timef CustomNullTime
Datef CustomNullDate
}{
CustomNullString{"abc", false},
CustomNullInt64{4, false},
CustomNullBool{false, false},
CustomNullFloat64{5.6, false},
nil,
CustomNullTime{t1, false},
CustomNullDate{d1, false},
},
listProto(
nullProto(),
nullProto(),
nullProto(),
nullProto(),
nullProto(),
nullProto(),
nullProto()),
StructTypeProto,
},
} {
encodeStructValue(test, t)
}
}
func TestEncodeStructValueArrayFields(t *testing.T) {
type CustomString string
type CustomBytes []byte
type CustomInt64 int64
type CustomBool bool
type CustomFloat64 float64
type CustomTime time.Time
type CustomDate civil.Date
type CustomNullString NullString
type CustomNullInt64 NullInt64
type CustomNullBool NullBool
type CustomNullFloat64 NullFloat64
type CustomNullTime NullTime
type CustomNullDate NullDate
sValue := "def"
var sNilPtr *string
iValue := int64(68)
var iNilPtr *int64
bValue := true
var bNilPtr *bool
fValue := 3.14
var fNilPtr *float64
tValue := t1
var tNilPtr *time.Time
dValue := d1
var dNilPtr *civil.Date
StructTypeProto := structType(
mkField("Stringf", listType(stringType())),
mkField("Intf", listType(intType())),
mkField("Int64f", listType(intType())),
mkField("Boolf", listType(boolType())),
mkField("Floatf", listType(floatType())),
mkField("Bytef", listType(bytesType())),
mkField("Timef", listType(timeType())),
mkField("Datef", listType(dateType())))
for _, test := range []encodeTest{
{
"Arrays of basic types with non-nullable elements",
struct {
Stringf []string
Intf []int
Int64f []int64
Boolf []bool
Floatf []float64
Bytef [][]byte
Timef []time.Time
Datef []civil.Date
}{
[]string{"abc", "def"},
[]int{4, 67},
[]int64{5, 68},
[]bool{false, true},
[]float64{3.45, 0.93},
[][]byte{[]byte("foo"), nil},
[]time.Time{t1, t2},
[]civil.Date{d1, d2},
},
listProto(
listProto(stringProto("abc"), stringProto("def")),
listProto(intProto(4), intProto(67)),
listProto(intProto(5), intProto(68)),
listProto(boolProto(false), boolProto(true)),
listProto(floatProto(3.45), floatProto(0.93)),
listProto(bytesProto([]byte("foo")), nullProto()),
listProto(timeProto(t1), timeProto(t2)),
listProto(dateProto(d1), dateProto(d2))),
StructTypeProto,
},
{
"Arrays of basic custom types with non-nullable elements",
struct {
Stringf []CustomString
Intf []CustomInt64
Int64f []CustomInt64
Boolf []CustomBool
Floatf []CustomFloat64
Bytef []CustomBytes
Timef []CustomTime
Datef []CustomDate
}{
[]CustomString{"abc", "def"},
[]CustomInt64{4, 67},
[]CustomInt64{5, 68},
[]CustomBool{false, true},
[]CustomFloat64{3.45, 0.93},
[]CustomBytes{[]byte("foo"), nil},
[]CustomTime{CustomTime(t1), CustomTime(t2)},
[]CustomDate{CustomDate(d1), CustomDate(d2)},
},
listProto(
listProto(stringProto("abc"), stringProto("def")),
listProto(intProto(4), intProto(67)),
listProto(intProto(5), intProto(68)),
listProto(boolProto(false), boolProto(true)),
listProto(floatProto(3.45), floatProto(0.93)),
listProto(bytesProto([]byte("foo")), nullProto()),
listProto(timeProto(t1), timeProto(t2)),
listProto(dateProto(d1), dateProto(d2))),
StructTypeProto,
},
{
"Arrays of basic types with nullable elements.",
struct {
Stringf []NullString
Intf []NullInt64
Int64f []NullInt64
Boolf []NullBool
Floatf []NullFloat64
Bytef [][]byte
Timef []NullTime
Datef []NullDate
}{
[]NullString{{"abc", false}, {"def", true}},
[]NullInt64{{4, false}, {67, true}},
[]NullInt64{{5, false}, {68, true}},
[]NullBool{{true, false}, {false, true}},
[]NullFloat64{{3.45, false}, {0.93, true}},
[][]byte{[]byte("foo"), nil},
[]NullTime{{t1, false}, {t2, true}},
[]NullDate{{d1, false}, {d2, true}},
},
listProto(
listProto(nullProto(), stringProto("def")),
listProto(nullProto(), intProto(67)),
listProto(nullProto(), intProto(68)),
listProto(nullProto(), boolProto(false)),
listProto(nullProto(), floatProto(0.93)),
listProto(bytesProto([]byte("foo")), nullProto()),
listProto(nullProto(), timeProto(t2)),
listProto(nullProto(), dateProto(d2))),
StructTypeProto,
},
{
"Arrays of pointers to basic types with nullable elements.",
struct {
Stringf []*string
Intf []*int64
Int64f []*int64
Boolf []*bool
Floatf []*float64
Bytef [][]byte
Timef []*time.Time
Datef []*civil.Date
}{
[]*string{sNilPtr, &sValue},
[]*int64{iNilPtr, &iValue},
[]*int64{iNilPtr, &iValue},
[]*bool{bNilPtr, &bValue},
[]*float64{fNilPtr, &fValue},
[][]byte{[]byte("foo"), nil},
[]*time.Time{tNilPtr, &tValue},
[]*civil.Date{dNilPtr, &dValue},
},
listProto(
listProto(nullProto(), stringProto("def")),
listProto(nullProto(), intProto(68)),
listProto(nullProto(), intProto(68)),
listProto(nullProto(), boolProto(true)),
listProto(nullProto(), floatProto(3.14)),
listProto(bytesProto([]byte("foo")), nullProto()),
listProto(nullProto(), timeProto(t1)),
listProto(nullProto(), dateProto(d1))),
StructTypeProto,
},
{
"Arrays of basic custom types with nullable elements.",
struct {
Stringf []CustomNullString
Intf []CustomNullInt64
Int64f []CustomNullInt64
Boolf []CustomNullBool
Floatf []CustomNullFloat64
Bytef []CustomBytes
Timef []CustomNullTime
Datef []CustomNullDate
}{
[]CustomNullString{{"abc", false}, {"def", true}},
[]CustomNullInt64{{4, false}, {67, true}},
[]CustomNullInt64{{5, false}, {68, true}},
[]CustomNullBool{{true, false}, {false, true}},
[]CustomNullFloat64{{3.45, false}, {0.93, true}},
[]CustomBytes{[]byte("foo"), nil},
[]CustomNullTime{{t1, false}, {t2, true}},
[]CustomNullDate{{d1, false}, {d2, true}},
},
listProto(
listProto(nullProto(), stringProto("def")),
listProto(nullProto(), intProto(67)),
listProto(nullProto(), intProto(68)),
listProto(nullProto(), boolProto(false)),
listProto(nullProto(), floatProto(0.93)),
listProto(bytesProto([]byte("foo")), nullProto()),
listProto(nullProto(), timeProto(t2)),
listProto(nullProto(), dateProto(d2))),
StructTypeProto,
},
{
"Null arrays of basic types.",
struct {
Stringf []NullString
Intf []NullInt64
Int64f []NullInt64
Boolf []NullBool
Floatf []NullFloat64
Bytef [][]byte
Timef []NullTime
Datef []NullDate
}{
nil,
nil,
nil,
nil,
nil,
nil,
nil,
nil,
},
listProto(
nullProto(),
nullProto(),
nullProto(),
nullProto(),
nullProto(),
nullProto(),
nullProto(),
nullProto()),
StructTypeProto,
},
{
"Null arrays of basic custom types.",
struct {
Stringf []CustomNullString
Intf []CustomNullInt64
Int64f []CustomNullInt64
Boolf []CustomNullBool
Floatf []CustomNullFloat64
Bytef []CustomBytes
Timef []CustomNullTime
Datef []CustomNullDate
}{
nil,
nil,
nil,
nil,
nil,
nil,
nil,
nil,
},
listProto(
nullProto(),
nullProto(),
nullProto(),
nullProto(),
nullProto(),
nullProto(),
nullProto(),
nullProto()),
StructTypeProto,
},
} {
encodeStructValue(test, t)
}
}
// Test decoding Values.
func TestDecodeValue(t *testing.T) {
type CustomString string
type CustomBytes []byte
type CustomInt64 int64
type CustomBool bool
type CustomFloat64 float64
type CustomTime time.Time
type CustomDate civil.Date
type CustomNullString NullString
type CustomNullInt64 NullInt64
type CustomNullBool NullBool
type CustomNullFloat64 NullFloat64
type CustomNullTime NullTime
type CustomNullDate NullDate
// Pointer values.
sValue := "abc"
var sNilPtr *string
s2Value := "bcd"
iValue := int64(15)
var iNilPtr *int64
i1Value := int64(91)
i2Value := int64(87)
bValue := true
var bNilPtr *bool
b2Value := false
fValue := 3.14
var fNilPtr *float64
f2Value := 6.626
tValue := t1
var tNilPtr *time.Time
t2Value := t2
dValue := d1
var dNilPtr *civil.Date
d2Value := d2
for _, test := range []struct {
desc string
proto *proto3.Value
protoType *sppb.Type
want interface{}
wantErr bool
}{
// STRING
{desc: "decode STRING to string", proto: stringProto("abc"), protoType: stringType(), want: "abc"},
{desc: "decode NULL to string", proto: nullProto(), protoType: stringType(), want: "abc", wantErr: true},
{desc: "decode STRING to *string", proto: stringProto("abc"), protoType: stringType(), want: &sValue},
{desc: "decode NULL to *string", proto: nullProto(), protoType: stringType(), want: sNilPtr},
{desc: "decode STRING to NullString", proto: stringProto("abc"), protoType: stringType(), want: NullString{"abc", true}},
{desc: "decode NULL to NullString", proto: nullProto(), protoType: stringType(), want: NullString{}},
// STRING ARRAY with []NullString
{desc: "decode ARRAY<STRING> to []NullString", proto: listProto(stringProto("abc"), nullProto(), stringProto("bcd")), protoType: listType(stringType()), want: []NullString{{"abc", true}, {}, {"bcd", true}}},
{desc: "decode NULL to []NullString", proto: nullProto(), protoType: listType(stringType()), want: []NullString(nil)},
// STRING ARRAY with []string
{desc: "decode ARRAY<STRING> to []string", proto: listProto(stringProto("abc"), stringProto("bcd")), protoType: listType(stringType()), want: []string{"abc", "bcd"}},
// STRING ARRAY with []*string
{desc: "decode ARRAY<STRING> to []*string", proto: listProto(stringProto("abc"), nullProto(), stringProto("bcd")), protoType: listType(stringType()), want: []*string{&sValue, sNilPtr, &s2Value}},
{desc: "decode NULL to []*string", proto: nullProto(), protoType: listType(stringType()), want: []*string(nil)},
// BYTES
{desc: "decode BYTES to []byte", proto: bytesProto([]byte("ab")), protoType: bytesType(), want: []byte("ab")},
{desc: "decode NULL to []byte", proto: nullProto(), protoType: bytesType(), want: []byte(nil)},
// BYTES ARRAY
{desc: "decode ARRAY<BYTES> to [][]byte", proto: listProto(bytesProto([]byte("ab")), nullProto()), protoType: listType(bytesType()), want: [][]byte{[]byte("ab"), nil}},
{desc: "decode NULL to [][]byte", proto: nullProto(), protoType: listType(bytesType()), want: [][]byte(nil)},
//INT64
{desc: "decode INT64 to int64", proto: intProto(15), protoType: intType(), want: int64(15)},
{desc: "decode NULL to int64", proto: nullProto(), protoType: intType(), want: int64(0), wantErr: true},
{desc: "decode INT64 to *int64", proto: intProto(15), protoType: intType(), want: &iValue},
{desc: "decode NULL to *int64", proto: nullProto(), protoType: intType(), want: iNilPtr},
{desc: "decode INT64 to NullInt64", proto: intProto(15), protoType: intType(), want: NullInt64{15, true}},
{desc: "decode NULL to NullInt64", proto: nullProto(), protoType: intType(), want: NullInt64{}},
// INT64 ARRAY with []NullInt64
{desc: "decode ARRAY<INT64> to []NullInt64", proto: listProto(intProto(91), nullProto(), intProto(87)), protoType: listType(intType()), want: []NullInt64{{91, true}, {}, {87, true}}},
{desc: "decode NULL to []NullInt64", proto: nullProto(), protoType: listType(intType()), want: []NullInt64(nil)},
// INT64 ARRAY with []int64
{desc: "decode ARRAY<INT64> to []int64", proto: listProto(intProto(91), intProto(87)), protoType: listType(intType()), want: []int64{91, 87}},
// INT64 ARRAY with []*int64
{desc: "decode ARRAY<INT64> to []*int64", proto: listProto(intProto(91), nullProto(), intProto(87)), protoType: listType(intType()), want: []*int64{&i1Value, nil, &i2Value}},
{desc: "decode NULL to []*int64", proto: nullProto(), protoType: listType(intType()), want: []*int64(nil)},
// BOOL
{desc: "decode BOOL to bool", proto: boolProto(true), protoType: boolType(), want: true},
{desc: "decode NULL to bool", proto: nullProto(), protoType: boolType(), want: true, wantErr: true},
{desc: "decode BOOL to *bool", proto: boolProto(true), protoType: boolType(), want: &bValue},
{desc: "decode NULL to *bool", proto: nullProto(), protoType: boolType(), want: bNilPtr},
{desc: "decode BOOL to NullBool", proto: boolProto(true), protoType: boolType(), want: NullBool{true, true}},
{desc: "decode BOOL to NullBool", proto: nullProto(), protoType: boolType(), want: NullBool{}},
// BOOL ARRAY with []NullBool
{desc: "decode ARRAY<BOOL> to []NullBool", proto: listProto(boolProto(true), boolProto(false), nullProto()), protoType: listType(boolType()), want: []NullBool{{true, true}, {false, true}, {}}},
{desc: "decode NULL to []NullBool", proto: nullProto(), protoType: listType(boolType()), want: []NullBool(nil)},
// BOOL ARRAY with []bool
{desc: "decode ARRAY<BOOL> to []bool", proto: listProto(boolProto(true), boolProto(false)), protoType: listType(boolType()), want: []bool{true, false}},
// BOOL ARRAY with []*bool
{desc: "decode ARRAY<BOOL> to []*bool", proto: listProto(boolProto(true), nullProto(), boolProto(false)), protoType: listType(boolType()), want: []*bool{&bValue, bNilPtr, &b2Value}},
{desc: "decode NULL to []*bool", proto: nullProto(), protoType: listType(boolType()), want: []*bool(nil)},
// FLOAT64
{desc: "decode FLOAT64 to float64", proto: floatProto(3.14), protoType: floatType(), want: 3.14},
{desc: "decode NULL to float64", proto: nullProto(), protoType: floatType(), want: 0.00, wantErr: true},
{desc: "decode FLOAT64 to *float64", proto: floatProto(3.14), protoType: floatType(), want: &fValue},
{desc: "decode NULL to *float64", proto: nullProto(), protoType: floatType(), want: fNilPtr},
{desc: "decode FLOAT64 to NullFloat64", proto: floatProto(3.14), protoType: floatType(), want: NullFloat64{3.14, true}},
{desc: "decode NULL to NullFloat64", proto: nullProto(), protoType: floatType(), want: NullFloat64{}},
// FLOAT64 ARRAY with []NullFloat64
{desc: "decode ARRAY<FLOAT64> to []NullFloat64", proto: listProto(floatProto(math.Inf(1)), floatProto(math.Inf(-1)), nullProto(), floatProto(3.1)), protoType: listType(floatType()), want: []NullFloat64{{math.Inf(1), true}, {math.Inf(-1), true}, {}, {3.1, true}}},
{desc: "decode NULL to []NullFloat64", proto: nullProto(), protoType: listType(floatType()), want: []NullFloat64(nil)},
// FLOAT64 ARRAY with []float64
{desc: "decode ARRAY<FLOAT64> to []float64", proto: listProto(floatProto(math.Inf(1)), floatProto(math.Inf(-1)), floatProto(3.1)), protoType: listType(floatType()), want: []float64{math.Inf(1), math.Inf(-1), 3.1}},
// FLOAT64 ARRAY with []NullFloat64
{desc: "decode ARRAY<FLOAT64> to []*float64", proto: listProto(floatProto(fValue), nullProto(), floatProto(f2Value)), protoType: listType(floatType()), want: []*float64{&fValue, nil, &f2Value}},
{desc: "decode NULL to []*float64", proto: nullProto(), protoType: listType(floatType()), want: []*float64(nil)},
// TIMESTAMP
{desc: "decode TIMESTAMP to time.Time", proto: timeProto(t1), protoType: timeType(), want: t1},
{desc: "decode TIMESTAMP to NullTime", proto: timeProto(t1), protoType: timeType(), want: NullTime{t1, true}},
{desc: "decode NULL to NullTime", proto: nullProto(), protoType: timeType(), want: NullTime{}},
{desc: "decode TIMESTAMP to *time.Time", proto: timeProto(t1), protoType: timeType(), want: &tValue},
{desc: "decode NULL to *time.Time", proto: nullProto(), protoType: timeType(), want: tNilPtr},
{desc: "decode INT64 to time.Time", proto: intProto(7), protoType: timeType(), want: time.Time{}, wantErr: true},
// TIMESTAMP ARRAY with []NullTime
{desc: "decode ARRAY<TIMESTAMP> to []NullTime", proto: listProto(timeProto(t1), timeProto(t2), timeProto(t3), nullProto()), protoType: listType(timeType()), want: []NullTime{{t1, true}, {t2, true}, {t3, true}, {}}},
{desc: "decode NULL to []NullTime", proto: nullProto(), protoType: listType(timeType()), want: []NullTime(nil)},
// TIMESTAMP ARRAY with []time.Time
{desc: "decode ARRAY<TIMESTAMP> to []time.Time", proto: listProto(timeProto(t1), timeProto(t2), timeProto(t3)), protoType: listType(timeType()), want: []time.Time{t1, t2, t3}},
// TIMESTAMP ARRAY with []*time.Time
{desc: "decode ARRAY<TIMESTAMP> to []*time.Time", proto: listProto(timeProto(t1), nullProto(), timeProto(t2)), protoType: listType(timeType()), want: []*time.Time{&tValue, nil, &t2Value}},
{desc: "decode NULL to []*time.Time", proto: nullProto(), protoType: listType(timeType()), want: []*time.Time(nil)},
// DATE
{desc: "decode DATE to civil.Date", proto: dateProto(d1), protoType: dateType(), want: d1},
{desc: "decode DATE to NullDate", proto: dateProto(d1), protoType: dateType(), want: NullDate{d1, true}},
{desc: "decode NULL to NullDate", proto: nullProto(), protoType: dateType(), want: NullDate{}},
{desc: "decode DATE to *civil.Date", proto: dateProto(d1), protoType: dateType(), want: &dValue},
{desc: "decode NULL to *civil.Date", proto: nullProto(), protoType: dateType(), want: dNilPtr},
// DATE ARRAY with []NullDate
{desc: "decode ARRAY<DATE> to []NullDate", proto: listProto(dateProto(d1), dateProto(d2), nullProto()), protoType: listType(dateType()), want: []NullDate{{d1, true}, {d2, true}, {}}},
{desc: "decode NULL to []NullDate", proto: nullProto(), protoType: listType(dateType()), want: []NullDate(nil)},
// DATE ARRAY with []civil.Date
{desc: "decode ARRAY<DATE> to []civil.Date", proto: listProto(dateProto(d1), dateProto(d2)), protoType: listType(dateType()), want: []civil.Date{d1, d2}},
// DATE ARRAY with []NullDate
{desc: "decode ARRAY<DATE> to []*civil.Date", proto: listProto(dateProto(d1), nullProto(), dateProto(d2)), protoType: listType(dateType()), want: []*civil.Date{&dValue, nil, &d2Value}},
{desc: "decode NULL to []*civil.Date", proto: nullProto(), protoType: listType(dateType()), want: []*civil.Date(nil)},
// STRUCT ARRAY
// STRUCT schema is equal to the following Go struct:
// type s struct {
// Col1 NullInt64
// Col2 []struct {
// SubCol1 float64
// SubCol2 string
// }
// }
{
desc: "decode ARRAY<STRUCT> to []NullRow",
proto: listProto(
listProto(
intProto(3),
listProto(
listProto(floatProto(3.14), stringProto("this")),
listProto(floatProto(0.57), stringProto("siht")),
),
),
listProto(
nullProto(),
nullProto(),
),
nullProto(),
),
protoType: listType(
structType(
mkField("Col1", intType()),
mkField(
"Col2",
listType(
structType(
mkField("SubCol1", floatType()),
mkField("SubCol2", stringType()),
),
),
),
),
),
want: []NullRow{
{
Row: Row{
fields: []*sppb.StructType_Field{
mkField("Col1", intType()),
mkField(
"Col2",
listType(
structType(
mkField("SubCol1", floatType()),
mkField("SubCol2", stringType()),
),
),
),
},
vals: []*proto3.Value{
intProto(3),
listProto(
listProto(floatProto(3.14), stringProto("this")),
listProto(floatProto(0.57), stringProto("siht")),
),
},
},
Valid: true,
},
{
Row: Row{
fields: []*sppb.StructType_Field{
mkField("Col1", intType()),
mkField(
"Col2",
listType(
structType(
mkField("SubCol1", floatType()),
mkField("SubCol2", stringType()),
),
),
),
},
vals: []*proto3.Value{
nullProto(),
nullProto(),
},
},
Valid: true,
},
{},
},
},
{
desc: "decode ARRAY<STRUCT> to []*struct",
proto: listProto(
listProto(
intProto(3),
listProto(
listProto(floatProto(3.14), stringProto("this")),
listProto(floatProto(0.57), stringProto("siht")),
),
),
listProto(
nullProto(),
nullProto(),
),
nullProto(),
),
protoType: listType(
structType(
mkField("Col1", intType()),
mkField(
"Col2",
listType(
structType(
mkField("SubCol1", floatType()),
mkField("SubCol2", stringType()),
),
),
),
),
),
want: []*struct {
Col1 NullInt64
StructCol []*struct {
SubCol1 NullFloat64
SubCol2 string
} `spanner:"Col2"`
}{
{
Col1: NullInt64{3, true},
StructCol: []*struct {
SubCol1 NullFloat64
SubCol2 string
}{
{
SubCol1: NullFloat64{3.14, true},
SubCol2: "this",
},
{
SubCol1: NullFloat64{0.57, true},
SubCol2: "siht",
},
},
},
{
Col1: NullInt64{},
StructCol: []*struct {
SubCol1 NullFloat64
SubCol2 string
}(nil),
},
nil,
},
},
// GenericColumnValue
{desc: "decode STRING to GenericColumnValue", proto: stringProto("abc"), protoType: stringType(), want: GenericColumnValue{stringType(), stringProto("abc")}},
{desc: "decode NULL to GenericColumnValue", proto: nullProto(), protoType: stringType(), want: GenericColumnValue{stringType(), nullProto()}},
// not actually valid (stringProto inside int list), but demonstrates pass-through.
{desc: "decode ARRAY<INT64> to GenericColumnValue", proto: listProto(intProto(5), nullProto(), stringProto("bcd")), protoType: listType(intType()), want: GenericColumnValue{Type: listType(intType()), Value: listProto(intProto(5), nullProto(), stringProto("bcd"))}},
// Custom base types.
{desc: "decode STRING to CustomString", proto: stringProto("bar"), protoType: stringType(), want: CustomString("bar")},
{desc: "decode BYTES to CustomBytes", proto: bytesProto([]byte("ab")), protoType: bytesType(), want: CustomBytes("ab")},
{desc: "decode INT64 to CustomInt64", proto: intProto(-100), protoType: intType(), want: CustomInt64(-100)},
{desc: "decode BOOL to CustomBool", proto: boolProto(true), protoType: boolType(), want: CustomBool(true)},
{desc: "decode FLOAT64 to CustomFloat64", proto: floatProto(6.626), protoType: floatType(), want: CustomFloat64(6.626)},
{desc: "decode TIMESTAMP to CustomTimestamp", proto: timeProto(t1), protoType: timeType(), want: CustomTime(t1)},
{desc: "decode DATE to CustomDate", proto: dateProto(d1), protoType: dateType(), want: CustomDate(d1)},
{desc: "decode NULL to CustomString", proto: nullProto(), protoType: stringType(), want: CustomString(""), wantErr: true},
{desc: "decode NULL to CustomBytes", proto: nullProto(), protoType: bytesType(), want: CustomBytes(nil)},
{desc: "decode NULL to CustomInt64", proto: nullProto(), protoType: intType(), want: CustomInt64(0), wantErr: true},
{desc: "decode NULL to CustomBool", proto: nullProto(), protoType: boolType(), want: CustomBool(false), wantErr: true},
{desc: "decode NULL to CustomFloat64", proto: nullProto(), protoType: floatType(), want: CustomFloat64(0), wantErr: true},
{desc: "decode NULL to CustomTime", proto: nullProto(), protoType: timeType(), want: CustomTime{}, wantErr: true},
{desc: "decode NULL to CustomDate", proto: nullProto(), protoType: dateType(), want: CustomDate{}, wantErr: true},
{desc: "decode STRING to CustomNullString", proto: stringProto("bar"), protoType: stringType(), want: CustomNullString{"bar", true}},
{desc: "decode INT64 to CustomNullInt64", proto: intProto(-100), protoType: intType(), want: CustomNullInt64{-100, true}},
{desc: "decode BOOL to CustomNullBool", proto: boolProto(true), protoType: boolType(), want: CustomNullBool{true, true}},
{desc: "decode FLOAT64 to CustomNullFloat64", proto: floatProto(6.626), protoType: floatType(), want: CustomNullFloat64{6.626, true}},
{desc: "decode TIMESTAMP to CustomNullTime", proto: timeProto(t1), protoType: timeType(), want: CustomNullTime{t1, true}},
{desc: "decode DATE to CustomNullDate", proto: dateProto(d1), protoType: dateType(), want: CustomNullDate{d1, true}},
{desc: "decode NULL to CustomNullString", proto: nullProto(), protoType: stringType(), want: CustomNullString{}},
{desc: "decode NULL to CustomNullInt64", proto: nullProto(), protoType: intType(), want: CustomNullInt64{}},
{desc: "decode NULL to CustomNullBool", proto: nullProto(), protoType: boolType(), want: CustomNullBool{}},
{desc: "decode NULL to CustomNullFloat64", proto: nullProto(), protoType: floatType(), want: CustomNullFloat64{}},
{desc: "decode NULL to CustomNullTime", proto: nullProto(), protoType: timeType(), want: CustomNullTime{}},
{desc: "decode NULL to CustomNullDate", proto: nullProto(), protoType: dateType(), want: CustomNullDate{}},
// STRING ARRAY
{desc: "decode NULL to []CustomString", proto: nullProto(), protoType: listType(stringType()), want: []CustomString(nil)},
{desc: "decode ARRAY<STRING> to []CustomString", proto: listProto(stringProto("abc"), stringProto("bcd")), protoType: listType(stringType()), want: []CustomString{"abc", "bcd"}},
{desc: "decode ARRAY<STRING> with NULL values to []CustomString", proto: listProto(stringProto("abc"), nullProto(), stringProto("bcd")), protoType: listType(stringType()), want: []CustomString{}, wantErr: true},
{desc: "decode NULL to []CustomNullString", proto: nullProto(), protoType: listType(stringType()), want: []CustomNullString(nil)},
{desc: "decode ARRAY<STRING> to []CustomNullString", proto: listProto(stringProto("abc"), nullProto(), stringProto("bcd")), protoType: listType(stringType()), want: []CustomNullString{{"abc", true}, {}, {"bcd", true}}},
// BYTES ARRAY
{desc: "decode NULL to []CustomBytes", proto: nullProto(), protoType: listType(bytesType()), want: []CustomBytes(nil)},
{desc: "decode ARRAY<BYTES> to []CustomBytes", proto: listProto(bytesProto([]byte("abc")), nullProto(), bytesProto([]byte("bcd"))), protoType: listType(bytesType()), want: []CustomBytes{CustomBytes("abc"), CustomBytes(nil), CustomBytes("bcd")}},
// INT64 ARRAY
{desc: "decode NULL to []CustomInt64", proto: nullProto(), protoType: listType(intType()), want: []CustomInt64(nil)},
{desc: "decode ARRAY<INT64> with NULL values to []CustomInt64", proto: listProto(intProto(-100), nullProto(), intProto(100)), protoType: listType(intType()), want: []CustomInt64{}, wantErr: true},
{desc: "decode ARRAY<INT64> to []CustomInt64", proto: listProto(intProto(-100), intProto(100)), protoType: listType(intType()), want: []CustomInt64{-100, 100}},
{desc: "decode NULL to []CustomNullInt64", proto: nullProto(), protoType: listType(intType()), want: []CustomNullInt64(nil)},
{desc: "decode ARRAY<INT64> to []CustomNullInt64", proto: listProto(intProto(-100), nullProto(), intProto(100)), protoType: listType(intType()), want: []CustomNullInt64{{-100, true}, {}, {100, true}}},
// BOOL ARRAY
{desc: "decode NULL to []CustomBool", proto: nullProto(), protoType: listType(boolType()), want: []CustomBool(nil)},
{desc: "decode ARRAY<BOOL> with NULL values to []CustomBool", proto: listProto(boolProto(false), nullProto(), boolProto(true)), protoType: listType(boolType()), want: []CustomBool{}, wantErr: true},
{desc: "decode ARRAY<BOOL> to []CustomBool", proto: listProto(boolProto(false), boolProto(true)), protoType: listType(boolType()), want: []CustomBool{false, true}},
{desc: "decode NULL to []CustomNullBool", proto: nullProto(), protoType: listType(boolType()), want: []CustomNullBool(nil)},
{desc: "decode ARRAY<BOOL> to []CustomNullBool", proto: listProto(boolProto(false), nullProto(), boolProto(true)), protoType: listType(boolType()), want: []CustomNullBool{{false, true}, {}, {true, true}}},
// FLOAT64 ARRAY
{desc: "decode NULL to []CustomFloat64", proto: nullProto(), protoType: listType(floatType()), want: []CustomFloat64(nil)},
{desc: "decode ARRAY<FLOAT64> with NULL values to []CustomFloat64", proto: listProto(floatProto(3.14), nullProto(), floatProto(6.626)), protoType: listType(floatType()), want: []CustomFloat64{}, wantErr: true},
{desc: "decode ARRAY<FLOAT64> to []CustomFloat64", proto: listProto(floatProto(3.14), floatProto(6.626)), protoType: listType(floatType()), want: []CustomFloat64{3.14, 6.626}},
{desc: "decode NULL to []CustomNullFloat64", proto: nullProto(), protoType: listType(floatType()), want: []CustomNullFloat64(nil)},
{desc: "decode ARRAY<FLOAT64> to []CustomNullFloat64", proto: listProto(floatProto(3.14), nullProto(), floatProto(6.626)), protoType: listType(floatType()), want: []CustomNullFloat64{{3.14, true}, {}, {6.626, true}}},
// TIME ARRAY
{desc: "decode NULL to []CustomTime", proto: nullProto(), protoType: listType(timeType()), want: []CustomTime(nil)},
{desc: "decode ARRAY<TIMESTAMP> with NULL values to []CustomTime", proto: listProto(timeProto(t1), nullProto(), timeProto(t2)), protoType: listType(timeType()), want: []CustomTime{}, wantErr: true},
{desc: "decode ARRAY<TIMESTAMP> to []CustomTime", proto: listProto(timeProto(t1), timeProto(t2)), protoType: listType(timeType()), want: []CustomTime{CustomTime(t1), CustomTime(t2)}},
{desc: "decode NULL to []CustomNullTime", proto: nullProto(), protoType: listType(timeType()), want: []CustomNullTime(nil)},
{desc: "decode ARRAY<TIMESTAMP> to []CustomNullTime", proto: listProto(timeProto(t1), nullProto(), timeProto(t2)), protoType: listType(timeType()), want: []CustomNullTime{{t1, true}, {}, {t2, true}}},
// DATE ARRAY
{desc: "decode NULL to []CustomDate", proto: nullProto(), protoType: listType(dateType()), want: []CustomDate(nil)},
{desc: "decode ARRAY<DATE> with NULL values to []CustomDate", proto: listProto(dateProto(d1), nullProto(), dateProto(d2)), protoType: listType(dateType()), want: []CustomDate{}, wantErr: true},
{desc: "decode ARRAY<DATE> to []CustomDate", proto: listProto(dateProto(d1), dateProto(d2)), protoType: listType(dateType()), want: []CustomDate{CustomDate(d1), CustomDate(d2)}},
{desc: "decode NULL to []CustomNullDate", proto: nullProto(), protoType: listType(dateType()), want: []CustomNullDate(nil)},
{desc: "decode ARRAY<DATE> to []CustomNullDate", proto: listProto(dateProto(d1), nullProto(), dateProto(d2)), protoType: listType(dateType()), want: []CustomNullDate{{d1, true}, {}, {d2, true}}},
} {
gotp := reflect.New(reflect.TypeOf(test.want))
err := decodeValue(test.proto, test.protoType, gotp.Interface())
if test.wantErr {
if err == nil {
t.Errorf("%s: missing expected decode failure for %v(%v)", test.desc, test.proto, test.protoType)
}
continue
}
if err != nil {
t.Errorf("%s: cannot decode %v(%v): %v", test.desc, test.proto, test.protoType, err)
continue
}
got := reflect.Indirect(gotp).Interface()
if !testutil.Equal(got, test.want, cmp.AllowUnexported(CustomTime{}, CustomDate{}, Row{})) {
t.Errorf("%s: unexpected decoding result - got %v (%T), want %v (%T)", test.desc, got, got, test.want, test.want)
}
}
}
// Test error cases for decodeValue.
func TestDecodeValueErrors(t *testing.T) {
var s string
for i, test := range []struct {
in *proto3.Value
t *sppb.Type
v interface{}
}{
{nullProto(), stringType(), nil},
{nullProto(), stringType(), 1},
{timeProto(t1), timeType(), &s},
} {
err := decodeValue(test.in, test.t, test.v)
if err == nil {
t.Errorf("#%d: want error, got nil", i)
}
}
}
func TestGetDecodableSpannerType(t *testing.T) {
type CustomString string
type CustomInt64 int64
type CustomBool bool
type CustomFloat64 float64
type CustomTime time.Time
type CustomDate civil.Date
type CustomNullString NullString
type CustomNullInt64 NullInt64
type CustomNullBool NullBool
type CustomNullFloat64 NullFloat64
type CustomNullTime NullTime
type CustomNullDate NullDate
type StringEmbedded struct {
string
}
type NullStringEmbedded struct {
NullString
}
for i, test := range []struct {
in interface{}
want decodableSpannerType
}{
{"foo", spannerTypeNonNullString},
{[]byte("ab"), spannerTypeByteArray},
{[]byte(nil), spannerTypeByteArray},
{int64(123), spannerTypeNonNullInt64},
{true, spannerTypeNonNullBool},
{3.14, spannerTypeNonNullFloat64},
{time.Now(), spannerTypeNonNullTime},
{civil.DateOf(time.Now()), spannerTypeNonNullDate},
{NullString{}, spannerTypeNullString},
{NullInt64{}, spannerTypeNullInt64},
{NullBool{}, spannerTypeNullBool},
{NullFloat64{}, spannerTypeNullFloat64},
{NullTime{}, spannerTypeNullTime},
{NullDate{}, spannerTypeNullDate},
{[]string{"foo", "bar"}, spannerTypeArrayOfNonNullString},
{[][]byte{{1, 2, 3}, {3, 2, 1}}, spannerTypeArrayOfByteArray},
{[][]byte{}, spannerTypeArrayOfByteArray},
{[]int64{int64(123)}, spannerTypeArrayOfNonNullInt64},
{[]bool{true}, spannerTypeArrayOfNonNullBool},
{[]float64{3.14}, spannerTypeArrayOfNonNullFloat64},
{[]time.Time{time.Now()}, spannerTypeArrayOfNonNullTime},
{[]civil.Date{civil.DateOf(time.Now())}, spannerTypeArrayOfNonNullDate},
{[]NullString{}, spannerTypeArrayOfNullString},
{[]NullInt64{}, spannerTypeArrayOfNullInt64},
{[]NullBool{}, spannerTypeArrayOfNullBool},
{[]NullFloat64{}, spannerTypeArrayOfNullFloat64},
{[]NullTime{}, spannerTypeArrayOfNullTime},
{[]NullDate{}, spannerTypeArrayOfNullDate},
{CustomString("foo"), spannerTypeNonNullString},
{CustomInt64(-100), spannerTypeNonNullInt64},
{CustomBool(true), spannerTypeNonNullBool},
{CustomFloat64(3.141592), spannerTypeNonNullFloat64},
{CustomTime(time.Now()), spannerTypeNonNullTime},
{CustomDate(civil.DateOf(time.Now())), spannerTypeNonNullDate},
{[]CustomString{}, spannerTypeArrayOfNonNullString},
{[]CustomInt64{}, spannerTypeArrayOfNonNullInt64},
{[]CustomBool{}, spannerTypeArrayOfNonNullBool},
{[]CustomFloat64{}, spannerTypeArrayOfNonNullFloat64},
{[]CustomTime{}, spannerTypeArrayOfNonNullTime},
{[]CustomDate{}, spannerTypeArrayOfNonNullDate},
{CustomNullString{}, spannerTypeNullString},
{CustomNullInt64{}, spannerTypeNullInt64},
{CustomNullBool{}, spannerTypeNullBool},
{CustomNullFloat64{}, spannerTypeNullFloat64},
{CustomNullTime{}, spannerTypeNullTime},
{CustomNullDate{}, spannerTypeNullDate},
{[]CustomNullString{}, spannerTypeArrayOfNullString},
{[]CustomNullInt64{}, spannerTypeArrayOfNullInt64},
{[]CustomNullBool{}, spannerTypeArrayOfNullBool},
{[]CustomNullFloat64{}, spannerTypeArrayOfNullFloat64},
{[]CustomNullTime{}, spannerTypeArrayOfNullTime},
{[]CustomNullDate{}, spannerTypeArrayOfNullDate},
{StringEmbedded{}, spannerTypeUnknown},
{NullStringEmbedded{}, spannerTypeUnknown},
} {
gotp := reflect.New(reflect.TypeOf(test.in))
got := getDecodableSpannerType(gotp.Interface())
if got != test.want {
t.Errorf("%d: unexpected decodable type - got %v, want %v", i, got, test.want)
}
}
}
// Test NaN encoding/decoding.
func TestNaN(t *testing.T) {
// Decode NaN value.
f := 0.0
nf := NullFloat64{}
// To float64
if err := decodeValue(floatProto(math.NaN()), floatType(), &f); err != nil {
t.Errorf("decodeValue returns %q for %v, want nil", err, floatProto(math.NaN()))
}
if !math.IsNaN(f) {
t.Errorf("f = %v, want %v", f, math.NaN())
}
// To NullFloat64
if err := decodeValue(floatProto(math.NaN()), floatType(), &nf); err != nil {
t.Errorf("decodeValue returns %q for %v, want nil", err, floatProto(math.NaN()))
}
if !math.IsNaN(nf.Float64) || !nf.Valid {
t.Errorf("f = %v, want %v", f, NullFloat64{math.NaN(), true})
}
// Encode NaN value
// From float64
v, _, err := encodeValue(math.NaN())
if err != nil {
t.Errorf("encodeValue returns %q for NaN, want nil", err)
}
x, ok := v.GetKind().(*proto3.Value_NumberValue)
if !ok {
t.Errorf("incorrect type for v.GetKind(): %T, want *proto3.Value_NumberValue", v.GetKind())
}
if !math.IsNaN(x.NumberValue) {
t.Errorf("x.NumberValue = %v, want %v", x.NumberValue, math.NaN())
}
// From NullFloat64
v, _, err = encodeValue(NullFloat64{math.NaN(), true})
if err != nil {
t.Errorf("encodeValue returns %q for NaN, want nil", err)
}
x, ok = v.GetKind().(*proto3.Value_NumberValue)
if !ok {
t.Errorf("incorrect type for v.GetKind(): %T, want *proto3.Value_NumberValue", v.GetKind())
}
if !math.IsNaN(x.NumberValue) {
t.Errorf("x.NumberValue = %v, want %v", x.NumberValue, math.NaN())
}
}
func TestGenericColumnValue(t *testing.T) {
for _, test := range []struct {
in GenericColumnValue
want interface{}
fail bool
}{
{GenericColumnValue{stringType(), stringProto("abc")}, "abc", false},
{GenericColumnValue{stringType(), stringProto("abc")}, 5, true},
{GenericColumnValue{listType(intType()), listProto(intProto(91), nullProto(), intProto(87))}, []NullInt64{{91, true}, {}, {87, true}}, false},
{GenericColumnValue{intType(), intProto(42)}, GenericColumnValue{intType(), intProto(42)}, false}, // trippy! :-)
} {
gotp := reflect.New(reflect.TypeOf(test.want))
if err := test.in.Decode(gotp.Interface()); err != nil {
if !test.fail {
t.Errorf("cannot decode %v to %v: %v", test.in, test.want, err)
}
continue
}
if test.fail {
t.Errorf("decoding %v to %v succeeds unexpectedly", test.in, test.want)
}
// Test we can go backwards as well.
v, err := newGenericColumnValue(test.want)
if err != nil {
t.Errorf("NewGenericColumnValue failed: %v", err)
continue
}
if !testEqual(*v, test.in) {
t.Errorf("unexpected encode result - got %v, want %v", v, test.in)
}
}
}
func TestDecodeStruct(t *testing.T) {
type CustomString string
type CustomTime time.Time
stype := &sppb.StructType{Fields: []*sppb.StructType_Field{
{Name: "Id", Type: stringType()},
{Name: "Time", Type: timeType()},
}}
lv := listValueProto(stringProto("id"), timeProto(t1))
type (
S1 struct {
ID string
Time time.Time
}
S2 struct {
ID string
Time string
}
S3 struct {
ID CustomString
Time CustomTime
}
S4 struct {
ID CustomString
Time CustomString
}
S5 struct {
NullString
Time CustomTime
}
)
var (
s1 S1
s2 S2
s3 S3
s4 S4
s5 S5
)
for _, test := range []struct {
desc string
ptr interface{}
want interface{}
fail bool
}{
{
desc: "decode to S1",
ptr: &s1,
want: &S1{ID: "id", Time: t1},
},
{
desc: "decode to S2",
ptr: &s2,
fail: true,
},
{
desc: "decode to S3",
ptr: &s3,
want: &S3{ID: CustomString("id"), Time: CustomTime(t1)},
},
{
desc: "decode to S4",
ptr: &s4,
fail: true,
},
{
desc: "decode to S5",
ptr: &s5,
fail: true,
},
} {
err := decodeStruct(stype, lv, test.ptr)
if (err != nil) != test.fail {
t.Errorf("%s: got error %v, wanted fail: %v", test.desc, err, test.fail)
}
if err == nil {
if !testutil.Equal(test.ptr, test.want, cmp.AllowUnexported(CustomTime{})) {
t.Errorf("%s: got %+v, want %+v", test.desc, test.ptr, test.want)
}
}
}
}
func TestDecodeStructWithPointers(t *testing.T) {
stype := &sppb.StructType{Fields: []*sppb.StructType_Field{
{Name: "Str", Type: stringType()},
{Name: "Int", Type: intType()},
{Name: "Bool", Type: boolType()},
{Name: "Float", Type: floatType()},
{Name: "Time", Type: timeType()},
{Name: "Date", Type: dateType()},
{Name: "StrArray", Type: listType(stringType())},
{Name: "IntArray", Type: listType(intType())},
{Name: "BoolArray", Type: listType(boolType())},
{Name: "FloatArray", Type: listType(floatType())},
{Name: "TimeArray", Type: listType(timeType())},
{Name: "DateArray", Type: listType(dateType())},
}}
lv := []*proto3.ListValue{
listValueProto(
stringProto("id"),
intProto(15),
boolProto(true),
floatProto(3.14),
timeProto(t1),
dateProto(d1),
listProto(stringProto("id1"), nullProto(), stringProto("id2")),
listProto(intProto(16), nullProto(), intProto(17)),
listProto(boolProto(true), nullProto(), boolProto(false)),
listProto(floatProto(3.14), nullProto(), floatProto(6.626)),
listProto(timeProto(t1), nullProto(), timeProto(t2)),
listProto(dateProto(d1), nullProto(), dateProto(d2)),
),
listValueProto(
nullProto(),
nullProto(),
nullProto(),
nullProto(),
nullProto(),
nullProto(),
nullProto(),
nullProto(),
nullProto(),
nullProto(),
nullProto(),
nullProto(),
),
}
type S1 struct {
Str *string
Int *int64
Bool *bool
Float *float64
Time *time.Time
Date *civil.Date
StrArray []*string
IntArray []*int64
BoolArray []*bool
FloatArray []*float64
TimeArray []*time.Time
DateArray []*civil.Date
}
var s1 S1
sValue := "id"
iValue := int64(15)
bValue := true
fValue := 3.14
tValue := t1
dValue := d1
sArrayValue1 := "id1"
sArrayValue2 := "id2"
sArrayValue := []*string{&sArrayValue1, nil, &sArrayValue2}
iArrayValue1 := int64(16)
iArrayValue2 := int64(17)
iArrayValue := []*int64{&iArrayValue1, nil, &iArrayValue2}
bArrayValue1 := true
bArrayValue2 := false
bArrayValue := []*bool{&bArrayValue1, nil, &bArrayValue2}
f1Value := 3.14
f2Value := 6.626
fArrayValue := []*float64{&f1Value, nil, &f2Value}
t1Value := t1
t2Value := t2
tArrayValue := []*time.Time{&t1Value, nil, &t2Value}
d1Value := d1
d2Value := d2
dArrayValue := []*civil.Date{&d1Value, nil, &d2Value}
for i, test := range []struct {
desc string
ptr *S1
want *S1
fail bool
}{
{
desc: "decode values to S1",
ptr: &s1,
want: &S1{Str: &sValue, Int: &iValue, Bool: &bValue, Float: &fValue, Time: &tValue, Date: &dValue, StrArray: sArrayValue, IntArray: iArrayValue, BoolArray: bArrayValue, FloatArray: fArrayValue, TimeArray: tArrayValue, DateArray: dArrayValue},
},
{
desc: "decode nulls to S1",
ptr: &s1,
want: &S1{Str: nil, Int: nil, Bool: nil, Float: nil, Time: nil, Date: nil, StrArray: nil, IntArray: nil, BoolArray: nil, FloatArray: nil, TimeArray: nil, DateArray: nil},
},
} {
err := decodeStruct(stype, lv[i], test.ptr)
if (err != nil) != test.fail {
t.Errorf("%s: got error %v, wanted fail: %v", test.desc, err, test.fail)
}
if err == nil {
if !testutil.Equal(test.ptr, test.want) {
t.Errorf("%s: got %+v, want %+v", test.desc, test.ptr, test.want)
}
}
}
}
func TestEncodeStructValueDynamicStructs(t *testing.T) {
dynStructType := reflect.StructOf([]reflect.StructField{
{Name: "A", Type: reflect.TypeOf(0), Tag: `spanner:"a"`},
{Name: "B", Type: reflect.TypeOf(""), Tag: `spanner:"b"`},
})
dynNullableStructType := reflect.PtrTo(dynStructType)
dynStructArrType := reflect.SliceOf(dynStructType)
dynNullableStructArrType := reflect.SliceOf(dynNullableStructType)
dynStructValue := reflect.New(dynStructType)
dynStructValue.Elem().Field(0).SetInt(10)
dynStructValue.Elem().Field(1).SetString("abc")
dynStructArrValue := reflect.MakeSlice(dynNullableStructArrType, 2, 2)
dynStructArrValue.Index(0).Set(reflect.Zero(dynNullableStructType))
dynStructArrValue.Index(1).Set(dynStructValue)
structProtoType := structType(
mkField("a", intType()),
mkField("b", stringType()))
arrProtoType := listType(structProtoType)
for _, test := range []encodeTest{
{
"Dynanic non-NULL struct value.",
dynStructValue.Elem().Interface(),
listProto(intProto(10), stringProto("abc")),
structProtoType,
},
{
"Dynanic NULL struct value.",
reflect.Zero(dynNullableStructType).Interface(),
nullProto(),
structProtoType,
},
{
"Empty array of dynamic structs.",
reflect.MakeSlice(dynStructArrType, 0, 0).Interface(),
listProto([]*proto3.Value{}...),
arrProtoType,
},
{
"NULL array of non-NULL-able dynamic structs.",
reflect.Zero(dynStructArrType).Interface(),
nullProto(),
arrProtoType,
},
{
"NULL array of NULL-able(nil) dynamic structs.",
reflect.Zero(dynNullableStructArrType).Interface(),
nullProto(),
arrProtoType,
},
{
"Array containing NULL(nil) dynamic-typed struct elements.",
dynStructArrValue.Interface(),
listProto(
nullProto(),
listProto(intProto(10), stringProto("abc"))),
arrProtoType,
},
} {
encodeStructValue(test, t)
}
}
func TestEncodeStructValueEmptyStruct(t *testing.T) {
emptyListValue := listProto([]*proto3.Value{}...)
emptyStructType := structType([]*sppb.StructType_Field{}...)
emptyStruct := struct{}{}
nullEmptyStruct := (*struct{})(nil)
dynamicEmptyStructType := reflect.StructOf(make([]reflect.StructField, 0, 0))
dynamicStructArrType := reflect.SliceOf(reflect.PtrTo((dynamicEmptyStructType)))
dynamicEmptyStruct := reflect.New(dynamicEmptyStructType)
dynamicNullEmptyStruct := reflect.Zero(reflect.PtrTo(dynamicEmptyStructType))
dynamicStructArrValue := reflect.MakeSlice(dynamicStructArrType, 2, 2)
dynamicStructArrValue.Index(0).Set(dynamicNullEmptyStruct)
dynamicStructArrValue.Index(1).Set(dynamicEmptyStruct)
for _, test := range []encodeTest{
{
"Go empty struct.",
emptyStruct,
emptyListValue,
emptyStructType,
},
{
"Dynamic empty struct.",
dynamicEmptyStruct.Interface(),
emptyListValue,
emptyStructType,
},
{
"Go NULL empty struct.",
nullEmptyStruct,
nullProto(),
emptyStructType,
},
{
"Dynamic NULL empty struct.",
dynamicNullEmptyStruct.Interface(),
nullProto(),
emptyStructType,
},
{
"Non-empty array of dynamic NULL and non-NULL empty structs.",
dynamicStructArrValue.Interface(),
listProto(nullProto(), emptyListValue),
listType(emptyStructType),
},
{
"Non-empty array of nullable empty structs.",
[]*struct{}{nullEmptyStruct, &emptyStruct},
listProto(nullProto(), emptyListValue),
listType(emptyStructType),
},
{
"Empty array of empty struct.",
[]struct{}{},
emptyListValue,
listType(emptyStructType),
},
{
"Null array of empty structs.",
[]struct{}(nil),
nullProto(),
listType(emptyStructType),
},
} {
encodeStructValue(test, t)
}
}
func TestEncodeStructValueMixedStructTypes(t *testing.T) {
type staticStruct struct {
F int `spanner:"fStatic"`
}
s1 := staticStruct{10}
s2 := (*staticStruct)(nil)
var f float64
dynStructType := reflect.StructOf([]reflect.StructField{
{Name: "A", Type: reflect.TypeOf(f), Tag: `spanner:"fDynamic"`},
})
s3 := reflect.New(dynStructType)
s3.Elem().Field(0).SetFloat(3.14)
for _, test := range []encodeTest{
{
"'struct' with static and dynamic *struct, []*struct, []struct fields",
struct {
A []staticStruct
B []*staticStruct
C interface{}
}{
[]staticStruct{s1, s1},
[]*staticStruct{&s1, s2},
s3.Interface(),
},
listProto(
listProto(listProto(intProto(10)), listProto(intProto(10))),
listProto(listProto(intProto(10)), nullProto()),
listProto(floatProto(3.14))),
structType(
mkField("A", listType(structType(mkField("fStatic", intType())))),
mkField("B", listType(structType(mkField("fStatic", intType())))),
mkField("C", structType(mkField("fDynamic", floatType())))),
},
} {
encodeStructValue(test, t)
}
}
func TestBindParamsDynamic(t *testing.T) {
// Verify Statement.bindParams generates correct values and types.
st := Statement{
SQL: "SELECT id from t_foo WHERE col = @var",
Params: map[string]interface{}{"var": nil},
}
want := &sppb.ExecuteSqlRequest{
Params: &proto3.Struct{
Fields: map[string]*proto3.Value{"var": nil},
},
ParamTypes: map[string]*sppb.Type{"var": nil},
}
var (
t1, _ = time.Parse(time.RFC3339Nano, "2016-11-15T15:04:05.999999999Z")
// Boundaries
t2, _ = time.Parse(time.RFC3339Nano, "0001-01-01T00:00:00.000000000Z")
)
dynamicStructType := reflect.StructOf([]reflect.StructField{
{Name: "A", Type: reflect.TypeOf(t1), Tag: `spanner:"field"`},
{Name: "B", Type: reflect.TypeOf(3.14), Tag: `spanner:""`},
})
dynamicStructArrType := reflect.SliceOf(reflect.PtrTo(dynamicStructType))
dynamicEmptyStructType := reflect.StructOf(make([]reflect.StructField, 0, 0))
dynamicStructTypeProto := structType(
mkField("field", timeType()),
mkField("", floatType()))
s3 := reflect.New(dynamicStructType)
s3.Elem().Field(0).Set(reflect.ValueOf(t1))
s3.Elem().Field(1).SetFloat(1.4)
s4 := reflect.New(dynamicStructType)
s4.Elem().Field(0).Set(reflect.ValueOf(t2))
s4.Elem().Field(1).SetFloat(-13.3)
dynamicStructArrayVal := reflect.MakeSlice(dynamicStructArrType, 2, 2)
dynamicStructArrayVal.Index(0).Set(s3)
dynamicStructArrayVal.Index(1).Set(s4)
for _, test := range []struct {
val interface{}
wantField *proto3.Value
wantType *sppb.Type
}{
{
s3.Interface(),
listProto(timeProto(t1), floatProto(1.4)),
structType(
mkField("field", timeType()),
mkField("", floatType())),
},
{
reflect.Zero(reflect.PtrTo(dynamicEmptyStructType)).Interface(),
nullProto(),
structType([]*sppb.StructType_Field{}...),
},
{
dynamicStructArrayVal.Interface(),
listProto(
listProto(timeProto(t1), floatProto(1.4)),
listProto(timeProto(t2), floatProto(-13.3))),
listType(dynamicStructTypeProto),
},
{
[]*struct {
F1 time.Time `spanner:"field"`
F2 float64 `spanner:""`
}{
nil,
{t1, 1.4},
},
listProto(
nullProto(),
listProto(timeProto(t1), floatProto(1.4))),
listType(dynamicStructTypeProto),
},
} {
st.Params["var"] = test.val
want.Params.Fields["var"] = test.wantField
want.ParamTypes["var"] = test.wantType
gotParams, gotParamTypes, gotErr := st.convertParams()
if gotErr != nil {
t.Error(gotErr)
continue
}
gotParamField := gotParams.Fields["var"]
if !proto.Equal(gotParamField, test.wantField) {
// handle NaN
if test.wantType.Code == floatType().Code && proto.MarshalTextString(gotParamField) == proto.MarshalTextString(test.wantField) {
continue
}
t.Errorf("%#v: got %v, want %v\n", test.val, gotParamField, test.wantField)
}
gotParamType := gotParamTypes["var"]
if !proto.Equal(gotParamType, test.wantType) {
t.Errorf("%#v: got %v, want %v\n", test.val, gotParamType, test.wantField)
}
}
}
// Test converting nullable types to json strings.
func TestJSONMarshal_NullTypes(t *testing.T) {
type testcase struct {
input interface{}
expect string
}
for _, test := range []struct {
name string
cases []testcase
}{
{
"NullString",
[]testcase{
{input: NullString{"this is a test string", true}, expect: `"this is a test string"`},
{input: &NullString{"this is a test string", true}, expect: `"this is a test string"`},
{input: &NullString{"this is a test string", false}, expect: "null"},
{input: NullString{}, expect: "null"},
},
},
{
"NullInt64",
[]testcase{
{input: NullInt64{int64(123), true}, expect: "123"},
{input: &NullInt64{int64(123), true}, expect: "123"},
{input: &NullInt64{int64(123), false}, expect: "null"},
{input: NullInt64{}, expect: "null"},
},
},
{
"NullFloat64",
[]testcase{
{input: NullFloat64{float64(123.123), true}, expect: "123.123"},
{input: &NullFloat64{float64(123.123), true}, expect: "123.123"},
{input: &NullFloat64{float64(123.123), false}, expect: "null"},
{input: NullFloat64{}, expect: "null"},
},
},
{
"NullBool",
[]testcase{
{input: NullBool{true, true}, expect: "true"},
{input: &NullBool{true, true}, expect: "true"},
{input: &NullBool{true, false}, expect: "null"},
{input: NullBool{}, expect: "null"},
},
},
{
"NullTime",
[]testcase{
{input: NullTime{time.Date(2009, 11, 17, 20, 34, 58, 651387237, time.UTC), true}, expect: `"2009-11-17T20:34:58.651387237Z"`},
{input: &NullTime{time.Date(2009, 11, 17, 20, 34, 58, 651387237, time.UTC), true}, expect: `"2009-11-17T20:34:58.651387237Z"`},
{input: &NullTime{time.Date(2009, 11, 17, 20, 34, 58, 651387237, time.UTC), false}, expect: "null"},
{input: NullTime{}, expect: "null"},
},
},
{
"NullDate",
[]testcase{
{input: NullDate{civil.Date{Year: 2009, Month: time.November, Day: 17}, true}, expect: `"2009-11-17"`},
{input: &NullDate{civil.Date{Year: 2009, Month: time.November, Day: 17}, true}, expect: `"2009-11-17"`},
{input: &NullDate{civil.Date{Year: 2009, Month: time.November, Day: 17}, false}, expect: "null"},
{input: NullDate{}, expect: "null"},
},
},
} {
t.Run(test.name, func(t *testing.T) {
for _, tc := range test.cases {
bytes, _ := json.Marshal(tc.input)
got := string(bytes)
if got != tc.expect {
t.Fatalf("Incorrect marshalling to json strings: got %v, want %v", got, tc.expect)
}
}
})
}
}
// Test converting json strings to nullable types.
func TestJSONUnmarshal_NullTypes(t *testing.T) {
type testcase struct {
input []byte
got interface{}
isNull bool
expect string
expectError bool
}
for _, test := range []struct {
name string
cases []testcase
}{
{
"NullString",
[]testcase{
{input: []byte(`"this is a test string"`), got: NullString{}, isNull: false, expect: "this is a test string", expectError: false},
{input: []byte(`""`), got: NullString{}, isNull: false, expect: "", expectError: false},
{input: []byte("null"), got: NullString{}, isNull: true, expect: nullString, expectError: false},
{input: nil, got: NullString{}, isNull: true, expect: nullString, expectError: true},
{input: []byte(""), got: NullString{}, isNull: true, expect: nullString, expectError: true},
{input: []byte(`"hello`), got: NullString{}, isNull: true, expect: nullString, expectError: true},
},
},
{
"NullInt64",
[]testcase{
{input: []byte("123"), got: NullInt64{}, isNull: false, expect: "123", expectError: false},
{input: []byte("null"), got: NullInt64{}, isNull: true, expect: nullString, expectError: false},
{input: nil, got: NullInt64{}, isNull: true, expect: nullString, expectError: true},
{input: []byte(""), got: NullInt64{}, isNull: true, expect: nullString, expectError: true},
{input: []byte(`"hello`), got: NullInt64{}, isNull: true, expect: nullString, expectError: true},
},
},
{
"NullFloat64",
[]testcase{
{input: []byte("123.123"), got: NullFloat64{}, isNull: false, expect: "123.123", expectError: false},
{input: []byte("null"), got: NullFloat64{}, isNull: true, expect: nullString, expectError: false},
{input: nil, got: NullFloat64{}, isNull: true, expect: nullString, expectError: true},
{input: []byte(""), got: NullFloat64{}, isNull: true, expect: nullString, expectError: true},
{input: []byte(`"hello`), got: NullFloat64{}, isNull: true, expect: nullString, expectError: true},
},
},
{
"NullBool",
[]testcase{
{input: []byte("true"), got: NullBool{}, isNull: false, expect: "true", expectError: false},
{input: []byte("null"), got: NullBool{}, isNull: true, expect: nullString, expectError: false},
{input: nil, got: NullBool{}, isNull: true, expect: nullString, expectError: true},
{input: []byte(""), got: NullBool{}, isNull: true, expect: nullString, expectError: true},
{input: []byte(`"hello`), got: NullBool{}, isNull: true, expect: nullString, expectError: true},
},
},
{
"NullTime",
[]testcase{
{input: []byte(`"2009-11-17T20:34:58.651387237Z"`), got: NullTime{}, isNull: false, expect: "2009-11-17T20:34:58.651387237Z", expectError: false},
{input: []byte("null"), got: NullTime{}, isNull: true, expect: nullString, expectError: false},
{input: nil, got: NullTime{}, isNull: true, expect: nullString, expectError: true},
{input: []byte(""), got: NullTime{}, isNull: true, expect: nullString, expectError: true},
{input: []byte(`"hello`), got: NullTime{}, isNull: true, expect: nullString, expectError: true},
},
},
{
"NullDate",
[]testcase{
{input: []byte(`"2009-11-17"`), got: NullDate{}, isNull: false, expect: "2009-11-17", expectError: false},
{input: []byte("null"), got: NullDate{}, isNull: true, expect: nullString, expectError: false},
{input: nil, got: NullDate{}, isNull: true, expect: nullString, expectError: true},
{input: []byte(""), got: NullDate{}, isNull: true, expect: nullString, expectError: true},
{input: []byte(`"hello`), got: NullDate{}, isNull: true, expect: nullString, expectError: true},
},
},
} {
t.Run(test.name, func(t *testing.T) {
for _, tc := range test.cases {
switch v := tc.got.(type) {
case NullString:
err := json.Unmarshal(tc.input, &v)
expectUnmarshalNullableTypes(t, err, v, tc.isNull, tc.expect, tc.expectError)
case NullInt64:
err := json.Unmarshal(tc.input, &v)
expectUnmarshalNullableTypes(t, err, v, tc.isNull, tc.expect, tc.expectError)
case NullFloat64:
err := json.Unmarshal(tc.input, &v)
expectUnmarshalNullableTypes(t, err, v, tc.isNull, tc.expect, tc.expectError)
case NullBool:
err := json.Unmarshal(tc.input, &v)
expectUnmarshalNullableTypes(t, err, v, tc.isNull, tc.expect, tc.expectError)
case NullTime:
err := json.Unmarshal(tc.input, &v)
expectUnmarshalNullableTypes(t, err, v, tc.isNull, tc.expect, tc.expectError)
case NullDate:
err := json.Unmarshal(tc.input, &v)
expectUnmarshalNullableTypes(t, err, v, tc.isNull, tc.expect, tc.expectError)
default:
t.Fatalf("Unknown type: %T", v)
}
}
})
}
}
func expectUnmarshalNullableTypes(t *testing.T, err error, v interface{}, isNull bool, expect string, expectError bool) {
if expectError {
if err == nil {
t.Fatalf("Expect to get an error, but got a nil")
}
return
}
if err != nil {
t.Fatalf("Got an error when unmarshalling a valid json string: %q", err)
}
if s, ok := v.(NullableValue); !ok || s.IsNull() != isNull {
t.Fatalf("Incorrect unmarshalling a json string to nullable types: got %q, want %q", v, expect)
}
if s, ok := v.(fmt.Stringer); !ok || s.String() != expect {
t.Fatalf("Incorrect unmarshalling a json string to nullable types: got %q, want %q", v, expect)
}
}
|