[mirror] convert json to go types olexsmir.xyz/json2go
at main 3.7 kB view raw
1package json2go 2 3import "testing" 4 5func TestTransformer_GetGoType(t *testing.T) { 6 tests := map[string]struct { 7 value any 8 fieldName string 9 output string 10 }{ 11 "struct": { 12 value: map[string]any{ 13 "username": "user-ovich", 14 "age": float64(20), 15 }, 16 output: "struct {" + 17 field(1, "Age", "int") + 18 field(1, "Username", "string") + 19 "\n}", 20 }, 21 "empty slice": { 22 value: make([]any, 0), 23 output: "[]any", 24 }, 25 "slice of ints": { 26 value: []any{float64(3), float64(123)}, 27 output: "[]int", 28 }, 29 "slice of floats": { 30 value: []any{float64(3.4), float64(123.3)}, 31 output: "[]float64", 32 }, 33 "slice of strings": { 34 value: []any{"asdf", "jalkjsd"}, 35 output: "[]string", 36 }, 37 "slice of bool": { 38 value: []any{false, true, false}, 39 output: "[]bool", 40 }, 41 "int": { 42 value: float64(1233), 43 output: "int", 44 }, 45 "float64": { 46 value: float64(1233.23), 47 output: "float64", 48 }, 49 "bool": { 50 value: false, 51 output: "bool", 52 }, 53 "any": { 54 value: nil, 55 output: "any", 56 }, 57 } 58 59 trans := NewTransformer() 60 for tname, tt := range tests { 61 t.Run(tname, func(t *testing.T) { 62 t.Parallel() 63 64 fieldName := "field" 65 if tt.fieldName != "" { 66 fieldName = tt.fieldName 67 } 68 69 res := trans.getGoType(fieldName, tt.value) 70 assertEqual(t, tt.output, res) 71 }) 72 } 73} 74 75func TestTransformer_buildStruct(t *testing.T) { 76 tests := map[string]struct { 77 input map[string]any 78 output string 79 }{ 80 "simple struct": { 81 input: map[string]any{ 82 // only one value, because of the inconsistent ordering of maps 83 "active": true, 84 }, 85 output: "struct {" + 86 field(1, "Active", "bool", "active") + 87 "\n}", 88 }, 89 "with no named field": { 90 input: map[string]any{"": "user"}, 91 output: "struct {" + 92 field(1, "NotNamedField", "string", "NotNamedField") + 93 "\n}", 94 }, 95 } 96 97 trans := NewTransformer() 98 for tname, tt := range tests { 99 t.Run(tname, func(t *testing.T) { 100 t.Parallel() 101 102 res := trans.buildStruct(tt.input) 103 assertEqual(t, tt.output, res) 104 }) 105 } 106} 107 108func TestTransformer_getTypeAnnotation(t *testing.T) { 109 c := "type Typeich " 110 tests := map[string]struct { 111 input any 112 output string 113 }{ 114 "struct": { 115 input: map[string]any{"field": false}, 116 output: c + "struct {" + 117 field(1, "Field", "bool") + "\n}", 118 }, 119 "slice": { 120 input: []any{"asdf", "jkl;"}, 121 output: c + "[]string", 122 }, 123 "empty slice": { 124 input: make([]any, 0), 125 output: c + "[]any", 126 }, 127 "string": { 128 input: "asdf", 129 output: c + "string", 130 }, 131 "int": { 132 input: float64(123), 133 output: c + "int", 134 }, 135 "float64": { 136 input: float64(123.69), 137 output: c + "float64", 138 }, 139 "bool": { 140 input: true, 141 output: c + "bool", 142 }, 143 "any": { 144 input: nil, 145 output: c + "any", 146 }, 147 } 148 149 trans := NewTransformer() 150 for tname, tt := range tests { 151 t.Run(tname, func(t *testing.T) { 152 t.Parallel() 153 154 res := trans.getTypeAnnotation("Typeich", tt.input) 155 assertEqual(t, tt.output, res) 156 }) 157 } 158} 159 160func TestTransformer_toGoFieldName(t *testing.T) { 161 tests := map[string]string{ 162 "input": "Input", 163 "Input": "Input", 164 "long_name": "LongName", 165 "a_lot_of_____": "ALotOf", 166 "__name": "Name", 167 } 168 169 trans := NewTransformer() 170 for input, output := range tests { 171 t.Run(input, func(t *testing.T) { 172 t.Parallel() 173 174 res := trans.toGoFieldName(input) 175 assertEqual(t, output, res) 176 }) 177 } 178} 179 180func TestMapToStructInput(t *testing.T) { 181 inp := map[string]any{ 182 "field1": nil, 183 "field2": true, 184 "a": 123, 185 "user": map[string]any{}, 186 } 187 188 assertEqual(t, mapToStructInput(inp), []structInput{ 189 {"a", 123}, 190 {"field1", nil}, 191 {"field2", true}, 192 {"user", map[string]any{}}, 193 }) 194}