github.com/isyscore/isc-gobase@v1.5.3-0.20231218061332-cbc7451899e9/isc/test/util_test.go (about) 1 package test 2 3 import ( 4 "encoding/json" 5 "github.com/isyscore/isc-gobase/test" 6 "testing" 7 8 "github.com/isyscore/isc-gobase/isc" 9 ) 10 11 // 对以下的api进行测试 12 // 13 // mapToObject 14 // strToObject 15 // arrayToObject 16 // dataToObject:这个是总况 17 // 18 // objectToJson 19 // objectToData:这个是总的 20 21 // mapToObject 22 type ValueInnerEntity1 struct { 23 Name string 24 Age int 25 } 26 27 func TestMapToObject1(t *testing.T) { 28 inner1 := map[string]any{} 29 inner1["name"] = "inner_1" 30 inner1["age"] = 1 31 32 var targetObj ValueInnerEntity1 33 _ = isc.MapToObject(inner1, &targetObj) 34 test.Equal(t, "{\"Name\":\"inner_1\",\"Age\":1}", isc.ToJsonString(targetObj)) 35 } 36 37 type ValueInnerEntity2 struct { 38 Name string 39 Age int 40 Inner1 ValueInnerEntity1 41 } 42 43 func TestMapToObject2(t *testing.T) { 44 inner1 := map[string]any{} 45 inner1["name"] = "inner_1" 46 inner1["age"] = 1 47 48 inner2 := map[string]any{} 49 inner2["name"] = "inner_2" 50 inner2["age"] = 2 51 inner2["inner1"] = inner1 52 53 var targetObj ValueInnerEntity2 54 _ = isc.MapToObject(inner2, &targetObj) 55 test.Equal(t, "{\"Name\":\"inner_2\",\"Age\":2,\"Inner1\":{\"Name\":\"inner_1\",\"Age\":1}}", isc.ToJsonString(targetObj)) 56 } 57 58 type ValueInnerEntity3 struct { 59 Name string 60 Age int 61 Inner2 ValueInnerEntity2 62 } 63 64 func TestMapToObject3(t *testing.T) { 65 inner1 := map[string]any{} 66 inner1["name"] = "inner_1" 67 inner1["age"] = 1 68 69 inner2 := map[string]any{} 70 inner2["name"] = "inner_2" 71 inner2["age"] = 2 72 inner2["inner1"] = inner1 73 74 inner3 := map[string]any{} 75 inner3["name"] = "inner_3" 76 inner3["age"] = 3 77 inner3["inner2"] = inner2 78 79 var targetObj ValueInnerEntity3 80 _ = isc.MapToObject(inner3, &targetObj) 81 test.Equal(t, "{\"Name\":\"inner_3\",\"Age\":3,\"Inner2\":{\"Name\":\"inner_2\",\"Age\":2,\"Inner1\":{\"Name\":\"inner_1\",\"Age\":1}}}", isc.ToJsonString(targetObj)) 82 } 83 84 type ValueInnerEntity4 struct { 85 Name string 86 Age int 87 DataMap map[string]string 88 } 89 90 func TestMapToObject4(t *testing.T) { 91 kvMap := map[string]any{} 92 kvMap["k1"] = "name1" 93 kvMap["k2"] = "name2" 94 95 inner1 := map[string]any{} 96 inner1["name"] = "inner_1" 97 inner1["age"] = 1 98 inner1["dataMap"] = kvMap 99 100 var targetObj ValueInnerEntity4 101 _ = isc.MapToObject(inner1, &targetObj) 102 test.Equal(t, "{\"Name\":\"inner_1\",\"Age\":1,\"DataMap\":{\"k1\":\"name1\",\"k2\":\"name2\"}}", isc.ToJsonString(targetObj)) 103 } 104 105 type ValueInnerEntity5 struct { 106 Name string 107 Age int 108 DataMap map[string]ValueInnerEntity1 109 } 110 111 func TestMapToObject5(t *testing.T) { 112 v1 := map[string]any{} 113 v1["name"] = "inner_1" 114 v1["age"] = 1 115 116 v2 := map[string]any{} 117 v2["name"] = "inner_2" 118 v2["age"] = 2 119 120 kvMap := map[string]any{} 121 kvMap["k1"] = v1 122 kvMap["k2"] = v2 123 124 inner1 := map[string]any{} 125 inner1["name"] = "inner_1" 126 inner1["age"] = 1 127 inner1["dataMap"] = kvMap 128 129 var targetObj ValueInnerEntity5 130 _ = isc.MapToObject(inner1, &targetObj) 131 test.Equal(t, "{\"Name\":\"inner_1\",\"Age\":1,\"DataMap\":{\"k1\":{\"Name\":\"inner_1\",\"Age\":1},\"k2\":{\"Name\":\"inner_2\",\"Age\":2}}}", isc.ToJsonString(targetObj)) 132 } 133 134 type ValueInnerEntity6 struct { 135 Name string 136 Age int 137 DataMap map[string][]int 138 } 139 140 func TestMapToObject6(t *testing.T) { 141 var dataList []int 142 dataList = append(dataList, 12) 143 dataList = append(dataList, 13) 144 145 kvMap := map[string]any{} 146 kvMap["k1"] = dataList 147 kvMap["k2"] = dataList 148 149 inner1 := map[string]any{} 150 inner1["name"] = "inner_1" 151 inner1["age"] = 1 152 inner1["dataMap"] = kvMap 153 154 var targetObj ValueInnerEntity6 155 _ = isc.MapToObject(inner1, &targetObj) 156 test.Equal(t, "{\"Name\":\"inner_1\",\"Age\":1,\"DataMap\":{\"k1\":[12,13],\"k2\":[12,13]}}", isc.ToJsonString(targetObj)) 157 } 158 159 type ValueInnerEntity7 struct { 160 Name string 161 Age int 162 DataMap map[string][]ValueInnerEntity1 163 } 164 165 func TestMapToObject7(t *testing.T) { 166 var dataList []ValueInnerEntity1 167 dataList = append(dataList, ValueInnerEntity1{Name: "name1", Age: 1}) 168 dataList = append(dataList, ValueInnerEntity1{Name: "name2", Age: 2}) 169 170 kvMap := map[string]any{} 171 kvMap["k1"] = dataList 172 kvMap["k2"] = dataList 173 174 inner1 := map[string]any{} 175 inner1["name"] = "inner_1" 176 inner1["age"] = 1 177 inner1["dataMap"] = kvMap 178 179 var targetObj ValueInnerEntity7 180 _ = isc.MapToObject(inner1, &targetObj) 181 test.Equal(t, "{\"Name\":\"inner_1\",\"Age\":1,\"DataMap\":{\"k1\":[{\"Name\":\"name1\",\"Age\":1},{\"Name\":\"name2\",\"Age\":2}],\"k2\":[{\"Name\":\"name1\",\"Age\":1},{\"Name\":\"name2\",\"Age\":2}]}}", isc.ToJsonString(targetObj)) 182 } 183 184 type ValueInnerEntity1Tem struct { 185 Name string 186 Address string 187 } 188 189 type ValueInnerEntity8 struct { 190 Name string 191 Age int 192 DataMap map[string][]ValueInnerEntity1Tem 193 } 194 195 func TestMapToObject8(t *testing.T) { 196 var dataList []ValueInnerEntity1 197 dataList = append(dataList, ValueInnerEntity1{Name: "name1", Age: 1}) 198 dataList = append(dataList, ValueInnerEntity1{Name: "name2", Age: 2}) 199 200 kvMap := map[string]any{} 201 kvMap["k1"] = dataList 202 kvMap["k2"] = dataList 203 204 inner1 := map[string]any{} 205 inner1["name"] = "inner_1" 206 inner1["age"] = 1 207 inner1["dataMap"] = kvMap 208 209 var targetObj ValueInnerEntity8 210 _ = isc.MapToObject(inner1, &targetObj) 211 test.Equal(t, "{\"Name\":\"inner_1\",\"Age\":1,\"DataMap\":{\"k1\":[{\"Name\":\"name1\",\"Address\":\"\"},{\"Name\":\"name2\",\"Address\":\"\"}],\"k2\":[{\"Name\":\"name1\",\"Address\":\"\"},{\"Name\":\"name2\",\"Address\":\"\"}]}}", isc.ToJsonString(targetObj)) 212 } 213 214 type ValueInnerEntity9Tem struct { 215 Name string 216 Age string 217 } 218 219 type ValueInnerEntity9 struct { 220 Name string 221 Age int 222 DataMap map[string][]ValueInnerEntity1 223 } 224 225 func TestMapToObject9(t *testing.T) { 226 var dataList []ValueInnerEntity9Tem 227 dataList = append(dataList, ValueInnerEntity9Tem{Name: "name1", Age: "1"}) 228 dataList = append(dataList, ValueInnerEntity9Tem{Name: "name2", Age: "2"}) 229 230 kvMap := map[string]any{} 231 kvMap["k1"] = dataList 232 kvMap["k2"] = dataList 233 234 inner1 := map[string]any{} 235 inner1["name"] = "inner_1" 236 inner1["age"] = 1 237 inner1["dataMap"] = kvMap 238 239 var targetObj ValueInnerEntity9 240 _ = isc.MapToObject(inner1, &targetObj) 241 test.Equal(t, "{\"Name\":\"inner_1\",\"Age\":1,\"DataMap\":{\"k1\":[{\"Name\":\"name1\",\"Age\":1},{\"Name\":\"name2\",\"Age\":2}],\"k2\":[{\"Name\":\"name1\",\"Age\":1},{\"Name\":\"name2\",\"Age\":2}]}}", isc.ToJsonString(targetObj)) 242 } 243 244 type ConfigValueTypeEnum int 245 246 const ( 247 YAML ConfigValueTypeEnum = 0 248 PROPERTIES ConfigValueTypeEnum = 1 249 JSON ConfigValueTypeEnum = 2 250 STRING ConfigValueTypeEnum = 3 251 ) 252 253 type ValueInnerEntity10 struct { 254 Name string 255 Age ConfigValueTypeEnum 256 DataMap map[string][]ValueInnerEntity1 257 } 258 259 func TestMapToObject10(t *testing.T) { 260 var dataList []ValueInnerEntity9Tem 261 dataList = append(dataList, ValueInnerEntity9Tem{Name: "name1", Age: "1"}) 262 dataList = append(dataList, ValueInnerEntity9Tem{Name: "name2", Age: "2"}) 263 264 kvMap := map[string]any{} 265 kvMap["k1"] = dataList 266 kvMap["k2"] = dataList 267 268 inner1 := map[string]any{} 269 inner1["name"] = "inner_1" 270 inner1["age"] = 1 271 inner1["dataMap"] = kvMap 272 273 var targetObj ValueInnerEntity9 274 _ = isc.MapToObject(inner1, &targetObj) 275 test.Equal(t, "{\"Name\":\"inner_1\",\"Age\":1,\"DataMap\":{\"k1\":[{\"Name\":\"name1\",\"Age\":1},{\"Name\":\"name2\",\"Age\":2}],\"k2\":[{\"Name\":\"name1\",\"Age\":1},{\"Name\":\"name2\",\"Age\":2}]}}", isc.ToJsonString(targetObj)) 276 } 277 278 func TestMapToObject11(t *testing.T) { 279 inner1 := map[string]any{} 280 inner1["name"] = "inner_1" 281 inner1["age"] = 12 282 283 inner2 := map[string]any{} 284 285 _ = isc.MapToObject(inner1, &inner2) 286 test.Equal(t, isc.ToJsonString(inner2), "{\"age\":12,\"name\":\"inner_1\"}") 287 } 288 289 func TestMapToObject12(t *testing.T) { 290 inner1 := map[string]string{} 291 inner1["name"] = "inner_1" 292 inner1["age"] = "12" 293 294 inner2 := map[string]any{} 295 296 _ = isc.MapToObject(inner1, &inner2) 297 test.Equal(t, "{\"age\":\"12\",\"name\":\"inner_1\"}", isc.ToJsonString(inner2)) 298 } 299 300 func TestMapToObject13(t *testing.T) { 301 inner1 := map[string]any{} 302 inner1["age"] = 12 303 304 inner2 := map[string]int{} 305 306 _ = isc.MapToObject(inner1, &inner2) 307 test.Equal(t, "{\"age\":12}", isc.ToJsonString(inner2)) 308 } 309 310 // dataToObject 311 func TestDataToObject1(t *testing.T) { 312 inner1 := map[string]any{} 313 inner1["name"] = "inner_1" 314 inner1["age"] = 1 315 316 var targetObj ValueInnerEntity1 317 _ = isc.DataToObject(inner1, &targetObj) 318 test.Equal(t, "{\"Name\":\"inner_1\",\"Age\":1}", isc.ToJsonString(targetObj)) 319 } 320 321 func TestDataToObject2(t *testing.T) { 322 inner1 := map[string]any{} 323 inner1["name"] = "inner_1" 324 inner1["age"] = 1 325 326 inner2 := map[string]any{} 327 inner2["name"] = "inner_2" 328 inner2["age"] = 2 329 inner2["inner1"] = inner1 330 331 var targetObj ValueInnerEntity2 332 _ = isc.DataToObject(inner2, &targetObj) 333 test.Equal(t, "{\"Name\":\"inner_2\",\"Age\":2,\"Inner1\":{\"Name\":\"inner_1\",\"Age\":1}}", isc.ToJsonString(targetObj)) 334 } 335 336 func TestDataToObject3(t *testing.T) { 337 inner1 := map[string]any{} 338 inner1["name"] = "inner_1" 339 inner1["age"] = 1 340 341 inner2 := map[string]any{} 342 inner2["name"] = "inner_2" 343 inner2["age"] = 2 344 inner2["inner1"] = inner1 345 346 inner3 := map[string]any{} 347 inner3["name"] = "inner_3" 348 inner3["age"] = 3 349 inner3["inner2"] = inner2 350 351 var targetObj ValueInnerEntity3 352 _ = isc.DataToObject(inner3, &targetObj) 353 test.Equal(t, "{\"Name\":\"inner_3\",\"Age\":3,\"Inner2\":{\"Name\":\"inner_2\",\"Age\":2,\"Inner1\":{\"Name\":\"inner_1\",\"Age\":1}}}", isc.ToJsonString(targetObj)) 354 } 355 356 func TestDataToObject4(t *testing.T) { 357 kvMap := map[string]any{} 358 kvMap["k1"] = "name1" 359 kvMap["k2"] = "name2" 360 361 inner1 := map[string]any{} 362 inner1["name"] = "inner_1" 363 inner1["age"] = 1 364 inner1["dataMap"] = kvMap 365 366 var targetObj ValueInnerEntity4 367 _ = isc.DataToObject(inner1, &targetObj) 368 test.Equal(t, "{\"Name\":\"inner_1\",\"Age\":1,\"DataMap\":{\"k1\":\"name1\",\"k2\":\"name2\"}}", isc.ToJsonString(targetObj)) 369 } 370 371 func TestDataToObject5(t *testing.T) { 372 v1 := map[string]any{} 373 v1["name"] = "inner_1" 374 v1["age"] = 1 375 376 v2 := map[string]any{} 377 v2["name"] = "inner_2" 378 v2["age"] = 2 379 380 kvMap := map[string]any{} 381 kvMap["k1"] = v1 382 kvMap["k2"] = v2 383 384 inner1 := map[string]any{} 385 inner1["name"] = "inner_1" 386 inner1["age"] = 1 387 inner1["dataMap"] = kvMap 388 389 var targetObj ValueInnerEntity5 390 _ = isc.DataToObject(inner1, &targetObj) 391 test.Equal(t, "{\"Name\":\"inner_1\",\"Age\":1,\"DataMap\":{\"k1\":{\"Name\":\"inner_1\",\"Age\":1},\"k2\":{\"Name\":\"inner_2\",\"Age\":2}}}", isc.ToJsonString(targetObj)) 392 } 393 394 func TestDataToObject6(t *testing.T) { 395 var dataList []int 396 dataList = append(dataList, 12) 397 dataList = append(dataList, 13) 398 399 kvMap := map[string]any{} 400 kvMap["k1"] = dataList 401 kvMap["k2"] = dataList 402 403 inner1 := map[string]any{} 404 inner1["name"] = "inner_1" 405 inner1["age"] = 1 406 inner1["dataMap"] = kvMap 407 408 var targetObj ValueInnerEntity6 409 _ = isc.DataToObject(inner1, &targetObj) 410 test.Equal(t, "{\"Name\":\"inner_1\",\"Age\":1,\"DataMap\":{\"k1\":[12,13],\"k2\":[12,13]}}", isc.ToJsonString(targetObj)) 411 } 412 413 func TestDataToObject7(t *testing.T) { 414 var dataList []ValueInnerEntity1 415 dataList = append(dataList, ValueInnerEntity1{Name: "name1", Age: 1}) 416 dataList = append(dataList, ValueInnerEntity1{Name: "name2", Age: 2}) 417 418 kvMap := map[string]any{} 419 kvMap["k1"] = dataList 420 kvMap["k2"] = dataList 421 422 inner1 := map[string]any{} 423 inner1["name"] = "inner_1" 424 inner1["age"] = 1 425 inner1["dataMap"] = kvMap 426 427 var targetObj ValueInnerEntity7 428 _ = isc.DataToObject(inner1, &targetObj) 429 test.Equal(t, "{\"Name\":\"inner_1\",\"Age\":1,\"DataMap\":{\"k1\":[{\"Name\":\"name1\",\"Age\":1},{\"Name\":\"name2\",\"Age\":2}],\"k2\":[{\"Name\":\"name1\",\"Age\":1},{\"Name\":\"name2\",\"Age\":2}]}}", isc.ToJsonString(targetObj)) 430 } 431 432 func TestDataToObject8(t *testing.T) { 433 var dataList []ValueInnerEntity1 434 dataList = append(dataList, ValueInnerEntity1{Name: "name1", Age: 1}) 435 dataList = append(dataList, ValueInnerEntity1{Name: "name2", Age: 2}) 436 437 kvMap := map[string]any{} 438 kvMap["k1"] = dataList 439 kvMap["k2"] = dataList 440 441 inner1 := map[string]any{} 442 inner1["name"] = "inner_1" 443 inner1["age"] = 1 444 inner1["dataMap"] = kvMap 445 446 var targetObj ValueInnerEntity8 447 _ = isc.DataToObject(inner1, &targetObj) 448 test.Equal(t, "{\"Name\":\"inner_1\",\"Age\":1,\"DataMap\":{\"k1\":[{\"Name\":\"name1\",\"Address\":\"\"},{\"Name\":\"name2\",\"Address\":\"\"}],\"k2\":[{\"Name\":\"name1\",\"Address\":\"\"},{\"Name\":\"name2\",\"Address\":\"\"}]}}", isc.ToJsonString(targetObj)) 449 } 450 451 func TestDataToObject9(t *testing.T) { 452 var dataList []ValueInnerEntity9Tem 453 dataList = append(dataList, ValueInnerEntity9Tem{Name: "name1", Age: "1"}) 454 dataList = append(dataList, ValueInnerEntity9Tem{Name: "name2", Age: "2"}) 455 456 kvMap := map[string]any{} 457 kvMap["k1"] = dataList 458 kvMap["k2"] = dataList 459 460 inner1 := map[string]any{} 461 inner1["name"] = "inner_1" 462 inner1["age"] = 1 463 inner1["dataMap"] = kvMap 464 465 var targetObj ValueInnerEntity9 466 _ = isc.DataToObject(inner1, &targetObj) 467 test.Equal(t, "{\"Name\":\"inner_1\",\"Age\":1,\"DataMap\":{\"k1\":[{\"Name\":\"name1\",\"Age\":1},{\"Name\":\"name2\",\"Age\":2}],\"k2\":[{\"Name\":\"name1\",\"Age\":1},{\"Name\":\"name2\",\"Age\":2}]}}", isc.ToJsonString(targetObj)) 468 } 469 470 func TestDataToObject10(t *testing.T) { 471 var dataList []ValueInnerEntity9Tem 472 dataList = append(dataList, ValueInnerEntity9Tem{Name: "name1", Age: "1"}) 473 dataList = append(dataList, ValueInnerEntity9Tem{Name: "name2", Age: "2"}) 474 475 kvMap := map[string]any{} 476 kvMap["k1"] = dataList 477 kvMap["k2"] = dataList 478 479 inner1 := map[string]any{} 480 inner1["name"] = "inner_1" 481 inner1["age"] = 1 482 inner1["dataMap"] = kvMap 483 484 var targetObj ValueInnerEntity9 485 _ = isc.DataToObject(inner1, &targetObj) 486 test.Equal(t, "{\"Name\":\"inner_1\",\"Age\":1,\"DataMap\":{\"k1\":[{\"Name\":\"name1\",\"Age\":1},{\"Name\":\"name2\",\"Age\":2}],\"k2\":[{\"Name\":\"name1\",\"Age\":1},{\"Name\":\"name2\",\"Age\":2}]}}", isc.ToJsonString(targetObj)) 487 } 488 489 // strToObject 490 func TestStrToObject1(t *testing.T) { 491 var targetObj int 492 _ = isc.StrToObject("123", &targetObj) 493 test.Equal(t, targetObj, 123) 494 } 495 496 func TestStrToObject2(t *testing.T) { 497 var targetObj string 498 _ = isc.StrToObject("ok", &targetObj) 499 test.Equal(t, targetObj, "ok") 500 } 501 502 func TestStrToObject3(t *testing.T) { 503 var targetObj string 504 _ = isc.StrToObject("{\"nihao\": \"haode\"}", &targetObj) 505 test.Equal(t, targetObj, "{\"nihao\": \"haode\"}") 506 } 507 508 func TestStrToObject4(t *testing.T) { 509 var targetObj ValueInnerEntity1 510 _ = isc.StrToObject("{\"Age\": 12}", &targetObj) 511 test.Equal(t, isc.ToJsonString(targetObj), "{\"Name\":\"\",\"Age\":12}") 512 } 513 514 func TestStrToObject5(t *testing.T) { 515 var targetObj ValueInnerEntity1 516 _ = isc.StrToObject("{\"age\": 12}", &targetObj) 517 test.Equal(t, isc.ToJsonString(targetObj), "{\"Name\":\"\",\"Age\":12}") 518 } 519 520 func TestStrToObject6(t *testing.T) { 521 targetObj := map[string]any{} 522 _ = isc.StrToObject("{\"age\": 12}", &targetObj) 523 test.Equal(t, isc.ToJsonString(targetObj), "{\"age\":12}") 524 } 525 526 func TestStrToObject7(t *testing.T) { 527 var targetObj []ValueInnerEntity1 528 _ = isc.StrToObject("[{\"Age\": 12},{\"Age\":14}]", &targetObj) 529 test.Equal(t, isc.ObjectToJson(targetObj), "[{\"age\":12,\"name\":\"\"},{\"age\":14,\"name\":\"\"}]") 530 } 531 532 type ValueInnerEntityStr1 struct { 533 //Name string 534 //Age int 535 DataMap any 536 } 537 538 func TestStrToObject8(t *testing.T) { 539 str := "{\"dataMap\":{\"haha\":12,\"innerKey\":\"ok\"}}" 540 541 var targetObj ValueInnerEntityStr1 542 _ = isc.StrToObject(str, &targetObj) 543 test.Equal(t, isc.ObjectToJson(targetObj), str) 544 } 545 546 // arrayToObject 547 func TestArrayToObject1(t *testing.T) { 548 var dstValues []ValueInnerEntity1 549 var targetObjs []ValueInnerEntity1 550 targetObjs = append(targetObjs, ValueInnerEntity1{Name: "zhou", Age: 1}) 551 552 _ = isc.ArrayToObject(targetObjs, &dstValues) 553 test.Equal(t, isc.ObjectToJson(dstValues), "[{\"age\":1,\"name\":\"zhou\"}]") 554 } 555 556 //todo 这个暂时还有点问题 557 //func TestArrayToObject2(t *testing.T) { 558 // var dstArray []map[string]any 559 // var srcArray []ValueInnerEntity1 560 // srcArray = append(srcArray, ValueInnerEntity1{Name: "zhou", Age: 1}) 561 // 562 // _ = isc.ArrayToObject(srcArray, &dstArray) 563 // Equal(t, isc.ObjectToJson(dstArray), "[{\"age\":1,\"name\":\"zhou\"}]") 564 //} 565 566 type ConfigItemFromCommonReq struct { 567 Profile string `match:"customize=ExistProfile" errMsg:"环境变量:#current 不存在或没有激活"` 568 AppName string 569 ConfigItemKey string 570 } 571 572 func TestTtt(t *testing.T) { 573 str := "{\"configItemKey\":null}" 574 req := ConfigItemFromCommonReq{} 575 _ = isc.StrToObject(str, &req) 576 t.Log(req) 577 } 578 579 // objectToJson 580 type ValueObjectTest1 struct { 581 AppName string 582 Age int 583 } 584 585 func TestObjectToJson1(t *testing.T) { 586 entity := ValueObjectTest1{AppName: "zhou", Age: 12} 587 test.Equal(t, isc.ObjectToJson(entity), "{\"age\":12,\"appName\":\"zhou\"}") 588 } 589 590 type ValueObjectTest2 struct { 591 AppName string 592 593 Age1 int 594 Age2 int8 595 Age3 int16 596 Age4 int32 597 Age5 int64 598 599 UAge1 uint 600 UAge2 uint8 601 UAge3 uint16 602 UAge4 uint32 603 UAge5 uint64 604 605 FAge1 float32 606 FAge2 float64 607 608 CAge1 complex64 609 CAge2 complex128 610 } 611 612 func TestObjectToJson2(t *testing.T) { 613 entity := ValueObjectTest2{ 614 AppName: "zhou", 615 Age1: 12, 616 Age2: 12, 617 Age3: 12, 618 Age4: 12, 619 Age5: 12, 620 UAge1: 12, 621 UAge2: 12, 622 UAge3: 12, 623 UAge4: 12, 624 UAge5: 12, 625 FAge1: 12.1, 626 FAge2: 12.2, 627 CAge1: 3.2 + 12i, 628 CAge2: 5.2 + 13i, 629 } 630 test.Equal(t, isc.ObjectToJson(entity), "{\"age1\":12,\"age2\":12,\"age3\":12,\"age4\":12,\"age5\":12,\"appName\":\"zhou\",\"cAge1\":\"(3.2+12i)\",\"cAge2\":\"(5.2+13i)\",\"fAge1\":12.1,\"fAge2\":12.2,\"uAge1\":12,\"uAge2\":12,\"uAge3\":12,\"uAge4\":12,\"uAge5\":12}") 631 } 632 633 type ValueObjectTest3 struct { 634 AppName []string 635 Age1 map[string]any 636 } 637 638 func TestObjectToJson3(t *testing.T) { 639 var arrays []string 640 arrays = append(arrays, "zhou") 641 arrays = append(arrays, "wang") 642 643 dataMap := map[string]any{} 644 dataMap["a"] = 1 645 dataMap["b"] = 2 646 647 entity := ValueObjectTest3{ 648 AppName: arrays, 649 Age1: dataMap, 650 } 651 test.Equal(t, isc.ObjectToJson(entity), "{\"age1\":{\"a\":1,\"b\":2},\"appName\":[\"zhou\",\"wang\"]}") 652 } 653 654 type ValueObjectTest4 struct { 655 AppName string 656 Inner ValueObjectTest3 657 } 658 659 func TestObjectToJson4(t *testing.T) { 660 var arrays []string 661 arrays = append(arrays, "zhou") 662 arrays = append(arrays, "wang") 663 664 dataMap := map[string]any{} 665 dataMap["a"] = 1 666 dataMap["b"] = 2 667 668 entity3 := ValueObjectTest3{ 669 AppName: arrays, 670 Age1: dataMap, 671 } 672 673 var entity4 ValueObjectTest4 674 entity4.Inner = entity3 675 entity4.AppName = "zhou" 676 test.Equal(t, isc.ObjectToJson(entity4), "{\"appName\":\"zhou\",\"inner\":{\"age1\":{\"a\":1,\"b\":2},\"appName\":[\"zhou\",\"wang\"]}}") 677 } 678 679 func TestObjectToJson5(t *testing.T) { 680 var arrays []string 681 arrays = append(arrays, "zhou") 682 arrays = append(arrays, "wang") 683 684 dataMap := map[string]any{} 685 dataMap["A"] = 1 686 dataMap["B"] = 2 687 688 act := ValueObjectTest3{ 689 AppName: arrays, 690 Age1: dataMap, 691 } 692 test.Equal(t, isc.ObjectToJson(act), "{\"age1\":{\"a\":1,\"b\":2},\"appName\":[\"zhou\",\"wang\"]}") 693 } 694 695 func TestObjectToJson6(t *testing.T) { 696 expect := "[1,2]" 697 var act []int 698 act = append(act, 1) 699 act = append(act, 2) 700 test.Equal(t, isc.ObjectToJson(act), expect) 701 } 702 703 func TestObjectToJson7(t *testing.T) { 704 var act []ValueInnerEntity1 705 act = append(act, ValueInnerEntity1{Name: "zhou1", Age: 1}) 706 act = append(act, ValueInnerEntity1{Name: "zhou2", Age: 2}) 707 expect := "[{\"age\":1,\"name\":\"zhou1\"},{\"age\":2,\"name\":\"zhou2\"}]" 708 test.Equal(t, isc.ObjectToJson(act), expect) 709 } 710 711 func TestObjectToJson8(t *testing.T) { 712 var act = []map[string]any{} 713 714 map1 := map[string]any{} 715 map1["name"] = "zhou1" 716 map1["age"] = 1 717 718 map2 := map[string]any{} 719 map2["name"] = "zhou2" 720 map2["age"] = 2 721 722 act = append(act, map1) 723 act = append(act, map2) 724 test.Equal(t, isc.ObjectToJson(act), "[{\"age\":1,\"name\":\"zhou1\"},{\"age\":2,\"name\":\"zhou2\"}]") 725 } 726 727 type PageRsp struct { 728 729 // 分页数据 730 Records []any 731 } 732 733 func TestObjectToJson9(t *testing.T) { 734 rel := "{\"Records\":[{\"Id\":121,\"AppName\":\"asdf\",\"AppDesc\":\"fffds\",\"ActiveStatus\":1,\"CreateTime\":\"2021-12-20 14:05:10 +0800 CST\",\"UpdateTime\":\"2021-12-21 14:19:13 +0800 CST\",\"CreateUser\":\"\",\"UpdateUser\":\"\",\"MachineNum\":0,\"ConfigNum\":0,\"Version\":0},{\"Id\":117,\"AppName\":\"isc-apaas-service\",\"AppDesc\":\"\",\"ActiveStatus\":1,\"CreateTime\":\"2021-12-06 10:32:11 +0800 CST\",\"UpdateTime\":\"2021-12-06 10:32:11 +0800 CST\",\"CreateUser\":\"\",\"UpdateUser\":\"\",\"MachineNum\":0,\"ConfigNum\":10,\"Version\":0},{\"Id\":116,\"AppName\":\"isc-config-sample-3\",\"AppDesc\":\"\",\"ActiveStatus\":1,\"CreateTime\":\"2021-11-05 19:35:07 +0800 CST\",\"UpdateTime\":\"2021-11-05 19:35:07 +0800 CST\",\"CreateUser\":\"\",\"UpdateUser\":\"\",\"MachineNum\":0,\"ConfigNum\":4,\"Version\":0},{\"Id\":115,\"AppName\":\"isc-config-sample-2\",\"AppDesc\":\"\",\"ActiveStatus\":1,\"CreateTime\":\"2021-11-05 19:27:55 +0800 CST\",\"UpdateTime\":\"2021-11-05 19:27:55 +0800 CST\",\"CreateUser\":\"\",\"UpdateUser\":\"\",\"MachineNum\":0,\"ConfigNum\":4,\"Version\":0},{\"Id\":113,\"AppName\":\"isc-config-sample1\",\"AppDesc\":\"\",\"ActiveStatus\":1,\"CreateTime\":\"2021-11-05 19:21:18 +0800 CST\",\"UpdateTime\":\"2021-11-05 19:21:18 +0800 CST\",\"CreateUser\":\"\",\"UpdateUser\":\"\",\"MachineNum\":0,\"ConfigNum\":0,\"Version\":0},{\"Id\":112,\"AppName\":\"app-demo-xxx\",\"AppDesc\":\"\",\"ActiveStatus\":1,\"CreateTime\":\"2021-09-28 14:29:46 +0800 CST\",\"UpdateTime\":\"2021-09-28 14:29:46 +0800 CST\",\"CreateUser\":\"\",\"UpdateUser\":\"\",\"MachineNum\":0,\"ConfigNum\":3,\"Version\":25},{\"Id\":84,\"AppName\":\"isc-config-sample3\",\"AppDesc\":\"\",\"ActiveStatus\":1,\"CreateTime\":\"2021-09-22 13:46:36 +0800 CST\",\"UpdateTime\":\"2021-09-22 13:46:36 +0800 CST\",\"CreateUser\":\"\",\"UpdateUser\":\"\",\"MachineNum\":0,\"ConfigNum\":2,\"Version\":23},{\"Id\":83,\"AppName\":\"isc-config-sample-local\",\"AppDesc\":\"\",\"ActiveStatus\":1,\"CreateTime\":\"2021-09-22 11:22:12 +0800 CST\",\"UpdateTime\":\"2021-09-22 11:22:12 +0800 CST\",\"CreateUser\":\"\",\"UpdateUser\":\"\",\"MachineNum\":0,\"ConfigNum\":29,\"Version\":22},{\"Id\":82,\"AppName\":\"isc-monitoring-service2\",\"AppDesc\":\"\",\"ActiveStatus\":1,\"CreateTime\":\"2021-09-18 11:47:50 +0800 CST\",\"UpdateTime\":\"2021-09-18 11:47:50 +0800 CST\",\"CreateUser\":\"\",\"UpdateUser\":\"\",\"MachineNum\":0,\"ConfigNum\":15,\"Version\":0},{\"Id\":81,\"AppName\":\"isc-monitoring-service1\",\"AppDesc\":\"\",\"ActiveStatus\":1,\"CreateTime\":\"2021-09-11 18:02:25 +0800 CST\",\"UpdateTime\":\"2021-09-11 18:02:25 +0800 CST\",\"CreateUser\":\"\",\"UpdateUser\":\"\",\"MachineNum\":0,\"ConfigNum\":15,\"Version\":21},{\"Id\":80,\"AppName\":\"lamp-demo-a\",\"AppDesc\":\"\",\"ActiveStatus\":1,\"CreateTime\":\"2021-09-10 17:21:40 +0800 CST\",\"UpdateTime\":\"2021-09-10 17:28:18 +0800 CST\",\"CreateUser\":\"\",\"UpdateUser\":\"\",\"MachineNum\":0,\"ConfigNum\":1,\"Version\":20},{\"Id\":79,\"AppName\":\"pivotdemoa\",\"AppDesc\":\"\",\"ActiveStatus\":1,\"CreateTime\":\"2021-09-10 17:17:33 +0800 CST\",\"UpdateTime\":\"2021-09-10 17:17:33 +0800 CST\",\"CreateUser\":\"\",\"UpdateUser\":\"\",\"MachineNum\":0,\"ConfigNum\":1,\"Version\":19},{\"Id\":78,\"AppName\":\"isc-config-sample2\",\"AppDesc\":\"\",\"ActiveStatus\":1,\"CreateTime\":\"2021-09-09 16:16:55 +0800 CST\",\"UpdateTime\":\"2021-09-09 16:16:55 +0800 CST\",\"CreateUser\":\"\",\"UpdateUser\":\"\",\"MachineNum\":0,\"ConfigNum\":25,\"Version\":18},{\"Id\":77,\"AppName\":\"isc-config-sample-client\",\"AppDesc\":\"\",\"ActiveStatus\":1,\"CreateTime\":\"2021-09-09 13:40:10 +0800 CST\",\"UpdateTime\":\"2021-09-09 13:40:10 +0800 CST\",\"CreateUser\":\"\",\"UpdateUser\":\"\",\"MachineNum\":0,\"ConfigNum\":25,\"Version\":17},{\"Id\":76,\"AppName\":\"isc-pivot-client\",\"AppDesc\":\"\",\"ActiveStatus\":1,\"CreateTime\":\"2021-08-30 17:53:10 +0800 CST\",\"UpdateTime\":\"2021-08-31 10:08:53 +0800 CST\",\"CreateUser\":\"\",\"UpdateUser\":\"\",\"MachineNum\":0,\"ConfigNum\":2,\"Version\":16},{\"Id\":74,\"AppName\":\"isc-rpc-3-os0\",\"AppDesc\":\"\",\"ActiveStatus\":1,\"CreateTime\":\"2021-08-30 17:52:17 +0800 CST\",\"UpdateTime\":\"2021-08-30 17:52:17 +0800 CST\",\"CreateUser\":\"\",\"UpdateUser\":\"\",\"MachineNum\":0,\"ConfigNum\":25,\"Version\":14},{\"Id\":73,\"AppName\":\"isc-rpc-os0\",\"AppDesc\":\"\",\"ActiveStatus\":1,\"CreateTime\":\"2021-07-06 15:23:25 +0800 CST\",\"UpdateTime\":\"2021-07-06 15:23:25 +0800 CST\",\"CreateUser\":\"\",\"UpdateUser\":\"\",\"MachineNum\":0,\"ConfigNum\":25,\"Version\":13},{\"Id\":71,\"AppName\":\"isc-common-service-test\",\"AppDesc\":\"\",\"ActiveStatus\":1,\"CreateTime\":\"2021-06-21 21:50:24 +0800 CST\",\"UpdateTime\":\"2021-06-21 21:50:24 +0800 CST\",\"CreateUser\":\"\",\"UpdateUser\":\"\",\"MachineNum\":0,\"ConfigNum\":63,\"Version\":11},{\"Id\":70,\"AppName\":\"isc-config-sample\",\"AppDesc\":\"\",\"ActiveStatus\":1,\"CreateTime\":\"2021-06-21 15:33:39 +0800 CST\",\"UpdateTime\":\"2021-06-21 15:33:39 +0800 CST\",\"CreateUser\":\"\",\"UpdateUser\":\"\",\"MachineNum\":0,\"ConfigNum\":30,\"Version\":10},{\"Id\":68,\"AppName\":\"isc-route-service1\",\"AppDesc\":\"\",\"ActiveStatus\":1,\"CreateTime\":\"2021-06-09 16:00:38 +0800 CST\",\"UpdateTime\":\"2021-06-09 16:00:38 +0800 CST\",\"CreateUser\":\"admin\",\"UpdateUser\":\"\",\"MachineNum\":0,\"ConfigNum\":1,\"Version\":8}]}\n" 735 736 rp := PageRsp{} 737 _ = isc.DataToObject(rel, &rp) 738 739 t.Log(isc.ToJsonString(rp)) 740 741 result := isc.ObjectToData(rp) 742 t.Log(isc.ToJsonString(result)) 743 744 //Equal(t, isc.ObjectToJson(act), "[{\"age\":1,\"name\":\"zhou1\"},{\"age\":2,\"name\":\"zhou2\"}]") 745 } 746 747 type JsonEntity10Inner struct { 748 Name string 749 } 750 751 type JsonEntity10 struct { 752 Data *JsonEntity10Inner 753 } 754 755 func TestObjectToJson10(t *testing.T) { 756 inner := JsonEntity10Inner{ 757 Name: "ok", 758 } 759 entity := JsonEntity10{ 760 Data: &inner, 761 } 762 763 test.Equal(t, isc.ObjectToJson(entity), "{\"data\":{\"name\":\"ok\"}}") 764 } 765 766 func TestObjectToJson11(t *testing.T) { 767 inner := JsonEntity10Inner{ 768 Name: "ok", 769 } 770 771 map1 := map[string]any{} 772 map1["data"] = &inner 773 774 test.Equal(t, isc.ObjectToJson(map1), "{\"data\":{\"name\":\"ok\"}}") 775 } 776 777 // objectToMap 778 779 // objectToArray 780 781 // objectToData 782 func TestObjectToData1(t *testing.T) { 783 test.Equal(t, isc.ObjectToData(1), 1) 784 } 785 786 func TestObjectToData2(t *testing.T) { 787 test.Equal(t, isc.ObjectToData("12"), "12") 788 } 789 790 func TestObjectToData3(t *testing.T) { 791 test.Equal(t, isc.ObjectToData("ab"), "ab") 792 } 793 794 func TestObjectToData4(t *testing.T) { 795 test.Equal(t, isc.ObjectToData(12.4), 12.4) 796 } 797 798 func TestObjectToData5(t *testing.T) { 799 src := ValueInnerEntity1{Name: "zhou", Age: 12} 800 dst := map[string]any{} 801 dst["name"] = "zhou" 802 dst["age"] = 12 803 test.Equal(t, isc.ObjectToJson(isc.ObjectToData(src)), isc.ObjectToJson(dst)) 804 } 805 806 func TestObjectToData6(t *testing.T) { 807 src := map[string]any{} 808 src["name"] = "zhou" 809 src["age"] = 12 810 811 dst := ValueInnerEntity1{Name: "zhou", Age: 12} 812 test.Equal(t, isc.ObjectToJson(isc.ObjectToData(src)), isc.ObjectToJson(dst)) 813 } 814 815 func TestObjectToData7(t *testing.T) { 816 src := map[string]any{} 817 src["name"] = "zhou" 818 src["age"] = 12 819 820 dst := map[string]any{} 821 dst["name"] = "zhou" 822 dst["age"] = 12 823 test.Equal(t, isc.ObjectToJson(isc.ObjectToData(src)), isc.ObjectToJson(dst)) 824 } 825 826 func TestObjectToData8(t *testing.T) { 827 src := ValueInnerEntity1{Name: "zhou", Age: 12} 828 dst := ValueInnerEntity1{Name: "zhou", Age: 12} 829 test.Equal(t, isc.ObjectToJson(isc.ObjectToData(src)), isc.ObjectToJson(dst)) 830 } 831 832 //type ValueInnerEntity1Json struct { 833 // Age int 834 // Address string 835 //} 836 837 //func TestObjectToData9(t *testing.T) { 838 // src := ValueInnerEntity1{Name: "zhou", Age: 12} 839 // dst := ValueInnerEntity1Json{Age: 12} 840 // Equal(t, isc.ObjectToJson(isc.ObjectToData(src)), isc.ObjectToJson(dst)) 841 //} 842 843 func TestObjectToData10(t *testing.T) { 844 src := ValueInnerEntity1{Name: "zhou", Age: 12} 845 dst := map[string]any{} 846 dst["name"] = "zhou" 847 dst["age"] = 12 848 test.Equal(t, isc.ObjectToJson(isc.ObjectToData(src)), isc.ObjectToJson(dst)) 849 } 850 851 func TestObjectToData11(t *testing.T) { 852 var src []ValueInnerEntity1 853 var dst []ValueInnerEntity1 854 src = append(src, ValueInnerEntity1{Name: "zhou", Age: 12}) 855 dst = append(dst, ValueInnerEntity1{Name: "zhou", Age: 12}) 856 test.Equal(t, isc.ObjectToJson(isc.ObjectToData(src)), isc.ObjectToJson(dst)) 857 } 858 859 func TestObjectToData12(t *testing.T) { 860 var src []ValueInnerEntity1 861 var dst []map[string]any 862 src = append(src, ValueInnerEntity1{Name: "zhou", Age: 12}) 863 864 map1 := map[string]any{} 865 map1["name"] = "zhou" 866 map1["age"] = 12 867 dst = append(dst, map1) 868 869 test.Equal(t, isc.ObjectToJson(isc.ObjectToData(src)), isc.ObjectToJson(dst)) 870 } 871 872 func TestObjectToData13(t *testing.T) { 873 var dst []ValueInnerEntity1 874 var src []map[string]any 875 dst = append(dst, ValueInnerEntity1{Name: "zhou", Age: 12}) 876 877 map1 := map[string]any{} 878 map1["name"] = "zhou" 879 map1["age"] = 12 880 src = append(src, map1) 881 882 test.Equal(t, isc.ObjectToJson(isc.ObjectToData(src)), isc.ObjectToJson(dst)) 883 } 884 885 func BenchmarkSprintfPress(b *testing.B) { 886 //func TestSimple(t *testing.T) { 887 var jsonStr = "{\"age\":12,\"name\":\"zhou\"}" 888 889 //b.ResetTimer() 890 for i := 0; i < b.N; i++ { 891 var entity ValueInnerEntity1 892 _ = isc.DataToObject(jsonStr, &entity) 893 894 resultMap := make(map[string]any) 895 _ = json.Unmarshal([]byte(jsonStr), &resultMap) 896 } 897 } 898 899 //type ValueInnerEntityPtr struct { 900 // Ptr *ValueInnerEntity1 901 //} 902 903 //func TestObjectToData14(t *testing.T) { 904 // entity := ValueInnerEntity1{ 905 // Name: "zhou", 906 // Age: 12, 907 // } 908 // 909 // act := ValueInnerEntityPtr{} 910 // 911 // map1 := map[string]any{} 912 // map1["ptr"] = &entity 913 // 914 // isc.MapToObject(map1, &act) 915 // fmt.Println(act.Ptr.Name) 916 // fmt.Println(act.Ptr.Age) 917 // 918 // //Equal(t, isc.ObjectToJson(isc.ObjectToData(src)), isc.ObjectToJson(dst)) 919 //}