github.com/gogf/gf@v1.16.9/util/gconv/gconv_z_unit_all_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 "time" 12 13 "github.com/gogf/gf/frame/g" 14 "github.com/gogf/gf/os/gtime" 15 "github.com/gogf/gf/test/gtest" 16 "github.com/gogf/gf/util/gconv" 17 ) 18 19 type apiString interface { 20 String() string 21 } 22 type S struct { 23 } 24 25 func (s S) String() string { 26 return "22222" 27 } 28 29 type apiError interface { 30 Error() string 31 } 32 type S1 struct { 33 } 34 35 func (s1 S1) Error() string { 36 return "22222" 37 } 38 39 func Test_Bool_All(t *testing.T) { 40 gtest.C(t, func(t *gtest.T) { 41 var any interface{} = nil 42 t.AssertEQ(gconv.Bool(any), false) 43 t.AssertEQ(gconv.Bool(false), false) 44 t.AssertEQ(gconv.Bool(nil), false) 45 t.AssertEQ(gconv.Bool(0), false) 46 t.AssertEQ(gconv.Bool("0"), false) 47 t.AssertEQ(gconv.Bool(""), false) 48 t.AssertEQ(gconv.Bool("false"), false) 49 t.AssertEQ(gconv.Bool("off"), false) 50 t.AssertEQ(gconv.Bool([]byte{}), false) 51 t.AssertEQ(gconv.Bool([]string{}), false) 52 t.AssertEQ(gconv.Bool([2]int{1, 2}), true) 53 t.AssertEQ(gconv.Bool([]interface{}{}), false) 54 t.AssertEQ(gconv.Bool([]map[int]int{}), false) 55 56 var countryCapitalMap = make(map[string]string) 57 /* map插入key - value对,各个国家对应的首都 */ 58 countryCapitalMap["France"] = "巴黎" 59 countryCapitalMap["Italy"] = "罗马" 60 countryCapitalMap["Japan"] = "东京" 61 countryCapitalMap["India "] = "新德里" 62 t.AssertEQ(gconv.Bool(countryCapitalMap), true) 63 64 t.AssertEQ(gconv.Bool("1"), true) 65 t.AssertEQ(gconv.Bool("on"), true) 66 t.AssertEQ(gconv.Bool(1), true) 67 t.AssertEQ(gconv.Bool(123.456), true) 68 t.AssertEQ(gconv.Bool(boolStruct{}), true) 69 t.AssertEQ(gconv.Bool(&boolStruct{}), true) 70 }) 71 } 72 73 func Test_Int_All(t *testing.T) { 74 gtest.C(t, func(t *gtest.T) { 75 var any interface{} = nil 76 t.AssertEQ(gconv.Int(any), 0) 77 t.AssertEQ(gconv.Int(false), 0) 78 t.AssertEQ(gconv.Int(nil), 0) 79 t.Assert(gconv.Int(nil), 0) 80 t.AssertEQ(gconv.Int(0), 0) 81 t.AssertEQ(gconv.Int("0"), 0) 82 t.AssertEQ(gconv.Int(""), 0) 83 t.AssertEQ(gconv.Int("false"), 0) 84 t.AssertEQ(gconv.Int("off"), 0) 85 t.AssertEQ(gconv.Int([]byte{}), 0) 86 t.AssertEQ(gconv.Int([]string{}), 0) 87 t.AssertEQ(gconv.Int([2]int{1, 2}), 0) 88 t.AssertEQ(gconv.Int([]interface{}{}), 0) 89 t.AssertEQ(gconv.Int([]map[int]int{}), 0) 90 91 var countryCapitalMap = make(map[string]string) 92 /* map插入key - value对,各个国家对应的首都 */ 93 countryCapitalMap["France"] = "巴黎" 94 countryCapitalMap["Italy"] = "罗马" 95 countryCapitalMap["Japan"] = "东京" 96 countryCapitalMap["India "] = "新德里" 97 t.AssertEQ(gconv.Int(countryCapitalMap), 0) 98 99 t.AssertEQ(gconv.Int("1"), 1) 100 t.AssertEQ(gconv.Int("on"), 0) 101 t.AssertEQ(gconv.Int(1), 1) 102 t.AssertEQ(gconv.Int(123.456), 123) 103 t.AssertEQ(gconv.Int(boolStruct{}), 0) 104 t.AssertEQ(gconv.Int(&boolStruct{}), 0) 105 }) 106 } 107 108 func Test_Int8_All(t *testing.T) { 109 gtest.C(t, func(t *gtest.T) { 110 var any interface{} = nil 111 t.Assert(gconv.Int8(any), int8(0)) 112 t.AssertEQ(gconv.Int8(false), int8(0)) 113 t.AssertEQ(gconv.Int8(nil), int8(0)) 114 t.AssertEQ(gconv.Int8(0), int8(0)) 115 t.AssertEQ(gconv.Int8("0"), int8(0)) 116 t.AssertEQ(gconv.Int8(""), int8(0)) 117 t.AssertEQ(gconv.Int8("false"), int8(0)) 118 t.AssertEQ(gconv.Int8("off"), int8(0)) 119 t.AssertEQ(gconv.Int8([]byte{}), int8(0)) 120 t.AssertEQ(gconv.Int8([]string{}), int8(0)) 121 t.AssertEQ(gconv.Int8([2]int{1, 2}), int8(0)) 122 t.AssertEQ(gconv.Int8([]interface{}{}), int8(0)) 123 t.AssertEQ(gconv.Int8([]map[int]int{}), int8(0)) 124 125 var countryCapitalMap = make(map[string]string) 126 /* map插入key - value对,各个国家对应的首都 */ 127 countryCapitalMap["France"] = "巴黎" 128 countryCapitalMap["Italy"] = "罗马" 129 countryCapitalMap["Japan"] = "东京" 130 countryCapitalMap["India "] = "新德里" 131 t.AssertEQ(gconv.Int8(countryCapitalMap), int8(0)) 132 133 t.AssertEQ(gconv.Int8("1"), int8(1)) 134 t.AssertEQ(gconv.Int8("on"), int8(0)) 135 t.AssertEQ(gconv.Int8(int8(1)), int8(1)) 136 t.AssertEQ(gconv.Int8(123.456), int8(123)) 137 t.AssertEQ(gconv.Int8(boolStruct{}), int8(0)) 138 t.AssertEQ(gconv.Int8(&boolStruct{}), int8(0)) 139 }) 140 } 141 142 func Test_Int16_All(t *testing.T) { 143 gtest.C(t, func(t *gtest.T) { 144 var any interface{} = nil 145 t.Assert(gconv.Int16(any), int16(0)) 146 t.AssertEQ(gconv.Int16(false), int16(0)) 147 t.AssertEQ(gconv.Int16(nil), int16(0)) 148 t.AssertEQ(gconv.Int16(0), int16(0)) 149 t.AssertEQ(gconv.Int16("0"), int16(0)) 150 t.AssertEQ(gconv.Int16(""), int16(0)) 151 t.AssertEQ(gconv.Int16("false"), int16(0)) 152 t.AssertEQ(gconv.Int16("off"), int16(0)) 153 t.AssertEQ(gconv.Int16([]byte{}), int16(0)) 154 t.AssertEQ(gconv.Int16([]string{}), int16(0)) 155 t.AssertEQ(gconv.Int16([2]int{1, 2}), int16(0)) 156 t.AssertEQ(gconv.Int16([]interface{}{}), int16(0)) 157 t.AssertEQ(gconv.Int16([]map[int]int{}), int16(0)) 158 159 var countryCapitalMap = make(map[string]string) 160 /* map插入key - value对,各个国家对应的首都 */ 161 countryCapitalMap["France"] = "巴黎" 162 countryCapitalMap["Italy"] = "罗马" 163 countryCapitalMap["Japan"] = "东京" 164 countryCapitalMap["India "] = "新德里" 165 t.AssertEQ(gconv.Int16(countryCapitalMap), int16(0)) 166 167 t.AssertEQ(gconv.Int16("1"), int16(1)) 168 t.AssertEQ(gconv.Int16("on"), int16(0)) 169 t.AssertEQ(gconv.Int16(int16(1)), int16(1)) 170 t.AssertEQ(gconv.Int16(123.456), int16(123)) 171 t.AssertEQ(gconv.Int16(boolStruct{}), int16(0)) 172 t.AssertEQ(gconv.Int16(&boolStruct{}), int16(0)) 173 }) 174 } 175 176 func Test_Int32_All(t *testing.T) { 177 gtest.C(t, func(t *gtest.T) { 178 var any interface{} = nil 179 t.Assert(gconv.Int32(any), int32(0)) 180 t.AssertEQ(gconv.Int32(false), int32(0)) 181 t.AssertEQ(gconv.Int32(nil), int32(0)) 182 t.AssertEQ(gconv.Int32(0), int32(0)) 183 t.AssertEQ(gconv.Int32("0"), int32(0)) 184 t.AssertEQ(gconv.Int32(""), int32(0)) 185 t.AssertEQ(gconv.Int32("false"), int32(0)) 186 t.AssertEQ(gconv.Int32("off"), int32(0)) 187 t.AssertEQ(gconv.Int32([]byte{}), int32(0)) 188 t.AssertEQ(gconv.Int32([]string{}), int32(0)) 189 t.AssertEQ(gconv.Int32([2]int{1, 2}), int32(0)) 190 t.AssertEQ(gconv.Int32([]interface{}{}), int32(0)) 191 t.AssertEQ(gconv.Int32([]map[int]int{}), int32(0)) 192 193 var countryCapitalMap = make(map[string]string) 194 /* map插入key - value对,各个国家对应的首都 */ 195 countryCapitalMap["France"] = "巴黎" 196 countryCapitalMap["Italy"] = "罗马" 197 countryCapitalMap["Japan"] = "东京" 198 countryCapitalMap["India "] = "新德里" 199 t.AssertEQ(gconv.Int32(countryCapitalMap), int32(0)) 200 201 t.AssertEQ(gconv.Int32("1"), int32(1)) 202 t.AssertEQ(gconv.Int32("on"), int32(0)) 203 t.AssertEQ(gconv.Int32(int32(1)), int32(1)) 204 t.AssertEQ(gconv.Int32(123.456), int32(123)) 205 t.AssertEQ(gconv.Int32(boolStruct{}), int32(0)) 206 t.AssertEQ(gconv.Int32(&boolStruct{}), int32(0)) 207 }) 208 } 209 210 func Test_Int64_All(t *testing.T) { 211 gtest.C(t, func(t *gtest.T) { 212 var any interface{} = nil 213 t.AssertEQ(gconv.Int64("0x00e"), int64(14)) 214 t.Assert(gconv.Int64("022"), int64(18)) 215 216 t.Assert(gconv.Int64(any), int64(0)) 217 t.Assert(gconv.Int64(true), 1) 218 t.Assert(gconv.Int64("1"), int64(1)) 219 t.Assert(gconv.Int64("0"), int64(0)) 220 t.Assert(gconv.Int64("X"), int64(0)) 221 t.Assert(gconv.Int64("x"), int64(0)) 222 t.Assert(gconv.Int64(int64(1)), int64(1)) 223 t.Assert(gconv.Int64(int(0)), int64(0)) 224 t.Assert(gconv.Int64(int8(0)), int64(0)) 225 t.Assert(gconv.Int64(int16(0)), int64(0)) 226 t.Assert(gconv.Int64(int32(0)), int64(0)) 227 t.Assert(gconv.Int64(uint64(0)), int64(0)) 228 t.Assert(gconv.Int64(uint32(0)), int64(0)) 229 t.Assert(gconv.Int64(uint16(0)), int64(0)) 230 t.Assert(gconv.Int64(uint8(0)), int64(0)) 231 t.Assert(gconv.Int64(uint(0)), int64(0)) 232 t.Assert(gconv.Int64(float32(0)), int64(0)) 233 234 t.AssertEQ(gconv.Int64(false), int64(0)) 235 t.AssertEQ(gconv.Int64(nil), int64(0)) 236 t.AssertEQ(gconv.Int64(0), int64(0)) 237 t.AssertEQ(gconv.Int64("0"), int64(0)) 238 t.AssertEQ(gconv.Int64(""), int64(0)) 239 t.AssertEQ(gconv.Int64("false"), int64(0)) 240 t.AssertEQ(gconv.Int64("off"), int64(0)) 241 t.AssertEQ(gconv.Int64([]byte{}), int64(0)) 242 t.AssertEQ(gconv.Int64([]string{}), int64(0)) 243 t.AssertEQ(gconv.Int64([2]int{1, 2}), int64(0)) 244 t.AssertEQ(gconv.Int64([]interface{}{}), int64(0)) 245 t.AssertEQ(gconv.Int64([]map[int]int{}), int64(0)) 246 247 var countryCapitalMap = make(map[string]string) 248 /* map插入key - value对,各个国家对应的首都 */ 249 countryCapitalMap["France"] = "巴黎" 250 countryCapitalMap["Italy"] = "罗马" 251 countryCapitalMap["Japan"] = "东京" 252 countryCapitalMap["India "] = "新德里" 253 t.AssertEQ(gconv.Int64(countryCapitalMap), int64(0)) 254 255 t.AssertEQ(gconv.Int64("1"), int64(1)) 256 t.AssertEQ(gconv.Int64("on"), int64(0)) 257 t.AssertEQ(gconv.Int64(int64(1)), int64(1)) 258 t.AssertEQ(gconv.Int64(123.456), int64(123)) 259 t.AssertEQ(gconv.Int64(boolStruct{}), int64(0)) 260 t.AssertEQ(gconv.Int64(&boolStruct{}), int64(0)) 261 }) 262 } 263 264 func Test_Uint_All(t *testing.T) { 265 gtest.C(t, func(t *gtest.T) { 266 var any interface{} = nil 267 t.AssertEQ(gconv.Uint(any), uint(0)) 268 t.AssertEQ(gconv.Uint(false), uint(0)) 269 t.AssertEQ(gconv.Uint(nil), uint(0)) 270 t.Assert(gconv.Uint(nil), uint(0)) 271 t.AssertEQ(gconv.Uint(uint(0)), uint(0)) 272 t.AssertEQ(gconv.Uint("0"), uint(0)) 273 t.AssertEQ(gconv.Uint(""), uint(0)) 274 t.AssertEQ(gconv.Uint("false"), uint(0)) 275 t.AssertEQ(gconv.Uint("off"), uint(0)) 276 t.AssertEQ(gconv.Uint([]byte{}), uint(0)) 277 t.AssertEQ(gconv.Uint([]string{}), uint(0)) 278 t.AssertEQ(gconv.Uint([2]int{1, 2}), uint(0)) 279 t.AssertEQ(gconv.Uint([]interface{}{}), uint(0)) 280 t.AssertEQ(gconv.Uint([]map[int]int{}), uint(0)) 281 282 var countryCapitalMap = make(map[string]string) 283 /* map插入key - value对,各个国家对应的首都 */ 284 countryCapitalMap["France"] = "巴黎" 285 countryCapitalMap["Italy"] = "罗马" 286 countryCapitalMap["Japan"] = "东京" 287 countryCapitalMap["India "] = "新德里" 288 t.AssertEQ(gconv.Uint(countryCapitalMap), uint(0)) 289 290 t.AssertEQ(gconv.Uint("1"), uint(1)) 291 t.AssertEQ(gconv.Uint("on"), uint(0)) 292 t.AssertEQ(gconv.Uint(1), uint(1)) 293 t.AssertEQ(gconv.Uint(123.456), uint(123)) 294 t.AssertEQ(gconv.Uint(boolStruct{}), uint(0)) 295 t.AssertEQ(gconv.Uint(&boolStruct{}), uint(0)) 296 }) 297 } 298 299 func Test_Uint8_All(t *testing.T) { 300 gtest.C(t, func(t *gtest.T) { 301 var any interface{} = nil 302 t.Assert(gconv.Uint8(any), uint8(0)) 303 t.AssertEQ(gconv.Uint8(uint8(1)), uint8(1)) 304 t.AssertEQ(gconv.Uint8(false), uint8(0)) 305 t.AssertEQ(gconv.Uint8(nil), uint8(0)) 306 t.AssertEQ(gconv.Uint8(0), uint8(0)) 307 t.AssertEQ(gconv.Uint8("0"), uint8(0)) 308 t.AssertEQ(gconv.Uint8(""), uint8(0)) 309 t.AssertEQ(gconv.Uint8("false"), uint8(0)) 310 t.AssertEQ(gconv.Uint8("off"), uint8(0)) 311 t.AssertEQ(gconv.Uint8([]byte{}), uint8(0)) 312 t.AssertEQ(gconv.Uint8([]string{}), uint8(0)) 313 t.AssertEQ(gconv.Uint8([2]int{1, 2}), uint8(0)) 314 t.AssertEQ(gconv.Uint8([]interface{}{}), uint8(0)) 315 t.AssertEQ(gconv.Uint8([]map[int]int{}), uint8(0)) 316 317 var countryCapitalMap = make(map[string]string) 318 /* map插入key - value对,各个国家对应的首都 */ 319 countryCapitalMap["France"] = "巴黎" 320 countryCapitalMap["Italy"] = "罗马" 321 countryCapitalMap["Japan"] = "东京" 322 countryCapitalMap["India "] = "新德里" 323 t.AssertEQ(gconv.Uint8(countryCapitalMap), uint8(0)) 324 325 t.AssertEQ(gconv.Uint8("1"), uint8(1)) 326 t.AssertEQ(gconv.Uint8("on"), uint8(0)) 327 t.AssertEQ(gconv.Uint8(int8(1)), uint8(1)) 328 t.AssertEQ(gconv.Uint8(123.456), uint8(123)) 329 t.AssertEQ(gconv.Uint8(boolStruct{}), uint8(0)) 330 t.AssertEQ(gconv.Uint8(&boolStruct{}), uint8(0)) 331 }) 332 } 333 334 func Test_Uint16_All(t *testing.T) { 335 gtest.C(t, func(t *gtest.T) { 336 var any interface{} = nil 337 t.Assert(gconv.Uint16(any), uint16(0)) 338 t.AssertEQ(gconv.Uint16(uint16(1)), uint16(1)) 339 t.AssertEQ(gconv.Uint16(false), uint16(0)) 340 t.AssertEQ(gconv.Uint16(nil), uint16(0)) 341 t.AssertEQ(gconv.Uint16(0), uint16(0)) 342 t.AssertEQ(gconv.Uint16("0"), uint16(0)) 343 t.AssertEQ(gconv.Uint16(""), uint16(0)) 344 t.AssertEQ(gconv.Uint16("false"), uint16(0)) 345 t.AssertEQ(gconv.Uint16("off"), uint16(0)) 346 t.AssertEQ(gconv.Uint16([]byte{}), uint16(0)) 347 t.AssertEQ(gconv.Uint16([]string{}), uint16(0)) 348 t.AssertEQ(gconv.Uint16([2]int{1, 2}), uint16(0)) 349 t.AssertEQ(gconv.Uint16([]interface{}{}), uint16(0)) 350 t.AssertEQ(gconv.Uint16([]map[int]int{}), uint16(0)) 351 352 var countryCapitalMap = make(map[string]string) 353 /* map插入key - value对,各个国家对应的首都 */ 354 countryCapitalMap["France"] = "巴黎" 355 countryCapitalMap["Italy"] = "罗马" 356 countryCapitalMap["Japan"] = "东京" 357 countryCapitalMap["India "] = "新德里" 358 t.AssertEQ(gconv.Uint16(countryCapitalMap), uint16(0)) 359 360 t.AssertEQ(gconv.Uint16("1"), uint16(1)) 361 t.AssertEQ(gconv.Uint16("on"), uint16(0)) 362 t.AssertEQ(gconv.Uint16(int16(1)), uint16(1)) 363 t.AssertEQ(gconv.Uint16(123.456), uint16(123)) 364 t.AssertEQ(gconv.Uint16(boolStruct{}), uint16(0)) 365 t.AssertEQ(gconv.Uint16(&boolStruct{}), uint16(0)) 366 }) 367 } 368 369 func Test_Uint32_All(t *testing.T) { 370 gtest.C(t, func(t *gtest.T) { 371 var any interface{} = nil 372 t.Assert(gconv.Uint32(any), uint32(0)) 373 t.AssertEQ(gconv.Uint32(uint32(1)), uint32(1)) 374 t.AssertEQ(gconv.Uint32(false), uint32(0)) 375 t.AssertEQ(gconv.Uint32(nil), uint32(0)) 376 t.AssertEQ(gconv.Uint32(0), uint32(0)) 377 t.AssertEQ(gconv.Uint32("0"), uint32(0)) 378 t.AssertEQ(gconv.Uint32(""), uint32(0)) 379 t.AssertEQ(gconv.Uint32("false"), uint32(0)) 380 t.AssertEQ(gconv.Uint32("off"), uint32(0)) 381 t.AssertEQ(gconv.Uint32([]byte{}), uint32(0)) 382 t.AssertEQ(gconv.Uint32([]string{}), uint32(0)) 383 t.AssertEQ(gconv.Uint32([2]int{1, 2}), uint32(0)) 384 t.AssertEQ(gconv.Uint32([]interface{}{}), uint32(0)) 385 t.AssertEQ(gconv.Uint32([]map[int]int{}), uint32(0)) 386 387 var countryCapitalMap = make(map[string]string) 388 /* map插入key - value对,各个国家对应的首都 */ 389 countryCapitalMap["France"] = "巴黎" 390 countryCapitalMap["Italy"] = "罗马" 391 countryCapitalMap["Japan"] = "东京" 392 countryCapitalMap["India "] = "新德里" 393 t.AssertEQ(gconv.Uint32(countryCapitalMap), uint32(0)) 394 395 t.AssertEQ(gconv.Uint32("1"), uint32(1)) 396 t.AssertEQ(gconv.Uint32("on"), uint32(0)) 397 t.AssertEQ(gconv.Uint32(int32(1)), uint32(1)) 398 t.AssertEQ(gconv.Uint32(123.456), uint32(123)) 399 t.AssertEQ(gconv.Uint32(boolStruct{}), uint32(0)) 400 t.AssertEQ(gconv.Uint32(&boolStruct{}), uint32(0)) 401 }) 402 } 403 404 func Test_Uint64_All(t *testing.T) { 405 gtest.C(t, func(t *gtest.T) { 406 var any interface{} = nil 407 t.AssertEQ(gconv.Uint64("0x00e"), uint64(14)) 408 t.Assert(gconv.Uint64("022"), uint64(18)) 409 410 t.AssertEQ(gconv.Uint64(any), uint64(0)) 411 t.AssertEQ(gconv.Uint64(true), uint64(1)) 412 t.Assert(gconv.Uint64("1"), int64(1)) 413 t.Assert(gconv.Uint64("0"), uint64(0)) 414 t.Assert(gconv.Uint64("X"), uint64(0)) 415 t.Assert(gconv.Uint64("x"), uint64(0)) 416 t.Assert(gconv.Uint64(int64(1)), uint64(1)) 417 t.Assert(gconv.Uint64(int(0)), uint64(0)) 418 t.Assert(gconv.Uint64(int8(0)), uint64(0)) 419 t.Assert(gconv.Uint64(int16(0)), uint64(0)) 420 t.Assert(gconv.Uint64(int32(0)), uint64(0)) 421 t.Assert(gconv.Uint64(uint64(0)), uint64(0)) 422 t.Assert(gconv.Uint64(uint32(0)), uint64(0)) 423 t.Assert(gconv.Uint64(uint16(0)), uint64(0)) 424 t.Assert(gconv.Uint64(uint8(0)), uint64(0)) 425 t.Assert(gconv.Uint64(uint(0)), uint64(0)) 426 t.Assert(gconv.Uint64(float32(0)), uint64(0)) 427 428 t.AssertEQ(gconv.Uint64(false), uint64(0)) 429 t.AssertEQ(gconv.Uint64(nil), uint64(0)) 430 t.AssertEQ(gconv.Uint64(0), uint64(0)) 431 t.AssertEQ(gconv.Uint64("0"), uint64(0)) 432 t.AssertEQ(gconv.Uint64(""), uint64(0)) 433 t.AssertEQ(gconv.Uint64("false"), uint64(0)) 434 t.AssertEQ(gconv.Uint64("off"), uint64(0)) 435 t.AssertEQ(gconv.Uint64([]byte{}), uint64(0)) 436 t.AssertEQ(gconv.Uint64([]string{}), uint64(0)) 437 t.AssertEQ(gconv.Uint64([2]int{1, 2}), uint64(0)) 438 t.AssertEQ(gconv.Uint64([]interface{}{}), uint64(0)) 439 t.AssertEQ(gconv.Uint64([]map[int]int{}), uint64(0)) 440 441 var countryCapitalMap = make(map[string]string) 442 /* map插入key - value对,各个国家对应的首都 */ 443 countryCapitalMap["France"] = "巴黎" 444 countryCapitalMap["Italy"] = "罗马" 445 countryCapitalMap["Japan"] = "东京" 446 countryCapitalMap["India "] = "新德里" 447 t.AssertEQ(gconv.Uint64(countryCapitalMap), uint64(0)) 448 449 t.AssertEQ(gconv.Uint64("1"), uint64(1)) 450 t.AssertEQ(gconv.Uint64("on"), uint64(0)) 451 t.AssertEQ(gconv.Uint64(int64(1)), uint64(1)) 452 t.AssertEQ(gconv.Uint64(123.456), uint64(123)) 453 t.AssertEQ(gconv.Uint64(boolStruct{}), uint64(0)) 454 t.AssertEQ(gconv.Uint64(&boolStruct{}), uint64(0)) 455 }) 456 } 457 458 func Test_Float32_All(t *testing.T) { 459 gtest.C(t, func(t *gtest.T) { 460 var any interface{} = nil 461 t.Assert(gconv.Float32(any), float32(0)) 462 t.AssertEQ(gconv.Float32(false), float32(0)) 463 t.AssertEQ(gconv.Float32(nil), float32(0)) 464 t.AssertEQ(gconv.Float32(0), float32(0)) 465 t.AssertEQ(gconv.Float32("0"), float32(0)) 466 t.AssertEQ(gconv.Float32(""), float32(0)) 467 t.AssertEQ(gconv.Float32("false"), float32(0)) 468 t.AssertEQ(gconv.Float32("off"), float32(0)) 469 t.AssertEQ(gconv.Float32([]byte{}), float32(0)) 470 t.AssertEQ(gconv.Float32([]string{}), float32(0)) 471 t.AssertEQ(gconv.Float32([2]int{1, 2}), float32(0)) 472 t.AssertEQ(gconv.Float32([]interface{}{}), float32(0)) 473 t.AssertEQ(gconv.Float32([]map[int]int{}), float32(0)) 474 475 var countryCapitalMap = make(map[string]string) 476 /* map插入key - value对,各个国家对应的首都 */ 477 countryCapitalMap["France"] = "巴黎" 478 countryCapitalMap["Italy"] = "罗马" 479 countryCapitalMap["Japan"] = "东京" 480 countryCapitalMap["India "] = "新德里" 481 t.AssertEQ(gconv.Float32(countryCapitalMap), float32(0)) 482 483 t.AssertEQ(gconv.Float32("1"), float32(1)) 484 t.AssertEQ(gconv.Float32("on"), float32(0)) 485 t.AssertEQ(gconv.Float32(float32(1)), float32(1)) 486 t.AssertEQ(gconv.Float32(123.456), float32(123.456)) 487 t.AssertEQ(gconv.Float32(boolStruct{}), float32(0)) 488 t.AssertEQ(gconv.Float32(&boolStruct{}), float32(0)) 489 }) 490 } 491 492 func Test_Float64_All(t *testing.T) { 493 gtest.C(t, func(t *gtest.T) { 494 var any interface{} = nil 495 t.Assert(gconv.Float64(any), float64(0)) 496 t.AssertEQ(gconv.Float64(false), float64(0)) 497 t.AssertEQ(gconv.Float64(nil), float64(0)) 498 t.AssertEQ(gconv.Float64(0), float64(0)) 499 t.AssertEQ(gconv.Float64("0"), float64(0)) 500 t.AssertEQ(gconv.Float64(""), float64(0)) 501 t.AssertEQ(gconv.Float64("false"), float64(0)) 502 t.AssertEQ(gconv.Float64("off"), float64(0)) 503 t.AssertEQ(gconv.Float64([]byte{}), float64(0)) 504 t.AssertEQ(gconv.Float64([]string{}), float64(0)) 505 t.AssertEQ(gconv.Float64([2]int{1, 2}), float64(0)) 506 t.AssertEQ(gconv.Float64([]interface{}{}), float64(0)) 507 t.AssertEQ(gconv.Float64([]map[int]int{}), float64(0)) 508 509 var countryCapitalMap = make(map[string]string) 510 /* map插入key - value对,各个国家对应的首都 */ 511 countryCapitalMap["France"] = "巴黎" 512 countryCapitalMap["Italy"] = "罗马" 513 countryCapitalMap["Japan"] = "东京" 514 countryCapitalMap["India "] = "新德里" 515 t.AssertEQ(gconv.Float64(countryCapitalMap), float64(0)) 516 517 t.AssertEQ(gconv.Float64("1"), float64(1)) 518 t.AssertEQ(gconv.Float64("on"), float64(0)) 519 t.AssertEQ(gconv.Float64(float64(1)), float64(1)) 520 t.AssertEQ(gconv.Float64(123.456), float64(123.456)) 521 t.AssertEQ(gconv.Float64(boolStruct{}), float64(0)) 522 t.AssertEQ(gconv.Float64(&boolStruct{}), float64(0)) 523 }) 524 } 525 526 func Test_String_All(t *testing.T) { 527 gtest.C(t, func(t *gtest.T) { 528 var s []rune 529 t.AssertEQ(gconv.String(s), "") 530 var any interface{} = nil 531 t.AssertEQ(gconv.String(any), "") 532 t.AssertEQ(gconv.String("1"), "1") 533 t.AssertEQ(gconv.String("0"), string("0")) 534 t.Assert(gconv.String("X"), string("X")) 535 t.Assert(gconv.String("x"), string("x")) 536 t.Assert(gconv.String(int64(1)), uint64(1)) 537 t.Assert(gconv.String(int(0)), string("0")) 538 t.Assert(gconv.String(int8(0)), string("0")) 539 t.Assert(gconv.String(int16(0)), string("0")) 540 t.Assert(gconv.String(int32(0)), string("0")) 541 t.Assert(gconv.String(uint64(0)), string("0")) 542 t.Assert(gconv.String(uint32(0)), string("0")) 543 t.Assert(gconv.String(uint16(0)), string("0")) 544 t.Assert(gconv.String(uint8(0)), string("0")) 545 t.Assert(gconv.String(uint(0)), string("0")) 546 t.Assert(gconv.String(float32(0)), string("0")) 547 t.AssertEQ(gconv.String(true), "true") 548 t.AssertEQ(gconv.String(false), "false") 549 t.AssertEQ(gconv.String(nil), "") 550 t.AssertEQ(gconv.String(0), string("0")) 551 t.AssertEQ(gconv.String("0"), string("0")) 552 t.AssertEQ(gconv.String(""), "") 553 t.AssertEQ(gconv.String("false"), "false") 554 t.AssertEQ(gconv.String("off"), string("off")) 555 t.AssertEQ(gconv.String([]byte{}), "") 556 t.AssertEQ(gconv.String([]string{}), "[]") 557 t.AssertEQ(gconv.String([2]int{1, 2}), "[1,2]") 558 t.AssertEQ(gconv.String([]interface{}{}), "[]") 559 t.AssertEQ(gconv.String(map[int]int{}), "{}") 560 561 var countryCapitalMap = make(map[string]string) 562 /* map插入key - value对,各个国家对应的首都 */ 563 countryCapitalMap["France"] = "巴黎" 564 countryCapitalMap["Italy"] = "罗马" 565 countryCapitalMap["Japan"] = "东京" 566 countryCapitalMap["India "] = "新德里" 567 t.AssertEQ(gconv.String(countryCapitalMap), `{"France":"巴黎","India ":"新德里","Italy":"罗马","Japan":"东京"}`) 568 t.AssertEQ(gconv.String(int64(1)), "1") 569 t.AssertEQ(gconv.String(123.456), "123.456") 570 t.AssertEQ(gconv.String(boolStruct{}), "{}") 571 t.AssertEQ(gconv.String(&boolStruct{}), "{}") 572 573 var info apiString 574 info = new(S) 575 t.AssertEQ(gconv.String(info), "22222") 576 var errinfo apiError 577 errinfo = new(S1) 578 t.AssertEQ(gconv.String(errinfo), "22222") 579 }) 580 } 581 582 func Test_Runes_All(t *testing.T) { 583 gtest.C(t, func(t *gtest.T) { 584 t.AssertEQ(gconv.Runes("www"), []int32{119, 119, 119}) 585 var s []rune 586 t.AssertEQ(gconv.Runes(s), nil) 587 }) 588 } 589 590 func Test_Rune_All(t *testing.T) { 591 gtest.C(t, func(t *gtest.T) { 592 t.AssertEQ(gconv.Rune("www"), int32(0)) 593 t.AssertEQ(gconv.Rune(int32(0)), int32(0)) 594 var s []rune 595 t.AssertEQ(gconv.Rune(s), int32(0)) 596 }) 597 } 598 599 func Test_Bytes_All(t *testing.T) { 600 gtest.C(t, func(t *gtest.T) { 601 t.AssertEQ(gconv.Bytes(nil), nil) 602 t.AssertEQ(gconv.Bytes(int32(0)), []uint8{0, 0, 0, 0}) 603 t.AssertEQ(gconv.Bytes("s"), []uint8{115}) 604 t.AssertEQ(gconv.Bytes([]byte("s")), []uint8{115}) 605 }) 606 } 607 608 func Test_Byte_All(t *testing.T) { 609 gtest.C(t, func(t *gtest.T) { 610 t.AssertEQ(gconv.Byte(uint8(0)), uint8(0)) 611 t.AssertEQ(gconv.Byte("s"), uint8(0)) 612 t.AssertEQ(gconv.Byte([]byte("s")), uint8(115)) 613 }) 614 } 615 616 func Test_Convert_All(t *testing.T) { 617 gtest.C(t, func(t *gtest.T) { 618 var any interface{} = nil 619 t.AssertEQ(gconv.Convert(any, "string"), "") 620 t.AssertEQ(gconv.Convert("1", "string"), "1") 621 t.Assert(gconv.Convert(int64(1), "int64"), int64(1)) 622 t.Assert(gconv.Convert(int(0), "int"), int(0)) 623 t.Assert(gconv.Convert(int8(0), "int8"), int8(0)) 624 t.Assert(gconv.Convert(int16(0), "int16"), int16(0)) 625 t.Assert(gconv.Convert(int32(0), "int32"), int32(0)) 626 t.Assert(gconv.Convert(uint64(0), "uint64"), uint64(0)) 627 t.Assert(gconv.Convert(uint32(0), "uint32"), uint32(0)) 628 t.Assert(gconv.Convert(uint16(0), "uint16"), uint16(0)) 629 t.Assert(gconv.Convert(uint8(0), "uint8"), uint8(0)) 630 t.Assert(gconv.Convert(uint(0), "uint"), uint(0)) 631 t.Assert(gconv.Convert(float32(0), "float32"), float32(0)) 632 t.Assert(gconv.Convert(float64(0), "float64"), float64(0)) 633 t.AssertEQ(gconv.Convert(true, "bool"), true) 634 t.AssertEQ(gconv.Convert([]byte{}, "[]byte"), []uint8{}) 635 t.AssertEQ(gconv.Convert([]string{}, "[]string"), []string{}) 636 t.AssertEQ(gconv.Convert([2]int{1, 2}, "[]int"), []int{1, 2}) 637 t.AssertEQ(gconv.Convert("1989-01-02", "Time", "Y-m-d"), gconv.Time("1989-01-02", "Y-m-d")) 638 t.AssertEQ(gconv.Convert(1989, "Time"), gconv.Time("1970-01-01 08:33:09 +0800 CST")) 639 t.AssertEQ(gconv.Convert(gtime.Now(), "gtime.Time", 1), *gtime.New()) 640 t.AssertEQ(gconv.Convert(1989, "gtime.Time"), *gconv.GTime("1970-01-01 08:33:09 +0800 CST")) 641 t.AssertEQ(gconv.Convert(gtime.Now(), "*gtime.Time", 1), gtime.New()) 642 t.AssertEQ(gconv.Convert(gtime.Now(), "GTime", 1), *gtime.New()) 643 t.AssertEQ(gconv.Convert(1989, "*gtime.Time"), gconv.GTime(1989)) 644 t.AssertEQ(gconv.Convert(1989, "Duration"), time.Duration(int64(1989))) 645 t.AssertEQ(gconv.Convert("1989", "Duration"), time.Duration(int64(1989))) 646 t.AssertEQ(gconv.Convert("1989", ""), "1989") 647 }) 648 } 649 650 func Test_Slice_All(t *testing.T) { 651 gtest.C(t, func(t *gtest.T) { 652 value := 123.456 653 t.AssertEQ(gconv.Ints(value), []int{123}) 654 t.AssertEQ(gconv.Ints(nil), nil) 655 t.AssertEQ(gconv.Ints([]string{"1", "2"}), []int{1, 2}) 656 t.AssertEQ(gconv.Ints([]int{}), []int{}) 657 t.AssertEQ(gconv.Ints([]int8{1, 2}), []int{1, 2}) 658 t.AssertEQ(gconv.Ints([]int16{1, 2}), []int{1, 2}) 659 t.AssertEQ(gconv.Ints([]int32{1, 2}), []int{1, 2}) 660 t.AssertEQ(gconv.Ints([]int64{1, 2}), []int{1, 2}) 661 t.AssertEQ(gconv.Ints([]uint{1}), []int{1}) 662 t.AssertEQ(gconv.Ints([]uint8{1, 2}), []int{1, 2}) 663 t.AssertEQ(gconv.Ints([]uint16{1, 2}), []int{1, 2}) 664 t.AssertEQ(gconv.Ints([]uint32{1, 2}), []int{1, 2}) 665 t.AssertEQ(gconv.Ints([]uint64{1, 2}), []int{1, 2}) 666 t.AssertEQ(gconv.Ints([]bool{true}), []int{1}) 667 t.AssertEQ(gconv.Ints([]float32{1, 2}), []int{1, 2}) 668 t.AssertEQ(gconv.Ints([]float64{1, 2}), []int{1, 2}) 669 var inter []interface{} = make([]interface{}, 2) 670 t.AssertEQ(gconv.Ints(inter), []int{0, 0}) 671 672 t.AssertEQ(gconv.Strings(value), []string{"123.456"}) 673 t.AssertEQ(gconv.Strings(nil), nil) 674 t.AssertEQ(gconv.Strings([]string{"1", "2"}), []string{"1", "2"}) 675 t.AssertEQ(gconv.Strings([]int{1}), []string{"1"}) 676 t.AssertEQ(gconv.Strings([]int8{1, 2}), []string{"1", "2"}) 677 t.AssertEQ(gconv.Strings([]int16{1, 2}), []string{"1", "2"}) 678 t.AssertEQ(gconv.Strings([]int32{1, 2}), []string{"1", "2"}) 679 t.AssertEQ(gconv.Strings([]int64{1, 2}), []string{"1", "2"}) 680 t.AssertEQ(gconv.Strings([]uint{1}), []string{"1"}) 681 t.AssertEQ(gconv.Strings([]uint8{1, 2}), []string{"1", "2"}) 682 t.AssertEQ(gconv.Strings([]uint16{1, 2}), []string{"1", "2"}) 683 t.AssertEQ(gconv.Strings([]uint32{1, 2}), []string{"1", "2"}) 684 t.AssertEQ(gconv.Strings([]uint64{1, 2}), []string{"1", "2"}) 685 t.AssertEQ(gconv.Strings([]bool{true}), []string{"true"}) 686 t.AssertEQ(gconv.Strings([]float32{1, 2}), []string{"1", "2"}) 687 t.AssertEQ(gconv.Strings([]float64{1, 2}), []string{"1", "2"}) 688 var strer = make([]interface{}, 2) 689 t.AssertEQ(gconv.Strings(strer), []string{"", ""}) 690 691 t.AssertEQ(gconv.Floats(value), []float64{123.456}) 692 t.AssertEQ(gconv.Floats(nil), nil) 693 t.AssertEQ(gconv.Floats([]string{"1", "2"}), []float64{1, 2}) 694 t.AssertEQ(gconv.Floats([]int{1}), []float64{1}) 695 t.AssertEQ(gconv.Floats([]int8{1, 2}), []float64{1, 2}) 696 t.AssertEQ(gconv.Floats([]int16{1, 2}), []float64{1, 2}) 697 t.AssertEQ(gconv.Floats([]int32{1, 2}), []float64{1, 2}) 698 t.AssertEQ(gconv.Floats([]int64{1, 2}), []float64{1, 2}) 699 t.AssertEQ(gconv.Floats([]uint{1}), []float64{1}) 700 t.AssertEQ(gconv.Floats([]uint8{1, 2}), []float64{1, 2}) 701 t.AssertEQ(gconv.Floats([]uint16{1, 2}), []float64{1, 2}) 702 t.AssertEQ(gconv.Floats([]uint32{1, 2}), []float64{1, 2}) 703 t.AssertEQ(gconv.Floats([]uint64{1, 2}), []float64{1, 2}) 704 t.AssertEQ(gconv.Floats([]bool{true}), []float64{0}) 705 t.AssertEQ(gconv.Floats([]float32{1, 2}), []float64{1, 2}) 706 t.AssertEQ(gconv.Floats([]float64{1, 2}), []float64{1, 2}) 707 var floer = make([]interface{}, 2) 708 t.AssertEQ(gconv.Floats(floer), []float64{0, 0}) 709 710 t.AssertEQ(gconv.Interfaces(value), []interface{}{123.456}) 711 t.AssertEQ(gconv.Interfaces(nil), nil) 712 t.AssertEQ(gconv.Interfaces([]interface{}{1}), []interface{}{1}) 713 t.AssertEQ(gconv.Interfaces([]string{"1"}), []interface{}{"1"}) 714 t.AssertEQ(gconv.Interfaces([]int{1}), []interface{}{1}) 715 t.AssertEQ(gconv.Interfaces([]int8{1}), []interface{}{1}) 716 t.AssertEQ(gconv.Interfaces([]int16{1}), []interface{}{1}) 717 t.AssertEQ(gconv.Interfaces([]int32{1}), []interface{}{1}) 718 t.AssertEQ(gconv.Interfaces([]int64{1}), []interface{}{1}) 719 t.AssertEQ(gconv.Interfaces([]uint{1}), []interface{}{1}) 720 t.AssertEQ(gconv.Interfaces([]uint8{1}), []interface{}{1}) 721 t.AssertEQ(gconv.Interfaces([]uint16{1}), []interface{}{1}) 722 t.AssertEQ(gconv.Interfaces([]uint32{1}), []interface{}{1}) 723 t.AssertEQ(gconv.Interfaces([]uint64{1}), []interface{}{1}) 724 t.AssertEQ(gconv.Interfaces([]bool{true}), []interface{}{true}) 725 t.AssertEQ(gconv.Interfaces([]float32{1}), []interface{}{1}) 726 t.AssertEQ(gconv.Interfaces([]float64{1}), []interface{}{1}) 727 t.AssertEQ(gconv.Interfaces([1]int{1}), []interface{}{1}) 728 729 type interSlice []int 730 slices := interSlice{1} 731 t.AssertEQ(gconv.Interfaces(slices), []interface{}{1}) 732 733 t.AssertEQ(gconv.Maps(nil), nil) 734 t.AssertEQ(gconv.Maps([]map[string]interface{}{{"a": "1"}}), []map[string]interface{}{{"a": "1"}}) 735 t.AssertEQ(gconv.Maps(1223), []map[string]interface{}{nil}) 736 t.AssertEQ(gconv.Maps([]int{}), nil) 737 }) 738 } 739 740 // 私有属性不会进行转换 741 func Test_Slice_PrivateAttribute_All(t *testing.T) { 742 type User struct { 743 Id int `json:"id"` 744 name string `json:"name"` 745 Ad []interface{} `json:"ad"` 746 } 747 gtest.C(t, func(t *gtest.T) { 748 user := &User{1, "john", []interface{}{2}} 749 array := gconv.Interfaces(user) 750 t.Assert(len(array), 1) 751 t.Assert(array[0].(*User).Id, 1) 752 t.Assert(array[0].(*User).name, "john") 753 t.Assert(array[0].(*User).Ad, []interface{}{2}) 754 }) 755 } 756 757 func Test_Map_Basic_All(t *testing.T) { 758 gtest.C(t, func(t *gtest.T) { 759 m1 := map[string]string{ 760 "k": "v", 761 } 762 m2 := map[int]string{ 763 3: "v", 764 } 765 m3 := map[float64]float32{ 766 1.22: 3.1, 767 } 768 t.Assert(gconv.Map(m1), g.Map{ 769 "k": "v", 770 }) 771 t.Assert(gconv.Map(m2), g.Map{ 772 "3": "v", 773 }) 774 t.Assert(gconv.Map(m3), g.Map{ 775 "1.22": "3.1", 776 }) 777 t.AssertEQ(gconv.Map(nil), nil) 778 t.AssertEQ(gconv.Map(map[string]interface{}{"a": 1}), map[string]interface{}{"a": 1}) 779 t.AssertEQ(gconv.Map(map[int]interface{}{1: 1}), map[string]interface{}{"1": 1}) 780 t.AssertEQ(gconv.Map(map[uint]interface{}{1: 1}), map[string]interface{}{"1": 1}) 781 t.AssertEQ(gconv.Map(map[uint]string{1: "1"}), map[string]interface{}{"1": "1"}) 782 783 t.AssertEQ(gconv.Map(map[interface{}]interface{}{"a": 1}), map[interface{}]interface{}{"a": 1}) 784 t.AssertEQ(gconv.Map(map[interface{}]string{"a": "1"}), map[interface{}]string{"a": "1"}) 785 t.AssertEQ(gconv.Map(map[interface{}]int{"a": 1}), map[interface{}]int{"a": 1}) 786 t.AssertEQ(gconv.Map(map[interface{}]uint{"a": 1}), map[interface{}]uint{"a": 1}) 787 t.AssertEQ(gconv.Map(map[interface{}]float32{"a": 1}), map[interface{}]float32{"a": 1}) 788 t.AssertEQ(gconv.Map(map[interface{}]float64{"a": 1}), map[interface{}]float64{"a": 1}) 789 790 t.AssertEQ(gconv.Map(map[string]bool{"a": true}), map[string]interface{}{"a": true}) 791 t.AssertEQ(gconv.Map(map[string]int{"a": 1}), map[string]interface{}{"a": 1}) 792 t.AssertEQ(gconv.Map(map[string]uint{"a": 1}), map[string]interface{}{"a": 1}) 793 t.AssertEQ(gconv.Map(map[string]float32{"a": 1}), map[string]interface{}{"a": 1}) 794 t.AssertEQ(gconv.Map(map[string]float64{"a": 1}), map[string]interface{}{"a": 1}) 795 796 }) 797 } 798 799 func Test_Map_StructWithGconvTag_All(t *testing.T) { 800 gtest.C(t, func(t *gtest.T) { 801 type User struct { 802 Uid int 803 Name string 804 SiteUrl string `gconv:"-"` 805 NickName string `gconv:"nickname,omitempty"` 806 Pass1 string `gconv:"password1"` 807 Pass2 string `gconv:"password2"` 808 Ss []string `gconv:"ss"` 809 } 810 user1 := User{ 811 Uid: 100, 812 Name: "john", 813 SiteUrl: "https://goframe.org", 814 Pass1: "123", 815 Pass2: "456", 816 Ss: []string{"sss", "2222"}, 817 } 818 user2 := &user1 819 map1 := gconv.Map(user1) 820 map2 := gconv.Map(user2) 821 t.Assert(map1["Uid"], 100) 822 t.Assert(map1["Name"], "john") 823 t.Assert(map1["SiteUrl"], nil) 824 t.Assert(map1["NickName"], nil) 825 t.Assert(map1["nickname"], nil) 826 t.Assert(map1["password1"], "123") 827 t.Assert(map1["password2"], "456") 828 t.Assert(map2["Uid"], 100) 829 t.Assert(map2["Name"], "john") 830 t.Assert(map2["SiteUrl"], nil) 831 t.Assert(map2["NickName"], nil) 832 t.Assert(map2["nickname"], nil) 833 t.Assert(map2["password1"], "123") 834 t.Assert(map2["password2"], "456") 835 }) 836 } 837 838 func Test_Map_StructWithJsonTag_All(t *testing.T) { 839 gtest.C(t, func(t *gtest.T) { 840 type User struct { 841 Uid int 842 Name string 843 SiteUrl string `json:"-"` 844 NickName string `json:"nickname, omitempty"` 845 Pass1 string `json:"password1,newpassword"` 846 Pass2 string `json:"password2"` 847 Ss []string `json:"omitempty"` 848 ssb, ssa string 849 } 850 user1 := User{ 851 Uid: 100, 852 Name: "john", 853 SiteUrl: "https://goframe.org", 854 Pass1: "123", 855 Pass2: "456", 856 Ss: []string{"sss", "2222"}, 857 ssb: "11", 858 ssa: "222", 859 } 860 user3 := User{ 861 Uid: 100, 862 Name: "john", 863 NickName: "SSS", 864 SiteUrl: "https://goframe.org", 865 Pass1: "123", 866 Pass2: "456", 867 Ss: []string{"sss", "2222"}, 868 ssb: "11", 869 ssa: "222", 870 } 871 user2 := &user1 872 _ = gconv.Map(user1, "Ss") 873 map1 := gconv.Map(user1, "json", "json2") 874 map2 := gconv.Map(user2) 875 map3 := gconv.Map(user3) 876 t.Assert(map1["Uid"], 100) 877 t.Assert(map1["Name"], "john") 878 t.Assert(map1["SiteUrl"], nil) 879 t.Assert(map1["NickName"], nil) 880 t.Assert(map1["nickname"], nil) 881 t.Assert(map1["password1"], "123") 882 t.Assert(map1["password2"], "456") 883 t.Assert(map2["Uid"], 100) 884 t.Assert(map2["Name"], "john") 885 t.Assert(map2["SiteUrl"], nil) 886 t.Assert(map2["NickName"], nil) 887 t.Assert(map2["nickname"], nil) 888 t.Assert(map2["password1"], "123") 889 t.Assert(map2["password2"], "456") 890 t.Assert(map3["NickName"], nil) 891 }) 892 } 893 894 func Test_Map_PrivateAttribute_All(t *testing.T) { 895 type User struct { 896 Id int 897 name string 898 } 899 gtest.C(t, func(t *gtest.T) { 900 user := &User{1, "john"} 901 t.Assert(gconv.Map(user), g.Map{"Id": 1}) 902 }) 903 } 904 905 func Test_Map_StructInherit_All(t *testing.T) { 906 gtest.C(t, func(t *gtest.T) { 907 type Ids struct { 908 Id int `json:"id"` 909 Uid int `json:"uid"` 910 } 911 type Base struct { 912 Ids 913 CreateTime string `json:"create_time"` 914 } 915 type User struct { 916 Base 917 Passport string `json:"passport"` 918 Password string `json:"password"` 919 Nickname string `json:"nickname"` 920 S *string `json:"nickname2"` 921 } 922 923 user := new(User) 924 user.Id = 100 925 user.Nickname = "john" 926 user.CreateTime = "2019" 927 var s = "s" 928 user.S = &s 929 930 m := gconv.MapDeep(user) 931 t.Assert(m["id"], user.Id) 932 t.Assert(m["nickname"], user.Nickname) 933 t.Assert(m["create_time"], user.CreateTime) 934 t.Assert(m["nickname2"], user.S) 935 }) 936 } 937 938 func Test_Struct_Basic1_All(t *testing.T) { 939 gtest.C(t, func(t *gtest.T) { 940 type Score struct { 941 Name int 942 Result string 943 } 944 945 type Score2 struct { 946 Name int 947 Result string 948 } 949 950 type User struct { 951 Uid int 952 Name string 953 Site_Url string 954 NickName string 955 Pass1 string `gconv:"password1"` 956 Pass2 string `gconv:"password2"` 957 As *Score 958 Ass Score 959 Assb []interface{} 960 } 961 // 使用默认映射规则绑定属性值到对象 962 user := new(User) 963 params1 := g.Map{ 964 "uid": 1, 965 "Name": "john", 966 "siteurl": "https://goframe.org", 967 "nick_name": "johng", 968 "PASS1": "123", 969 "PASS2": "456", 970 "As": g.Map{"Name": 1, "Result": "22222"}, 971 "Ass": &Score{11, "11"}, 972 "Assb": []string{"wwww"}, 973 } 974 _ = gconv.Struct(nil, user) 975 _ = gconv.Struct(params1, nil) 976 _ = gconv.Struct([]interface{}{nil}, user) 977 _ = gconv.Struct(user, []interface{}{nil}) 978 979 var a = []interface{}{nil} 980 ab := &a 981 _ = gconv.Struct(params1, *ab) 982 var pi *int = nil 983 _ = gconv.Struct(params1, pi) 984 985 _ = gconv.Struct(params1, user) 986 _ = gconv.Struct(params1, user, map[string]string{"uid": "Names"}) 987 _ = gconv.Struct(params1, user, map[string]string{"uid": "as"}) 988 989 // 使用struct tag映射绑定属性值到对象 990 user = new(User) 991 params2 := g.Map{ 992 "uid": 2, 993 "name": "smith", 994 "site-url": "https://goframe.org", 995 "nick name": "johng", 996 "password1": "111", 997 "password2": "222", 998 } 999 if err := gconv.Struct(params2, user); err != nil { 1000 gtest.Error(err) 1001 } 1002 t.Assert(user, &User{ 1003 Uid: 2, 1004 Name: "smith", 1005 Site_Url: "https://goframe.org", 1006 NickName: "johng", 1007 Pass1: "111", 1008 Pass2: "222", 1009 }) 1010 }) 1011 } 1012 1013 // 使用默认映射规则绑定属性值到对象 1014 func Test_Struct_Basic2_All(t *testing.T) { 1015 gtest.C(t, func(t *gtest.T) { 1016 type User struct { 1017 Uid int 1018 Name string 1019 SiteUrl string 1020 Pass1 string 1021 Pass2 string 1022 } 1023 user := new(User) 1024 params := g.Map{ 1025 "uid": 1, 1026 "Name": "john", 1027 "site_url": "https://goframe.org", 1028 "PASS1": "123", 1029 "PASS2": "456", 1030 } 1031 if err := gconv.Struct(params, user); err != nil { 1032 gtest.Error(err) 1033 } 1034 t.Assert(user, &User{ 1035 Uid: 1, 1036 Name: "john", 1037 SiteUrl: "https://goframe.org", 1038 Pass1: "123", 1039 Pass2: "456", 1040 }) 1041 }) 1042 } 1043 1044 // 带有指针的基础类型属性 1045 func Test_Struct_Basic3_All(t *testing.T) { 1046 gtest.C(t, func(t *gtest.T) { 1047 type User struct { 1048 Uid int 1049 Name *string 1050 } 1051 user := new(User) 1052 params := g.Map{ 1053 "uid": 1, 1054 "Name": "john", 1055 } 1056 if err := gconv.Struct(params, user); err != nil { 1057 gtest.Error(err) 1058 } 1059 t.Assert(user.Uid, 1) 1060 t.Assert(*user.Name, "john") 1061 }) 1062 } 1063 1064 // slice类型属性的赋值 1065 func Test_Struct_Attr_Slice_All(t *testing.T) { 1066 gtest.C(t, func(t *gtest.T) { 1067 type User struct { 1068 Scores []int 1069 } 1070 scores := []interface{}{99, 100, 60, 140} 1071 user := new(User) 1072 if err := gconv.Struct(g.Map{"Scores": scores}, user); err != nil { 1073 gtest.Error(err) 1074 } else { 1075 t.Assert(user, &User{ 1076 Scores: []int{99, 100, 60, 140}, 1077 }) 1078 } 1079 }) 1080 } 1081 1082 // 属性为struct对象 1083 func Test_Struct_Attr_Struct_All(t *testing.T) { 1084 gtest.C(t, func(t *gtest.T) { 1085 type Score struct { 1086 Name string 1087 Result int 1088 } 1089 type User struct { 1090 Scores Score 1091 } 1092 1093 user := new(User) 1094 scores := map[string]interface{}{ 1095 "Scores": map[string]interface{}{ 1096 "Name": "john", 1097 "Result": 100, 1098 }, 1099 } 1100 1101 // 嵌套struct转换 1102 if err := gconv.Struct(scores, user); err != nil { 1103 gtest.Error(err) 1104 } else { 1105 t.Assert(user, &User{ 1106 Scores: Score{ 1107 Name: "john", 1108 Result: 100, 1109 }, 1110 }) 1111 } 1112 }) 1113 } 1114 1115 // 属性为struct对象指针 1116 func Test_Struct_Attr_Struct_Ptr_All(t *testing.T) { 1117 gtest.C(t, func(t *gtest.T) { 1118 type Score struct { 1119 Name string 1120 Result int 1121 } 1122 type User struct { 1123 Scores *Score 1124 } 1125 1126 user := new(User) 1127 scores := map[string]interface{}{ 1128 "Scores": map[string]interface{}{ 1129 "Name": "john", 1130 "Result": 100, 1131 }, 1132 } 1133 1134 // 嵌套struct转换 1135 if err := gconv.Struct(scores, user); err != nil { 1136 gtest.Error(err) 1137 } else { 1138 t.Assert(user.Scores, &Score{ 1139 Name: "john", 1140 Result: 100, 1141 }) 1142 } 1143 }) 1144 } 1145 1146 // 属性为struct对象slice 1147 func Test_Struct_Attr_Struct_Slice1_All(t *testing.T) { 1148 gtest.C(t, func(t *gtest.T) { 1149 type Score struct { 1150 Name string 1151 Result int 1152 } 1153 type User struct { 1154 Scores []Score 1155 } 1156 1157 user := new(User) 1158 scores := map[string]interface{}{ 1159 "Scores": map[string]interface{}{ 1160 "Name": "john", 1161 "Result": 100, 1162 }, 1163 } 1164 1165 // 嵌套struct转换,属性为slice类型,数值为map类型 1166 if err := gconv.Struct(scores, user); err != nil { 1167 gtest.Error(err) 1168 } else { 1169 t.Assert(user.Scores, []Score{ 1170 { 1171 Name: "john", 1172 Result: 100, 1173 }, 1174 }) 1175 } 1176 }) 1177 } 1178 1179 // 属性为struct对象slice 1180 func Test_Struct_Attr_Struct_Slice2_All(t *testing.T) { 1181 gtest.C(t, func(t *gtest.T) { 1182 type Score struct { 1183 Name string 1184 Result int 1185 } 1186 type User struct { 1187 Scores []Score 1188 } 1189 1190 user := new(User) 1191 scores := map[string]interface{}{ 1192 "Scores": []interface{}{ 1193 map[string]interface{}{ 1194 "Name": "john", 1195 "Result": 100, 1196 }, 1197 map[string]interface{}{ 1198 "Name": "smith", 1199 "Result": 60, 1200 }, 1201 }, 1202 } 1203 1204 // 嵌套struct转换,属性为slice类型,数值为slice map类型 1205 if err := gconv.Struct(scores, user); err != nil { 1206 gtest.Error(err) 1207 } else { 1208 t.Assert(user.Scores, []Score{ 1209 { 1210 Name: "john", 1211 Result: 100, 1212 }, 1213 { 1214 Name: "smith", 1215 Result: 60, 1216 }, 1217 }) 1218 } 1219 }) 1220 } 1221 1222 // 属性为struct对象slice ptr 1223 func Test_Struct_Attr_Struct_Slice_Ptr_All(t *testing.T) { 1224 gtest.C(t, func(t *gtest.T) { 1225 type Score struct { 1226 Name string 1227 Result int 1228 } 1229 type User struct { 1230 Scores []*Score 1231 } 1232 1233 user := new(User) 1234 scores := map[string]interface{}{ 1235 "Scores": []interface{}{ 1236 map[string]interface{}{ 1237 "Name": "john", 1238 "Result": 100, 1239 }, 1240 map[string]interface{}{ 1241 "Name": "smith", 1242 "Result": 60, 1243 }, 1244 }, 1245 } 1246 1247 // 嵌套struct转换,属性为slice类型,数值为slice map类型 1248 if err := gconv.Struct(scores, user); err != nil { 1249 gtest.Error(err) 1250 } else { 1251 t.Assert(len(user.Scores), 2) 1252 t.Assert(user.Scores[0], &Score{ 1253 Name: "john", 1254 Result: 100, 1255 }) 1256 t.Assert(user.Scores[1], &Score{ 1257 Name: "smith", 1258 Result: 60, 1259 }) 1260 } 1261 }) 1262 } 1263 1264 func Test_Struct_PrivateAttribute_All(t *testing.T) { 1265 type User struct { 1266 Id int 1267 name string 1268 } 1269 gtest.C(t, func(t *gtest.T) { 1270 user := new(User) 1271 err := gconv.Struct(g.Map{"id": 1, "name": "john"}, user) 1272 t.Assert(err, nil) 1273 t.Assert(user.Id, 1) 1274 t.Assert(user.name, "") 1275 }) 1276 } 1277 1278 func Test_Struct_Embedded_All(t *testing.T) { 1279 gtest.C(t, func(t *gtest.T) { 1280 type Ids struct { 1281 Id int `json:"id"` 1282 Uid int `json:"uid"` 1283 } 1284 type Base struct { 1285 Ids 1286 CreateTime string `json:"create_time"` 1287 } 1288 type User struct { 1289 Base 1290 Passport string `json:"passport"` 1291 Password string `json:"password"` 1292 Nickname string `json:"nickname"` 1293 } 1294 data := g.Map{ 1295 "id": 100, 1296 "uid": 101, 1297 "passport": "t1", 1298 "password": "123456", 1299 "nickname": "T1", 1300 "create_time": "2019", 1301 } 1302 user := new(User) 1303 gconv.Struct(data, user) 1304 t.Assert(user.Id, 100) 1305 t.Assert(user.Uid, 101) 1306 t.Assert(user.Nickname, "T1") 1307 t.Assert(user.CreateTime, "2019") 1308 }) 1309 } 1310 1311 func Test_Struct_Time_All(t *testing.T) { 1312 gtest.C(t, func(t *gtest.T) { 1313 type User struct { 1314 CreateTime time.Time 1315 } 1316 now := time.Now() 1317 user := new(User) 1318 gconv.Struct(g.Map{ 1319 "create_time": now, 1320 }, user) 1321 t.Assert(user.CreateTime.UTC().String(), now.UTC().String()) 1322 }) 1323 1324 gtest.C(t, func(t *gtest.T) { 1325 type User struct { 1326 CreateTime *time.Time 1327 } 1328 now := time.Now() 1329 user := new(User) 1330 gconv.Struct(g.Map{ 1331 "create_time": &now, 1332 }, user) 1333 t.Assert(user.CreateTime.UTC().String(), now.UTC().String()) 1334 }) 1335 1336 gtest.C(t, func(t *gtest.T) { 1337 type User struct { 1338 CreateTime *gtime.Time 1339 } 1340 now := time.Now() 1341 user := new(User) 1342 gconv.Struct(g.Map{ 1343 "create_time": &now, 1344 }, user) 1345 t.Assert(user.CreateTime.Time.UTC().String(), now.UTC().String()) 1346 }) 1347 1348 gtest.C(t, func(t *gtest.T) { 1349 type User struct { 1350 CreateTime gtime.Time 1351 } 1352 now := time.Now() 1353 user := new(User) 1354 gconv.Struct(g.Map{ 1355 "create_time": &now, 1356 }, user) 1357 t.Assert(user.CreateTime.Time.UTC().String(), now.UTC().String()) 1358 }) 1359 1360 gtest.C(t, func(t *gtest.T) { 1361 type User struct { 1362 CreateTime gtime.Time 1363 } 1364 now := time.Now() 1365 user := new(User) 1366 gconv.Struct(g.Map{ 1367 "create_time": now, 1368 }, user) 1369 t.Assert(user.CreateTime.Time.UTC().String(), now.UTC().String()) 1370 }) 1371 }