github.com/gogf/gf@v1.16.9/util/gconv/gconv_z_unit_maptomap_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 "testing" 11 12 "github.com/gogf/gf/frame/g" 13 "github.com/gogf/gf/test/gtest" 14 "github.com/gogf/gf/util/gconv" 15 ) 16 17 func Test_MapToMap1(t *testing.T) { 18 // map[int]int -> map[string]string 19 // empty original map. 20 gtest.C(t, func(t *gtest.T) { 21 m1 := g.MapIntInt{} 22 m2 := g.MapStrStr{} 23 t.Assert(gconv.MapToMap(m1, &m2), nil) 24 t.Assert(len(m1), len(m2)) 25 }) 26 // map[int]int -> map[string]string 27 gtest.C(t, func(t *gtest.T) { 28 m1 := g.MapIntInt{ 29 1: 100, 30 2: 200, 31 } 32 m2 := g.MapStrStr{} 33 t.Assert(gconv.MapToMap(m1, &m2), nil) 34 t.Assert(m2["1"], m1[1]) 35 t.Assert(m2["2"], m1[2]) 36 }) 37 // map[string]interface{} -> map[string]string 38 gtest.C(t, func(t *gtest.T) { 39 m1 := g.Map{ 40 "k1": "v1", 41 "k2": "v2", 42 } 43 m2 := g.MapStrStr{} 44 t.Assert(gconv.MapToMap(m1, &m2), nil) 45 t.Assert(m2["k1"], m1["k1"]) 46 t.Assert(m2["k2"], m1["k2"]) 47 }) 48 // map[string]string -> map[string]interface{} 49 gtest.C(t, func(t *gtest.T) { 50 m1 := g.MapStrStr{ 51 "k1": "v1", 52 "k2": "v2", 53 } 54 m2 := g.Map{} 55 t.Assert(gconv.MapToMap(m1, &m2), nil) 56 t.Assert(m2["k1"], m1["k1"]) 57 t.Assert(m2["k2"], m1["k2"]) 58 }) 59 // map[string]interface{} -> map[interface{}]interface{} 60 gtest.C(t, func(t *gtest.T) { 61 m1 := g.MapStrStr{ 62 "k1": "v1", 63 "k2": "v2", 64 } 65 m2 := g.MapAnyAny{} 66 t.Assert(gconv.MapToMap(m1, &m2), nil) 67 t.Assert(m2["k1"], m1["k1"]) 68 t.Assert(m2["k2"], m1["k2"]) 69 }) 70 } 71 72 func Test_MapToMap2(t *testing.T) { 73 type User struct { 74 Id int 75 Name string 76 } 77 params := g.Map{ 78 "key": g.Map{ 79 "id": 1, 80 "name": "john", 81 }, 82 } 83 gtest.C(t, func(t *gtest.T) { 84 m := make(map[string]User) 85 err := gconv.MapToMap(params, &m) 86 t.AssertNil(err) 87 t.Assert(len(m), 1) 88 t.Assert(m["key"].Id, 1) 89 t.Assert(m["key"].Name, "john") 90 }) 91 gtest.C(t, func(t *gtest.T) { 92 m := (map[string]User)(nil) 93 err := gconv.MapToMap(params, &m) 94 t.Assert(err, nil) 95 t.Assert(len(m), 1) 96 t.Assert(m["key"].Id, 1) 97 t.Assert(m["key"].Name, "john") 98 }) 99 gtest.C(t, func(t *gtest.T) { 100 m := make(map[string]*User) 101 err := gconv.MapToMap(params, &m) 102 t.Assert(err, nil) 103 t.Assert(len(m), 1) 104 t.Assert(m["key"].Id, 1) 105 t.Assert(m["key"].Name, "john") 106 }) 107 gtest.C(t, func(t *gtest.T) { 108 m := (map[string]*User)(nil) 109 err := gconv.MapToMap(params, &m) 110 t.Assert(err, nil) 111 t.Assert(len(m), 1) 112 t.Assert(m["key"].Id, 1) 113 t.Assert(m["key"].Name, "john") 114 }) 115 } 116 117 func Test_MapToMapDeep(t *testing.T) { 118 type Ids struct { 119 Id int 120 Uid int 121 } 122 type Base struct { 123 Ids 124 Time string 125 } 126 type User struct { 127 Base 128 Name string 129 } 130 params := g.Map{ 131 "key": g.Map{ 132 "id": 1, 133 "name": "john", 134 }, 135 } 136 gtest.C(t, func(t *gtest.T) { 137 m := (map[string]*User)(nil) 138 err := gconv.MapToMap(params, &m) 139 t.Assert(err, nil) 140 t.Assert(len(m), 1) 141 t.Assert(m["key"].Id, 1) 142 t.Assert(m["key"].Name, "john") 143 }) 144 } 145 146 func Test_MapToMaps(t *testing.T) { 147 params := g.Slice{ 148 g.Map{"id": 1, "name": "john"}, 149 g.Map{"id": 2, "name": "smith"}, 150 } 151 gtest.C(t, func(t *gtest.T) { 152 var s []g.Map 153 err := gconv.MapToMaps(params, &s) 154 t.AssertNil(err) 155 t.Assert(len(s), 2) 156 t.Assert(s, params) 157 }) 158 gtest.C(t, func(t *gtest.T) { 159 var s []*g.Map 160 err := gconv.MapToMaps(params, &s) 161 t.AssertNil(err) 162 t.Assert(len(s), 2) 163 t.Assert(s, params) 164 }) 165 } 166 167 func Test_MapToMaps_StructParams(t *testing.T) { 168 type User struct { 169 Id int 170 Name string 171 } 172 params := g.Slice{ 173 User{1, "name1"}, 174 User{2, "name2"}, 175 } 176 gtest.C(t, func(t *gtest.T) { 177 var s []g.Map 178 err := gconv.MapToMaps(params, &s) 179 t.AssertNil(err) 180 t.Assert(len(s), 2) 181 }) 182 gtest.C(t, func(t *gtest.T) { 183 var s []*g.Map 184 err := gconv.MapToMaps(params, &s) 185 t.AssertNil(err) 186 t.Assert(len(s), 2) 187 }) 188 }