gitlab.com/evatix-go/core@v1.3.55/coredata/corepayload/Attributes.go (about) 1 package corepayload 2 3 import ( 4 "bytes" 5 6 "gitlab.com/evatix-go/core/coredata/coredynamic" 7 "gitlab.com/evatix-go/core/coredata/corejson" 8 "gitlab.com/evatix-go/core/coredata/corestr" 9 "gitlab.com/evatix-go/core/coreinstruction" 10 "gitlab.com/evatix-go/core/coreinterface/errcoreinf" 11 "gitlab.com/evatix-go/core/coreinterface/payloadinf" 12 "gitlab.com/evatix-go/core/defaulterr" 13 "gitlab.com/evatix-go/core/errcore" 14 ) 15 16 type Attributes struct { 17 BasicErrWrapper errcoreinf.BasicErrWrapper `json:"BasicErrWrapper,omitempty"` 18 AuthInfo *AuthInfo `json:"AuthInfo,omitempty"` 19 PagingInfo *PagingInfo `json:"PagingInfo,omitempty"` 20 KeyValuePairs *corestr.Hashmap `json:"KeyValuePairs,omitempty"` 21 AnyKeyValuePairs *coredynamic.MapAnyItems `json:"AnyKeyValuePairs,omitempty"` 22 FromTo *coreinstruction.FromTo `json:"FromTo,omitempty"` // Invoker and Receiver Details 23 DynamicPayloads []byte `json:"DynamicPayloads,omitempty"` 24 } 25 26 func (it *Attributes) IsNull() bool { 27 return it == nil 28 } 29 30 func (it *Attributes) HasSafeItems() bool { 31 return !it.HasIssuesOrEmpty() 32 } 33 34 func (it *Attributes) HandleErr() { 35 if it.HasError() { 36 it.BasicErrWrapper.HandleError() 37 } 38 } 39 40 func (it *Attributes) HasStringKey(key string) bool { 41 if it.HasKeyValuePairs() { 42 return it.KeyValuePairs.Has(key) 43 } 44 45 return false 46 } 47 48 func (it *Attributes) HasAnyKey(key string) bool { 49 if it.HasAnyKeyValuePairs() { 50 return it.AnyKeyValuePairs.HasKey(key) 51 } 52 53 return false 54 } 55 56 // SetAuthInfo 57 // 58 // On nil create new attributes 59 func (it *Attributes) SetAuthInfo(authInfo *AuthInfo) *Attributes { 60 if it == nil { 61 return New. 62 Attributes. 63 UsingAuthInfo(authInfo) 64 } 65 66 it.AuthInfo = authInfo 67 68 return it 69 } 70 71 func (it *Attributes) SetUserInfo( 72 userInfo *UserInfo, 73 ) *Attributes { 74 if it == nil { 75 return &Attributes{ 76 AuthInfo: &AuthInfo{ 77 UserInfo: userInfo, 78 }, 79 } 80 } 81 82 it.AuthInfo.SetUserInfo(userInfo) 83 84 return it 85 } 86 87 func (it *Attributes) AddNewStringKeyValueOnly(key, value string) (isAdded bool) { 88 if it == nil || it.KeyValuePairs == nil { 89 return false 90 } 91 92 it.KeyValuePairs. 93 AddOrUpdate(key, value) 94 95 return true 96 } 97 98 func (it *Attributes) AddNewAnyKeyValueOnly( 99 key string, value interface{}, 100 ) (isAdded bool) { 101 if it == nil || it.AnyKeyValuePairs == nil { 102 return false 103 } 104 105 it.AnyKeyValuePairs.Add(key, value) 106 107 return true 108 } 109 110 func (it *Attributes) GetStringKeyValue( 111 key string, 112 ) (value string, isFound bool) { 113 if it == nil || it.KeyValuePairs == nil { 114 return "", false 115 } 116 117 return it.KeyValuePairs.Get(key) 118 } 119 120 func (it *Attributes) GetAnyKeyValue( 121 key string, 122 ) (valueAny interface{}, isFound bool) { 123 if it == nil || it.KeyValuePairs == nil { 124 return nil, false 125 } 126 127 return it.AnyKeyValuePairs.Get(key) 128 } 129 130 func (it *Attributes) AnyKeyReflectSetTo( 131 key string, 132 toPtr interface{}, 133 ) error { 134 if it == nil || it.KeyValuePairs == nil { 135 return errcore. 136 CannotBeNilOrEmptyType.ErrorNoRefs( 137 "KeyValuePairs is nil") 138 } 139 140 return it.AnyKeyValuePairs.ReflectSetTo(key, toPtr) 141 } 142 143 func (it *Attributes) HandleError() { 144 if it.HasError() { 145 it.BasicErrWrapper.HandleError() 146 } 147 } 148 149 func (it *Attributes) ReflectSetTo( 150 toPointer interface{}, 151 ) error { 152 return coredynamic.ReflectSetFromTo(it, toPointer) 153 } 154 155 func (it *Attributes) Payloads() []byte { 156 if it.IsEmpty() { 157 return []byte{} 158 } 159 160 return it.DynamicPayloads 161 } 162 163 func (it *Attributes) PayloadsString() string { 164 if it.IsEmpty() || len(it.DynamicPayloads) == 0 { 165 return "" 166 } 167 168 return string(it.DynamicPayloads) 169 } 170 171 func (it *Attributes) PayloadsPrettyString() string { 172 if it.IsEmpty() || len(it.DynamicPayloads) == 0 { 173 return "" 174 } 175 176 return corejson.BytesToPrettyString(it.DynamicPayloads) 177 } 178 179 func (it *Attributes) PayloadsJsonResult() *corejson.Result { 180 if it.IsEmpty() || len(it.DynamicPayloads) == 0 { 181 return corejson.Empty.ResultPtr() 182 } 183 184 return corejson.NewResult.UsingTypeBytesPtr( 185 attributesTypeName, 186 it.DynamicPayloads) 187 } 188 189 func (it *Attributes) AnyKeyValMap() map[string]interface{} { 190 if it.IsEmpty() { 191 return map[string]interface{}{} 192 } 193 194 return it.AnyKeyValuePairs.Items 195 } 196 197 func (it *Attributes) Hashmap() map[string]string { 198 if it.IsEmpty() { 199 return map[string]string{} 200 } 201 202 return it.KeyValuePairs.Items() 203 } 204 205 func (it *Attributes) CompiledError() error { 206 return it.Error() 207 } 208 209 func (it *Attributes) HasIssuesOrEmpty() bool { 210 return it.IsEmpty() || 211 !it.IsValid() || 212 it.BasicErrWrapper != nil && 213 it.BasicErrWrapper.HasError() 214 } 215 216 func (it *Attributes) IsSafeValid() bool { 217 return it.HasIssuesOrEmpty() 218 } 219 220 func (it *Attributes) JsonString() string { 221 return it.JsonPtr().JsonString() 222 } 223 224 func (it *Attributes) JsonStringMust() string { 225 jsonResult := it.JsonPtr() 226 jsonResult.MustBeSafe() 227 228 return jsonResult.JsonString() 229 } 230 231 func (it *Attributes) HasAnyItem() bool { 232 return !it.IsEmpty() 233 } 234 235 func (it *Attributes) Count() int { 236 return it.Length() 237 } 238 239 func (it *Attributes) Capacity() int { 240 return it.Length() 241 } 242 243 func (it *Attributes) Length() int { 244 if it == nil { 245 return 0 246 } 247 248 return len(it.DynamicPayloads) 249 } 250 251 func (it *Attributes) HasPagingInfo() bool { 252 return it != nil && it.PagingInfo != nil 253 } 254 255 func (it *Attributes) HasKeyValuePairs() bool { 256 return it != nil && it.KeyValuePairs.HasAnyItem() 257 } 258 259 func (it *Attributes) HasFromTo() bool { 260 return it != nil && it.FromTo != nil 261 } 262 263 func (it *Attributes) IsValid() bool { 264 return it != nil && 265 it.IsEmptyError() 266 } 267 268 func (it *Attributes) IsInvalid() bool { 269 return it == nil || it.HasIssuesOrEmpty() 270 } 271 272 func (it *Attributes) HasError() bool { 273 return it != nil && 274 it.BasicErrWrapper != nil && 275 it.BasicErrWrapper.HasError() 276 } 277 278 func (it *Attributes) Error() error { 279 if it.IsEmptyError() { 280 return nil 281 } 282 283 return it. 284 BasicErrWrapper. 285 CompiledErrorWithStackTraces() 286 } 287 288 func (it *Attributes) MustBeEmptyError() { 289 if it.IsEmptyError() { 290 return 291 } 292 293 panic(it.Error()) 294 } 295 296 // BasicErrorDeserializedTo 297 // 298 // Expectation Attributes.ErrorMessage needs to 299 // be in json format and toPtr 300 // should match reflection types 301 func (it *Attributes) BasicErrorDeserializedTo( 302 toPtr interface{}, 303 ) error { 304 if it.IsEmptyError() { 305 return nil 306 } 307 308 return corejson. 309 Deserialize. 310 UsingBytes( 311 it.BasicErrWrapper.SerializeMust(), 312 toPtr) 313 } 314 315 func (it *Attributes) DeserializeDynamicPayloads( 316 toPtr interface{}, 317 ) error { 318 return corejson. 319 Deserialize. 320 UsingBytes( 321 it.DynamicPayloads, 322 toPtr) 323 } 324 325 func (it *Attributes) DeserializeDynamicPayloadsToAttributes() ( 326 newAttr *Attributes, err error, 327 ) { 328 newAttr = &Attributes{} 329 err = corejson.Deserialize.UsingBytes( 330 it.DynamicPayloads, 331 newAttr) 332 333 return newAttr, err 334 } 335 336 func (it *Attributes) DeserializeDynamicPayloadsToPayloadWrapper() ( 337 payloadWrapper *PayloadWrapper, err error, 338 ) { 339 payloadWrapper = New.PayloadWrapper.Empty() 340 err = corejson.Deserialize.UsingBytes( 341 it.DynamicPayloads, 342 payloadWrapper) 343 344 return payloadWrapper, err 345 } 346 347 func (it *Attributes) DeserializeDynamicPayloadsToPayloadWrappersCollection() ( 348 payloadsCollection *PayloadsCollection, err error, 349 ) { 350 return New. 351 PayloadsCollection. 352 Deserialize( 353 it.DynamicPayloads) 354 } 355 356 func (it *Attributes) DeserializeDynamicPayloadsMust( 357 toPtr interface{}, 358 ) { 359 corejson.Deserialize. 360 UsingBytesMust( 361 it.DynamicPayloads, 362 toPtr) 363 } 364 365 func (it *Attributes) IsEmptyError() bool { 366 return it == nil || 367 it.BasicErrWrapper == nil || 368 it.BasicErrWrapper.IsEmpty() 369 } 370 371 func (it *Attributes) DynamicBytesLength() int { 372 if it == nil { 373 return 0 374 } 375 376 return len(it.DynamicPayloads) 377 } 378 379 func (it *Attributes) StringKeyValuePairsLength() int { 380 if it == nil { 381 return 0 382 } 383 384 return it.KeyValuePairs.Length() 385 } 386 387 func (it *Attributes) AnyKeyValuePairsLength() int { 388 if it == nil { 389 return 0 390 } 391 392 return it.AnyKeyValuePairs.Length() 393 } 394 395 func (it *Attributes) IsEmpty() bool { 396 return it == nil || 397 it.DynamicBytesLength() == 0 && 398 it.StringKeyValuePairsLength() == 0 && 399 it.AnyKeyValuePairsLength() == 0 400 } 401 402 func (it *Attributes) HasItems() bool { 403 return !it.IsEmpty() 404 } 405 406 func (it *Attributes) IsPagingInfoEmpty() bool { 407 return it == nil || 408 it.PagingInfo.IsEmpty() 409 } 410 411 func (it *Attributes) IsKeyValuePairsEmpty() bool { 412 return it == nil || 413 it.KeyValuePairs.IsEmpty() 414 } 415 416 func (it *Attributes) IsAnyKeyValuePairsEmpty() bool { 417 return it == nil || 418 it.AnyKeyValuePairs.IsEmpty() 419 } 420 421 func (it *Attributes) IsUserInfoEmpty() bool { 422 return it == nil || 423 it.AuthInfo.IsUserInfoEmpty() 424 } 425 426 func (it *Attributes) VirtualUser() *User { 427 if it.IsUserInfoEmpty() { 428 return nil 429 } 430 431 return it.AuthInfo.UserInfo.User 432 } 433 434 func (it *Attributes) SystemUser() *User { 435 if it.IsUserInfoEmpty() { 436 return nil 437 } 438 439 return it.AuthInfo.UserInfo.SystemUser 440 } 441 442 func (it *Attributes) SessionUser() *User { 443 if it.IsSessionInfoEmpty() { 444 return nil 445 } 446 447 return it.AuthInfo.SessionInfo.User 448 } 449 450 func (it *Attributes) IsAuthInfoEmpty() bool { 451 return it == nil || 452 it.AuthInfo.IsEmpty() 453 } 454 455 func (it *Attributes) IsSessionInfoEmpty() bool { 456 return it == nil || 457 it.AuthInfo.IsSessionInfoEmpty() 458 } 459 460 func (it *Attributes) HasUserInfo() bool { 461 return !it.IsUserInfoEmpty() 462 } 463 464 func (it *Attributes) HasAuthInfo() bool { 465 return !it.IsAuthInfoEmpty() 466 } 467 468 func (it *Attributes) HasSessionInfo() bool { 469 return !it.IsSessionInfoEmpty() 470 } 471 472 func (it *Attributes) SessionInfo() *SessionInfo { 473 if it.IsSessionInfoEmpty() { 474 return nil 475 } 476 477 return it.AuthInfo.SessionInfo 478 } 479 480 func (it *Attributes) AuthType() string { 481 if it.IsAuthInfoEmpty() { 482 return "" 483 } 484 485 return it.AuthInfo.ActionType 486 } 487 488 func (it *Attributes) ResourceName() string { 489 if it.IsAuthInfoEmpty() { 490 return "" 491 } 492 493 return it.AuthInfo.ResourceName 494 } 495 496 func (it *Attributes) HasStringKeyValuePairs() bool { 497 return it.StringKeyValuePairsLength() > 0 498 } 499 500 func (it *Attributes) HasAnyKeyValuePairs() bool { 501 return it.AnyKeyValuePairsLength() > 0 502 } 503 504 func (it *Attributes) HasDynamicPayloads() bool { 505 return it.DynamicBytesLength() > 0 506 } 507 508 func (it *Attributes) DynamicPayloadsDeserialize( 509 unmarshallingPointer interface{}, 510 ) error { 511 if it == nil { 512 return defaulterr.AttributeNull 513 } 514 515 return corejson.Deserialize.UsingBytes( 516 it.DynamicPayloads, 517 unmarshallingPointer) 518 } 519 520 func (it *Attributes) DynamicPayloadsDeserializeMust( 521 unmarshallingPointer interface{}, 522 ) { 523 err := corejson.Deserialize.UsingBytes( 524 it.DynamicPayloads, 525 unmarshallingPointer) 526 527 if err != nil { 528 panic(err) 529 } 530 } 531 532 func (it *Attributes) AddOrUpdateString( 533 key, value string, 534 ) (isNewlyAdded bool) { 535 return it. 536 KeyValuePairs. 537 AddOrUpdate(key, value) 538 } 539 540 func (it *Attributes) AddOrUpdateAnyItem( 541 key string, 542 anyItem interface{}, 543 ) (isNewlyAdded bool) { 544 return it. 545 AnyKeyValuePairs. 546 Add(key, anyItem) 547 } 548 549 func (it *Attributes) JsonModel() *Attributes { 550 return it 551 } 552 553 func (it *Attributes) JsonModelAny() interface{} { 554 return it.JsonModel() 555 } 556 557 func (it *Attributes) String() string { 558 return it.JsonString() 559 } 560 561 func (it *Attributes) PrettyJsonString() string { 562 return it.JsonPtr().PrettyJsonString() 563 } 564 565 func (it *Attributes) Json() corejson.Result { 566 return corejson.New(it) 567 } 568 569 func (it *Attributes) JsonPtr() *corejson.Result { 570 return corejson.NewPtr(it) 571 } 572 573 //goland:noinspection GoLinterLocal 574 func (it *Attributes) ParseInjectUsingJson( 575 jsonResult *corejson.Result, 576 ) (*Attributes, error) { 577 err := jsonResult.Unmarshal(it) 578 579 if err != nil { 580 return &Attributes{}, err 581 } 582 583 return it, nil 584 } 585 586 // ParseInjectUsingJsonMust Panic if error 587 //goland:noinspection GoLinterLocal 588 func (it *Attributes) ParseInjectUsingJsonMust( 589 jsonResult *corejson.Result, 590 ) *Attributes { 591 newUsingJson, err := 592 it.ParseInjectUsingJson(jsonResult) 593 594 if err != nil { 595 panic(err) 596 } 597 598 return newUsingJson 599 } 600 601 func (it *Attributes) JsonParseSelfInject( 602 jsonResult *corejson.Result, 603 ) error { 604 _, err := it.ParseInjectUsingJson( 605 jsonResult, 606 ) 607 608 return err 609 } 610 611 // SetBasicErr 612 // 613 // on nil creates and attach new error and returns the attributes 614 func (it *Attributes) SetBasicErr( 615 basicErr errcoreinf.BasicErrWrapper, 616 ) payloadinf.AttributesBinder { 617 if it == nil { 618 return New.Attributes.UsingBasicError( 619 basicErr) 620 } 621 622 it.BasicErrWrapper = basicErr 623 624 return it 625 } 626 627 func (it *Attributes) Clear() { 628 if it == nil { 629 return 630 } 631 632 it.KeyValuePairs.Clear() 633 it.AnyKeyValuePairs.Clear() 634 it.DynamicPayloads = []byte{} 635 } 636 637 func (it *Attributes) Dispose() { 638 it.Clear() 639 } 640 641 func (it Attributes) AsJsonContractsBinder() corejson.JsonContractsBinder { 642 return &it 643 } 644 645 func (it *Attributes) IsEqual(attributes *Attributes) bool { 646 if it == nil && attributes == nil { 647 return true 648 } 649 650 if it == nil || attributes == nil { 651 return false 652 } 653 654 if it == attributes { 655 return true 656 } 657 658 if it.IsErrorDifferent(attributes.BasicErrWrapper) { 659 return false 660 } 661 662 if !it.PagingInfo.IsEqual(attributes.PagingInfo) { 663 return false 664 } 665 666 if !it.KeyValuePairs.IsEqualPtr(attributes.KeyValuePairs) { 667 return false 668 } 669 670 if !bytes.Equal( 671 it.DynamicPayloads, 672 attributes.DynamicPayloads) { 673 return false 674 } 675 676 if !it.AnyKeyValuePairs.IsEqual(attributes.AnyKeyValuePairs) { 677 return false 678 } 679 680 return true 681 } 682 683 func (it *Attributes) Clone( 684 isDeepClone bool, 685 ) (Attributes, error) { 686 clonedPtr, err := it.ClonePtr(isDeepClone) 687 688 if err != nil { 689 return Attributes{}, err 690 } 691 692 if clonedPtr == nil { 693 return Attributes{}, nil 694 } 695 696 return clonedPtr.NonPtr(), nil 697 } 698 699 func (it *Attributes) ClonePtr( 700 isDeepClone bool, 701 ) (*Attributes, error) { 702 if it == nil { 703 return nil, nil 704 } 705 706 if isDeepClone { 707 return it.deepClonePtr() 708 } 709 710 // NOT deep clone 711 return New. 712 Attributes. 713 All( 714 it.AuthInfo, 715 it.KeyValuePairs, 716 it.AnyKeyValuePairs, 717 it.PagingInfo, 718 it.DynamicPayloads, 719 it.FromTo, 720 it.BasicErrWrapper, 721 ), nil 722 } 723 724 func (it *Attributes) deepClonePtr() (*Attributes, error) { 725 anyMap, err := it.AnyKeyValuePairs.ClonePtr() 726 727 if err != nil { 728 return nil, err 729 } 730 731 var basicErr errcoreinf.BasicErrWrapper 732 733 if it.HasError() { 734 basicErr = it.BasicErrWrapper.CloneInterface() 735 } 736 737 return New. 738 Attributes. 739 All( 740 it.AuthInfo.ClonePtr(), 741 it.KeyValuePairs.ClonePtr(), 742 anyMap, 743 it.PagingInfo.ClonePtr(), 744 corejson.BytesDeepClone(it.DynamicPayloads), 745 it.FromTo.ClonePtr(), 746 basicErr), nil 747 } 748 749 func (it Attributes) NonPtr() Attributes { 750 return it 751 } 752 753 func (it Attributes) AsAttributesBinder() payloadinf.AttributesBinder { 754 return &it 755 } 756 757 func (it *Attributes) IsErrorDifferent(basicErr errcoreinf.BasicErrWrapper) bool { 758 return !it.IsErrorEqual(basicErr) 759 } 760 761 func (it *Attributes) IsErrorEqual(basicErr errcoreinf.BasicErrWrapper) bool { 762 if it.IsEmptyError() || basicErr == nil || basicErr.IsEmpty() { 763 return true 764 } 765 766 return it.BasicErrWrapper.IsBasicErrEqual(basicErr) 767 }