|
4 | 4 | package mapify_test |
5 | 5 |
|
6 | 6 | import ( |
| 7 | + "fmt" |
| 8 | + "reflect" |
7 | 9 | "testing" |
8 | 10 |
|
9 | 11 | "github.com/elgopher/mapify" |
@@ -700,3 +702,138 @@ func assertStructField(t *testing.T, fieldName string, e mapify.Element) { |
700 | 702 | require.True(t, ok) |
701 | 703 | assert.Equal(t, fieldName, field.Name) |
702 | 704 | } |
| 705 | + |
| 706 | +func TestShouldConvert(t *testing.T) { |
| 707 | + t.Run("should not convert root element", func(t *testing.T) { |
| 708 | + mapper := mapify.Mapper{ |
| 709 | + ShouldConvert: func(path string, value reflect.Value) (bool, error) { |
| 710 | + if path == "" { |
| 711 | + return false, nil |
| 712 | + } |
| 713 | + |
| 714 | + return true, nil |
| 715 | + }, |
| 716 | + } |
| 717 | + |
| 718 | + tests := map[string]interface{}{ |
| 719 | + "struct": struct{ Field string }{Field: "v"}, |
| 720 | + "map": map[string]string{"key": "value"}, |
| 721 | + "slice": []struct{ Field string }{{Field: "v"}}, |
| 722 | + } |
| 723 | + |
| 724 | + for name, instance := range tests { |
| 725 | + t.Run(name, func(t *testing.T) { |
| 726 | + // when |
| 727 | + mapped, err := mapper.MapAny(instance) |
| 728 | + // then |
| 729 | + require.NoError(t, err) |
| 730 | + assert.Equal(t, instance, mapped) |
| 731 | + }) |
| 732 | + } |
| 733 | + }) |
| 734 | + |
| 735 | + t.Run("should not convert nested object", func(t *testing.T) { |
| 736 | + type nestedStruct struct{ Field string } |
| 737 | + |
| 738 | + nestedStructInstance := nestedStruct{Field: "a"} |
| 739 | + nestedMap := map[string]string{"Field": "a"} |
| 740 | + |
| 741 | + tests := map[string]struct { |
| 742 | + input, expected interface{} |
| 743 | + }{ |
| 744 | + "struct in a struct field": { |
| 745 | + input: struct{ Nested nestedStruct }{Nested: nestedStructInstance}, |
| 746 | + expected: map[string]interface{}{"Nested": nestedStructInstance}, |
| 747 | + }, |
| 748 | + "struct pointer in a struct field": { |
| 749 | + input: struct{ Nested *nestedStruct }{Nested: &nestedStructInstance}, |
| 750 | + expected: map[string]interface{}{"Nested": &nestedStructInstance}, |
| 751 | + }, |
| 752 | + "struct in a map key": { |
| 753 | + input: map[string]nestedStruct{"Nested": nestedStructInstance}, |
| 754 | + expected: map[string]interface{}{"Nested": nestedStructInstance}, |
| 755 | + }, |
| 756 | + "map in a struct field": { |
| 757 | + input: struct{ Nested map[string]string }{Nested: nestedMap}, |
| 758 | + expected: map[string]interface{}{"Nested": nestedMap}, |
| 759 | + }, |
| 760 | + "map in a map key": { |
| 761 | + input: map[string]map[string]string{"Nested": nestedMap}, |
| 762 | + expected: map[string]interface{}{"Nested": nestedMap}, |
| 763 | + }, |
| 764 | + "structs slice in a struct field": { |
| 765 | + input: struct{ Nested []nestedStruct }{Nested: []nestedStruct{nestedStructInstance}}, |
| 766 | + expected: map[string]interface{}{"Nested": []nestedStruct{nestedStructInstance}}, |
| 767 | + }, |
| 768 | + "structs slice in a map key": { |
| 769 | + input: map[string][]nestedStruct{"Nested": {nestedStructInstance}}, |
| 770 | + expected: map[string]interface{}{"Nested": []nestedStruct{nestedStructInstance}}, |
| 771 | + }, |
| 772 | + "map slice in a struct field": { |
| 773 | + input: struct{ Nested []map[string]string }{Nested: []map[string]string{nestedMap}}, |
| 774 | + expected: map[string]interface{}{"Nested": []map[string]string{nestedMap}}, |
| 775 | + }, |
| 776 | + "2d structs slice in a map key": { |
| 777 | + input: map[string][][]nestedStruct{"Nested": {{nestedStructInstance}}}, |
| 778 | + expected: map[string]interface{}{"Nested": [][]nestedStruct{{nestedStructInstance}}}, |
| 779 | + }, |
| 780 | + } |
| 781 | + |
| 782 | + mapper := mapify.Mapper{ |
| 783 | + ShouldConvert: func(path string, value reflect.Value) (bool, error) { |
| 784 | + if path == ".Nested" { |
| 785 | + return false, nil |
| 786 | + } |
| 787 | + |
| 788 | + return true, nil |
| 789 | + }, |
| 790 | + } |
| 791 | + |
| 792 | + for name, test := range tests { |
| 793 | + t.Run(name, func(t *testing.T) { |
| 794 | + // when |
| 795 | + mapped, err := mapper.MapAny(test.input) |
| 796 | + // then |
| 797 | + require.NoError(t, err) |
| 798 | + assert.Equal(t, test.expected, mapped) |
| 799 | + }) |
| 800 | + } |
| 801 | + }) |
| 802 | + |
| 803 | + t.Run("should not run ShouldConvert for inconvertible values", func(t *testing.T) { |
| 804 | + mapper := mapify.Mapper{ |
| 805 | + ShouldConvert: func(path string, value reflect.Value) (bool, error) { |
| 806 | + panic("ShouldConvert run but should not") |
| 807 | + }, |
| 808 | + } |
| 809 | + |
| 810 | + tests := []interface{}{nil, 1, "str", map[int]interface{}{}, []int{1}} |
| 811 | + |
| 812 | + for _, test := range tests { |
| 813 | + name := fmt.Sprintf("%+v", test) |
| 814 | + |
| 815 | + t.Run(name, func(t *testing.T) { |
| 816 | + assert.NotPanics(t, func() { |
| 817 | + _, _ = mapper.MapAny(test) |
| 818 | + }) |
| 819 | + }) |
| 820 | + } |
| 821 | + }) |
| 822 | + |
| 823 | + t.Run("should run ShouldConvert only once for pointer to struct", func(t *testing.T) { |
| 824 | + s := &struct{ Field string }{Field: "v"} |
| 825 | + |
| 826 | + timesRun := 0 |
| 827 | + mapper := mapify.Mapper{ |
| 828 | + ShouldConvert: func(path string, value reflect.Value) (bool, error) { |
| 829 | + assert.Equal(t, s, value.Interface()) |
| 830 | + timesRun++ |
| 831 | + return true, nil |
| 832 | + }, |
| 833 | + } |
| 834 | + |
| 835 | + _, err := mapper.MapAny(s) |
| 836 | + require.NoError(t, err) |
| 837 | + assert.Equal(t, 1, timesRun, "ShouldConvert must be executed only once") |
| 838 | + }) |
| 839 | +} |
0 commit comments