github.com/wangyougui/gf/v2@v2.6.5/util/gconv/gconv_z_unit_structs_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_Structs_WithTag(t *testing.T) { 18 type User struct { 19 Uid int `json:"id"` 20 NickName string `json:"name"` 21 } 22 gtest.C(t, func(t *gtest.T) { 23 var users []User 24 params := g.Slice{ 25 g.Map{ 26 "id": 1, 27 "name": "name1", 28 }, 29 g.Map{ 30 "id": 2, 31 "name": "name2", 32 }, 33 } 34 err := gconv.Structs(params, &users) 35 t.AssertNil(err) 36 t.Assert(len(users), 2) 37 t.Assert(users[0].Uid, 1) 38 t.Assert(users[0].NickName, "name1") 39 t.Assert(users[1].Uid, 2) 40 t.Assert(users[1].NickName, "name2") 41 }) 42 gtest.C(t, func(t *gtest.T) { 43 var users []*User 44 params := g.Slice{ 45 g.Map{ 46 "id": 1, 47 "name": "name1", 48 }, 49 g.Map{ 50 "id": 2, 51 "name": "name2", 52 }, 53 } 54 err := gconv.Structs(params, &users) 55 t.AssertNil(err) 56 t.Assert(len(users), 2) 57 t.Assert(users[0].Uid, 1) 58 t.Assert(users[0].NickName, "name1") 59 t.Assert(users[1].Uid, 2) 60 t.Assert(users[1].NickName, "name2") 61 }) 62 } 63 64 func Test_Structs_WithoutTag(t *testing.T) { 65 type User struct { 66 Uid int 67 NickName string 68 } 69 gtest.C(t, func(t *gtest.T) { 70 var users []User 71 params := g.Slice{ 72 g.Map{ 73 "uid": 1, 74 "nick-name": "name1", 75 }, 76 g.Map{ 77 "uid": 2, 78 "nick-name": "name2", 79 }, 80 } 81 err := gconv.Structs(params, &users) 82 t.AssertNil(err) 83 t.Assert(len(users), 2) 84 t.Assert(users[0].Uid, 1) 85 t.Assert(users[0].NickName, "name1") 86 t.Assert(users[1].Uid, 2) 87 t.Assert(users[1].NickName, "name2") 88 }) 89 gtest.C(t, func(t *gtest.T) { 90 var users []*User 91 params := g.Slice{ 92 g.Map{ 93 "uid": 1, 94 "nick-name": "name1", 95 }, 96 g.Map{ 97 "uid": 2, 98 "nick-name": "name2", 99 }, 100 } 101 err := gconv.Structs(params, &users) 102 t.AssertNil(err) 103 t.Assert(len(users), 2) 104 t.Assert(users[0].Uid, 1) 105 t.Assert(users[0].NickName, "name1") 106 t.Assert(users[1].Uid, 2) 107 t.Assert(users[1].NickName, "name2") 108 }) 109 } 110 111 func Test_Structs_SliceParameter(t *testing.T) { 112 gtest.C(t, func(t *gtest.T) { 113 type User struct { 114 Uid int 115 NickName string 116 } 117 var users []User 118 params := g.Slice{ 119 g.Map{ 120 "uid": 1, 121 "nick-name": "name1", 122 }, 123 g.Map{ 124 "uid": 2, 125 "nick-name": "name2", 126 }, 127 } 128 err := gconv.Structs(params, users) 129 t.AssertNE(err, nil) 130 }) 131 gtest.C(t, func(t *gtest.T) { 132 type User struct { 133 Uid int 134 NickName string 135 } 136 type A struct { 137 Users []User 138 } 139 var a A 140 params := g.Slice{ 141 g.Map{ 142 "uid": 1, 143 "nick-name": "name1", 144 }, 145 g.Map{ 146 "uid": 2, 147 "nick-name": "name2", 148 }, 149 } 150 err := gconv.Structs(params, a.Users) 151 t.AssertNE(err, nil) 152 }) 153 } 154 155 func Test_Structs_DirectReflectSet(t *testing.T) { 156 type A struct { 157 Id int 158 Name string 159 } 160 gtest.C(t, func(t *gtest.T) { 161 var ( 162 a = []*A{ 163 {Id: 1, Name: "john"}, 164 {Id: 2, Name: "smith"}, 165 } 166 b []*A 167 ) 168 err := gconv.Structs(a, &b) 169 t.AssertNil(err) 170 t.AssertEQ(a, b) 171 }) 172 gtest.C(t, func(t *gtest.T) { 173 var ( 174 a = []A{ 175 {Id: 1, Name: "john"}, 176 {Id: 2, Name: "smith"}, 177 } 178 b []A 179 ) 180 err := gconv.Structs(a, &b) 181 t.AssertNil(err) 182 t.AssertEQ(a, b) 183 }) 184 } 185 186 func Test_Structs_IntSliceAttribute(t *testing.T) { 187 type A struct { 188 Id []int 189 } 190 type B struct { 191 *A 192 Name string 193 } 194 gtest.C(t, func(t *gtest.T) { 195 var ( 196 array []*B 197 ) 198 err := gconv.Structs(g.Slice{ 199 g.Map{"id": nil, "name": "john"}, 200 g.Map{"id": nil, "name": "smith"}, 201 }, &array) 202 t.AssertNil(err) 203 t.Assert(len(array), 2) 204 t.Assert(array[0].Name, "john") 205 t.Assert(array[1].Name, "smith") 206 }) 207 }