gitlab.com/evatix-go/core@v1.3.55/coredata/corestr/SimpleStringOnce.go (about) 1 package corestr 2 3 import ( 4 "encoding/json" 5 "math" 6 "regexp" 7 "strconv" 8 "strings" 9 10 "gitlab.com/evatix-go/core/constants" 11 "gitlab.com/evatix-go/core/constants/bitsize" 12 "gitlab.com/evatix-go/core/coredata/corejson" 13 "gitlab.com/evatix-go/core/coreindexes" 14 "gitlab.com/evatix-go/core/errcore" 15 "gitlab.com/evatix-go/core/internal/strutilinternal" 16 "gitlab.com/evatix-go/core/issetter" 17 ) 18 19 type SimpleStringOnce struct { 20 value string 21 isInitialize bool 22 } 23 24 func (it *SimpleStringOnce) Value() string { 25 return it.value 26 } 27 28 func (it *SimpleStringOnce) IsInitialized() bool { 29 return it.isInitialize 30 } 31 32 // IsUnInit Not initialized yet 33 // 34 // !it.isInitialize 35 func (it *SimpleStringOnce) IsUnInit() bool { 36 return !it.isInitialize 37 } 38 39 // Invalidate 40 // 41 // Will make initialize to false 42 func (it *SimpleStringOnce) Invalidate() { 43 it.isInitialize = false 44 } 45 46 // IsInvalid 47 // 48 // !it.isInitialize || it.value == "" 49 func (it *SimpleStringOnce) IsInvalid() bool { 50 return !it.isInitialize || it.value == "" 51 } 52 53 func (it *SimpleStringOnce) ValueBytes() []byte { 54 return []byte(it.value) 55 } 56 57 func (it *SimpleStringOnce) ValueBytesPtr() *[]byte { 58 allBytes := []byte(it.value) 59 60 return &allBytes 61 } 62 63 func (it *SimpleStringOnce) SetOnUninitializedError(setVal string) error { 64 if it.isInitialize { 65 return errcore. 66 AlreadyInitializedType. 67 Error("cannot set :"+setVal, it.value) 68 } 69 70 it.value = setVal 71 it.SetInitialize() 72 73 return nil 74 } 75 76 func (it *SimpleStringOnce) GetPlusSetOnUninitialized( 77 setValPlusGetOnUnInit string, 78 ) (valGet string) { 79 if it.isInitialize { 80 return it.value 81 } 82 83 it.value = setValPlusGetOnUnInit 84 it.SetInitialize() 85 86 return it.value 87 } 88 89 func (it *SimpleStringOnce) GetPlusSetEmptyOnUninitialized() (valGet string) { 90 if it.isInitialize { 91 return it.value 92 } 93 94 it.value = constants.EmptyString 95 it.SetInitialize() 96 97 return it.value 98 } 99 100 func (it *SimpleStringOnce) GetPlusSetOnUninitializedFunc( 101 setValPlusGetOnUnInitFunc func() string, 102 ) (valGet string) { 103 if it.isInitialize { 104 return it.value 105 } 106 107 it.value = setValPlusGetOnUnInitFunc() 108 it.SetInitialize() 109 110 return it.value 111 } 112 113 func (it *SimpleStringOnce) SetOnUninitialized(setVal string) (isSet bool) { 114 if it.isInitialize { 115 return false 116 } 117 118 it.value = setVal 119 it.SetInitialize() 120 121 return true 122 } 123 124 func (it *SimpleStringOnce) SetInitialize() { 125 it.isInitialize = true 126 } 127 128 func (it *SimpleStringOnce) SetUnInit() { 129 it.isInitialize = false 130 } 131 132 func (it *SimpleStringOnce) ConcatNew(appendingText string) SimpleStringOnce { 133 return SimpleStringOnce{ 134 value: it.value + appendingText, 135 isInitialize: it.isInitialize, 136 } 137 } 138 139 func (it *SimpleStringOnce) ConcatNewUsingStrings( 140 joiner string, 141 appendingTexts ...string, 142 ) SimpleStringOnce { 143 slice := append([]string{it.value}, appendingTexts...) 144 145 return SimpleStringOnce{ 146 value: strings.Join(slice, joiner), 147 isInitialize: it.isInitialize, 148 } 149 } 150 151 func (it *SimpleStringOnce) IsEmpty() bool { 152 return it.value == "" 153 } 154 155 func (it *SimpleStringOnce) IsWhitespace() bool { 156 return strutilinternal.IsEmptyOrWhitespace(it.value) 157 } 158 159 func (it *SimpleStringOnce) Trim() string { 160 return strings.TrimSpace(it.value) 161 } 162 163 func (it *SimpleStringOnce) HasValidNonEmpty() bool { 164 return it.isInitialize && !it.IsEmpty() 165 } 166 167 func (it *SimpleStringOnce) HasValidNonWhitespace() bool { 168 return it.isInitialize && !it.IsWhitespace() 169 } 170 171 func (it *SimpleStringOnce) IsValueBool() bool { 172 return it.Boolean(false) 173 } 174 175 func (it *SimpleStringOnce) SafeValue() string { 176 if it.IsInitialized() { 177 return it.value 178 } 179 180 return constants.EmptyString 181 } 182 183 func (it *SimpleStringOnce) Uint16() (val uint16, isInRange bool) { 184 toUint16, isInRange := it.WithinRange( 185 true, 186 constants.Zero, 187 math.MaxUint16) 188 189 return uint16(toUint16), isInRange 190 } 191 192 func (it *SimpleStringOnce) Uint32() (val uint32, isInRange bool) { 193 converted, isInRange := it.WithinRange( 194 true, 195 constants.Zero, 196 math.MaxInt) 197 198 return uint32(converted), isInRange 199 } 200 201 func (it *SimpleStringOnce) WithinRangeDefault( 202 min, max int, 203 ) (val int, isInRange bool) { 204 return it.WithinRange( 205 true, 206 min, 207 max) 208 } 209 210 func (it *SimpleStringOnce) WithinRange( 211 isUsageMinMaxBoundary bool, 212 min, max int, 213 ) (val int, isInRange bool) { 214 toInt, err := strconv.Atoi(it.value) 215 216 if err != nil { 217 return constants.Zero, false 218 } 219 220 if toInt >= min && toInt <= max { 221 return toInt, true 222 } 223 224 if !isUsageMinMaxBoundary { 225 return toInt, false 226 } 227 228 if toInt < min { 229 return min, false 230 } 231 232 if toInt > max { 233 return max, false 234 } 235 236 return constants.Zero, false 237 } 238 239 func (it *SimpleStringOnce) Int() int { 240 toInt, err := strconv.Atoi(it.value) 241 242 if err != nil { 243 return constants.Zero 244 } 245 246 return toInt 247 } 248 249 func (it *SimpleStringOnce) Byte() byte { 250 toInt, err := strconv.Atoi(it.value) 251 252 if err != nil { 253 return constants.Zero 254 } 255 256 if toInt >= constants.Zero && toInt <= constants.MaxUnit8AsInt { 257 return byte(toInt) 258 } 259 260 return constants.Zero 261 } 262 263 func (it *SimpleStringOnce) Int16() int16 { 264 toInt, err := strconv.Atoi(it.value) 265 266 if err != nil { 267 return constants.Zero 268 } 269 270 if toInt >= math.MinInt16 && toInt <= constants.MaxInt16AsInt { 271 return int16(toInt) 272 } 273 274 return constants.Zero 275 } 276 277 func (it *SimpleStringOnce) Int32() int32 { 278 toInt, err := strconv.Atoi(it.value) 279 280 if err != nil { 281 return constants.Zero 282 } 283 284 if toInt >= math.MinInt32 && toInt <= math.MaxInt32 { 285 return int32(toInt) 286 } 287 288 return constants.Zero 289 } 290 291 func (it *SimpleStringOnce) BooleanDefault() bool { 292 return it.Boolean(true) 293 } 294 295 func (it *SimpleStringOnce) Boolean(isConsiderInit bool) bool { 296 if isConsiderInit && it.IsUnInit() { 297 return false 298 } 299 300 value := it.value 301 302 if value == "yes" || value == "y" || value == "1" || value == "YES" || value == "Y" { 303 return true 304 } 305 306 parsedBool, err := strconv.ParseBool(value) 307 if err != nil { 308 return false 309 } 310 311 return parsedBool 312 } 313 314 func (it *SimpleStringOnce) IsSetter(isConsiderInit bool) issetter.Value { 315 if isConsiderInit && it.IsUnInit() { 316 return issetter.False 317 } 318 319 value := it.value 320 321 if value == "yes" || value == "y" || value == "1" || value == "YES" || value == "Y" { 322 return issetter.True 323 } 324 325 parsedBool, err := strconv.ParseBool(value) 326 if err != nil { 327 return issetter.Uninitialized 328 } 329 330 return issetter.GetBool(parsedBool) 331 } 332 333 func (it *SimpleStringOnce) ValueInt(defaultInteger int) int { 334 toInt, err := strconv.Atoi(it.value) 335 336 if err != nil { 337 return defaultInteger 338 } 339 340 return toInt 341 } 342 343 func (it *SimpleStringOnce) ValueDefInt() int { 344 toInt, err := strconv.Atoi(it.value) 345 346 if err != nil { 347 return constants.Zero 348 } 349 350 return toInt 351 } 352 353 func (it *SimpleStringOnce) ValueByte(defVal byte) byte { 354 toInt, err := strconv.Atoi(it.value) 355 356 if err != nil || toInt > constants.MaxUnit8AsInt { 357 return defVal 358 } 359 360 return byte(toInt) 361 } 362 363 func (it *SimpleStringOnce) ValueDefByte() byte { 364 toInt, err := strconv.Atoi(it.value) 365 366 if err != nil || toInt > constants.MaxUnit8AsInt { 367 return constants.Zero 368 } 369 370 return byte(toInt) 371 } 372 373 func (it *SimpleStringOnce) ValueFloat64(defVal float64) float64 { 374 toFloat, err := strconv.ParseFloat(it.value, bitsize.Of64) 375 376 if err != nil { 377 return defVal 378 } 379 380 return toFloat 381 } 382 383 func (it *SimpleStringOnce) ValueDefFloat64() float64 { 384 return it.ValueFloat64(constants.Zero) 385 } 386 387 func (it SimpleStringOnce) NonPtr() SimpleStringOnce { 388 return it 389 } 390 391 func (it *SimpleStringOnce) Ptr() *SimpleStringOnce { 392 return it 393 } 394 395 // HasSafeNonEmpty 396 // it.isInitialize && 397 // !it.IsEmpty() 398 func (it *SimpleStringOnce) HasSafeNonEmpty() bool { 399 return it.isInitialize && 400 !it.IsEmpty() 401 } 402 403 func (it *SimpleStringOnce) Is(val string) bool { 404 return it.value == val 405 } 406 407 // IsAnyOf if length of values are 0 then returns true 408 func (it *SimpleStringOnce) IsAnyOf(values ...string) bool { 409 if len(values) == 0 { 410 return true 411 } 412 413 for _, value := range values { 414 if it.value == value { 415 return true 416 } 417 } 418 419 return false 420 } 421 422 func (it *SimpleStringOnce) IsContains(val string) bool { 423 return strings.Contains(it.value, val) 424 } 425 426 // IsAnyContains if length of values are 0 then returns true 427 func (it *SimpleStringOnce) IsAnyContains(values ...string) bool { 428 if len(values) == 0 { 429 return true 430 } 431 432 for _, value := range values { 433 if it.IsContains(value) { 434 return true 435 } 436 } 437 438 return false 439 } 440 441 func (it *SimpleStringOnce) IsEqualNonSensitive(val string) bool { 442 return strings.EqualFold(it.value, val) 443 } 444 445 func (it *SimpleStringOnce) IsRegexMatches(regexp *regexp.Regexp) bool { 446 if regexp == nil { 447 return false 448 } 449 450 return regexp.MatchString(it.value) 451 } 452 453 func (it *SimpleStringOnce) RegexFindString( 454 regexp *regexp.Regexp, 455 ) string { 456 if regexp == nil { 457 return constants.EmptyString 458 } 459 460 return regexp.FindString(it.value) 461 } 462 463 func (it *SimpleStringOnce) RegexFindAllStringsWithFlag( 464 regexp *regexp.Regexp, 465 n int, 466 ) (foundItems []string, hasAny bool) { 467 if regexp == nil { 468 return []string{}, false 469 } 470 471 items := regexp.FindAllString( 472 it.value, n) 473 474 return items, len(items) > 0 475 } 476 477 func (it *SimpleStringOnce) RegexFindAllStrings( 478 regexp *regexp.Regexp, 479 n int, 480 ) []string { 481 if regexp == nil { 482 return []string{} 483 } 484 485 return regexp.FindAllString(it.value, n) 486 } 487 488 func (it *SimpleStringOnce) LinesSimpleSlice() *SimpleSlice { 489 lines := strings.Split(it.value, constants.DefaultLine) 490 491 return New.SimpleSlice.Direct(false, lines) 492 } 493 494 func (it *SimpleStringOnce) SimpleSlice( 495 sep string, 496 ) *SimpleSlice { 497 lines := strings.Split(it.value, sep) 498 499 return New.SimpleSlice.Direct(false, lines) 500 } 501 502 func (it *SimpleStringOnce) Split( 503 sep string, 504 ) []string { 505 return strings.Split(it.value, sep) 506 } 507 508 func (it *SimpleStringOnce) SplitLeftRight( 509 sep string, 510 ) (left, right string) { 511 splits := strings.SplitN( 512 it.String(), 513 sep, 514 expectedLeftRightLength) 515 516 length := len(splits) 517 first := splits[coreindexes.First] 518 519 if length == expectedLeftRightLength { 520 return first, splits[coreindexes.Second] 521 } 522 523 return first, constants.EmptyString 524 } 525 526 func (it *SimpleStringOnce) SplitLeftRightTrim( 527 sep string, 528 ) (left, right string) { 529 splits := strings.SplitN( 530 it.String(), sep, 531 expectedLeftRightLength) 532 533 length := len(splits) 534 first := splits[coreindexes.First] 535 536 if length == expectedLeftRightLength { 537 return strings.TrimSpace(first), strings.TrimSpace(splits[coreindexes.Second]) 538 } 539 540 return strings.TrimSpace(first), constants.EmptyString 541 } 542 543 func (it *SimpleStringOnce) SplitNonEmpty( 544 sep string, 545 ) []string { 546 slice := strings.Split(it.value, sep) 547 548 nonEmptySlice := make([]string, 0, len(slice)) 549 550 for _, item := range slice { 551 if item == constants.EmptyString { 552 continue 553 } 554 555 nonEmptySlice = append(nonEmptySlice, item) 556 } 557 558 return slice 559 } 560 561 func (it *SimpleStringOnce) SplitTrimNonWhitespace( 562 sep string, 563 ) []string { 564 slice := strings.Split(it.value, sep) 565 566 nonEmptySlice := make([]string, 0, len(slice)) 567 568 for _, item := range slice { 569 itemTrimmed := strings.TrimSpace(item) 570 if itemTrimmed == constants.EmptyString { 571 continue 572 } 573 574 nonEmptySlice = append(nonEmptySlice, itemTrimmed) 575 } 576 577 return slice 578 } 579 580 func (it *SimpleStringOnce) ClonePtr() *SimpleStringOnce { 581 if it == nil { 582 return nil 583 } 584 585 return &SimpleStringOnce{ 586 value: it.value, 587 isInitialize: it.isInitialize, 588 } 589 } 590 591 func (it SimpleStringOnce) Clone() SimpleStringOnce { 592 return SimpleStringOnce{ 593 value: it.value, 594 isInitialize: it.isInitialize, 595 } 596 } 597 598 func (it SimpleStringOnce) CloneUsingNewVal(val string) SimpleStringOnce { 599 return SimpleStringOnce{ 600 value: val, 601 isInitialize: it.isInitialize, 602 } 603 } 604 605 func (it *SimpleStringOnce) Dispose() { 606 if it == nil { 607 return 608 } 609 610 it.value = constants.EmptyString 611 it.isInitialize = true 612 } 613 614 func (it *SimpleStringOnce) String() string { 615 if it == nil { 616 return constants.EmptyString 617 } 618 619 return it.value 620 } 621 622 func (it *SimpleStringOnce) StringPtr() *string { 623 if it == nil { 624 emptyString := "" 625 return &emptyString 626 } 627 628 return &it.value 629 } 630 631 func (it *SimpleStringOnce) JsonModel() SimpleStringOnceModel { 632 return SimpleStringOnceModel{ 633 Value: it.Value(), 634 IsInitialize: it.IsInitialized(), 635 } 636 } 637 638 func (it *SimpleStringOnce) JsonModelAny() interface{} { 639 return it.JsonModel() 640 } 641 642 func (it *SimpleStringOnce) MarshalJSON() ([]byte, error) { 643 return json.Marshal(it.JsonModel()) 644 } 645 646 func (it *SimpleStringOnce) UnmarshalJSON( 647 jsonBytes []byte, 648 ) error { 649 var dataModel SimpleStringOnceModel 650 err := corejson.Deserialize.UsingBytes( 651 jsonBytes, &dataModel) 652 653 if err == nil { 654 it.value = dataModel.Value 655 it.isInitialize = dataModel.IsInitialize 656 } 657 658 return err 659 } 660 661 func (it SimpleStringOnce) Json() corejson.Result { 662 return corejson.New(it) 663 } 664 665 func (it SimpleStringOnce) JsonPtr() *corejson.Result { 666 return corejson.NewPtr(it) 667 } 668 669 func (it *SimpleStringOnce) ParseInjectUsingJson( 670 jsonResult *corejson.Result, 671 ) (*SimpleStringOnce, error) { 672 err := jsonResult.Unmarshal(it) 673 674 if err != nil { 675 return nil, err 676 } 677 678 return it, nil 679 } 680 681 // ParseInjectUsingJsonMust Panic if error 682 func (it *SimpleStringOnce) ParseInjectUsingJsonMust( 683 jsonResult *corejson.Result, 684 ) *SimpleStringOnce { 685 parsedResult, err := it. 686 ParseInjectUsingJson(jsonResult) 687 688 if err != nil { 689 panic(err) 690 } 691 692 return parsedResult 693 } 694 695 func (it *SimpleStringOnce) AsJsonContractsBinder() corejson.JsonContractsBinder { 696 return it 697 } 698 699 func (it *SimpleStringOnce) AsJsoner() corejson.Jsoner { 700 return it 701 } 702 703 func (it *SimpleStringOnce) JsonParseSelfInject( 704 jsonResult *corejson.Result, 705 ) error { 706 _, err := it.ParseInjectUsingJson( 707 jsonResult, 708 ) 709 710 return err 711 } 712 713 func (it *SimpleStringOnce) AsJsonParseSelfInjector() corejson.JsonParseSelfInjector { 714 return it 715 } 716 717 func (it *SimpleStringOnce) AsJsonMarshaller() corejson.JsonMarshaller { 718 return it 719 } 720 721 func (it *SimpleStringOnce) Serialize() ([]byte, error) { 722 return corejson.Serialize.Raw(it) 723 } 724 725 func (it *SimpleStringOnce) Deserialize(toPtr interface{}) (parsingErr error) { 726 return it.JsonPtr().Deserialize(toPtr) 727 }