github.com/lingyao2333/mo-zero@v1.4.1/core/mapping/jsonunmarshaler_test.go (about) 1 package mapping 2 3 import ( 4 "bytes" 5 "reflect" 6 "strings" 7 "testing" 8 9 "github.com/stretchr/testify/assert" 10 ) 11 12 func TestUnmarshalBytes(t *testing.T) { 13 var c struct { 14 Name string 15 } 16 content := []byte(`{"Name": "liao"}`) 17 18 assert.Nil(t, UnmarshalJsonBytes(content, &c)) 19 assert.Equal(t, "liao", c.Name) 20 } 21 22 func TestUnmarshalBytesOptional(t *testing.T) { 23 var c struct { 24 Name string 25 Age int `json:",optional"` 26 } 27 content := []byte(`{"Name": "liao"}`) 28 29 assert.Nil(t, UnmarshalJsonBytes(content, &c)) 30 assert.Equal(t, "liao", c.Name) 31 } 32 33 func TestUnmarshalBytesOptionalDefault(t *testing.T) { 34 var c struct { 35 Name string 36 Age int `json:",optional,default=1"` 37 } 38 content := []byte(`{"Name": "liao"}`) 39 40 assert.Nil(t, UnmarshalJsonBytes(content, &c)) 41 assert.Equal(t, "liao", c.Name) 42 assert.Equal(t, 1, c.Age) 43 } 44 45 func TestUnmarshalBytesDefaultOptional(t *testing.T) { 46 var c struct { 47 Name string 48 Age int `json:",default=1,optional"` 49 } 50 content := []byte(`{"Name": "liao"}`) 51 52 assert.Nil(t, UnmarshalJsonBytes(content, &c)) 53 assert.Equal(t, "liao", c.Name) 54 assert.Equal(t, 1, c.Age) 55 } 56 57 func TestUnmarshalBytesDefault(t *testing.T) { 58 var c struct { 59 Name string `json:",default=liao"` 60 } 61 content := []byte(`{}`) 62 63 assert.Nil(t, UnmarshalJsonBytes(content, &c)) 64 assert.Equal(t, "liao", c.Name) 65 } 66 67 func TestUnmarshalBytesBool(t *testing.T) { 68 var c struct { 69 Great bool 70 } 71 content := []byte(`{"Great": true}`) 72 73 assert.Nil(t, UnmarshalJsonBytes(content, &c)) 74 assert.True(t, c.Great) 75 } 76 77 func TestUnmarshalBytesInt(t *testing.T) { 78 var c struct { 79 Age int 80 } 81 content := []byte(`{"Age": 1}`) 82 83 assert.Nil(t, UnmarshalJsonBytes(content, &c)) 84 assert.Equal(t, 1, c.Age) 85 } 86 87 func TestUnmarshalBytesUint(t *testing.T) { 88 var c struct { 89 Age uint 90 } 91 content := []byte(`{"Age": 1}`) 92 93 assert.Nil(t, UnmarshalJsonBytes(content, &c)) 94 assert.Equal(t, uint(1), c.Age) 95 } 96 97 func TestUnmarshalBytesFloat(t *testing.T) { 98 var c struct { 99 Age float32 100 } 101 content := []byte(`{"Age": 1.5}`) 102 103 assert.Nil(t, UnmarshalJsonBytes(content, &c)) 104 assert.Equal(t, float32(1.5), c.Age) 105 } 106 107 func TestUnmarshalBytesMustInOptional(t *testing.T) { 108 var c struct { 109 Inner struct { 110 There string 111 Must string 112 Optional string `json:",optional"` 113 } `json:",optional"` 114 } 115 content := []byte(`{}`) 116 117 assert.Nil(t, UnmarshalJsonBytes(content, &c)) 118 } 119 120 func TestUnmarshalBytesMustInOptionalMissedPart(t *testing.T) { 121 var c struct { 122 Inner struct { 123 There string 124 Must string 125 Optional string `json:",optional"` 126 } `json:",optional"` 127 } 128 content := []byte(`{"Inner": {"There": "sure"}}`) 129 130 assert.NotNil(t, UnmarshalJsonBytes(content, &c)) 131 } 132 133 func TestUnmarshalBytesMustInOptionalOnlyOptionalFilled(t *testing.T) { 134 var c struct { 135 Inner struct { 136 There string 137 Must string 138 Optional string `json:",optional"` 139 } `json:",optional"` 140 } 141 content := []byte(`{"Inner": {"Optional": "sure"}}`) 142 143 assert.NotNil(t, UnmarshalJsonBytes(content, &c)) 144 } 145 146 func TestUnmarshalBytesNil(t *testing.T) { 147 var c struct { 148 Int int64 `json:"int,optional"` 149 } 150 content := []byte(`{"int":null}`) 151 152 assert.Nil(t, UnmarshalJsonBytes(content, &c)) 153 assert.Equal(t, int64(0), c.Int) 154 } 155 156 func TestUnmarshalBytesNilSlice(t *testing.T) { 157 var c struct { 158 Ints []int64 `json:"ints"` 159 } 160 content := []byte(`{"ints":[null]}`) 161 162 assert.Nil(t, UnmarshalJsonBytes(content, &c)) 163 assert.Equal(t, 0, len(c.Ints)) 164 } 165 166 func TestUnmarshalBytesPartial(t *testing.T) { 167 var c struct { 168 Name string 169 Age float32 170 } 171 content := []byte(`{"Age": 1.5}`) 172 173 assert.NotNil(t, UnmarshalJsonBytes(content, &c)) 174 } 175 176 func TestUnmarshalBytesStruct(t *testing.T) { 177 var c struct { 178 Inner struct { 179 Name string 180 } 181 } 182 content := []byte(`{"Inner": {"Name": "liao"}}`) 183 184 assert.Nil(t, UnmarshalJsonBytes(content, &c)) 185 assert.Equal(t, "liao", c.Inner.Name) 186 } 187 188 func TestUnmarshalBytesStructOptional(t *testing.T) { 189 var c struct { 190 Inner struct { 191 Name string 192 Age int `json:",optional"` 193 } 194 } 195 content := []byte(`{"Inner": {"Name": "liao"}}`) 196 197 assert.Nil(t, UnmarshalJsonBytes(content, &c)) 198 assert.Equal(t, "liao", c.Inner.Name) 199 } 200 201 func TestUnmarshalBytesStructPtr(t *testing.T) { 202 var c struct { 203 Inner *struct { 204 Name string 205 } 206 } 207 content := []byte(`{"Inner": {"Name": "liao"}}`) 208 209 assert.Nil(t, UnmarshalJsonBytes(content, &c)) 210 assert.Equal(t, "liao", c.Inner.Name) 211 } 212 213 func TestUnmarshalBytesStructPtrOptional(t *testing.T) { 214 var c struct { 215 Inner *struct { 216 Name string 217 Age int `json:",optional"` 218 } 219 } 220 content := []byte(`{"Inner": {"Name": "liao"}}`) 221 222 assert.Nil(t, UnmarshalJsonBytes(content, &c)) 223 } 224 225 func TestUnmarshalBytesStructPtrDefault(t *testing.T) { 226 var c struct { 227 Inner *struct { 228 Name string 229 Age int `json:",default=4"` 230 } 231 } 232 content := []byte(`{"Inner": {"Name": "liao"}}`) 233 234 assert.Nil(t, UnmarshalJsonBytes(content, &c)) 235 assert.Equal(t, "liao", c.Inner.Name) 236 assert.Equal(t, 4, c.Inner.Age) 237 } 238 239 func TestUnmarshalBytesSliceString(t *testing.T) { 240 var c struct { 241 Names []string 242 } 243 content := []byte(`{"Names": ["liao", "chaoxin"]}`) 244 245 assert.Nil(t, UnmarshalJsonBytes(content, &c)) 246 247 want := []string{"liao", "chaoxin"} 248 if !reflect.DeepEqual(c.Names, want) { 249 t.Fatalf("want %q, got %q", c.Names, want) 250 } 251 } 252 253 func TestUnmarshalBytesSliceStringOptional(t *testing.T) { 254 var c struct { 255 Names []string 256 Age []int `json:",optional"` 257 } 258 content := []byte(`{"Names": ["liao", "chaoxin"]}`) 259 260 assert.Nil(t, UnmarshalJsonBytes(content, &c)) 261 262 want := []string{"liao", "chaoxin"} 263 if !reflect.DeepEqual(c.Names, want) { 264 t.Fatalf("want %q, got %q", c.Names, want) 265 } 266 } 267 268 func TestUnmarshalBytesSliceStruct(t *testing.T) { 269 var c struct { 270 People []struct { 271 Name string 272 Age int 273 } 274 } 275 content := []byte(`{"People": [{"Name": "liao", "Age": 1}, {"Name": "chaoxin", "Age": 2}]}`) 276 277 assert.Nil(t, UnmarshalJsonBytes(content, &c)) 278 279 want := []struct { 280 Name string 281 Age int 282 }{ 283 {"liao", 1}, 284 {"chaoxin", 2}, 285 } 286 if !reflect.DeepEqual(c.People, want) { 287 t.Fatalf("want %q, got %q", c.People, want) 288 } 289 } 290 291 func TestUnmarshalBytesSliceStructOptional(t *testing.T) { 292 var c struct { 293 People []struct { 294 Name string 295 Age int 296 Emails []string `json:",optional"` 297 } 298 } 299 content := []byte(`{"People": [{"Name": "liao", "Age": 1}, {"Name": "chaoxin", "Age": 2}]}`) 300 301 assert.Nil(t, UnmarshalJsonBytes(content, &c)) 302 303 want := []struct { 304 Name string 305 Age int 306 Emails []string `json:",optional"` 307 }{ 308 {"liao", 1, nil}, 309 {"chaoxin", 2, nil}, 310 } 311 if !reflect.DeepEqual(c.People, want) { 312 t.Fatalf("want %q, got %q", c.People, want) 313 } 314 } 315 316 func TestUnmarshalBytesSliceStructPtr(t *testing.T) { 317 var c struct { 318 People []*struct { 319 Name string 320 Age int 321 } 322 } 323 content := []byte(`{"People": [{"Name": "liao", "Age": 1}, {"Name": "chaoxin", "Age": 2}]}`) 324 325 assert.Nil(t, UnmarshalJsonBytes(content, &c)) 326 327 want := []*struct { 328 Name string 329 Age int 330 }{ 331 {"liao", 1}, 332 {"chaoxin", 2}, 333 } 334 if !reflect.DeepEqual(c.People, want) { 335 t.Fatalf("want %v, got %v", c.People, want) 336 } 337 } 338 339 func TestUnmarshalBytesSliceStructPtrOptional(t *testing.T) { 340 var c struct { 341 People []*struct { 342 Name string 343 Age int 344 Emails []string `json:",optional"` 345 } 346 } 347 content := []byte(`{"People": [{"Name": "liao", "Age": 1}, {"Name": "chaoxin", "Age": 2}]}`) 348 349 assert.Nil(t, UnmarshalJsonBytes(content, &c)) 350 351 want := []*struct { 352 Name string 353 Age int 354 Emails []string `json:",optional"` 355 }{ 356 {"liao", 1, nil}, 357 {"chaoxin", 2, nil}, 358 } 359 if !reflect.DeepEqual(c.People, want) { 360 t.Fatalf("want %v, got %v", c.People, want) 361 } 362 } 363 364 func TestUnmarshalBytesSliceStructPtrPartial(t *testing.T) { 365 var c struct { 366 People []*struct { 367 Name string 368 Age int 369 Email string 370 } 371 } 372 content := []byte(`{"People": [{"Name": "liao", "Age": 1}, {"Name": "chaoxin", "Age": 2}]}`) 373 374 assert.NotNil(t, UnmarshalJsonBytes(content, &c)) 375 } 376 377 func TestUnmarshalBytesSliceStructPtrDefault(t *testing.T) { 378 var c struct { 379 People []*struct { 380 Name string 381 Age int 382 Email string `json:",default=chaoxin@liao.com"` 383 } 384 } 385 content := []byte(`{"People": [{"Name": "liao", "Age": 1}, {"Name": "chaoxin", "Age": 2}]}`) 386 387 assert.Nil(t, UnmarshalJsonBytes(content, &c)) 388 389 want := []*struct { 390 Name string 391 Age int 392 Email string 393 }{ 394 {"liao", 1, "chaoxin@liao.com"}, 395 {"chaoxin", 2, "chaoxin@liao.com"}, 396 } 397 398 for i := range c.People { 399 actual := c.People[i] 400 expect := want[i] 401 assert.Equal(t, expect.Age, actual.Age) 402 assert.Equal(t, expect.Email, actual.Email) 403 assert.Equal(t, expect.Name, actual.Name) 404 } 405 } 406 407 func TestUnmarshalBytesSliceStringPartial(t *testing.T) { 408 var c struct { 409 Names []string 410 Age int 411 } 412 content := []byte(`{"Age": 1}`) 413 414 assert.NotNil(t, UnmarshalJsonBytes(content, &c)) 415 } 416 417 func TestUnmarshalBytesSliceStructPartial(t *testing.T) { 418 var c struct { 419 Group string 420 People []struct { 421 Name string 422 Age int 423 } 424 } 425 content := []byte(`{"Group": "chaoxin"}`) 426 427 assert.NotNil(t, UnmarshalJsonBytes(content, &c)) 428 } 429 430 func TestUnmarshalBytesInnerAnonymousPartial(t *testing.T) { 431 type ( 432 Deep struct { 433 A string 434 B string `json:",optional"` 435 } 436 Inner struct { 437 Deep 438 InnerV string `json:",optional"` 439 } 440 ) 441 442 var c struct { 443 Value Inner `json:",optional"` 444 } 445 content := []byte(`{"Value": {"InnerV": "chaoxin"}}`) 446 447 assert.NotNil(t, UnmarshalJsonBytes(content, &c)) 448 } 449 450 func TestUnmarshalBytesStructPartial(t *testing.T) { 451 var c struct { 452 Group string 453 Person struct { 454 Name string 455 Age int 456 } 457 } 458 content := []byte(`{"Group": "chaoxin"}`) 459 460 assert.NotNil(t, UnmarshalJsonBytes(content, &c)) 461 } 462 463 func TestUnmarshalBytesEmptyMap(t *testing.T) { 464 var c struct { 465 Persons map[string]int `json:",optional"` 466 } 467 content := []byte(`{"Persons": {}}`) 468 469 assert.Nil(t, UnmarshalJsonBytes(content, &c)) 470 assert.Empty(t, c.Persons) 471 } 472 473 func TestUnmarshalBytesMap(t *testing.T) { 474 var c struct { 475 Persons map[string]int 476 } 477 content := []byte(`{"Persons": {"first": 1, "second": 2}}`) 478 479 assert.Nil(t, UnmarshalJsonBytes(content, &c)) 480 assert.Equal(t, 2, len(c.Persons)) 481 assert.Equal(t, 1, c.Persons["first"]) 482 assert.Equal(t, 2, c.Persons["second"]) 483 } 484 485 func TestUnmarshalBytesMapStruct(t *testing.T) { 486 var c struct { 487 Persons map[string]struct { 488 ID int 489 Name string `json:"name,optional"` 490 } 491 } 492 content := []byte(`{"Persons": {"first": {"ID": 1, "name": "kevin"}}}`) 493 494 assert.Nil(t, UnmarshalJsonBytes(content, &c)) 495 assert.Equal(t, 1, len(c.Persons)) 496 assert.Equal(t, 1, c.Persons["first"].ID) 497 assert.Equal(t, "kevin", c.Persons["first"].Name) 498 } 499 500 func TestUnmarshalBytesMapStructPtr(t *testing.T) { 501 var c struct { 502 Persons map[string]*struct { 503 ID int 504 Name string `json:"name,optional"` 505 } 506 } 507 content := []byte(`{"Persons": {"first": {"ID": 1, "name": "kevin"}}}`) 508 509 assert.Nil(t, UnmarshalJsonBytes(content, &c)) 510 assert.Equal(t, 1, len(c.Persons)) 511 assert.Equal(t, 1, c.Persons["first"].ID) 512 assert.Equal(t, "kevin", c.Persons["first"].Name) 513 } 514 515 func TestUnmarshalBytesMapStructMissingPartial(t *testing.T) { 516 var c struct { 517 Persons map[string]*struct { 518 ID int 519 Name string 520 } 521 } 522 content := []byte(`{"Persons": {"first": {"ID": 1}}}`) 523 524 assert.NotNil(t, UnmarshalJsonBytes(content, &c)) 525 } 526 527 func TestUnmarshalBytesMapStructOptional(t *testing.T) { 528 var c struct { 529 Persons map[string]*struct { 530 ID int 531 Name string `json:"name,optional"` 532 } 533 } 534 content := []byte(`{"Persons": {"first": {"ID": 1}}}`) 535 536 assert.Nil(t, UnmarshalJsonBytes(content, &c)) 537 assert.Equal(t, 1, len(c.Persons)) 538 assert.Equal(t, 1, c.Persons["first"].ID) 539 } 540 541 func TestUnmarshalBytesMapEmptyStructSlice(t *testing.T) { 542 var c struct { 543 Persons map[string][]struct { 544 ID int 545 Name string `json:"name,optional"` 546 } 547 } 548 content := []byte(`{"Persons": {"first": []}}`) 549 550 assert.Nil(t, UnmarshalJsonBytes(content, &c)) 551 assert.Equal(t, 1, len(c.Persons)) 552 assert.Empty(t, c.Persons["first"]) 553 } 554 555 func TestUnmarshalBytesMapStructSlice(t *testing.T) { 556 var c struct { 557 Persons map[string][]struct { 558 ID int 559 Name string `json:"name,optional"` 560 } 561 } 562 content := []byte(`{"Persons": {"first": [{"ID": 1, "name": "kevin"}]}}`) 563 564 assert.Nil(t, UnmarshalJsonBytes(content, &c)) 565 assert.Equal(t, 1, len(c.Persons)) 566 assert.Equal(t, 1, c.Persons["first"][0].ID) 567 assert.Equal(t, "kevin", c.Persons["first"][0].Name) 568 } 569 570 func TestUnmarshalBytesMapEmptyStructPtrSlice(t *testing.T) { 571 var c struct { 572 Persons map[string][]*struct { 573 ID int 574 Name string `json:"name,optional"` 575 } 576 } 577 content := []byte(`{"Persons": {"first": []}}`) 578 579 assert.Nil(t, UnmarshalJsonBytes(content, &c)) 580 assert.Equal(t, 1, len(c.Persons)) 581 assert.Empty(t, c.Persons["first"]) 582 } 583 584 func TestUnmarshalBytesMapStructPtrSlice(t *testing.T) { 585 var c struct { 586 Persons map[string][]*struct { 587 ID int 588 Name string `json:"name,optional"` 589 } 590 } 591 content := []byte(`{"Persons": {"first": [{"ID": 1, "name": "kevin"}]}}`) 592 593 assert.Nil(t, UnmarshalJsonBytes(content, &c)) 594 assert.Equal(t, 1, len(c.Persons)) 595 assert.Equal(t, 1, c.Persons["first"][0].ID) 596 assert.Equal(t, "kevin", c.Persons["first"][0].Name) 597 } 598 599 func TestUnmarshalBytesMapStructPtrSliceMissingPartial(t *testing.T) { 600 var c struct { 601 Persons map[string][]*struct { 602 ID int 603 Name string 604 } 605 } 606 content := []byte(`{"Persons": {"first": [{"ID": 1}]}}`) 607 608 assert.NotNil(t, UnmarshalJsonBytes(content, &c)) 609 } 610 611 func TestUnmarshalBytesMapStructPtrSliceOptional(t *testing.T) { 612 var c struct { 613 Persons map[string][]*struct { 614 ID int 615 Name string `json:"name,optional"` 616 } 617 } 618 content := []byte(`{"Persons": {"first": [{"ID": 1}]}}`) 619 620 assert.Nil(t, UnmarshalJsonBytes(content, &c)) 621 assert.Equal(t, 1, len(c.Persons)) 622 assert.Equal(t, 1, c.Persons["first"][0].ID) 623 } 624 625 func TestUnmarshalStructOptional(t *testing.T) { 626 var c struct { 627 Name string 628 Etcd struct { 629 Hosts []string 630 Key string 631 } `json:",optional"` 632 } 633 content := []byte(`{"Name": "kevin"}`) 634 635 err := UnmarshalJsonBytes(content, &c) 636 assert.Nil(t, err) 637 assert.Equal(t, "kevin", c.Name) 638 } 639 640 func TestUnmarshalStructLowerCase(t *testing.T) { 641 var c struct { 642 Name string 643 Etcd struct { 644 Key string 645 } `json:"etcd"` 646 } 647 content := []byte(`{"Name": "kevin", "etcd": {"Key": "the key"}}`) 648 649 err := UnmarshalJsonBytes(content, &c) 650 assert.Nil(t, err) 651 assert.Equal(t, "kevin", c.Name) 652 assert.Equal(t, "the key", c.Etcd.Key) 653 } 654 655 func TestUnmarshalWithStructAllOptionalWithEmpty(t *testing.T) { 656 var c struct { 657 Inner struct { 658 Optional string `json:",optional"` 659 } 660 Else string 661 } 662 content := []byte(`{"Else": "sure", "Inner": {}}`) 663 664 assert.Nil(t, UnmarshalJsonBytes(content, &c)) 665 } 666 667 func TestUnmarshalWithStructAllOptionalPtr(t *testing.T) { 668 var c struct { 669 Inner *struct { 670 Optional string `json:",optional"` 671 } 672 Else string 673 } 674 content := []byte(`{"Else": "sure", "Inner": {}}`) 675 676 assert.Nil(t, UnmarshalJsonBytes(content, &c)) 677 } 678 679 func TestUnmarshalWithStructOptional(t *testing.T) { 680 type Inner struct { 681 Must string 682 } 683 684 var c struct { 685 In Inner `json:",optional"` 686 Else string 687 } 688 content := []byte(`{"Else": "sure"}`) 689 690 assert.Nil(t, UnmarshalJsonBytes(content, &c)) 691 assert.Equal(t, "sure", c.Else) 692 assert.Equal(t, "", c.In.Must) 693 } 694 695 func TestUnmarshalWithStructPtrOptional(t *testing.T) { 696 type Inner struct { 697 Must string 698 } 699 700 var c struct { 701 In *Inner `json:",optional"` 702 Else string 703 } 704 content := []byte(`{"Else": "sure"}`) 705 706 assert.Nil(t, UnmarshalJsonBytes(content, &c)) 707 assert.Equal(t, "sure", c.Else) 708 assert.Nil(t, c.In) 709 } 710 711 func TestUnmarshalWithStructAllOptionalAnonymous(t *testing.T) { 712 type Inner struct { 713 Optional string `json:",optional"` 714 } 715 716 var c struct { 717 Inner 718 Else string 719 } 720 content := []byte(`{"Else": "sure"}`) 721 722 assert.Nil(t, UnmarshalJsonBytes(content, &c)) 723 } 724 725 func TestUnmarshalWithStructAllOptionalAnonymousPtr(t *testing.T) { 726 type Inner struct { 727 Optional string `json:",optional"` 728 } 729 730 var c struct { 731 *Inner 732 Else string 733 } 734 content := []byte(`{"Else": "sure"}`) 735 736 assert.Nil(t, UnmarshalJsonBytes(content, &c)) 737 } 738 739 func TestUnmarshalWithStructAllOptionalProvoidedAnonymous(t *testing.T) { 740 type Inner struct { 741 Optional string `json:",optional"` 742 } 743 744 var c struct { 745 Inner 746 Else string 747 } 748 content := []byte(`{"Else": "sure", "Optional": "optional"}`) 749 750 assert.Nil(t, UnmarshalJsonBytes(content, &c)) 751 assert.Equal(t, "sure", c.Else) 752 assert.Equal(t, "optional", c.Optional) 753 } 754 755 func TestUnmarshalWithStructAllOptionalProvoidedAnonymousPtr(t *testing.T) { 756 type Inner struct { 757 Optional string `json:",optional"` 758 } 759 760 var c struct { 761 *Inner 762 Else string 763 } 764 content := []byte(`{"Else": "sure", "Optional": "optional"}`) 765 766 assert.Nil(t, UnmarshalJsonBytes(content, &c)) 767 assert.Equal(t, "sure", c.Else) 768 assert.Equal(t, "optional", c.Optional) 769 } 770 771 func TestUnmarshalWithStructAnonymous(t *testing.T) { 772 type Inner struct { 773 Must string 774 } 775 776 var c struct { 777 Inner 778 Else string 779 } 780 content := []byte(`{"Else": "sure", "Must": "must"}`) 781 782 assert.Nil(t, UnmarshalJsonBytes(content, &c)) 783 assert.Equal(t, "sure", c.Else) 784 assert.Equal(t, "must", c.Must) 785 } 786 787 func TestUnmarshalWithStructAnonymousPtr(t *testing.T) { 788 type Inner struct { 789 Must string 790 } 791 792 var c struct { 793 *Inner 794 Else string 795 } 796 content := []byte(`{"Else": "sure", "Must": "must"}`) 797 798 assert.Nil(t, UnmarshalJsonBytes(content, &c)) 799 assert.Equal(t, "sure", c.Else) 800 assert.Equal(t, "must", c.Must) 801 } 802 803 func TestUnmarshalWithStructAnonymousOptional(t *testing.T) { 804 type Inner struct { 805 Must string 806 } 807 808 var c struct { 809 Inner `json:",optional"` 810 Else string 811 } 812 content := []byte(`{"Else": "sure"}`) 813 814 assert.Nil(t, UnmarshalJsonBytes(content, &c)) 815 assert.Equal(t, "sure", c.Else) 816 assert.Equal(t, "", c.Must) 817 } 818 819 func TestUnmarshalWithStructPtrAnonymousOptional(t *testing.T) { 820 type Inner struct { 821 Must string 822 } 823 824 var c struct { 825 *Inner `json:",optional"` 826 Else string 827 } 828 content := []byte(`{"Else": "sure"}`) 829 830 assert.Nil(t, UnmarshalJsonBytes(content, &c)) 831 assert.Equal(t, "sure", c.Else) 832 assert.Nil(t, c.Inner) 833 } 834 835 func TestUnmarshalWithZeroValues(t *testing.T) { 836 type inner struct { 837 False bool `json:"no"` 838 Int int `json:"int"` 839 String string `json:"string"` 840 } 841 content := []byte(`{"no": false, "int": 0, "string": ""}`) 842 reader := bytes.NewReader(content) 843 844 var in inner 845 ast := assert.New(t) 846 ast.Nil(UnmarshalJsonReader(reader, &in)) 847 ast.False(in.False) 848 ast.Equal(0, in.Int) 849 ast.Equal("", in.String) 850 } 851 852 func TestUnmarshalBytesError(t *testing.T) { 853 payload := `[{"abcd": "cdef"}]` 854 var v struct { 855 Any string 856 } 857 858 err := UnmarshalJsonBytes([]byte(payload), &v) 859 assert.NotNil(t, err) 860 assert.True(t, strings.Contains(err.Error(), payload)) 861 } 862 863 func TestUnmarshalReaderError(t *testing.T) { 864 payload := `[{"abcd": "cdef"}]` 865 reader := strings.NewReader(payload) 866 var v struct { 867 Any string 868 } 869 870 err := UnmarshalJsonReader(reader, &v) 871 assert.NotNil(t, err) 872 assert.True(t, strings.Contains(err.Error(), payload)) 873 } 874 875 func TestUnmarshalMap(t *testing.T) { 876 t.Run("nil map and valid", func(t *testing.T) { 877 var m map[string]interface{} 878 var v struct { 879 Any string `json:",optional"` 880 } 881 882 err := UnmarshalJsonMap(m, &v) 883 assert.Nil(t, err) 884 assert.True(t, len(v.Any) == 0) 885 }) 886 887 t.Run("empty map but not valid", func(t *testing.T) { 888 m := map[string]interface{}{} 889 var v struct { 890 Any string 891 } 892 893 err := UnmarshalJsonMap(m, &v) 894 assert.NotNil(t, err) 895 }) 896 897 t.Run("empty map and valid", func(t *testing.T) { 898 m := map[string]interface{}{} 899 var v struct { 900 Any string `json:",optional"` 901 } 902 903 err := UnmarshalJsonMap(m, &v) 904 assert.Nil(t, err) 905 assert.True(t, len(v.Any) == 0) 906 }) 907 908 t.Run("valid map", func(t *testing.T) { 909 m := map[string]interface{}{ 910 "Any": "foo", 911 } 912 var v struct { 913 Any string 914 } 915 916 err := UnmarshalJsonMap(m, &v) 917 assert.Nil(t, err) 918 assert.Equal(t, "foo", v.Any) 919 }) 920 }