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