gitlab.com/evatix-go/core@v1.3.55/coredata/corepayload/PayloadWrapper.go (about) 1 package corepayload 2 3 import ( 4 "bytes" 5 6 "gitlab.com/evatix-go/core/constants" 7 "gitlab.com/evatix-go/core/converters" 8 "gitlab.com/evatix-go/core/coredata/coredynamic" 9 "gitlab.com/evatix-go/core/coredata/corejson" 10 "gitlab.com/evatix-go/core/coreinterface/entityinf" 11 "gitlab.com/evatix-go/core/coreinterface/enuminf" 12 "gitlab.com/evatix-go/core/coreinterface/errcoreinf" 13 "gitlab.com/evatix-go/core/coreinterface/payloadinf" 14 "gitlab.com/evatix-go/core/defaulterr" 15 "gitlab.com/evatix-go/core/errcore" 16 ) 17 18 type PayloadWrapper struct { 19 Name string `json:"Name,omitempty"` 20 Identifier string `json:"Identifier,omitempty"` 21 TaskTypeName string `json:"TaskTypeName,omitempty"` 22 EntityType string `json:"EntityType,omitempty"` 23 CategoryName string `json:"CategoryName,omitempty"` 24 HasManyRecords bool `json:"HasManyRecords,omitempty"` 25 Payloads []byte `json:"Payloads,omitempty"` 26 Attributes *Attributes `json:"AnyAttributes,omitempty"` 27 } 28 29 func (it *PayloadWrapper) MarshalJSON() (jsonBytes []byte, parsedErr error) { 30 if it == nil { 31 return nil, errcore. 32 MarshallingFailedType. 33 ErrorNoRefs("payloadWrapper was nil and tried to marshal") 34 } 35 36 model := payloadWrapperModel{ 37 Name: it.Name, 38 Identifier: it.Identifier, 39 TaskTypeName: it.TaskTypeName, 40 EntityType: it.EntityType, 41 CategoryName: it.CategoryName, 42 HasManyRecords: it.HasManyRecords, 43 Payloads: it.PayloadsString(), 44 Attributes: it.Attributes, 45 } 46 47 return corejson.Serialize.Raw(model) 48 } 49 50 func (it *PayloadWrapper) UnmarshalJSON(rawJsonBytes []byte) error { 51 if it == nil { 52 return errcore. 53 UnMarshallingFailedType. 54 ErrorNoRefs("payloadWrapper was nil and tried to Unmarshal or deserialize") 55 } 56 57 var payloadWrapperModelInstance payloadWrapperModel 58 err := corejson.Deserialize.UsingBytes( 59 rawJsonBytes, 60 &payloadWrapperModelInstance) 61 62 if err == nil { 63 it.Name = payloadWrapperModelInstance.Name 64 it.Identifier = payloadWrapperModelInstance.Identifier 65 it.TaskTypeName = payloadWrapperModelInstance.TaskTypeName 66 it.EntityType = payloadWrapperModelInstance.EntityType 67 it.CategoryName = payloadWrapperModelInstance.CategoryName 68 it.HasManyRecords = payloadWrapperModelInstance.HasManyRecords 69 it.Payloads = []byte(payloadWrapperModelInstance.Payloads) 70 it.Attributes = payloadWrapperModelInstance.Attributes 71 } 72 73 return err 74 } 75 76 func (it *PayloadWrapper) ReCreateUsingJsonBytes( 77 rawJsonBytes []byte, 78 ) (payloadWrapper *PayloadWrapper, parsingErr error) { 79 return New. 80 PayloadWrapper. 81 Deserialize(rawJsonBytes) 82 } 83 84 func (it *PayloadWrapper) ReCreateUsingJsonResult( 85 jsonResult *corejson.Result, 86 ) (payloadWrapper *PayloadWrapper, parsingErr error) { 87 return New. 88 PayloadWrapper. 89 DeserializeUsingJsonResult( 90 jsonResult) 91 } 92 93 func (it *PayloadWrapper) HasSafeItems() bool { 94 if it.IsEmpty() || it.HasError() { 95 return false 96 } 97 98 return true 99 } 100 101 func (it *PayloadWrapper) DynamicPayloads() []byte { 102 if it == nil { 103 return []byte{} 104 } 105 106 return it.Payloads 107 } 108 109 func (it *PayloadWrapper) SetDynamicPayloads(payloads []byte) error { 110 if it == nil { 111 return defaulterr.NilResult 112 } 113 114 it.Payloads = payloads 115 116 return nil 117 } 118 119 func (it *PayloadWrapper) AttrAsBinder() payloadinf.AttributesBinder { 120 return it.Attributes 121 } 122 123 func (it *PayloadWrapper) InitializeAttributesOnNull() payloadinf.AttributesBinder { 124 if it.Attributes == nil { 125 it.Attributes = New.Attributes.Empty() 126 } 127 128 return it.Attributes 129 } 130 131 func (it *PayloadWrapper) BasicError() errcoreinf.BasicErrWrapper { 132 if it.HasError() { 133 return it.Attributes.BasicErrWrapper 134 } 135 136 return nil 137 } 138 139 func (it *PayloadWrapper) PayloadDeserializeToPayloadBinder() (payloadinf.PayloadsBinder, error) { 140 if it.IsNull() { 141 return nil, defaulterr.NilResult 142 } 143 144 if it.HasError() { 145 return nil, it.Attributes.BasicErrWrapper.CompiledError() 146 } 147 148 return it.DeserializePayloadsToPayloadWrapper() 149 } 150 151 func (it *PayloadWrapper) All() (id, name, entity, category string, dynamicPayloads []byte) { 152 return it.Identifier, it.Name, it.EntityType, it.CategoryName, it.Payloads 153 } 154 155 func (it *PayloadWrapper) AllSafe() (id, name, entity, category string, dynamicPayloads []byte) { 156 if it.IsNull() { 157 return "", "", "", "", []byte{} 158 } 159 160 return it.All() 161 } 162 163 func (it *PayloadWrapper) PayloadName() string { 164 return it.Name 165 } 166 167 func (it *PayloadWrapper) PayloadCategory() string { 168 return it.CategoryName 169 } 170 171 func (it *PayloadWrapper) PayloadTaskType() string { 172 return it.TaskTypeName 173 } 174 175 func (it *PayloadWrapper) PayloadEntityType() string { 176 return it.EntityType 177 } 178 179 func (it *PayloadWrapper) PayloadDynamic() []byte { 180 return it.Payloads 181 } 182 183 func (it *PayloadWrapper) SetPayloadDynamic( 184 dynamicPayload []byte, 185 ) *PayloadWrapper { 186 if it == nil { 187 it.InitializeAttributesOnNull() 188 } 189 190 it.Payloads = dynamicPayload 191 192 return it 193 } 194 195 // SetPayloadDynamicAny 196 // 197 // Casting happens: 198 // - self or self pointer returns directly 199 // - []Bytes to Result 200 // - string (json) to Result 201 // - Jsoner to Result 202 // - bytesSerializer to Result 203 // - error to Result 204 // - AnyItem 205 func (it *PayloadWrapper) SetPayloadDynamicAny( 206 dynamicPayloadAny interface{}, 207 ) (*PayloadWrapper, error) { 208 if it == nil { 209 it.InitializeAttributesOnNull() 210 } 211 212 jsonResult := corejson.AnyTo.SerializedJsonResult( 213 dynamicPayloadAny) 214 215 if jsonResult.HasError() { 216 return nil, jsonResult.MeaningfulError() 217 } 218 219 it.Payloads = jsonResult.Bytes 220 221 return it, nil 222 } 223 224 func (it *PayloadWrapper) SetAuthInfo( 225 authInfo *AuthInfo, 226 ) *PayloadWrapper { 227 if it == nil { 228 it.InitializeAttributesOnNull() 229 } 230 231 it.Attributes.SetAuthInfo(authInfo) 232 233 return it 234 } 235 236 func (it *PayloadWrapper) SetUserInfo( 237 userInfo *UserInfo, 238 ) *PayloadWrapper { 239 if it == nil { 240 it.InitializeAttributesOnNull() 241 } 242 243 it.Attributes.SetUserInfo(userInfo) 244 245 return it 246 } 247 248 func (it *PayloadWrapper) SetUser( 249 user *User, 250 ) *PayloadWrapper { 251 it.initializeAuthOnDemand() 252 253 it.Attributes.AuthInfo.UserInfo.SetUser( 254 user) 255 256 return it 257 } 258 259 func (it *PayloadWrapper) SetSysUser( 260 sysUser *User, 261 ) *PayloadWrapper { 262 it.initializeAuthOnDemand() 263 264 it.Attributes.AuthInfo.UserInfo.SetSystemUser( 265 sysUser) 266 267 return it 268 } 269 270 func (it *PayloadWrapper) initializeAuthOnDemand() { 271 if it == nil { 272 it.InitializeAttributesOnNull() 273 } 274 275 if it.Attributes == nil { 276 it.Attributes = &Attributes{} 277 } 278 279 if it.Attributes.AuthInfo == nil { 280 it.Attributes.AuthInfo = &AuthInfo{} 281 } 282 } 283 284 func (it *PayloadWrapper) PayloadProperties() payloadinf.PayloadPropertiesDefiner { 285 return &payloadProperties{it} 286 } 287 288 func (it *PayloadWrapper) HandleError() { 289 if it.HasError() { 290 it.BasicError().HandleError() 291 } 292 } 293 294 func (it *PayloadWrapper) ReflectSetTo( 295 toPointer interface{}, 296 ) error { 297 return coredynamic.ReflectSetFromTo( 298 it, 299 toPointer) 300 } 301 302 func (it *PayloadWrapper) AnyAttributes() interface{} { 303 return it.Attributes 304 } 305 306 func (it *PayloadWrapper) ReflectSetAttributes( 307 toPointer interface{}, 308 ) error { 309 return coredynamic.ReflectSetFromTo( 310 it.Attributes, 311 toPointer) 312 } 313 314 func (it *PayloadWrapper) IdString() string { 315 return it.Identifier 316 } 317 318 func (it *PayloadWrapper) IdInteger() int { 319 return it.IdentifierInteger() 320 } 321 322 func (it *PayloadWrapper) IsStandardTaskEntityEqual( 323 entity entityinf.StandardTaskEntityDefiner, 324 ) bool { 325 another, isSuccess := entity.(*PayloadWrapper) 326 327 if !isSuccess { 328 return false 329 } 330 331 return it.IsEqual(another) 332 } 333 334 func (it *PayloadWrapper) ValueReflectSet( 335 setterPtr interface{}, 336 ) error { 337 return coredynamic.ReflectSetFromTo( 338 it.Payloads, 339 setterPtr) 340 } 341 342 func (it *PayloadWrapper) Serialize() ([]byte, error) { 343 return corejson.Serialize.Raw(it) 344 } 345 346 func (it *PayloadWrapper) SerializeMust() []byte { 347 json := it.Json() 348 json.HandleError() 349 350 return json.Bytes 351 } 352 353 func (it *PayloadWrapper) Username() string { 354 if it.IsEmptyAttributes() { 355 return "" 356 } 357 358 virtualUser := it.Attributes.VirtualUser() 359 360 if virtualUser == nil { 361 return "" 362 } 363 364 return virtualUser.Name 365 } 366 367 func (it *PayloadWrapper) Value() interface{} { 368 return it.Payloads 369 } 370 371 func (it *PayloadWrapper) Error() error { 372 if it.IsEmptyError() { 373 return nil 374 } 375 376 return it.Attributes.Error() 377 } 378 379 func (it *PayloadWrapper) IsEqual(right *PayloadWrapper) bool { 380 if it == nil && right == nil { 381 return true 382 } 383 384 if it == nil || right == nil { 385 return false 386 } 387 388 if it == right { 389 return true 390 } 391 392 if it.Name != right.Name { 393 return false 394 } 395 if it.Identifier != right.Identifier { 396 return false 397 } 398 399 if it.TaskTypeName != right.TaskTypeName { 400 return false 401 } 402 403 if it.EntityType != right.EntityType { 404 return false 405 } 406 407 if it.CategoryName != right.CategoryName { 408 return false 409 } 410 411 if it.HasManyRecords != right.HasManyRecords { 412 return false 413 } 414 415 if !bytes.Equal(it.Payloads, right.Payloads) { 416 return false 417 } 418 419 if !it.Attributes.IsEqual(right.Attributes) { 420 return false 421 } 422 423 return true 424 } 425 426 func (it *PayloadWrapper) IsPayloadsEqual(nextPayloads []byte) bool { 427 return it != nil && bytes.Equal(it.Payloads, nextPayloads) 428 } 429 430 func (it *PayloadWrapper) IsName(name string) bool { 431 return it != nil && it.Name == name 432 } 433 434 func (it *PayloadWrapper) IsIdentifier(id string) bool { 435 return it != nil && it.Name == id 436 } 437 438 func (it *PayloadWrapper) IsTaskTypeName(taskType string) bool { 439 return it != nil && it.TaskTypeName == taskType 440 } 441 442 func (it *PayloadWrapper) IsEntityType(entityType string) bool { 443 return it != nil && it.EntityType == entityType 444 } 445 446 func (it *PayloadWrapper) IsEntityTypeNamer(entityTyper enuminf.Namer) bool { 447 return it != nil && 448 entityTyper != nil && 449 it.EntityType == entityTyper.Name() 450 } 451 452 func (it *PayloadWrapper) IsCategory(category string) bool { 453 return it != nil && it.CategoryName == category 454 } 455 456 func (it *PayloadWrapper) IsCategoryNamer(categoryNamer enuminf.Namer) bool { 457 return it != nil && 458 categoryNamer != nil && 459 it.EntityType == categoryNamer.Name() 460 } 461 462 func (it PayloadWrapper) String() string { 463 return it.JsonString() 464 } 465 466 func (it PayloadWrapper) PrettyJsonString() string { 467 return it.JsonPtr().PrettyJsonString() 468 } 469 470 func (it *PayloadWrapper) JsonString() string { 471 return it.JsonPtr().JsonString() 472 } 473 474 func (it *PayloadWrapper) JsonStringMust() string { 475 return it.JsonPtr().JsonString() 476 } 477 478 func (it *PayloadWrapper) HasAnyItem() bool { 479 return it.Length() > 0 480 } 481 482 func (it *PayloadWrapper) HasIssuesOrEmpty() bool { 483 return it == nil || 484 it. 485 Attributes. 486 HasError() || 487 it.Length() == 0 488 } 489 490 func (it *PayloadWrapper) HasError() bool { 491 return it != nil && it.Attributes.HasError() 492 } 493 494 func (it *PayloadWrapper) IsEmptyError() bool { 495 return it == nil || it.Attributes.IsEmptyError() 496 } 497 498 func (it *PayloadWrapper) HasAttributes() bool { 499 return it != nil && it.Attributes != nil 500 } 501 502 func (it *PayloadWrapper) IsEmptyAttributes() bool { 503 return it == nil || it.Attributes == nil 504 } 505 506 func (it *PayloadWrapper) HasSingleRecord() bool { 507 return it != nil && !it.HasManyRecords 508 } 509 510 func (it *PayloadWrapper) IsNull() bool { 511 return it == nil 512 } 513 514 func (it *PayloadWrapper) HasAnyNil() bool { 515 return it == nil 516 } 517 518 func (it *PayloadWrapper) Count() int { 519 return it.Length() 520 } 521 522 func (it *PayloadWrapper) Length() int { 523 if it == nil { 524 return 0 525 } 526 527 return len(it.Payloads) 528 } 529 530 func (it *PayloadWrapper) IsEmpty() bool { 531 return it.Length() == 0 532 } 533 534 func (it *PayloadWrapper) HasItems() bool { 535 return it.Length() > 0 536 } 537 538 // IdentifierInteger 539 // 540 // Invalid value returns constants.InvalidValue 541 func (it *PayloadWrapper) IdentifierInteger() int { 542 if it.Identifier == "" { 543 return constants.InvalidValue 544 } 545 546 idInt, _ := converters.StringToIntegerWithDefault( 547 it.Identifier, 548 constants.InvalidValue) 549 550 return idInt 551 } 552 553 // IdentifierUnsignedInteger 554 // 555 // Invalid value returns constants.Zero 556 func (it *PayloadWrapper) IdentifierUnsignedInteger() uint { 557 idInt := it.IdentifierInteger() 558 559 if idInt < 0 { 560 return constants.Zero 561 } 562 563 return uint(idInt) 564 } 565 566 func (it *PayloadWrapper) BytesConverter() *coredynamic.BytesConverter { 567 return coredynamic.NewBytesConverter(it.Payloads) 568 } 569 570 func (it *PayloadWrapper) Deserialize( 571 unmarshallingPointer interface{}, 572 ) error { 573 return corejson. 574 Deserialize. 575 UsingBytes( 576 it.Payloads, 577 unmarshallingPointer) 578 } 579 580 func (it *PayloadWrapper) DeserializeMust( 581 unmarshallingPointer interface{}, 582 ) { 583 corejson. 584 Deserialize. 585 UsingBytesMust( 586 it.Payloads, 587 unmarshallingPointer) 588 } 589 590 func (it *PayloadWrapper) PayloadDeserialize( 591 unmarshallingPointer interface{}, 592 ) error { 593 return corejson.Deserialize.UsingBytes( 594 it.Payloads, 595 unmarshallingPointer) 596 } 597 598 func (it *PayloadWrapper) PayloadDeserializeMust( 599 unmarshallingPointer interface{}, 600 ) { 601 err := corejson. 602 Deserialize. 603 UsingBytes( 604 it.Payloads, 605 unmarshallingPointer) 606 607 if err != nil { 608 panic(err) 609 } 610 } 611 612 func (it *PayloadWrapper) DeserializePayloadsToPayloadsCollection() ( 613 payloadsCollection *PayloadsCollection, err error, 614 ) { 615 return New. 616 PayloadsCollection. 617 Deserialize(it.Payloads) 618 } 619 620 func (it *PayloadWrapper) DeserializePayloadsToPayloadWrapper() ( 621 payloadWrapper *PayloadWrapper, err error, 622 ) { 623 return New. 624 PayloadWrapper. 625 Deserialize( 626 it.Payloads) 627 } 628 629 func (it *PayloadWrapper) DeserializePayloadsToPayloadWrapperMust() ( 630 payloadWrapper *PayloadWrapper, 631 ) { 632 rs, err := New. 633 PayloadWrapper. 634 Deserialize(it.Payloads) 635 636 errcore.HandleErr(err) 637 638 return rs 639 } 640 641 func (it *PayloadWrapper) JsonModel() PayloadWrapper { 642 return it.NonPtr() 643 } 644 645 func (it *PayloadWrapper) JsonModelAny() interface{} { 646 return it.JsonModel() 647 } 648 649 func (it *PayloadWrapper) Json() corejson.Result { 650 return corejson.New(it) 651 } 652 653 func (it *PayloadWrapper) JsonPtr() *corejson.Result { 654 return corejson.NewPtr(it) 655 } 656 657 //goland:noinspection GoLinterLocal 658 func (it *PayloadWrapper) ParseInjectUsingJson( 659 jsonResult *corejson.Result, 660 ) (*PayloadWrapper, error) { 661 err := jsonResult.Unmarshal(it) 662 663 if err != nil { 664 return nil, err 665 } 666 667 return it, nil 668 } 669 670 // ParseInjectUsingJsonMust Panic if error 671 //goland:noinspection GoLinterLocal 672 func (it *PayloadWrapper) ParseInjectUsingJsonMust( 673 jsonResult *corejson.Result, 674 ) *PayloadWrapper { 675 newUsingJson, err := 676 it.ParseInjectUsingJson(jsonResult) 677 678 if err != nil { 679 panic(err) 680 } 681 682 return newUsingJson 683 } 684 685 func (it *PayloadWrapper) JsonParseSelfInject( 686 jsonResult *corejson.Result, 687 ) error { 688 _, err := it.ParseInjectUsingJson( 689 jsonResult, 690 ) 691 692 return err 693 } 694 695 func (it *PayloadWrapper) PayloadsString() string { 696 if it.IsEmpty() || len(it.Payloads) == 0 { 697 return "" 698 } 699 700 return string(it.Payloads) 701 } 702 703 func (it *PayloadWrapper) PayloadsPrettyString() string { 704 if it.IsEmpty() || len(it.Payloads) == 0 { 705 return "" 706 } 707 708 return corejson.BytesToPrettyString(it.Payloads) 709 } 710 711 func (it *PayloadWrapper) PayloadsJsonResult() *corejson.Result { 712 if it.IsEmpty() || len(it.Payloads) == 0 { 713 return corejson.Empty.ResultPtr() 714 } 715 716 return corejson.NewResult.UsingTypeBytesPtr( 717 attributesTypeName, 718 it.Payloads) 719 } 720 721 func (it *PayloadWrapper) Clear() { 722 if it == nil { 723 return 724 } 725 726 it.Payloads = it.Payloads[:0] 727 it.Attributes.Clear() 728 } 729 730 func (it *PayloadWrapper) Dispose() { 731 if it == nil { 732 return 733 } 734 735 it.Clear() 736 it.Attributes = nil 737 } 738 739 func (it *PayloadWrapper) AsJsonContractsBinder() corejson.JsonContractsBinder { 740 return it 741 } 742 743 func (it *PayloadWrapper) Clone( 744 isDeepClone bool, 745 ) (PayloadWrapper, error) { 746 clonedPtr, err := it.ClonePtr(isDeepClone) 747 748 if err != nil { 749 return PayloadWrapper{}, err 750 } 751 752 if clonedPtr == nil { 753 return PayloadWrapper{}, defaulterr.NilResult 754 } 755 756 return clonedPtr.NonPtr(), nil 757 } 758 759 func (it *PayloadWrapper) ClonePtr( 760 isDeepClone bool, 761 ) (*PayloadWrapper, error) { 762 if it == nil { 763 return nil, nil 764 } 765 766 attrCloned, err := it. 767 Attributes. 768 ClonePtr(isDeepClone) 769 770 if err != nil { 771 return nil, err 772 } 773 774 if isDeepClone { 775 return &PayloadWrapper{ 776 Name: it.Name, 777 Identifier: it.Identifier, 778 TaskTypeName: it.TaskTypeName, 779 EntityType: it.EntityType, 780 CategoryName: it.CategoryName, 781 HasManyRecords: it.HasManyRecords, 782 Payloads: corejson.BytesDeepClone( 783 it.Payloads), 784 Attributes: attrCloned, 785 }, nil 786 } 787 788 // NOT deep clone 789 return &PayloadWrapper{ 790 Name: it.Name, 791 Identifier: it.Identifier, 792 TaskTypeName: it.TaskTypeName, 793 EntityType: it.EntityType, 794 CategoryName: it.CategoryName, 795 HasManyRecords: it.HasManyRecords, 796 Payloads: it.Payloads, 797 Attributes: attrCloned, 798 }, nil 799 } 800 801 func (it *PayloadWrapper) NonPtr() PayloadWrapper { 802 if it == nil { 803 return PayloadWrapper{} 804 } 805 806 return *it 807 } 808 809 // ToPtr 810 // 811 // can panic if nil 812 func (it PayloadWrapper) ToPtr() *PayloadWrapper { 813 return &it 814 } 815 816 func (it PayloadWrapper) AsStandardTaskEntityDefinerContractsBinder() entityinf.StandardTaskEntityDefinerContractsBinder { 817 return &it 818 } 819 820 func (it PayloadWrapper) AsPayloadsBinder() payloadinf.PayloadsBinder { 821 return &it 822 } 823 func (it PayloadWrapper) AsJsonMarshaller() corejson.JsonMarshaller { 824 return &it 825 }