gitlab.com/evatix-go/core@v1.3.55/enums/stringcompareas/Variant.go (about)

     1  package stringcompareas
     2  
     3  import (
     4  	"errors"
     5  
     6  	"gitlab.com/evatix-go/core/constants"
     7  	"gitlab.com/evatix-go/core/coreinterface/enuminf"
     8  	"gitlab.com/evatix-go/core/errcore"
     9  )
    10  
    11  type Variant byte
    12  
    13  const (
    14  	Equal Variant = iota
    15  	StartsWith
    16  	EndsWith
    17  	Anywhere
    18  	Contains // alias for Anywhere
    19  	AnyChars // If given search chars is found in the content
    20  	// Regex strings will be cached and
    21  	// compiled using map, mutex
    22  	// will be used to lock,
    23  	// if failed to compile then panic
    24  	Regex
    25  	NotEqual      // invert of Equal
    26  	NotStartsWith // invert of StartsWith
    27  	NotEndsWith   // invert of EndsWith
    28  	NotContains   // invert of Anywhere
    29  	NotAnyChars   // invert of AnyChars
    30  	NotMatchRegex // invert of Regex
    31  	Invalid
    32  )
    33  
    34  func (it Variant) Value() byte {
    35  	return byte(it)
    36  }
    37  
    38  func (it Variant) IsAnyMethod(methodNames ...string) bool {
    39  	return BasicEnumImpl.IsAnyNamesOf(it.Value(), methodNames...)
    40  }
    41  
    42  func (it Variant) AllNameValues() []string {
    43  	return BasicEnumImpl.AllNameValues()
    44  }
    45  
    46  func (it Variant) OnlySupportedErr(names ...string) error {
    47  	return BasicEnumImpl.OnlySupportedErr(names...)
    48  }
    49  
    50  func (it Variant) OnlySupportedMsgErr(message string, names ...string) error {
    51  	return BasicEnumImpl.OnlySupportedMsgErr(message, names...)
    52  }
    53  
    54  func (it Variant) ValueUInt16() uint16 {
    55  	return uint16(it)
    56  }
    57  
    58  func (it Variant) IntegerEnumRanges() []int {
    59  	return BasicEnumImpl.IntegerEnumRanges()
    60  }
    61  
    62  func (it Variant) MinMaxAny() (min, max interface{}) {
    63  	return BasicEnumImpl.MinMaxAny()
    64  }
    65  
    66  func (it Variant) MinValueString() string {
    67  	return BasicEnumImpl.MinValueString()
    68  }
    69  
    70  func (it Variant) MaxValueString() string {
    71  	return BasicEnumImpl.MaxValueString()
    72  }
    73  
    74  func (it Variant) MaxInt() int {
    75  	return BasicEnumImpl.MaxInt()
    76  }
    77  
    78  func (it Variant) MinInt() int {
    79  	return BasicEnumImpl.MinInt()
    80  }
    81  
    82  func (it Variant) RangesDynamicMap() map[string]interface{} {
    83  	return BasicEnumImpl.RangesDynamicMap()
    84  }
    85  
    86  func (it Variant) IsByteValueEqual(value byte) bool {
    87  	return byte(it) == value
    88  }
    89  
    90  func (it Variant) Format(format string) (compiled string) {
    91  	return BasicEnumImpl.Format(format, it)
    92  }
    93  
    94  func (it Variant) IsEnumEqual(enum enuminf.BasicEnumer) bool {
    95  	return it.ValueByte() == enum.ValueByte()
    96  }
    97  
    98  func (it *Variant) IsAnyEnumsEqual(enums ...enuminf.BasicEnumer) bool {
    99  	for _, enum := range enums {
   100  		if it.IsEnumEqual(enum) {
   101  			return true
   102  		}
   103  	}
   104  
   105  	return false
   106  }
   107  
   108  func (it Variant) IsNameEqual(name string) bool {
   109  	return it.Name() == name
   110  }
   111  
   112  func (it Variant) IsAnyNamesOf(names ...string) bool {
   113  	for _, name := range names {
   114  		if it.IsNameEqual(name) {
   115  			return true
   116  		}
   117  	}
   118  
   119  	return false
   120  }
   121  
   122  func (it Variant) IsValueEqual(value byte) bool {
   123  	return it.ValueByte() == value
   124  }
   125  
   126  func (it Variant) IsAnyValuesEqual(anyByteValues ...byte) bool {
   127  	for _, currentVal := range anyByteValues {
   128  		if it.IsValueEqual(currentVal) {
   129  			return true
   130  		}
   131  	}
   132  
   133  	return false
   134  }
   135  
   136  func (it Variant) ValueInt() int {
   137  	return int(it)
   138  }
   139  
   140  func (it Variant) ValueInt8() int8 {
   141  	return int8(it)
   142  }
   143  
   144  func (it Variant) ValueInt16() int16 {
   145  	return int16(it)
   146  }
   147  
   148  func (it Variant) ValueInt32() int32 {
   149  	return int32(it)
   150  }
   151  
   152  func (it Variant) ValueString() string {
   153  	return it.ToNumberString()
   154  }
   155  
   156  func (it Variant) IsValid() bool {
   157  	return it != Invalid
   158  }
   159  
   160  func (it Variant) IsInvalid() bool {
   161  	return it == Invalid
   162  }
   163  
   164  func (it Variant) Name() string {
   165  	return BasicEnumImpl.ToEnumString(it.ValueByte())
   166  }
   167  
   168  func (it Variant) NameValue() string {
   169  	return BasicEnumImpl.NameWithValue(it)
   170  }
   171  
   172  func (it Variant) TypeName() string {
   173  	return BasicEnumImpl.TypeName()
   174  }
   175  
   176  func (it Variant) ToNumberString() string {
   177  	return BasicEnumImpl.ToNumberString(it.ValueByte())
   178  }
   179  
   180  func (it Variant) UnmarshallEnumToValue(
   181  	jsonUnmarshallingValue []byte,
   182  ) (byte, error) {
   183  	return BasicEnumImpl.UnmarshallToValue(
   184  		isMappedToDefault,
   185  		jsonUnmarshallingValue)
   186  }
   187  
   188  func (it Variant) String() string {
   189  	return BasicEnumImpl.ToEnumString(it.ValueByte())
   190  }
   191  
   192  func (it Variant) Is(compare Variant) bool {
   193  	return it == compare
   194  }
   195  
   196  func (it Variant) IsEqual() bool {
   197  	return it == Equal
   198  }
   199  
   200  func (it Variant) IsStartsWith() bool {
   201  	return it == StartsWith
   202  }
   203  
   204  func (it Variant) IsEndsWith() bool {
   205  	return it == EndsWith
   206  }
   207  
   208  func (it Variant) IsAnywhere() bool {
   209  	return it == Anywhere
   210  }
   211  
   212  func (it Variant) IsContains() bool {
   213  	return it == Contains
   214  }
   215  
   216  func (it Variant) IsAnyChars() bool {
   217  	return it == AnyChars
   218  }
   219  
   220  func (it Variant) IsRegex() bool {
   221  	return it == Regex
   222  }
   223  
   224  // IsNegativeCondition returns true for any of the cases mentioned in negativeCases
   225  //
   226  //	NotEqual      // invert of Equal
   227  //	NotStartsWith // invert of StartsWith
   228  //	NotEndsWith   // invert of EndsWith
   229  //	NotContains   // invert of Anywhere
   230  //	NotAnyChars   // invert of AnyChars
   231  //	NotMatchRegex // invert of Regex
   232  func (it Variant) IsNegativeCondition() bool {
   233  	for _, negativeCase := range negativeCases {
   234  		if negativeCase == it {
   235  			return true
   236  		}
   237  	}
   238  
   239  	return false
   240  }
   241  
   242  func (it Variant) IsNotEqual() bool {
   243  	return it == NotEqual
   244  }
   245  
   246  func (it Variant) IsNotStartsWith() bool {
   247  	return it == NotStartsWith
   248  }
   249  
   250  func (it Variant) IsNotEndsWith() bool {
   251  	return it == NotEndsWith
   252  }
   253  
   254  func (it Variant) IsNotContains() bool {
   255  	return it == NotContains
   256  }
   257  
   258  func (it Variant) IsNotMatchRegex() bool {
   259  	return it == NotMatchRegex
   260  }
   261  
   262  func (it Variant) MarshalJSON() ([]byte, error) {
   263  	return BasicEnumImpl.ToEnumJsonBytes(it.ValueByte())
   264  }
   265  
   266  func (it *Variant) UnmarshalJSON(data []byte) error {
   267  	rawScriptType, err := BasicEnumImpl.UnmarshallToValue(
   268  		isMappedToDefault, data)
   269  
   270  	if err == nil {
   271  		*it = Variant(rawScriptType)
   272  	}
   273  
   274  	return err
   275  }
   276  
   277  func (it Variant) RangeNamesCsv() string {
   278  	return BasicEnumImpl.RangeNamesCsv()
   279  }
   280  
   281  func (it *Variant) MaxByte() byte {
   282  	return BasicEnumImpl.Max()
   283  }
   284  
   285  func (it *Variant) MinByte() byte {
   286  	return BasicEnumImpl.Min()
   287  }
   288  
   289  func (it *Variant) ValueByte() byte {
   290  	return byte(*it)
   291  }
   292  
   293  func (it *Variant) RangesByte() []byte {
   294  	return BasicEnumImpl.Ranges()
   295  }
   296  
   297  // IsLineCompareFunc for
   298  // Regex case has no use, use regex
   299  // pattern for case sensitive or insensitive search
   300  //
   301  // Functions Mapping:
   302  //  Equal:         isEqualFunc,
   303  //  StartsWith:    isStartsWithFunc,
   304  //  EndsWith:      isEndsWithFunc,
   305  //  Anywhere:      isAnywhereFunc,
   306  //  AnyChars:      isAnyCharsFunc,
   307  //  Contains:      isAnywhereFunc,
   308  //  Regex:         isRegexFunc,
   309  //  NotEqual:      isNotEqualFunc,
   310  //  NotStartsWith: isNotStartsWithFunc,
   311  //  NotEndsWith:   isNotEndsWithFunc,
   312  //  NotContains:   isNotContainsFunc,
   313  //  NotAnyChars:   isNotAnyCharsFunc,
   314  //  NotMatchRegex: isNotMatchRegex,
   315  func (it Variant) IsLineCompareFunc() IsLineCompareFunc {
   316  	return rangesMap[it]
   317  }
   318  
   319  func (it Variant) DynamicCompare(
   320  	isDynamicCompareFunc IsDynamicCompareFunc,
   321  	lineNumber int, content string,
   322  ) bool {
   323  	return isDynamicCompareFunc(
   324  		lineNumber,
   325  		content,
   326  		it)
   327  }
   328  
   329  // IsCompareSuccess
   330  // Regex case has no use,
   331  // use regex pattern for case sensitive or insensitive search
   332  //
   333  // Regex will be cached to map for the syntax,
   334  // if running twice it will not create new but the same one from the map.
   335  // It save the regex into map using mutex lock, so async codes can run.
   336  func (it Variant) IsCompareSuccess(
   337  	isIgnoreCase bool,
   338  	content,
   339  	search string,
   340  ) bool {
   341  	return it.IsLineCompareFunc()(
   342  		content,
   343  		search,
   344  		isIgnoreCase)
   345  }
   346  
   347  func (it Variant) VerifyMessage(
   348  	isIgnoreCase bool,
   349  	content,
   350  	search string,
   351  ) string {
   352  	isMatch := it.IsCompareSuccess(
   353  		isIgnoreCase,
   354  		content,
   355  		search,
   356  	)
   357  
   358  	if isMatch {
   359  		return constants.EmptyString
   360  	}
   361  
   362  	isIgnoreCaseString := "- {case strict}"
   363  
   364  	if isIgnoreCase {
   365  		isIgnoreCaseString = "- {case ignored}"
   366  	}
   367  
   368  	if it.IsNegativeCondition() {
   369  		return errcore.ExpectingNotEqualSimpleNoType(
   370  			"CompareMethod \""+it.Name()+"\" - {negative} match failed "+isIgnoreCaseString,
   371  			search,
   372  			content)
   373  	}
   374  
   375  	return errcore.ExpectingSimpleNoType(
   376  		"CompareMethod \""+it.Name()+"\" - match failed "+isIgnoreCaseString,
   377  		search,
   378  		content)
   379  }
   380  
   381  func (it Variant) VerifyError(
   382  	isIgnoreCase bool,
   383  	content,
   384  	search string,
   385  ) error {
   386  	message := it.VerifyMessage(
   387  		isIgnoreCase,
   388  		content,
   389  		search,
   390  	)
   391  
   392  	if message == constants.EmptyString {
   393  		return nil
   394  	}
   395  
   396  	return errors.New(message)
   397  }
   398  
   399  func (it Variant) VerifyMessageCaseSensitive(
   400  	content,
   401  	search string,
   402  ) string {
   403  	return it.VerifyMessage(
   404  		false,
   405  		content,
   406  		search,
   407  	)
   408  }
   409  
   410  func (it Variant) VerifyErrorCaseSensitive(
   411  	content,
   412  	search string,
   413  ) error {
   414  	return it.VerifyError(
   415  		false,
   416  		content,
   417  		search,
   418  	)
   419  }
   420  
   421  // IsCompareSuccessCaseSensitive for
   422  // Regex case has no use, use regex
   423  // pattern for case sensitive or insensitive search
   424  func (it *Variant) IsCompareSuccessCaseSensitive(content, search string) bool {
   425  	return it.IsLineCompareFunc()(
   426  		content,
   427  		search,
   428  		false)
   429  }
   430  
   431  // IsCompareSuccessNonCaseSensitive for
   432  // Regex case has no use, use regex
   433  // pattern for case sensitive or insensitive search
   434  func (it *Variant) IsCompareSuccessNonCaseSensitive(content, search string) bool {
   435  	return it.IsLineCompareFunc()(
   436  		content,
   437  		search,
   438  		true)
   439  }
   440  
   441  func (it Variant) EnumType() enuminf.EnumTyper {
   442  	return BasicEnumImpl.EnumType()
   443  }
   444  
   445  func (it Variant) AsBasicEnumContractsBinder() enuminf.BasicEnumContractsBinder {
   446  	return &it
   447  }
   448  
   449  func (it Variant) AsStringCompareTyper() enuminf.StringCompareTyper {
   450  	return &it
   451  }
   452  
   453  func (it Variant) AsBasicByteEnumContractsBinder() enuminf.BasicByteEnumContractsBinder {
   454  	return &it
   455  }
   456  
   457  func (it Variant) ToPtr() *Variant {
   458  	return &it
   459  }