github.com/gogf/gf/v2@v2.7.4/util/gconv/gconv_z_unit_map_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 package gconv_test 8 9 import ( 10 "fmt" 11 "reflect" 12 "testing" 13 14 "github.com/gogf/gf/v2/frame/g" 15 "github.com/gogf/gf/v2/test/gtest" 16 "github.com/gogf/gf/v2/util/gconv" 17 ) 18 19 type SubMapTest struct { 20 Name string 21 } 22 23 var mapTests = []struct { 24 value interface{} 25 expect interface{} 26 }{ 27 {map[string]int{"k1": 1}, map[string]interface{}{"k1": 1}}, 28 {map[string]uint{"k1": 1}, map[string]interface{}{"k1": 1}}, 29 {map[string]string{"k1": "v1"}, map[string]interface{}{"k1": "v1"}}, 30 {map[string]float32{"k1": 1.1}, map[string]interface{}{"k1": 1.1}}, 31 {map[string]float64{"k1": 1.1}, map[string]interface{}{"k1": 1.1}}, 32 {map[string]bool{"k1": true}, map[string]interface{}{"k1": true}}, 33 {map[string]interface{}{"k1": "v1"}, map[string]interface{}{"k1": "v1"}}, 34 35 {map[interface{}]int{"k1": 1}, map[string]interface{}{"k1": 1}}, 36 {map[interface{}]uint{"k1": 1}, map[string]interface{}{"k1": 1}}, 37 {map[interface{}]string{"k1": "v1"}, map[string]interface{}{"k1": "v1"}}, 38 {map[interface{}]float32{"k1": 1.1}, map[string]interface{}{"k1": 1.1}}, 39 {map[interface{}]float64{"k1": 1.1}, map[string]interface{}{"k1": 1.1}}, 40 {map[interface{}]bool{"k1": true}, map[string]interface{}{"k1": true}}, 41 {map[interface{}]interface{}{"k1": "v1"}, map[string]interface{}{"k1": "v1"}}, 42 43 {map[int]int{1: 1}, map[string]interface{}{"1": 1}}, 44 {map[int]string{1: "v1"}, map[string]interface{}{"1": "v1"}}, 45 {map[uint]int{1: 1}, map[string]interface{}{"1": 1}}, 46 {map[uint]string{1: "v1"}, map[string]interface{}{"1": "v1"}}, 47 48 {[]int{1, 2, 3}, map[string]interface{}{"1": 2, "3": nil}}, 49 {[]int{1, 2, 3, 4}, map[string]interface{}{"1": 2, "3": 4}}, 50 51 {`{"earth": "亚马逊雨林"}`, 52 map[string]interface{}{"earth": "亚马逊雨林"}}, 53 {[]byte(`{"earth": "撒哈拉沙漠"}`), 54 map[string]interface{}{"earth": "撒哈拉沙漠"}}, 55 {`{Earth}`, nil}, 56 57 {"", nil}, 58 {[]byte(""), nil}, 59 {`"{earth亚马逊雨林}`, nil}, 60 {[]byte(`{earth撒哈拉沙漠}`), nil}, 61 {[]byte(`{Earth}`), nil}, 62 63 {nil, nil}, 64 65 {&struct { 66 Earth string 67 }{ 68 Earth: "大峡谷", 69 }, map[string]interface{}{"Earth": "大峡谷"}}, 70 71 {struct { 72 Earth string 73 }{ 74 Earth: "马里亚纳海沟", 75 }, map[string]interface{}{"Earth": "马里亚纳海沟"}}, 76 77 {struct { 78 Earth string 79 mars string 80 }{ 81 Earth: "大堡礁", 82 mars: "奥林帕斯山", 83 }, map[string]interface{}{"Earth": "大堡礁"}}, 84 85 {struct { 86 Earth string 87 SubMapTest 88 }{ 89 Earth: "中国", 90 SubMapTest: SubMapTest{ 91 Name: "长江", 92 }, 93 }, map[string]interface{}{"Earth": "中国", "Name": "长江"}}, 94 95 {struct { 96 Earth string 97 China SubMapTest 98 }{ 99 Earth: "中国", 100 China: SubMapTest{ 101 Name: "黄河", 102 }, 103 }, map[string]interface{}{"Earth": "中国", "China": map[string]interface{}{"Name": "黄河"}}}, 104 105 {struct { 106 Earth string 107 SubMapTest `json:"sub_map_test"` 108 }{ 109 Earth: "中国", 110 SubMapTest: SubMapTest{ 111 Name: "淮河", 112 }, 113 }, map[string]interface{}{"Earth": "中国", "sub_map_test": map[string]interface{}{"Name": "淮河"}}}, 114 115 {struct { 116 Earth string 117 China SubMapTest `gconv:"中国"` 118 }{ 119 Earth: "中国", 120 China: SubMapTest{ 121 Name: "黄河", 122 }, 123 }, map[string]interface{}{"Earth": "中国", "中国": map[string]interface{}{"Name": "黄河"}}}, 124 125 {struct { 126 China string `c:"中国"` 127 America string `c:"-"` 128 UnitedKingdom string `c:"UK,omitempty"` 129 }{ 130 China: "长城", 131 America: "Statue of Liberty", 132 UnitedKingdom: "", 133 }, map[string]interface{}{"中国": "长城", "UK": ""}}, 134 135 {struct { 136 China string `gconv:"中国"` 137 America string `gconv:"-"` 138 UnitedKingdom string `c:"UK,omitempty"` 139 }{ 140 China: "故宫", 141 America: "White House", 142 UnitedKingdom: "", 143 }, map[string]interface{}{"中国": "故宫", "UK": ""}}, 144 145 {struct { 146 China string `json:"中国"` 147 America string `json:"-"` 148 UnitedKingdom string `json:"UK,omitempty"` 149 }{ 150 China: "东方明珠", 151 America: "Empire State Building", 152 UnitedKingdom: "", 153 }, map[string]interface{}{"中国": "东方明珠", "UK": ""}}, 154 155 {struct { 156 China interface{} `json:",omitempty"` 157 America string `json:",omitempty"` 158 }{ 159 China: "黄山", 160 America: "", 161 }, map[string]interface{}{"China": "黄山", "America": ""}}, 162 } 163 164 func TestMap(t *testing.T) { 165 gtest.C(t, func(t *gtest.T) { 166 for _, test := range mapTests { 167 t.Assert(gconv.Map(test.value), test.expect) 168 } 169 }) 170 } 171 172 func TestMaps(t *testing.T) { 173 gtest.C(t, func(t *gtest.T) { 174 for _, test := range mapTests { 175 var ( 176 maps interface{} 177 expects interface{} 178 ) 179 180 if v, ok := test.value.(string); ok { 181 maps = fmt.Sprintf(`[%s,%s]`, v, v) 182 } else if v, ok := test.value.([]byte); ok { 183 maps = []byte(fmt.Sprintf(`[%s,%s]`, v, v)) 184 } else if test.value == nil { 185 maps = nil 186 } else { 187 maps = []interface{}{ 188 test.value, 189 test.value, 190 } 191 } 192 193 if test.expect == nil { 194 expects = test.expect 195 } else { 196 expects = []interface{}{ 197 test.expect, 198 test.expect, 199 } 200 } 201 t.Assert(gconv.Maps(maps), expects) 202 203 // The following is the same as gconv.Maps. 204 t.Assert(gconv.MapsDeep(maps), expects) 205 t.Assert(gconv.SliceMap(maps), expects) 206 t.Assert(gconv.SliceMapDeep(maps), expects) 207 } 208 }) 209 210 // Test for special types. 211 gtest.C(t, func(t *gtest.T) { 212 mapStrAny := []map[string]interface{}{ 213 {"earth": "亚马逊雨林"}, 214 {"mars": "奥林帕斯山"}, 215 } 216 t.Assert(gconv.Maps(mapStrAny), mapStrAny) 217 218 mapEmpty := []map[string]string{} 219 t.AssertNil(gconv.Maps(mapEmpty)) 220 221 t.Assert(gconv.Maps(`test`), nil) 222 t.Assert(gconv.Maps([]byte(`test`)), nil) 223 }) 224 } 225 226 func TestMapsDeepExtra(t *testing.T) { 227 gtest.C(t, func(t *gtest.T) { 228 type s struct { 229 Earth g.Map `c:"earth_map"` 230 } 231 232 t.Assert(gconv.MapDeep(&s{ 233 Earth: g.Map{ 234 "sea_num": 4, 235 "one_sea": g.Map{ 236 "sea_name": "太平洋", 237 }, 238 "map_sat": g.MapAnyAny{ 239 1: "Arctic", 240 "Pacific": 2, 241 "Indian": "印度洋", 242 }, 243 }, 244 }), g.Map{ 245 "earth_map": g.Map{ 246 "sea_num": 4, 247 "one_sea": g.Map{ 248 "sea_name": "太平洋", 249 }, 250 "map_sat": g.Map{ 251 "1": "Arctic", 252 "Pacific": 2, 253 "Indian": "印度洋", 254 }, 255 }, 256 }) 257 }) 258 259 gtest.C(t, func(t *gtest.T) { 260 t.Assert(gconv.MapsDeep(`test`), nil) 261 t.Assert(gconv.MapsDeep([]byte(`test`)), nil) 262 }) 263 } 264 265 func TestMapStrStr(t *testing.T) { 266 gtest.C(t, func(t *gtest.T) { 267 for _, test := range mapTests { 268 var expect map[string]interface{} 269 if v, ok := test.expect.(map[string]interface{}); ok { 270 expect = v 271 } 272 for k, v := range expect { 273 expect[k] = gconv.String(v) 274 } 275 t.Assert(gconv.MapStrStr(test.value), test.expect) 276 } 277 }) 278 } 279 280 func TestMapStrStrDeepExtra(t *testing.T) { 281 gtest.C(t, func(t *gtest.T) { 282 t.Assert(gconv.MapStrStrDeep(map[string]string{"mars": "Syrtis"}), map[string]string{"mars": "Syrtis"}) 283 t.Assert(gconv.MapStrStrDeep(`{}`), nil) 284 }) 285 } 286 287 func TestMapWithMapOption(t *testing.T) { 288 // Test for option: Deep. 289 gtest.C(t, func(t *gtest.T) { 290 var testMapDeep = struct { 291 Earth string 292 SubMapTest SubMapTest 293 }{ 294 Earth: "中国", 295 SubMapTest: SubMapTest{ 296 Name: "黄山", 297 }, 298 } 299 var ( 300 dt = gconv.Map(testMapDeep, gconv.MapOption{Deep: true}) 301 df = gconv.Map(testMapDeep, gconv.MapOption{Deep: false}) 302 dtk = reflect.TypeOf(dt["SubMapTest"]).Kind() 303 dfk = reflect.TypeOf(df["SubMapTest"]).Kind() 304 ) 305 t.AssertNE(dtk, dfk) 306 }) 307 308 // Test for option: OmitEmpty. 309 gtest.C(t, func(t *gtest.T) { 310 var testMapOmitEmpty = struct { 311 Earth string 312 Venus int `gconv:",omitempty"` 313 Mars string `c:",omitempty"` 314 Mercury interface{} `json:",omitempty"` 315 }{ 316 Earth: "死海", 317 Venus: 0, 318 Mars: "", 319 Mercury: nil, 320 } 321 r := gconv.Map(testMapOmitEmpty, gconv.MapOption{OmitEmpty: true}) 322 t.Assert(r, map[string]interface{}{"Earth": "死海"}) 323 }) 324 325 // Test for option: Tags. 326 gtest.C(t, func(t *gtest.T) { 327 var testMapOmitEmpty = struct { 328 Earth string `gconv:"errEarth" chinese:"地球" french:"Terre"` 329 }{ 330 Earth: "尼莫点", 331 } 332 c := gconv.Map(testMapOmitEmpty, gconv.MapOption{Tags: []string{"chinese", "french"}}) 333 t.Assert(c, map[string]interface{}{"地球": "尼莫点"}) 334 335 f := gconv.Map(testMapOmitEmpty, gconv.MapOption{Tags: []string{"french", "chinese"}}) 336 t.Assert(f, map[string]interface{}{"Terre": "尼莫点"}) 337 }) 338 } 339 340 func TestMapToMapExtra(t *testing.T) { 341 gtest.C(t, func(t *gtest.T) { 342 var ( 343 err error 344 value = map[string]string{"k1": "v1"} 345 expect = make(map[string]interface{}) 346 ) 347 err = gconv.MapToMap(value, &expect) 348 t.Assert(err, nil) 349 t.Assert(value["k1"], expect["k1"]) 350 }) 351 352 gtest.C(t, func(t *gtest.T) { 353 v := g.Map{ 354 "k": g.Map{ 355 "name": "Earth", 356 }, 357 } 358 e := make(map[string]SubMapTest) 359 err := gconv.MapToMap(v, &e) 360 t.AssertNil(err) 361 t.Assert(len(e), 1) 362 t.Assert(e["k"].Name, "Earth") 363 }) 364 } 365 366 func TestMaptoMapsExtra(t *testing.T) { 367 gtest.C(t, func(t *gtest.T) { 368 v := g.Slice{ 369 g.Map{"id": 1, "name": "john"}, 370 g.Map{"id": 2, "name": "smith"}, 371 } 372 var e []*g.Map 373 err := gconv.MapToMaps(v, &e) 374 t.AssertNil(err) 375 t.Assert(len(v), 2) 376 t.Assert(v, e) 377 }) 378 }