github.com/wangyougui/gf/v2@v2.6.5/util/gconv/gconv_z_unit_slice_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/wangyougui/gf. 6 7 package gconv_test 8 9 import ( 10 "testing" 11 12 "github.com/wangyougui/gf/v2/container/gvar" 13 "github.com/wangyougui/gf/v2/database/gdb" 14 "github.com/wangyougui/gf/v2/encoding/gjson" 15 "github.com/wangyougui/gf/v2/frame/g" 16 "github.com/wangyougui/gf/v2/test/gtest" 17 "github.com/wangyougui/gf/v2/util/gconv" 18 ) 19 20 func Test_Slice(t *testing.T) { 21 gtest.C(t, func(t *gtest.T) { 22 value := 123.456 23 t.AssertEQ(gconv.Bytes("123"), []byte("123")) 24 t.AssertEQ(gconv.Bytes([]interface{}{1}), []byte{1}) 25 t.AssertEQ(gconv.Bytes([]interface{}{300}), []byte("[300]")) 26 t.AssertEQ(gconv.Strings(value), []string{"123.456"}) 27 t.AssertEQ(gconv.SliceStr(value), []string{"123.456"}) 28 t.AssertEQ(gconv.SliceInt(value), []int{123}) 29 t.AssertEQ(gconv.SliceUint(value), []uint{123}) 30 t.AssertEQ(gconv.SliceUint32(value), []uint32{123}) 31 t.AssertEQ(gconv.SliceUint64(value), []uint64{123}) 32 t.AssertEQ(gconv.SliceInt32(value), []int32{123}) 33 t.AssertEQ(gconv.SliceInt64(value), []int64{123}) 34 t.AssertEQ(gconv.Ints(value), []int{123}) 35 t.AssertEQ(gconv.SliceFloat(value), []float64{123.456}) 36 t.AssertEQ(gconv.Floats(value), []float64{123.456}) 37 t.AssertEQ(gconv.SliceFloat32(value), []float32{123.456}) 38 t.AssertEQ(gconv.SliceFloat64(value), []float64{123.456}) 39 t.AssertEQ(gconv.Interfaces(value), []interface{}{123.456}) 40 t.AssertEQ(gconv.SliceAny(" [26, 27] "), []interface{}{26, 27}) 41 }) 42 gtest.C(t, func(t *gtest.T) { 43 s := gvar.Vars{ 44 gvar.New(1), 45 gvar.New(2), 46 } 47 t.AssertEQ(gconv.SliceInt64(s), []int64{1, 2}) 48 }) 49 } 50 51 func Test_Slice_Ints(t *testing.T) { 52 gtest.C(t, func(t *gtest.T) { 53 t.AssertEQ(gconv.Ints(nil), nil) 54 t.AssertEQ(gconv.Ints("[26, 27]"), []int{26, 27}) 55 t.AssertEQ(gconv.Ints(" [26, 27] "), []int{26, 27}) 56 t.AssertEQ(gconv.Ints([]uint8(`[{"id": 1, "name":"john"},{"id": 2, "name":"huang"}]`)), []int{0, 0}) 57 t.AssertEQ(gconv.Ints([]bool{true, false}), []int{1, 0}) 58 t.AssertEQ(gconv.Ints([][]byte{{byte(1)}, {byte(2)}}), []int{1, 2}) 59 }) 60 } 61 62 func Test_Slice_Int32s(t *testing.T) { 63 gtest.C(t, func(t *gtest.T) { 64 t.AssertEQ(gconv.Int32s(nil), nil) 65 t.AssertEQ(gconv.Int32s(" [26, 27] "), []int32{26, 27}) 66 t.AssertEQ(gconv.Int32s([]string{"1", "2"}), []int32{1, 2}) 67 t.AssertEQ(gconv.Int32s([]int{1, 2}), []int32{1, 2}) 68 t.AssertEQ(gconv.Int32s([]int8{1, 2}), []int32{1, 2}) 69 t.AssertEQ(gconv.Int32s([]int16{1, 2}), []int32{1, 2}) 70 t.AssertEQ(gconv.Int32s([]int32{1, 2}), []int32{1, 2}) 71 t.AssertEQ(gconv.Int32s([]int64{1, 2}), []int32{1, 2}) 72 t.AssertEQ(gconv.Int32s([]uint{1, 2}), []int32{1, 2}) 73 t.AssertEQ(gconv.Int32s([]uint8{1, 2}), []int32{1, 2}) 74 t.AssertEQ(gconv.Int32s([]uint8(`[{"id": 1, "name":"john"},{"id": 2, "name":"huang"}]`)), []int32{0, 0}) 75 t.AssertEQ(gconv.Int32s([]uint16{1, 2}), []int32{1, 2}) 76 t.AssertEQ(gconv.Int32s([]uint32{1, 2}), []int32{1, 2}) 77 t.AssertEQ(gconv.Int32s([]uint64{1, 2}), []int32{1, 2}) 78 t.AssertEQ(gconv.Int32s([]bool{true, false}), []int32{1, 0}) 79 t.AssertEQ(gconv.Int32s([]float32{1, 2}), []int32{1, 2}) 80 t.AssertEQ(gconv.Int32s([]float64{1, 2}), []int32{1, 2}) 81 t.AssertEQ(gconv.Int32s([][]byte{{byte(1)}, {byte(2)}}), []int32{1, 2}) 82 83 s := gvar.Vars{ 84 gvar.New(1), 85 gvar.New(2), 86 } 87 t.AssertEQ(gconv.SliceInt32(s), []int32{1, 2}) 88 }) 89 } 90 91 func Test_Slice_Int64s(t *testing.T) { 92 gtest.C(t, func(t *gtest.T) { 93 t.AssertEQ(gconv.Int64s(nil), nil) 94 t.AssertEQ(gconv.Int64s(" [26, 27] "), []int64{26, 27}) 95 t.AssertEQ(gconv.Int64s([]string{"1", "2"}), []int64{1, 2}) 96 t.AssertEQ(gconv.Int64s([]int{1, 2}), []int64{1, 2}) 97 t.AssertEQ(gconv.Int64s([]int8{1, 2}), []int64{1, 2}) 98 t.AssertEQ(gconv.Int64s([]int16{1, 2}), []int64{1, 2}) 99 t.AssertEQ(gconv.Int64s([]int32{1, 2}), []int64{1, 2}) 100 t.AssertEQ(gconv.Int64s([]int64{1, 2}), []int64{1, 2}) 101 t.AssertEQ(gconv.Int64s([]uint{1, 2}), []int64{1, 2}) 102 t.AssertEQ(gconv.Int64s([]uint8{1, 2}), []int64{1, 2}) 103 t.AssertEQ(gconv.Int64s([]uint8(`[{"id": 1, "name":"john"},{"id": 2, "name":"huang"}]`)), []int64{0, 0}) 104 t.AssertEQ(gconv.Int64s([]uint16{1, 2}), []int64{1, 2}) 105 t.AssertEQ(gconv.Int64s([]uint32{1, 2}), []int64{1, 2}) 106 t.AssertEQ(gconv.Int64s([]uint64{1, 2}), []int64{1, 2}) 107 t.AssertEQ(gconv.Int64s([]bool{true, false}), []int64{1, 0}) 108 t.AssertEQ(gconv.Int64s([]float32{1, 2}), []int64{1, 2}) 109 t.AssertEQ(gconv.Int64s([]float64{1, 2}), []int64{1, 2}) 110 t.AssertEQ(gconv.Int64s([][]byte{{byte(1)}, {byte(2)}}), []int64{1, 2}) 111 112 s := gvar.Vars{ 113 gvar.New(1), 114 gvar.New(2), 115 } 116 t.AssertEQ(gconv.Int64s(s), []int64{1, 2}) 117 }) 118 } 119 120 func Test_Slice_Uints(t *testing.T) { 121 gtest.C(t, func(t *gtest.T) { 122 t.AssertEQ(gconv.Uints(nil), nil) 123 t.AssertEQ(gconv.Uints("1"), []uint{1}) 124 t.AssertEQ(gconv.Uints(" [26, 27] "), []uint{26, 27}) 125 t.AssertEQ(gconv.Uints([]string{"1", "2"}), []uint{1, 2}) 126 t.AssertEQ(gconv.Uints([]int{1, 2}), []uint{1, 2}) 127 t.AssertEQ(gconv.Uints([]int8{1, 2}), []uint{1, 2}) 128 t.AssertEQ(gconv.Uints([]int16{1, 2}), []uint{1, 2}) 129 t.AssertEQ(gconv.Uints([]int32{1, 2}), []uint{1, 2}) 130 t.AssertEQ(gconv.Uints([]int64{1, 2}), []uint{1, 2}) 131 t.AssertEQ(gconv.Uints([]uint{1, 2}), []uint{1, 2}) 132 t.AssertEQ(gconv.Uints([]uint8{1, 2}), []uint{1, 2}) 133 t.AssertEQ(gconv.Uints([]uint8(`[{"id": 1, "name":"john"},{"id": 2, "name":"huang"}]`)), []uint{0, 0}) 134 t.AssertEQ(gconv.Uints([]uint16{1, 2}), []uint{1, 2}) 135 t.AssertEQ(gconv.Uints([]uint32{1, 2}), []uint{1, 2}) 136 t.AssertEQ(gconv.Uints([]uint64{1, 2}), []uint{1, 2}) 137 t.AssertEQ(gconv.Uints([]bool{true, false}), []uint{1, 0}) 138 t.AssertEQ(gconv.Uints([]float32{1, 2}), []uint{1, 2}) 139 t.AssertEQ(gconv.Uints([]float64{1, 2}), []uint{1, 2}) 140 t.AssertEQ(gconv.Uints([][]byte{{byte(1)}, {byte(2)}}), []uint{1, 2}) 141 142 s := gvar.Vars{ 143 gvar.New(1), 144 gvar.New(2), 145 } 146 t.AssertEQ(gconv.Uints(s), []uint{1, 2}) 147 }) 148 } 149 150 func Test_Slice_Uint32s(t *testing.T) { 151 gtest.C(t, func(t *gtest.T) { 152 t.AssertEQ(gconv.Uint32s(nil), nil) 153 t.AssertEQ(gconv.Uint32s("1"), []uint32{1}) 154 t.AssertEQ(gconv.Uint32s(" [26, 27] "), []uint32{26, 27}) 155 t.AssertEQ(gconv.Uint32s([]string{"1", "2"}), []uint32{1, 2}) 156 t.AssertEQ(gconv.Uint32s([]int{1, 2}), []uint32{1, 2}) 157 t.AssertEQ(gconv.Uint32s([]int8{1, 2}), []uint32{1, 2}) 158 t.AssertEQ(gconv.Uint32s([]int16{1, 2}), []uint32{1, 2}) 159 t.AssertEQ(gconv.Uint32s([]int32{1, 2}), []uint32{1, 2}) 160 t.AssertEQ(gconv.Uint32s([]int64{1, 2}), []uint32{1, 2}) 161 t.AssertEQ(gconv.Uint32s([]uint{1, 2}), []uint32{1, 2}) 162 t.AssertEQ(gconv.Uint32s([]uint8{1, 2}), []uint32{1, 2}) 163 t.AssertEQ(gconv.Uint32s([]uint8(`[{"id": 1, "name":"john"},{"id": 2, "name":"huang"}]`)), []uint32{0, 0}) 164 t.AssertEQ(gconv.Uint32s([]uint16{1, 2}), []uint32{1, 2}) 165 t.AssertEQ(gconv.Uint32s([]uint32{1, 2}), []uint32{1, 2}) 166 t.AssertEQ(gconv.Uint32s([]uint64{1, 2}), []uint32{1, 2}) 167 t.AssertEQ(gconv.Uint32s([]bool{true, false}), []uint32{1, 0}) 168 t.AssertEQ(gconv.Uint32s([]float32{1, 2}), []uint32{1, 2}) 169 t.AssertEQ(gconv.Uint32s([]float64{1, 2}), []uint32{1, 2}) 170 t.AssertEQ(gconv.Uint32s([][]byte{{byte(1)}, {byte(2)}}), []uint32{1, 2}) 171 172 s := gvar.Vars{ 173 gvar.New(1), 174 gvar.New(2), 175 } 176 t.AssertEQ(gconv.Uint32s(s), []uint32{1, 2}) 177 }) 178 } 179 180 func Test_Slice_Uint64s(t *testing.T) { 181 gtest.C(t, func(t *gtest.T) { 182 t.AssertEQ(gconv.Uint64s(nil), nil) 183 t.AssertEQ(gconv.Uint64s("1"), []uint64{1}) 184 t.AssertEQ(gconv.Uint64s(" [26, 27] "), []uint64{26, 27}) 185 t.AssertEQ(gconv.Uint64s([]string{"1", "2"}), []uint64{1, 2}) 186 t.AssertEQ(gconv.Uint64s([]int{1, 2}), []uint64{1, 2}) 187 t.AssertEQ(gconv.Uint64s([]int8{1, 2}), []uint64{1, 2}) 188 t.AssertEQ(gconv.Uint64s([]int16{1, 2}), []uint64{1, 2}) 189 t.AssertEQ(gconv.Uint64s([]int32{1, 2}), []uint64{1, 2}) 190 t.AssertEQ(gconv.Uint64s([]int64{1, 2}), []uint64{1, 2}) 191 t.AssertEQ(gconv.Uint64s([]uint{1, 2}), []uint64{1, 2}) 192 t.AssertEQ(gconv.Uint64s([]uint8{1, 2}), []uint64{1, 2}) 193 t.AssertEQ(gconv.Uint64s([]uint8(`[{"id": 1, "name":"john"},{"id": 2, "name":"huang"}]`)), []uint64{0, 0}) 194 t.AssertEQ(gconv.Uint64s([]uint16{1, 2}), []uint64{1, 2}) 195 t.AssertEQ(gconv.Uint64s([]uint64{1, 2}), []uint64{1, 2}) 196 t.AssertEQ(gconv.Uint64s([]uint64{1, 2}), []uint64{1, 2}) 197 t.AssertEQ(gconv.Uint64s([]bool{true, false}), []uint64{1, 0}) 198 t.AssertEQ(gconv.Uint64s([]float32{1, 2}), []uint64{1, 2}) 199 t.AssertEQ(gconv.Uint64s([]float64{1, 2}), []uint64{1, 2}) 200 t.AssertEQ(gconv.Uint64s([][]byte{{byte(1)}, {byte(2)}}), []uint64{1, 2}) 201 202 s := gvar.Vars{ 203 gvar.New(1), 204 gvar.New(2), 205 } 206 t.AssertEQ(gconv.Uint64s(s), []uint64{1, 2}) 207 }) 208 } 209 210 func Test_Slice_Float32s(t *testing.T) { 211 gtest.C(t, func(t *gtest.T) { 212 t.AssertEQ(gconv.Float32s("123.4"), []float32{123.4}) 213 t.AssertEQ(gconv.Float32s([]string{"123.4", "123.5"}), []float32{123.4, 123.5}) 214 t.AssertEQ(gconv.Float32s([]int{123}), []float32{123}) 215 t.AssertEQ(gconv.Float32s([]int8{123}), []float32{123}) 216 t.AssertEQ(gconv.Float32s([]int16{123}), []float32{123}) 217 t.AssertEQ(gconv.Float32s([]int32{123}), []float32{123}) 218 t.AssertEQ(gconv.Float32s([]int64{123}), []float32{123}) 219 t.AssertEQ(gconv.Float32s([]uint{123}), []float32{123}) 220 t.AssertEQ(gconv.Float32s([]uint8{123}), []float32{123}) 221 t.AssertEQ(gconv.Float32s([]uint8(`[{"id": 1, "name":"john"},{"id": 2, "name":"huang"}]`)), []float32{0, 0}) 222 t.AssertEQ(gconv.Float32s([]uint16{123}), []float32{123}) 223 t.AssertEQ(gconv.Float32s([]uint32{123}), []float32{123}) 224 t.AssertEQ(gconv.Float32s([]uint64{123}), []float32{123}) 225 t.AssertEQ(gconv.Float32s([]bool{true, false}), []float32{0, 0}) 226 t.AssertEQ(gconv.Float32s([]float32{123}), []float32{123}) 227 t.AssertEQ(gconv.Float32s([]float64{123}), []float32{123}) 228 229 s := gvar.Vars{ 230 gvar.New(1.1), 231 gvar.New(2.1), 232 } 233 t.AssertEQ(gconv.SliceFloat32(s), []float32{1.1, 2.1}) 234 }) 235 } 236 237 func Test_Slice_Float64s(t *testing.T) { 238 gtest.C(t, func(t *gtest.T) { 239 t.AssertEQ(gconv.Float64s("123.4"), []float64{123.4}) 240 t.AssertEQ(gconv.Float64s([]string{"123.4", "123.5"}), []float64{123.4, 123.5}) 241 t.AssertEQ(gconv.Float64s([]int{123}), []float64{123}) 242 t.AssertEQ(gconv.Float64s([]int8{123}), []float64{123}) 243 t.AssertEQ(gconv.Float64s([]int16{123}), []float64{123}) 244 t.AssertEQ(gconv.Float64s([]int32{123}), []float64{123}) 245 t.AssertEQ(gconv.Float64s([]int64{123}), []float64{123}) 246 t.AssertEQ(gconv.Float64s([]uint{123}), []float64{123}) 247 t.AssertEQ(gconv.Float64s([]uint8{123}), []float64{123}) 248 t.AssertEQ(gconv.Float64s([]uint8(`[{"id": 1, "name":"john"},{"id": 2, "name":"huang"}]`)), []float64{0, 0}) 249 t.AssertEQ(gconv.Float64s([]uint16{123}), []float64{123}) 250 t.AssertEQ(gconv.Float64s([]uint32{123}), []float64{123}) 251 t.AssertEQ(gconv.Float64s([]uint64{123}), []float64{123}) 252 t.AssertEQ(gconv.Float64s([]bool{true, false}), []float64{0, 0}) 253 t.AssertEQ(gconv.Float64s([]float32{123}), []float64{123}) 254 t.AssertEQ(gconv.Float64s([]float64{123}), []float64{123}) 255 }) 256 } 257 258 func Test_Slice_Empty(t *testing.T) { 259 // Int. 260 gtest.C(t, func(t *gtest.T) { 261 t.AssertEQ(gconv.Ints(""), []int{}) 262 t.Assert(gconv.Ints(nil), nil) 263 }) 264 gtest.C(t, func(t *gtest.T) { 265 t.AssertEQ(gconv.Int32s(""), []int32{}) 266 t.Assert(gconv.Int32s(nil), nil) 267 }) 268 gtest.C(t, func(t *gtest.T) { 269 t.AssertEQ(gconv.Int64s(""), []int64{}) 270 t.Assert(gconv.Int64s(nil), nil) 271 }) 272 // Uint. 273 gtest.C(t, func(t *gtest.T) { 274 t.AssertEQ(gconv.Uints(""), []uint{}) 275 t.Assert(gconv.Uints(nil), nil) 276 }) 277 gtest.C(t, func(t *gtest.T) { 278 t.AssertEQ(gconv.Uint32s(""), []uint32{}) 279 t.Assert(gconv.Uint32s(nil), nil) 280 }) 281 gtest.C(t, func(t *gtest.T) { 282 t.AssertEQ(gconv.Uint64s(""), []uint64{}) 283 t.Assert(gconv.Uint64s(nil), nil) 284 }) 285 // Float. 286 gtest.C(t, func(t *gtest.T) { 287 t.AssertEQ(gconv.Floats(""), []float64{}) 288 t.Assert(gconv.Floats(nil), nil) 289 }) 290 gtest.C(t, func(t *gtest.T) { 291 t.AssertEQ(gconv.Float32s(""), []float32{}) 292 t.Assert(gconv.Float32s(nil), nil) 293 }) 294 gtest.C(t, func(t *gtest.T) { 295 t.AssertEQ(gconv.Float64s(""), []float64{}) 296 t.Assert(gconv.Float64s(nil), nil) 297 }) 298 gtest.C(t, func(t *gtest.T) { 299 t.AssertEQ(gconv.Strings(""), []string{}) 300 t.Assert(gconv.Strings(nil), nil) 301 }) 302 gtest.C(t, func(t *gtest.T) { 303 t.AssertEQ(gconv.SliceAny(""), []interface{}{""}) 304 t.Assert(gconv.SliceAny(nil), nil) 305 }) 306 } 307 308 func Test_Strings(t *testing.T) { 309 gtest.C(t, func(t *gtest.T) { 310 array := []*g.Var{ 311 g.NewVar(1), 312 g.NewVar(2), 313 g.NewVar(3), 314 } 315 t.AssertEQ(gconv.Strings(array), []string{"1", "2", "3"}) 316 317 t.AssertEQ(gconv.Strings([]uint8(`["1","2"]`)), []string{"1", "2"}) 318 t.AssertEQ(gconv.Strings([][]byte{{byte(0)}, {byte(1)}}), []string{"\u0000", "\u0001"}) 319 }) 320 // https://github.com/wangyougui/gf/issues/1750 321 gtest.C(t, func(t *gtest.T) { 322 t.AssertEQ(gconv.Strings("123"), []string{"123"}) 323 }) 324 } 325 326 func Test_Slice_Interfaces(t *testing.T) { 327 gtest.C(t, func(t *gtest.T) { 328 array := gconv.Interfaces([]uint8(`[{"id": 1, "name":"john"},{"id": 2, "name":"huang"}]`)) 329 t.Assert(len(array), 2) 330 t.Assert(array[0].(g.Map)["id"], 1) 331 t.Assert(array[0].(g.Map)["name"], "john") 332 }) 333 // map 334 gtest.C(t, func(t *gtest.T) { 335 array := gconv.Interfaces(g.Map{ 336 "id": 1, 337 "name": "john", 338 }) 339 t.Assert(len(array), 1) 340 t.Assert(array[0].(g.Map)["id"], 1) 341 t.Assert(array[0].(g.Map)["name"], "john") 342 }) 343 // struct 344 gtest.C(t, func(t *gtest.T) { 345 type A struct { 346 Id int `json:"id"` 347 Name string 348 } 349 array := gconv.Interfaces(&A{ 350 Id: 1, 351 Name: "john", 352 }) 353 t.Assert(len(array), 1) 354 t.Assert(array[0].(*A).Id, 1) 355 t.Assert(array[0].(*A).Name, "john") 356 }) 357 } 358 359 func Test_Slice_PrivateAttribute(t *testing.T) { 360 type User struct { 361 Id int `json:"id"` 362 name string `json:"name"` 363 } 364 gtest.C(t, func(t *gtest.T) { 365 user := &User{1, "john"} 366 array := gconv.Interfaces(user) 367 t.Assert(len(array), 1) 368 t.Assert(array[0].(*User).Id, 1) 369 t.Assert(array[0].(*User).name, "john") 370 }) 371 } 372 373 func Test_Slice_Structs(t *testing.T) { 374 type Base struct { 375 Age int 376 } 377 type User struct { 378 Id int 379 Name string 380 Base 381 } 382 383 gtest.C(t, func(t *gtest.T) { 384 users := make([]User, 0) 385 params := []g.Map{ 386 {"id": 1, "name": "john", "age": 18}, 387 {"id": 2, "name": "smith", "age": 20}, 388 } 389 err := gconv.Structs(params, &users) 390 t.AssertNil(err) 391 t.Assert(len(users), 2) 392 t.Assert(users[0].Id, params[0]["id"]) 393 t.Assert(users[0].Name, params[0]["name"]) 394 t.Assert(users[0].Age, 18) 395 396 t.Assert(users[1].Id, params[1]["id"]) 397 t.Assert(users[1].Name, params[1]["name"]) 398 t.Assert(users[1].Age, 20) 399 }) 400 401 gtest.C(t, func(t *gtest.T) { 402 users := make([]User, 0) 403 params := []g.Map{ 404 {"id": 1, "name": "john", "age": 18}, 405 {"id": 2, "name": "smith", "age": 20}, 406 } 407 err := gconv.StructsTag(params, &users, "") 408 t.AssertNil(err) 409 t.Assert(len(users), 2) 410 t.Assert(users[0].Id, params[0]["id"]) 411 t.Assert(users[0].Name, params[0]["name"]) 412 t.Assert(users[0].Age, 18) 413 414 t.Assert(users[1].Id, params[1]["id"]) 415 t.Assert(users[1].Name, params[1]["name"]) 416 t.Assert(users[1].Age, 20) 417 }) 418 } 419 420 func Test_EmptyString_To_CustomType(t *testing.T) { 421 gtest.C(t, func(t *gtest.T) { 422 type Status string 423 type Req struct { 424 Name string 425 Statuses []Status 426 Types []string 427 } 428 var ( 429 req *Req 430 data = g.Map{ 431 "Name": "john", 432 "Statuses": "", 433 "Types": "", 434 } 435 ) 436 err := gconv.Scan(data, &req) 437 t.AssertNil(err) 438 t.Assert(len(req.Statuses), 0) 439 t.Assert(len(req.Types), 0) 440 }) 441 gtest.C(t, func(t *gtest.T) { 442 type Status string 443 type Req struct { 444 Name string 445 Statuses []*Status 446 Types []string 447 } 448 var ( 449 req *Req 450 data = g.Map{ 451 "Name": "john", 452 "Statuses": "", 453 "Types": "", 454 } 455 ) 456 err := gconv.Scan(data, &req) 457 t.AssertNil(err) 458 t.Assert(len(req.Statuses), 0) 459 t.Assert(len(req.Types), 0) 460 }) 461 } 462 463 func Test_SliceMap_WithNilMapValue(t *testing.T) { 464 gtest.C(t, func(t *gtest.T) { 465 var ( 466 list1 = []gdb.Record{ 467 {"name": nil}, 468 } 469 list2 []map[string]any 470 ) 471 list2 = gconv.SliceMap(list1) 472 t.Assert(len(list2), 1) 473 t.Assert(list1[0], list2[0]) 474 t.Assert(gjson.MustEncodeString(list1), gjson.MustEncodeString(list2)) 475 }) 476 }