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