gitlab.com/evatix-go/core@v1.3.55/issetter/Value.go (about) 1 package issetter 2 3 import ( 4 "errors" 5 "fmt" 6 "strconv" 7 "strings" 8 9 "gitlab.com/evatix-go/core/constants" 10 "gitlab.com/evatix-go/core/coreimpl/enumimpl/enumtype" 11 "gitlab.com/evatix-go/core/coreinterface/enuminf" 12 "gitlab.com/evatix-go/core/defaulterr" 13 "gitlab.com/evatix-go/core/internal/csvinternal" 14 ) 15 16 // Value 17 // 18 // Used evaluate lazy boolean valuesNames. 19 // 20 // Values: 21 // - Uninitialized Value = 0 22 // - True Value = 1 23 // - False Value = 2 24 // - Unset Value = 3 25 // - Set Value = 4 26 // - Wildcard Value = 5 27 type Value byte 28 29 const ( 30 Uninitialized Value = 0 31 True Value = 1 32 False Value = 2 33 Unset Value = 3 34 Set Value = 4 35 Wildcard Value = 5 36 ) 37 38 func (it Value) AllNameValues() []string { 39 slice := make([]string, len(valuesNames)) 40 41 for i := range valuesNames { 42 slice[i] = Value(i).NameValue() 43 } 44 45 return slice 46 } 47 48 func (it Value) OnlySupportedErr(names ...string) error { 49 if len(names) == 0 { 50 return nil 51 } 52 53 hashset := toHashset(names...) 54 var unsupportedNames []string 55 56 for _, name := range valuesNames { 57 _, has := hashset[name] 58 59 if !has { 60 unsupportedNames = append(unsupportedNames, name) 61 } 62 } 63 64 if len(unsupportedNames) > 0 { 65 return errors.New(csvinternal.StringsToStringDefault(unsupportedNames...) + " not supported") 66 } 67 68 return nil 69 70 } 71 72 func (it Value) OnlySupportedMsgErr(message string, names ...string) error { 73 err := it.OnlySupportedErr(names...) 74 75 if err == nil { 76 return nil 77 } 78 79 return errors.New(message + err.Error()) 80 } 81 82 func (it Value) ValueUInt16() uint16 { 83 return uint16(it) 84 } 85 86 func (it Value) IntegerEnumRanges() []int { 87 return integerRanges 88 } 89 90 func (it Value) MinMaxAny() (min, max interface{}) { 91 return Min(), Max() 92 } 93 94 func (it Value) MinValueString() string { 95 return Min().StringValue() 96 } 97 98 func (it Value) MaxValueString() string { 99 return Max().StringValue() 100 } 101 102 func (it Value) MaxInt() int { 103 return Max().ValueInt() 104 } 105 106 func (it Value) MinInt() int { 107 return Min().ValueInt() 108 } 109 110 func (it Value) RangesDynamicMap() map[string]interface{} { 111 return dynamicRangesMap 112 } 113 114 func (it Value) IsValueEqual(value byte) bool { 115 return byte(it) == value 116 } 117 118 func (it Value) RangeNamesCsv() string { 119 return RangeNamesCsv() 120 } 121 122 func (it Value) IsByteValueEqual(value byte) bool { 123 return byte(it) == value 124 } 125 126 func (it Value) IsOn() bool { 127 return trueMap[it] 128 } 129 130 func (it Value) IsOff() bool { 131 return falseMap[it] 132 } 133 134 func (it Value) IsLater() bool { 135 return it.IsUndefinedLogically() 136 } 137 138 // IsNo 139 // 140 // Returns true if False or Unset 141 func (it Value) IsNo() bool { 142 return falseMap[it] 143 } 144 145 // IsAsk 146 // 147 // Returns true if Uninitialized or Wildcard 148 func (it Value) IsAsk() bool { 149 return undefinedMap[it] 150 } 151 152 // IsIndeterminate 153 // 154 // Returns true if Uninitialized or Wildcard 155 func (it Value) IsIndeterminate() bool { 156 return undefinedMap[it] 157 } 158 159 // IsAccept 160 // 161 // Returns true if True or Set 162 func (it Value) IsAccept() bool { 163 return trueMap[it] 164 } 165 166 // IsReject 167 // 168 // Returns true if False or Unset 169 func (it Value) IsReject() bool { 170 return falseMap[it] 171 } 172 173 func (it Value) IsFailed() bool { 174 return falseMap[it] 175 } 176 177 func (it Value) IsSuccess() bool { 178 return trueMap[it] 179 } 180 181 // IsSkip 182 // 183 // Returns true if Uninitialized or Wildcard 184 func (it Value) IsSkip() bool { 185 return undefinedMap[it] 186 } 187 188 func (it Value) NameValue() string { 189 return fmt.Sprintf( 190 constants.EnumNameValueFormat, 191 it.Name(), 192 it.Value()) 193 } 194 195 func (it Value) IsNameEqual(name string) bool { 196 return it.Name() == name 197 } 198 199 func (it Value) IsAnyNamesOf(names ...string) bool { 200 for _, name := range names { 201 if it.IsNameEqual(name) { 202 return true 203 } 204 } 205 206 return false 207 } 208 209 func (it Value) ToNumberString() string { 210 return strconv.Itoa(it.ValueInt()) 211 } 212 213 func (it Value) ValueByte() byte { 214 return byte(it) 215 } 216 217 func (it Value) ValueInt() int { 218 return int(it) 219 } 220 221 func (it Value) ValueInt8() int8 { 222 return int8(it) 223 } 224 225 func (it Value) ValueInt16() int16 { 226 return int16(it) 227 } 228 229 func (it Value) ValueInt32() int32 { 230 return int32(it) 231 } 232 233 func (it Value) ValueString() string { 234 return strconv.Itoa(it.ValueInt()) 235 } 236 237 func (it Value) Format(format string) (compiled string) { 238 newMap := map[string]string{ 239 "{type-name}": typeName, 240 "{name}": it.Name(), 241 "{value}": it.ValueString(), 242 } 243 244 for search, replacer := range newMap { 245 format = strings.ReplaceAll(format, search, replacer) 246 } 247 248 return format 249 } 250 251 func (it Value) EnumType() enuminf.EnumTyper { 252 return enumtype.Byte 253 } 254 255 func (it Value) Value() byte { 256 return byte(it) 257 } 258 259 func (it Value) StringValue() string { 260 return strconv.Itoa(it.ValueInt()) 261 } 262 263 func (it Value) String() string { 264 return valuesNames[it] 265 } 266 267 // IsTrue v == True 268 func (it Value) IsTrue() bool { 269 return it == True 270 } 271 272 // IsFalse v == False 273 func (it Value) IsFalse() bool { 274 return it == False 275 } 276 277 func (it Value) IsTrueOrSet() bool { 278 return it == True || it == Set 279 } 280 281 // IsSet v == Set 282 func (it Value) IsSet() bool { 283 return it == Set 284 } 285 286 // IsUnset v == Unset 287 func (it Value) IsUnset() bool { 288 return it == Unset 289 } 290 291 func (it Value) HasInitialized() bool { 292 return it != Uninitialized 293 } 294 295 func (it Value) HasInitializedAndSet() bool { 296 return it == Set 297 } 298 299 func (it Value) HasInitializedAndTrue() bool { 300 return it == True 301 } 302 303 func (it Value) IsWildcard() bool { 304 return it == Wildcard 305 } 306 307 func (it Value) IsInit() bool { 308 return it != Uninitialized 309 } 310 311 func (it Value) IsInitBoolean() bool { 312 return it == True || it == False 313 } 314 315 func (it Value) IsDefinedBoolean() bool { 316 return it == True || it == False 317 } 318 319 func (it Value) IsInitBooleanWild() bool { 320 return it == True || it == False || it == Wildcard 321 } 322 323 func (it Value) IsInitSet() bool { 324 return it == Set || it == Unset 325 } 326 327 func (it Value) IsInitSetWild() bool { 328 return it == Set || it == Unset || it == Wildcard 329 } 330 331 func (it Value) IsYes() bool { 332 return it == True 333 } 334 335 func (it Value) Boolean() bool { 336 return it == True 337 } 338 339 func (it Value) IsOnLogically() bool { 340 return it.IsInitialized() && trueMap[it] 341 } 342 343 func (it Value) IsOffLogically() bool { 344 return it.IsInitialized() && falseMap[it] 345 } 346 347 func (it Value) IsAccepted() bool { 348 return it.IsOnLogically() 349 } 350 351 func (it Value) IsRejected() bool { 352 return it.IsOffLogically() 353 } 354 355 // IsDefinedLogically 356 // 357 // Not Uninitialized, Wildcard 358 func (it Value) IsDefinedLogically() bool { 359 return !undefinedMap[it] 360 } 361 362 // IsUndefinedLogically 363 // 364 // Either Uninitialized, Wildcard 365 func (it Value) IsUndefinedLogically() bool { 366 return undefinedMap[it] 367 } 368 369 func (it Value) IsInvalid() bool { 370 return it == Uninitialized 371 } 372 373 func (it Value) IsValid() bool { 374 return it != Uninitialized 375 } 376 377 func (it *Value) GetSetBoolOnInvalid( 378 setterValue bool, 379 ) bool { 380 if it.IsDefinedBoolean() { 381 return it.IsTrue() 382 } 383 384 *it = GetBool(setterValue) 385 386 return it.IsTrue() 387 } 388 389 func (it *Value) GetSetBoolOnInvalidFunc( 390 setterFunc func() bool, 391 ) bool { 392 if it.IsDefinedBoolean() { 393 return it.IsTrue() 394 } 395 396 *it = GetBool(setterFunc()) 397 398 return it.IsTrue() 399 } 400 401 func (it Value) ToBooleanValue() Value { 402 return convSetUnsetToTrueFalseMap[it] 403 } 404 405 func (it Value) ToSetUnsetValue() Value { 406 return convTrueFalseToSetUnsetMap[it] 407 } 408 409 // LazyEvaluateBool 410 // 411 // Only execute evaluatorFunc if Uninitialized 412 // and then set True to self and returns t/f based on called or not 413 func (it *Value) LazyEvaluateBool( 414 evaluatorFunc func(), 415 ) (isCalled bool) { 416 if it.IsDefinedBoolean() { 417 return false 418 } 419 420 evaluatorFunc() 421 *it = True 422 423 return it.IsTrue() 424 } 425 426 // LazyEvaluateSet 427 // 428 // Only execute evaluatorFunc if Uninitialized 429 // and then set True to self and returns t/f based on called or not 430 func (it *Value) LazyEvaluateSet( 431 evaluatorFunc func(), 432 ) (isCalled bool) { 433 if it.IsInitSet() { 434 return false 435 } 436 437 evaluatorFunc() 438 *it = Set 439 440 return it.IsSet() 441 } 442 443 // IsWildcardOrBool 444 // 445 // if v.IsWildcard() then returns true regardless 446 // 447 // or else 448 // 449 // returns (isBool && v.IsTrue()) || (!isBool && v.IsFalse()) 450 func (it Value) IsWildcardOrBool(isBool bool) bool { 451 if it.IsWildcard() { 452 return true 453 } 454 455 return isBool 456 } 457 458 func (it Value) ToByteCondition(trueVal, falseVal, invalid byte) byte { 459 if it.IsTrue() { 460 return trueVal 461 } 462 463 if it.IsFalse() { 464 return falseVal 465 } 466 467 return invalid 468 } 469 470 func (it Value) ToByteConditionWithWildcard(wildcard, trueVal, falseVal, invalid byte) byte { 471 if it.IsWildcard() { 472 return wildcard 473 } 474 475 return it.ToByteCondition(trueVal, falseVal, invalid) 476 } 477 478 // WildcardApply 479 // 480 // if IsWildcard() || IsUnSetOrUninitialized() then 481 // 482 // return inputVal 483 // else 484 // 485 // return v. IsTrue() 486 func (it Value) WildcardApply(inputBool bool) bool { 487 if it.IsWildcard() || it.IsUnSetOrUninitialized() { 488 return inputBool 489 } 490 491 return it.IsTrue() 492 } 493 494 // WildcardValueApply 495 // 496 // if IsWildcard() || IsUnSetOrUninitialized() then 497 // 498 // return inputVal 499 // else 500 // 501 // return v. IsTrue() 502 func (it Value) WildcardValueApply(inputVal Value) bool { 503 if it.IsWildcard() || it.IsUnSetOrUninitialized() { 504 return inputVal.IsTrue() 505 } 506 507 return it.IsTrue() 508 } 509 510 // OrBool 511 // 512 // if IsWildcard() || IsUnSetOrUninitialized() then 513 // 514 // return inputBool 515 // else 516 // 517 // return v. IsTrue() || inputBool 518 func (it Value) OrBool(inputBool bool) bool { 519 if it.IsWildcard() || it.IsUnSetOrUninitialized() { 520 return inputBool 521 } 522 523 return it.IsTrue() || inputBool 524 } 525 526 // OrValue 527 // 528 // if IsWildcard() || IsUnSetOrUninitialized() then 529 // 530 // return inputVal 531 // else 532 // 533 // return v. IsTrue() || inputVal. IsTrue() 534 func (it Value) OrValue(inputVal Value) bool { 535 if it.IsWildcard() || it.IsUnSetOrUninitialized() { 536 return inputVal.IsTrue() 537 } 538 539 return it.IsTrue() || inputVal.IsTrue() 540 } 541 542 // AndBool 543 // 544 // if IsWildcard() || IsUnSetOrUninitialized() then 545 // 546 // return inputVal 547 // else 548 // 549 // return v. IsTrue() && inputBool 550 func (it Value) AndBool(inputBool bool) bool { 551 if it.IsWildcard() || it.IsUnSetOrUninitialized() { 552 return inputBool 553 } 554 555 return it.IsTrue() && inputBool 556 } 557 558 // And 559 // 560 // if IsWildcard() || IsUnSetOrUninitialized() then 561 // 562 // return inputVal 563 // else 564 // 565 // return GetBool(v. IsTrue() && inputVal. IsTrue()) 566 func (it Value) And(inputVal Value) Value { 567 if it.IsWildcard() || it.IsUnSetOrUninitialized() { 568 return inputVal 569 } 570 571 return GetBool(it.IsTrue() && inputVal.IsTrue()) 572 } 573 574 // IsUninitialized v == Uninitialized 575 func (it Value) IsUninitialized() bool { 576 return it == Uninitialized 577 } 578 579 func (it Value) IsInitialized() bool { 580 return it != Uninitialized 581 } 582 583 // IsUnSetOrUninitialized v == Uninitialized || v == Unset 584 func (it Value) IsUnSetOrUninitialized() bool { 585 return it == Uninitialized || it == Unset 586 } 587 588 // IsNegative v == Uninitialized || v == Unset || v == False 589 func (it Value) IsNegative() bool { 590 return it == Uninitialized || it == Unset || it == False 591 } 592 593 // IsPositive v == True || v == Set 594 func (it Value) IsPositive() bool { 595 return it == True || it == Set 596 } 597 598 // IsBetween val >= start && val <= end 599 func (it Value) IsBetween(start, end byte) bool { 600 val := it.Value() 601 602 return val >= start && val <= end 603 } 604 605 // IsBetweenInt val >= start && val <= end 606 func (it Value) IsBetweenInt(start, end int) bool { 607 val := it.Value() 608 609 return val >= byte(start) && val <= byte(end) 610 } 611 612 // Add v + n 613 func (it Value) Add(n byte) Value { 614 return Value(it.Value() + n) 615 } 616 617 func (it Value) Is(n Value) bool { 618 return it.Value() == n.Value() 619 } 620 621 func (it Value) IsEqual(n byte) bool { 622 return it.Value() == n 623 } 624 625 // IsGreater v.Value() > n 626 func (it Value) IsGreater(n byte) bool { 627 return it.Value() > n 628 } 629 630 // IsGreaterEqual v.Value() >= n 631 func (it Value) IsGreaterEqual(n byte) bool { 632 return it.Value() >= n 633 } 634 635 // IsLess v.Value() < n 636 func (it Value) IsLess(n byte) bool { 637 return it.Value() < n 638 } 639 640 // IsLessEqual v.Value() <= n 641 func (it Value) IsLessEqual(n byte) bool { 642 return it.Value() <= n 643 } 644 645 func (it Value) IsEqualInt(n int) bool { 646 return it.Value() == byte(n) 647 } 648 649 // IsGreaterInt v.Value() > n 650 func (it Value) IsGreaterInt(n int) bool { 651 return it.Value() > byte(n) 652 } 653 654 // IsGreaterEqualInt v.Value() >= n 655 func (it Value) IsGreaterEqualInt(n int) bool { 656 return it.Value() >= byte(n) 657 } 658 659 // IsLessInt v.Value() < n 660 func (it Value) IsLessInt(n int) bool { 661 return it.Value() < byte(n) 662 } 663 664 // IsLessEqualInt v.Value() <= n 665 func (it Value) IsLessEqualInt(n int) bool { 666 return it.Value() <= byte(n) 667 } 668 669 func (it Value) PanicOnOutOfRange(n byte, msg string) { 670 if IsOutOfRange(n) { 671 panic(msg) 672 } 673 } 674 675 func (it Value) GetErrorOnOutOfRange(n byte, msg string) error { 676 if IsOutOfRange(n) { 677 return errors.New(msg) 678 } 679 680 return nil 681 } 682 683 func (it Value) Name() string { 684 return valuesToNameMap[it] 685 } 686 687 func (it Value) YesNoMappedValue() string { 688 if it.IsUninitialized() { 689 return constants.EmptyString 690 } 691 692 if it.IsTrueOrSet() { 693 return Yes 694 } 695 696 return No 697 } 698 699 func (it Value) YesNoLowercaseName() string { 700 return lowerCaseYesNoNames[it] 701 } 702 703 func (it Value) YesNoName() string { 704 return yesNoNames[it] 705 } 706 707 func (it Value) TrueFalseName() string { 708 return trueFalseNames[it] 709 } 710 711 func (it Value) OnOffLowercaseName() string { 712 return lowerCaseOnOffNames[it] 713 } 714 715 func (it Value) OnOffName() string { 716 return onOffNames[it] 717 } 718 719 func (it Value) TrueFalseLowercaseName() string { 720 return trueFalseLowerNames[it] 721 } 722 723 func (it Value) SetUnsetLowercaseName() string { 724 return setUnsetLowerNames[it] 725 } 726 727 func (it Value) MarshalJSON() ([]byte, error) { 728 return valuesToJsonBytesMap[it], nil 729 } 730 731 func (it *Value) UnmarshalJSON(data []byte) error { 732 if data == nil { 733 return defaulterr.UnmarshallingFailedDueToNilOrEmpty 734 } 735 736 str := string(data) 737 val, has := jsonValuesMap[str] 738 739 if !has { 740 //goland:noinspection SpellCheckingInspection 741 return errors.New( 742 "UnmarshalJSON failed , cannot map " + 743 str + 744 " to issetter.Value") 745 } 746 747 *it = val 748 749 return nil 750 } 751 752 func (it Value) Serialize() ([]byte, error) { 753 return it.MarshalJSON() 754 } 755 756 func (it Value) TypeName() string { 757 return typeName 758 } 759 760 func (it Value) IsAnyValuesEqual( 761 anyByteValues ...byte, 762 ) bool { 763 for _, value := range anyByteValues { 764 if it.Value() == value { 765 return true 766 } 767 } 768 769 return false 770 } 771 772 func (it Value) UnmarshallEnumToValue( 773 jsonUnmarshallingValue []byte, 774 ) (byte, error) { 775 err := it.UnmarshalJSON(jsonUnmarshallingValue) 776 777 return it.ValueByte(), err 778 } 779 780 func (it Value) Deserialize( 781 jsonBytes []byte, 782 ) (Value, error) { 783 currentVal, err := it.UnmarshallEnumToValue(jsonBytes) 784 785 if err != nil { 786 return Uninitialized, err 787 } 788 789 return Value(currentVal), err 790 } 791 792 func (it Value) MaxByte() byte { 793 return Wildcard.ValueByte() 794 } 795 796 func (it Value) MinByte() byte { 797 return Uninitialized.ValueByte() 798 } 799 800 func (it Value) RangesByte() []byte { 801 panic("not implemented, later, todo") 802 } 803 804 func (it Value) ToPtr() *Value { 805 return &it 806 }