github.com/wangyougui/gf/v2@v2.6.5/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/wangyougui/gf. 6 7 package gconv_test 8 9 import ( 10 "testing" 11 12 "github.com/wangyougui/gf/v2/frame/g" 13 "github.com/wangyougui/gf/v2/test/gtest" 14 "github.com/wangyougui/gf/v2/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 // string -> map[string]interface{} 71 gtest.C(t, func(t *gtest.T) { 72 jsonStr := `{"id":100, "name":"john"}` 73 74 m1 := g.MapStrAny{} 75 t.Assert(gconv.MapToMap(jsonStr, &m1), nil) 76 t.Assert(m1["id"], 100) 77 78 m2 := g.MapStrAny{} 79 t.Assert(gconv.MapToMap([]byte(jsonStr), &m2), nil) 80 t.Assert(m2["id"], 100) 81 }) 82 } 83 84 func Test_MapToMap2(t *testing.T) { 85 type User struct { 86 Id int 87 Name string 88 } 89 params := g.Map{ 90 "key": g.Map{ 91 "id": 1, 92 "name": "john", 93 }, 94 } 95 gtest.C(t, func(t *gtest.T) { 96 m := make(map[string]User) 97 err := gconv.MapToMap(params, &m) 98 t.AssertNil(err) 99 t.Assert(len(m), 1) 100 t.Assert(m["key"].Id, 1) 101 t.Assert(m["key"].Name, "john") 102 }) 103 gtest.C(t, func(t *gtest.T) { 104 m := (map[string]User)(nil) 105 err := gconv.MapToMap(params, &m) 106 t.AssertNil(err) 107 t.Assert(len(m), 1) 108 t.Assert(m["key"].Id, 1) 109 t.Assert(m["key"].Name, "john") 110 }) 111 gtest.C(t, func(t *gtest.T) { 112 m := make(map[string]*User) 113 err := gconv.MapToMap(params, &m) 114 t.AssertNil(err) 115 t.Assert(len(m), 1) 116 t.Assert(m["key"].Id, 1) 117 t.Assert(m["key"].Name, "john") 118 }) 119 gtest.C(t, func(t *gtest.T) { 120 m := (map[string]*User)(nil) 121 err := gconv.MapToMap(params, &m) 122 t.AssertNil(err) 123 t.Assert(len(m), 1) 124 t.Assert(m["key"].Id, 1) 125 t.Assert(m["key"].Name, "john") 126 }) 127 } 128 129 func Test_MapToMapDeep(t *testing.T) { 130 type Ids struct { 131 Id int 132 Uid int 133 } 134 type Base struct { 135 Ids 136 Time string 137 } 138 type User struct { 139 Base 140 Name string 141 } 142 params := g.Map{ 143 "key": g.Map{ 144 "id": 1, 145 "name": "john", 146 }, 147 } 148 gtest.C(t, func(t *gtest.T) { 149 m := (map[string]*User)(nil) 150 err := gconv.MapToMap(params, &m) 151 t.AssertNil(err) 152 t.Assert(len(m), 1) 153 t.Assert(m["key"].Id, 1) 154 t.Assert(m["key"].Name, "john") 155 }) 156 } 157 158 func Test_MapToMaps(t *testing.T) { 159 params := g.Slice{ 160 g.Map{"id": 1, "name": "john"}, 161 g.Map{"id": 2, "name": "smith"}, 162 } 163 gtest.C(t, func(t *gtest.T) { 164 var s []g.Map 165 err := gconv.MapToMaps(params, &s) 166 t.AssertNil(err) 167 t.Assert(len(s), 2) 168 t.Assert(s, params) 169 }) 170 gtest.C(t, func(t *gtest.T) { 171 var s []*g.Map 172 err := gconv.MapToMaps(params, &s) 173 t.AssertNil(err) 174 t.Assert(len(s), 2) 175 t.Assert(s, params) 176 }) 177 gtest.C(t, func(t *gtest.T) { 178 jsonStr := `[{"id":100, "name":"john"},{"id":200, "name":"smith"}]` 179 180 var m1 []g.Map 181 t.Assert(gconv.MapToMaps(jsonStr, &m1), nil) 182 t.Assert(m1[0]["id"], 100) 183 t.Assert(m1[1]["id"], 200) 184 185 t.Assert(gconv.MapToMaps([]byte(jsonStr), &m1), nil) 186 t.Assert(m1[0]["id"], 100) 187 t.Assert(m1[1]["id"], 200) 188 }) 189 } 190 191 func Test_MapToMaps_StructParams(t *testing.T) { 192 type User struct { 193 Id int 194 Name string 195 } 196 params := g.Slice{ 197 User{1, "name1"}, 198 User{2, "name2"}, 199 } 200 gtest.C(t, func(t *gtest.T) { 201 var s []g.Map 202 err := gconv.MapToMaps(params, &s) 203 t.AssertNil(err) 204 t.Assert(len(s), 2) 205 }) 206 gtest.C(t, func(t *gtest.T) { 207 var s []*g.Map 208 err := gconv.MapToMaps(params, &s) 209 t.AssertNil(err) 210 t.Assert(len(s), 2) 211 }) 212 }