gitlab.com/evatix-go/core@v1.3.55/coredata/corejson/BytesCollection.go (about) 1 package corejson 2 3 import ( 4 "encoding/json" 5 "math" 6 "sync" 7 8 "gitlab.com/evatix-go/core/constants" 9 "gitlab.com/evatix-go/core/defaultcapacity" 10 "gitlab.com/evatix-go/core/errcore" 11 ) 12 13 // BytesCollection 14 // 15 // Only collects json byes nothing else. 16 // errors will be ignored or returned during add. 17 type BytesCollection struct { 18 Items [][]byte `json:"JsonBytesCollection"` 19 } 20 21 func (it *BytesCollection) Length() int { 22 if it == nil || it.Items == nil { 23 return 0 24 } 25 26 return len(it.Items) 27 } 28 29 func (it *BytesCollection) LastIndex() int { 30 return it.Length() - 1 31 } 32 33 func (it *BytesCollection) IsEmpty() bool { 34 return it.Length() == 0 35 } 36 37 func (it *BytesCollection) HasAnyItem() bool { 38 return it.Length() > 0 39 } 40 41 func (it *BytesCollection) FirstOrDefault() []byte { 42 if it.IsEmpty() { 43 return nil 44 } 45 46 return it.Items[0] 47 } 48 49 func (it *BytesCollection) LastOrDefault() []byte { 50 if it.IsEmpty() { 51 return nil 52 } 53 54 return it.Items[it.LastIndex()] 55 } 56 57 func (it *BytesCollection) Take(limit int) *BytesCollection { 58 if it.IsEmpty() { 59 return Empty.BytesCollectionPtr() 60 } 61 62 return &BytesCollection{ 63 Items: it.Items[:limit], 64 } 65 } 66 67 func (it *BytesCollection) Limit(limit int) *BytesCollection { 68 if it.IsEmpty() { 69 return Empty.BytesCollectionPtr() 70 } 71 72 if limit <= constants.TakeAllMinusOne { 73 return it 74 } 75 76 limit = defaultcapacity. 77 MaxLimit(it.Length(), limit) 78 79 return &BytesCollection{ 80 Items: it.Items[:limit], 81 } 82 } 83 84 func (it *BytesCollection) Skip(skip int) *BytesCollection { 85 if it.IsEmpty() { 86 return Empty.BytesCollectionPtr() 87 } 88 89 return &BytesCollection{ 90 Items: it.Items[skip:], 91 } 92 } 93 94 // AddSkipOnNil skip on nil 95 func (it *BytesCollection) AddSkipOnNil( 96 rawBytes []byte, 97 ) *BytesCollection { 98 if rawBytes == nil { 99 return it 100 } 101 102 it.Items = append( 103 it.Items, 104 rawBytes) 105 106 return it 107 } 108 109 // AddNonEmpty 110 // 111 // skip on empty 112 func (it *BytesCollection) AddNonEmpty( 113 rawBytes []byte, 114 ) *BytesCollection { 115 if len(rawBytes) == 0 { 116 return it 117 } 118 119 it.Items = append( 120 it.Items, 121 rawBytes) 122 123 return it 124 } 125 126 // AddResultPtr 127 // 128 // skip on empty or has issue 129 func (it *BytesCollection) AddResultPtr( 130 result *Result, 131 ) *BytesCollection { 132 if result.HasIssuesOrEmpty() { 133 return it 134 } 135 136 it.Items = append( 137 it.Items, 138 result.Bytes) 139 140 return it 141 } 142 143 // AddResult 144 // 145 // skip on empty or has issue 146 func (it *BytesCollection) AddResult( 147 result Result, 148 ) *BytesCollection { 149 if result.HasIssuesOrEmpty() { 150 return it 151 } 152 153 it.Items = append( 154 it.Items, 155 result.Bytes) 156 157 return it 158 } 159 160 func (it *BytesCollection) GetAt( 161 index int, 162 ) []byte { 163 return it.Items[index] 164 } 165 166 func (it *BytesCollection) JsonResultAt( 167 index int, 168 ) *Result { 169 return &Result{ 170 Bytes: it.Items[index], 171 } 172 } 173 174 func (it *BytesCollection) UnmarshalAt( 175 index int, 176 any interface{}, 177 ) error { 178 rawBytes := it.Items[index] 179 180 return json.Unmarshal( 181 rawBytes, 182 any) 183 } 184 185 func (it *BytesCollection) AddSerializer( 186 serializer bytesSerializer, 187 ) *BytesCollection { 188 if serializer == nil { 189 return it 190 } 191 192 result := NewResult.UsingSerializer( 193 serializer) 194 195 return it.AddResultPtr(result) 196 } 197 198 func (it *BytesCollection) AddSerializers( 199 serializers ...bytesSerializer, 200 ) *BytesCollection { 201 if len(serializers) == 0 { 202 return it 203 } 204 205 for _, serializer := range serializers { 206 it.AddSerializer(serializer) 207 } 208 209 return it 210 } 211 212 func (it *BytesCollection) AddSerializerFunc( 213 serializerFunc func() ([]byte, error), 214 ) *BytesCollection { 215 if serializerFunc == nil { 216 return it 217 } 218 219 result := NewResult.UsingSerializerFunc( 220 serializerFunc) 221 222 return it.AddResultPtr(result) 223 } 224 225 func (it *BytesCollection) AddSerializerFunctions( 226 serializerFunctions ...func() ([]byte, error), 227 ) *BytesCollection { 228 if len(serializerFunctions) == 0 { 229 return it 230 } 231 232 for _, serializer := range serializerFunctions { 233 it.AddSerializerFunc(serializer) 234 } 235 236 return it 237 } 238 239 func (it *BytesCollection) InjectIntoAt( 240 index int, 241 injector JsonParseSelfInjector, 242 ) error { 243 return injector.JsonParseSelfInject( 244 it.JsonResultAt(index)) 245 } 246 247 // InjectIntoSameIndex any nil skip 248 func (it *BytesCollection) InjectIntoSameIndex( 249 injectors ...JsonParseSelfInjector, 250 ) ( 251 errListPtr []error, 252 hasAnyError bool, 253 ) { 254 if injectors == nil { 255 return []error{}, false 256 } 257 258 length := len(injectors) 259 errList := make([]error, length) 260 261 for i := 0; i < length; i++ { 262 result := it.JsonResultAt(i) 263 injector := injectors[i] 264 265 if injector == nil { 266 continue 267 } 268 269 err := injector. 270 JsonParseSelfInject( 271 result) 272 273 if err != nil { 274 hasAnyError = true 275 } 276 277 errList[i] = err 278 } 279 280 return errList, hasAnyError 281 } 282 283 // UnmarshalIntoSameIndex any nil skip 284 func (it *BytesCollection) UnmarshalIntoSameIndex( 285 anys ...interface{}, 286 ) ( 287 errListPtr []error, 288 hasAnyError bool, 289 ) { 290 if anys == nil { 291 return []error{}, false 292 } 293 294 length := len(anys) 295 errList := make([]error, length) 296 297 for i := 0; i < length; i++ { 298 result := it.JsonResultAt(i) 299 any := anys[i] 300 301 if any == nil { 302 continue 303 } 304 305 err := result.Unmarshal( 306 any) 307 308 if err != nil { 309 hasAnyError = true 310 } 311 312 errList[i] = err 313 } 314 315 return errList, hasAnyError 316 } 317 318 func (it *BytesCollection) GetAtSafe( 319 index int, 320 ) []byte { 321 if index > constants.InvalidNotFoundCase && index <= it.Length()-1 { 322 return it.Items[index] 323 } 324 325 return nil 326 } 327 328 func (it *BytesCollection) GetAtSafePtr( 329 index int, 330 ) *[]byte { 331 if index > constants.InvalidNotFoundCase && index <= it.Length()-1 { 332 return &it.Items[index] 333 } 334 335 return nil 336 } 337 338 func (it *BytesCollection) GetResultAtSafe( 339 index int, 340 ) *Result { 341 if index > constants.InvalidNotFoundCase && index <= it.Length()-1 { 342 return it.JsonResultAt(index) 343 } 344 345 return nil 346 } 347 348 func (it *BytesCollection) GetAtSafeUsingLength( 349 index, length int, 350 ) *Result { 351 if index > constants.InvalidNotFoundCase && index <= length-1 { 352 return it.JsonResultAt(index) 353 } 354 355 return nil 356 } 357 358 func (it *BytesCollection) AddPtr( 359 rawBytes *[]byte, 360 ) *BytesCollection { 361 if rawBytes == nil || len(*rawBytes) == 0 { 362 return it 363 } 364 365 it.Items = append( 366 it.Items, 367 *rawBytes) 368 369 return it 370 } 371 372 func (it *BytesCollection) Add( 373 result []byte, 374 ) *BytesCollection { 375 it.Items = append( 376 it.Items, 377 result) 378 379 return it 380 } 381 382 func (it *BytesCollection) Adds( 383 rawBytesCollection ...[]byte, 384 ) *BytesCollection { 385 if len(rawBytesCollection) == 0 { 386 return it 387 } 388 389 for _, rawBytes := range rawBytesCollection { 390 if len(rawBytes) == 0 { 391 continue 392 } 393 394 it.Items = append( 395 it.Items, 396 rawBytes) 397 } 398 399 return it 400 } 401 402 func (it *BytesCollection) AddAnyItems( 403 anyItems ...interface{}, 404 ) error { 405 if len(anyItems) == 0 { 406 return nil 407 } 408 409 for _, anyItem := range anyItems { 410 jsonResult := NewPtr(anyItem) 411 if jsonResult.HasError() { 412 return jsonResult.MeaningfulError() 413 } 414 415 it.Items = append( 416 it.Items, 417 jsonResult.Bytes) 418 } 419 420 return nil 421 } 422 423 func (it *BytesCollection) AddMapResults( 424 mapResults *MapResults, 425 ) *BytesCollection { 426 if mapResults.IsEmpty() { 427 return it 428 } 429 430 return it.AddRawMapResults(mapResults.Items) 431 } 432 433 func (it *BytesCollection) AddRawMapResults( 434 mapResults map[string]Result, 435 ) *BytesCollection { 436 if len(mapResults) == 0 { 437 return it 438 } 439 440 for _, result := range mapResults { 441 if result.HasError() { 442 continue 443 } 444 445 it.Items = append( 446 it.Items, 447 result.Bytes) 448 } 449 450 return it 451 } 452 453 func (it *BytesCollection) AddsPtr( 454 results ...*Result, 455 ) *BytesCollection { 456 if results == nil { 457 return it 458 } 459 460 for _, result := range results { 461 if result.IsAnyNull() { 462 continue 463 } 464 465 it.Items = append( 466 it.Items, 467 result.Bytes) 468 } 469 470 return it 471 } 472 473 func (it *BytesCollection) AddAny( 474 any interface{}, 475 ) error { 476 result := New(any) 477 478 if result.HasError() { 479 return result.MeaningfulError() 480 } 481 482 it.Items = append( 483 it.Items, 484 result.Bytes) 485 486 return nil 487 } 488 489 // AddBytesCollection skip on nil items 490 func (it *BytesCollection) AddBytesCollection( 491 collection *BytesCollection, 492 ) *BytesCollection { 493 if collection.IsEmpty() { 494 return it 495 } 496 497 return it.Adds(collection.Items...) 498 } 499 500 func (it *BytesCollection) Clear() *BytesCollection { 501 if it == nil { 502 return it 503 } 504 505 tempItems := it.Items 506 clearFunc := func() { 507 for i := range tempItems { 508 tempItems[i] = nil 509 } 510 } 511 512 go clearFunc() 513 it.Items = [][]byte{} 514 515 return it 516 } 517 518 func (it *BytesCollection) Dispose() { 519 if it == nil { 520 return 521 } 522 523 it.Clear() 524 it.Items = nil 525 } 526 527 func (it *BytesCollection) Strings() []string { 528 length := it.Length() 529 list := make([]string, length) 530 531 if length == 0 { 532 return list 533 } 534 535 for i, rawBytes := range it.Items { 536 list[i] = string(rawBytes) 537 } 538 539 return list 540 } 541 542 func (it *BytesCollection) StringsPtr() *[]string { 543 list := it.Strings() 544 545 return &list 546 } 547 548 // AddJsoners skip on nil 549 func (it *BytesCollection) AddJsoners( 550 isIgnoreNilOrError bool, 551 jsoners ...Jsoner, 552 ) *BytesCollection { 553 if jsoners == nil { 554 return it 555 } 556 557 for _, jsoner := range jsoners { 558 if jsoner == nil { 559 continue 560 } 561 562 result := jsoner.Json() 563 564 if isIgnoreNilOrError && result.HasError() { 565 continue 566 } 567 568 it.Items = append( 569 it.Items, 570 result.Bytes) 571 } 572 573 return it 574 } 575 576 func (it *BytesCollection) GetPagesSize( 577 eachPageSize int, 578 ) int { 579 length := it.Length() 580 581 pagesPossibleFloat := float64(length) / float64(eachPageSize) 582 pagesPossibleCeiling := int(math.Ceil(pagesPossibleFloat)) 583 584 return pagesPossibleCeiling 585 } 586 587 func (it *BytesCollection) GetPagedCollection( 588 eachPageSize int, 589 ) []*BytesCollection { 590 length := it.Length() 591 592 if length < eachPageSize { 593 return []*BytesCollection{ 594 it, 595 } 596 } 597 598 pagesPossibleFloat := float64(length) / float64(eachPageSize) 599 pagesPossibleCeiling := int(math.Ceil(pagesPossibleFloat)) 600 collectionOfCollection := make([]*BytesCollection, pagesPossibleCeiling) 601 602 wg := sync.WaitGroup{} 603 addPagedItemsFunc := func(oneBasedPageIndex int) { 604 pagedCollection := it.GetSinglePageCollection( 605 eachPageSize, 606 oneBasedPageIndex, 607 ) 608 609 collectionOfCollection[oneBasedPageIndex-1] = pagedCollection 610 611 wg.Done() 612 } 613 614 wg.Add(pagesPossibleCeiling) 615 for i := 1; i <= pagesPossibleCeiling; i++ { 616 go addPagedItemsFunc(i) 617 } 618 619 wg.Wait() 620 621 return collectionOfCollection 622 } 623 624 // GetSinglePageCollection PageIndex is one based index. Should be above or equal 1 625 func (it *BytesCollection) GetSinglePageCollection( 626 eachPageSize int, 627 pageIndex int, 628 ) *BytesCollection { 629 length := it.Length() 630 631 if length < eachPageSize { 632 return it 633 } 634 635 /** 636 * eachPageItems = 10 637 * pageIndex = 4 638 * skipItems = 10 * (4 - 1) = 30 639 */ 640 skipItems := eachPageSize * (pageIndex - 1) 641 if skipItems < 0 { 642 errcore. 643 CannotBeNegativeIndexType. 644 HandleUsingPanic( 645 "pageIndex cannot be negative or zero.", 646 pageIndex) 647 } 648 649 endingIndex := skipItems + eachPageSize 650 651 if endingIndex > length { 652 endingIndex = length 653 } 654 655 list := it.Items[skipItems:endingIndex] 656 657 return &BytesCollection{ 658 Items: list, 659 } 660 } 661 662 //goland:noinspection GoLinterLocal 663 func (it *BytesCollection) JsonModel() [][]byte { 664 return it.Items 665 } 666 667 //goland:noinspection GoLinterLocal 668 func (it *BytesCollection) JsonModelAny() interface{} { 669 return it.JsonModel() 670 } 671 672 func (it BytesCollection) MarshalJSON() ([]byte, error) { 673 return Serialize.Raw(it.JsonModel()) 674 } 675 676 func (it BytesCollection) UnmarshalJSON( 677 rawJsonBytes []byte, 678 ) error { 679 var items [][]byte 680 err := Deserialize.UsingBytes( 681 rawJsonBytes, 682 &items) 683 684 if err == nil { 685 it.Items = items 686 } 687 688 return err 689 } 690 691 func (it BytesCollection) Json() Result { 692 return New(it) 693 } 694 695 func (it BytesCollection) JsonPtr() *Result { 696 return NewPtr(it) 697 } 698 699 // ParseInjectUsingJson It will not update the self but creates a new one. 700 func (it *BytesCollection) ParseInjectUsingJson( 701 jsonResult *Result, 702 ) (*BytesCollection, error) { 703 err := jsonResult.Unmarshal( 704 &it, 705 ) 706 707 if err != nil { 708 return Empty.BytesCollectionPtr(), err 709 } 710 711 return it, nil 712 } 713 714 // ParseInjectUsingJsonMust Panic if error 715 func (it *BytesCollection) ParseInjectUsingJsonMust( 716 jsonResult *Result, 717 ) *BytesCollection { 718 resultCollection, err := it. 719 ParseInjectUsingJson(jsonResult) 720 721 if err != nil { 722 panic(err) 723 } 724 725 return resultCollection 726 } 727 728 func (it *BytesCollection) AsJsonContractsBinder() JsonContractsBinder { 729 return it 730 } 731 732 func (it *BytesCollection) AsJsoner() Jsoner { 733 return it 734 } 735 736 func (it *BytesCollection) JsonParseSelfInject( 737 jsonResult *Result, 738 ) error { 739 _, err := it.ParseInjectUsingJson( 740 jsonResult, 741 ) 742 743 return err 744 } 745 746 func (it *BytesCollection) AsJsonParseSelfInjector() JsonParseSelfInjector { 747 return it 748 } 749 750 func (it *BytesCollection) ShadowClone() BytesCollection { 751 return it.Clone(false) 752 } 753 754 func (it BytesCollection) Clone(isDeepCloneEach bool) BytesCollection { 755 newResults := NewBytesCollection.UsingCap( 756 it.Length()) 757 758 if newResults.Length() == 0 { 759 return *newResults 760 } 761 762 for _, item := range it.Items { 763 newResults.Add(BytesCloneIf(isDeepCloneEach, item)) 764 } 765 766 return *newResults 767 } 768 769 func (it *BytesCollection) ClonePtr(isDeepCloneEach bool) *BytesCollection { 770 if it == nil { 771 return nil 772 } 773 774 newResults := NewBytesCollection.UsingCap( 775 it.Length()) 776 777 if newResults.Length() == 0 { 778 return newResults 779 } 780 781 for _, item := range it.Items { 782 newResults.Add(BytesCloneIf(isDeepCloneEach, item)) 783 } 784 785 return newResults 786 }