gitlab.com/evatix-go/core@v1.3.55/coredata/corestr/ValidValue.go (about)

     1  package corestr
     2  
     3  import (
     4  	"fmt"
     5  	"regexp"
     6  	"strconv"
     7  	"strings"
     8  
     9  	"gitlab.com/evatix-go/core/constants"
    10  	"gitlab.com/evatix-go/core/constants/bitsize"
    11  	"gitlab.com/evatix-go/core/coredata/corejson"
    12  	"gitlab.com/evatix-go/core/internal/strutilinternal"
    13  )
    14  
    15  type ValidValue struct {
    16  	Value      string
    17  	valueBytes *[]byte
    18  	IsValid    bool
    19  	Message    string
    20  }
    21  
    22  func NewValidValueUsingAny(
    23  	isIncludeFieldName bool,
    24  	isValid bool,
    25  	any interface{},
    26  ) *ValidValue {
    27  	toString := AnyToString(
    28  		isIncludeFieldName,
    29  		any)
    30  
    31  	return &ValidValue{
    32  		Value:   toString,
    33  		IsValid: isValid,
    34  		Message: constants.EmptyString,
    35  	}
    36  }
    37  
    38  // NewValidValueUsingAnyAutoValid IsValid to false on nil or Empty string
    39  func NewValidValueUsingAnyAutoValid(
    40  	isIncludeFieldName bool,
    41  	any interface{},
    42  ) *ValidValue {
    43  	toString := AnyToString(
    44  		isIncludeFieldName,
    45  		any)
    46  
    47  	return &ValidValue{
    48  		Value:   toString,
    49  		IsValid: toString == constants.EmptyString,
    50  		Message: constants.EmptyString,
    51  	}
    52  }
    53  
    54  func NewValidValue(value string) *ValidValue {
    55  	return &ValidValue{
    56  		Value:   value,
    57  		IsValid: true,
    58  		Message: constants.EmptyString,
    59  	}
    60  }
    61  
    62  func NewValidValueEmpty() *ValidValue {
    63  	return &ValidValue{
    64  		Value:   constants.EmptyString,
    65  		IsValid: true,
    66  		Message: constants.EmptyString,
    67  	}
    68  }
    69  
    70  func InvalidValidValueNoMessage() *ValidValue {
    71  	return InvalidValidValue(constants.EmptyString)
    72  }
    73  
    74  func InvalidValidValue(message string) *ValidValue {
    75  	return &ValidValue{
    76  		Value:   constants.EmptyString,
    77  		IsValid: false,
    78  		Message: message,
    79  	}
    80  }
    81  
    82  func (it *ValidValue) ValueBytesOnce() []byte {
    83  	return *it.ValueBytesOncePtr()
    84  }
    85  
    86  func (it *ValidValue) ValueBytesOncePtr() *[]byte {
    87  	if it.valueBytes == nil {
    88  		valueBytes := []byte(it.Value)
    89  
    90  		it.valueBytes = &valueBytes
    91  	}
    92  
    93  	return it.valueBytes
    94  }
    95  
    96  func (it *ValidValue) IsEmpty() bool {
    97  	return it.Value == ""
    98  }
    99  
   100  func (it *ValidValue) IsWhitespace() bool {
   101  	return strutilinternal.IsEmptyOrWhitespace(it.Value)
   102  }
   103  
   104  func (it *ValidValue) Trim() string {
   105  	return strings.TrimSpace(it.Value)
   106  }
   107  
   108  func (it *ValidValue) HasValidNonEmpty() bool {
   109  	return it.IsValid && !it.IsEmpty()
   110  }
   111  
   112  func (it *ValidValue) HasValidNonWhitespace() bool {
   113  	return it.IsValid && !it.IsWhitespace()
   114  }
   115  
   116  func (it *ValidValue) ValueBool() bool {
   117  	if it.Value == "" {
   118  		return false
   119  	}
   120  
   121  	toBool, err := strconv.ParseBool(it.Value)
   122  
   123  	if err != nil {
   124  		return false
   125  	}
   126  
   127  	return toBool
   128  }
   129  
   130  func (it *ValidValue) ValueInt(defaultInteger int) int {
   131  	toInt, err := strconv.Atoi(it.Value)
   132  
   133  	if err != nil {
   134  		return defaultInteger
   135  	}
   136  
   137  	return toInt
   138  }
   139  
   140  func (it *ValidValue) ValueDefInt() int {
   141  	toInt, err := strconv.Atoi(it.Value)
   142  
   143  	if err != nil {
   144  		return constants.Zero
   145  	}
   146  
   147  	return toInt
   148  }
   149  
   150  func (it *ValidValue) ValueByte(defVal byte) byte {
   151  	toInt, err := strconv.Atoi(it.Value)
   152  
   153  	if err != nil || toInt > constants.MaxUnit8AsInt {
   154  		return defVal
   155  	}
   156  
   157  	return byte(toInt)
   158  }
   159  
   160  func (it *ValidValue) ValueDefByte() byte {
   161  	toInt, err := strconv.Atoi(it.Value)
   162  
   163  	if err != nil || toInt > constants.MaxUnit8AsInt {
   164  		return constants.Zero
   165  	}
   166  
   167  	return byte(toInt)
   168  }
   169  
   170  func (it *ValidValue) ValueFloat64(defVal float64) float64 {
   171  	toFloat, err := strconv.ParseFloat(it.Value, bitsize.Of64)
   172  
   173  	if err != nil {
   174  		return defVal
   175  	}
   176  
   177  	return toFloat
   178  }
   179  
   180  func (it *ValidValue) ValueDefFloat64() float64 {
   181  	return it.ValueFloat64(constants.Zero)
   182  }
   183  
   184  // HasSafeNonEmpty receiver.IsValid &&
   185  //		!receiver.IsLeftEmpty() &&
   186  //		!receiver.IsMiddleEmpty() &&
   187  //		!receiver.IsRightEmpty()
   188  func (it *ValidValue) HasSafeNonEmpty() bool {
   189  	return it.IsValid &&
   190  		!it.IsEmpty()
   191  }
   192  
   193  func (it *ValidValue) Is(val string) bool {
   194  	return it.Value == val
   195  }
   196  
   197  // IsAnyOf if length of values are 0 then returns true
   198  func (it *ValidValue) IsAnyOf(values ...string) bool {
   199  	if len(values) == 0 {
   200  		return true
   201  	}
   202  
   203  	for _, value := range values {
   204  		if it.Value == value {
   205  			return true
   206  		}
   207  	}
   208  
   209  	return false
   210  }
   211  
   212  func (it *ValidValue) IsContains(val string) bool {
   213  	return strings.Contains(it.Value, val)
   214  }
   215  
   216  // IsAnyContains if length of values are 0 then returns true
   217  func (it *ValidValue) IsAnyContains(values ...string) bool {
   218  	if len(values) == 0 {
   219  		return true
   220  	}
   221  
   222  	for _, value := range values {
   223  		if it.IsContains(value) {
   224  			return true
   225  		}
   226  	}
   227  
   228  	return false
   229  }
   230  
   231  func (it *ValidValue) IsEqualNonSensitive(val string) bool {
   232  	return strings.EqualFold(it.Value, val)
   233  }
   234  
   235  func (it *ValidValue) IsRegexMatches(regexp *regexp.Regexp) bool {
   236  	if regexp == nil {
   237  		return false
   238  	}
   239  
   240  	return regexp.MatchString(it.Value)
   241  }
   242  
   243  func (it *ValidValue) RegexFindString(
   244  	regexp *regexp.Regexp,
   245  ) string {
   246  	if regexp == nil {
   247  		return constants.EmptyString
   248  	}
   249  
   250  	return regexp.FindString(it.Value)
   251  }
   252  
   253  func (it *ValidValue) RegexFindAllStringsWithFlag(
   254  	regexp *regexp.Regexp,
   255  	n int,
   256  ) (foundItems []string, hasAny bool) {
   257  	if regexp == nil {
   258  		return []string{}, false
   259  	}
   260  
   261  	items := regexp.FindAllString(
   262  		it.Value, n)
   263  
   264  	return items, len(items) > 0
   265  }
   266  
   267  func (it *ValidValue) RegexFindAllStrings(
   268  	regexp *regexp.Regexp,
   269  	n int,
   270  ) []string {
   271  	if regexp == nil {
   272  		return []string{}
   273  	}
   274  
   275  	return regexp.FindAllString(it.Value, n)
   276  }
   277  
   278  func (it *ValidValue) Split(
   279  	sep string,
   280  ) []string {
   281  	return strings.Split(it.Value, sep)
   282  }
   283  
   284  func (it *ValidValue) SplitNonEmpty(
   285  	sep string,
   286  ) []string {
   287  	slice := strings.Split(it.Value, sep)
   288  
   289  	nonEmptySlice := make([]string, 0, len(slice))
   290  
   291  	for _, item := range slice {
   292  		if item == constants.EmptyString {
   293  			continue
   294  		}
   295  
   296  		nonEmptySlice = append(nonEmptySlice, item)
   297  	}
   298  
   299  	return slice
   300  }
   301  
   302  func (it *ValidValue) SplitTrimNonWhitespace(
   303  	sep string,
   304  ) []string {
   305  	slice := strings.Split(it.Value, sep)
   306  
   307  	nonEmptySlice := make([]string, 0, len(slice))
   308  
   309  	for _, item := range slice {
   310  		itemTrimmed := strings.TrimSpace(item)
   311  		if itemTrimmed == constants.EmptyString {
   312  			continue
   313  		}
   314  
   315  		nonEmptySlice = append(nonEmptySlice, itemTrimmed)
   316  	}
   317  
   318  	return slice
   319  }
   320  
   321  func (it *ValidValue) Clone() *ValidValue {
   322  	if it == nil {
   323  		return nil
   324  	}
   325  
   326  	return &ValidValue{
   327  		Value:   it.Value,
   328  		IsValid: it.IsValid,
   329  		Message: it.Message,
   330  	}
   331  }
   332  
   333  func (it *ValidValue) String() string {
   334  	if it == nil {
   335  		return constants.EmptyString
   336  	}
   337  
   338  	return it.Value
   339  }
   340  
   341  func (it *ValidValue) FullString() string {
   342  	if it == nil {
   343  		return constants.EmptyString
   344  	}
   345  
   346  	return fmt.Sprintf(
   347  		constants.SprintPropertyNameValueFormat,
   348  		*it)
   349  }
   350  
   351  func (it *ValidValue) Clear() {
   352  	if it == nil {
   353  		return
   354  	}
   355  
   356  	it.Value = ""
   357  	it.valueBytes = nil
   358  	it.IsValid = false
   359  	it.Message = ""
   360  }
   361  
   362  func (it *ValidValue) Dispose() {
   363  	if it == nil {
   364  		return
   365  	}
   366  
   367  	it.Clear()
   368  }
   369  
   370  func (it ValidValue) Json() corejson.Result {
   371  	return corejson.New(it)
   372  }
   373  
   374  func (it ValidValue) JsonPtr() *corejson.Result {
   375  	return corejson.NewPtr(it)
   376  }
   377  
   378  func (it *ValidValue) ParseInjectUsingJson(
   379  	jsonResult *corejson.Result,
   380  ) (*ValidValue, error) {
   381  	err := jsonResult.Deserialize(it)
   382  
   383  	if err == nil {
   384  		return it, err
   385  	}
   386  
   387  	// has err
   388  	return nil, err
   389  }
   390  
   391  func (it *ValidValue) Serialize() ([]byte, error) {
   392  	return corejson.Serialize.Raw(it)
   393  }
   394  
   395  func (it *ValidValue) Deserialize(toPtr interface{}) (parsingErr error) {
   396  	return it.JsonPtr().Deserialize(toPtr)
   397  }