gitlab.com/evatix-go/core@v1.3.55/coreinstruction/StringCompare.go (about)

     1  package coreinstruction
     2  
     3  import (
     4  	"gitlab.com/evatix-go/core/corecomparator"
     5  	"gitlab.com/evatix-go/core/enums/stringcompareas"
     6  	"gitlab.com/evatix-go/core/regexnew"
     7  )
     8  
     9  type StringCompare struct {
    10  	StringSearch
    11  	Content string
    12  }
    13  
    14  func NewStringCompare(
    15  	method stringcompareas.Variant,
    16  	isIgnoreCase bool,
    17  	search,
    18  	content string,
    19  ) *StringCompare {
    20  	return &StringCompare{
    21  		StringSearch: StringSearch{
    22  			CompareMethod: method,
    23  			Search:        search,
    24  			BaseIsIgnoreCase: corecomparator.BaseIsIgnoreCase{
    25  				IsIgnoreCase: isIgnoreCase,
    26  			},
    27  		},
    28  		Content: content,
    29  	}
    30  }
    31  
    32  func NewStringCompareEqual(
    33  	search,
    34  	content string,
    35  ) *StringCompare {
    36  	return &StringCompare{
    37  		StringSearch: StringSearch{
    38  			CompareMethod: stringcompareas.Equal,
    39  			Search:        search,
    40  		},
    41  		Content: content,
    42  	}
    43  }
    44  
    45  func NewStringCompareRegex(
    46  	regex,
    47  	content string,
    48  ) *StringCompare {
    49  	return &StringCompare{
    50  		StringSearch: StringSearch{
    51  			CompareMethod: stringcompareas.Regex,
    52  			Search:        regex,
    53  		},
    54  		Content: content,
    55  	}
    56  }
    57  
    58  func NewStringCompareStartsWith(
    59  	isIgnoreCase bool,
    60  	search,
    61  	content string,
    62  ) *StringCompare {
    63  	return &StringCompare{
    64  		StringSearch: StringSearch{
    65  			CompareMethod: stringcompareas.StartsWith,
    66  			Search:        search,
    67  			BaseIsIgnoreCase: corecomparator.BaseIsIgnoreCase{
    68  				IsIgnoreCase: isIgnoreCase,
    69  			},
    70  		},
    71  		Content: content,
    72  	}
    73  }
    74  
    75  func NewStringCompareEndsWith(
    76  	isIgnoreCase bool,
    77  	search,
    78  	content string,
    79  ) *StringCompare {
    80  	return &StringCompare{
    81  		StringSearch: StringSearch{
    82  			CompareMethod: stringcompareas.EndsWith,
    83  			Search:        search,
    84  			BaseIsIgnoreCase: corecomparator.BaseIsIgnoreCase{
    85  				IsIgnoreCase: isIgnoreCase,
    86  			},
    87  		},
    88  		Content: content,
    89  	}
    90  }
    91  
    92  func NewStringCompareContains(
    93  	isIgnoreCase bool,
    94  	search,
    95  	content string,
    96  ) *StringCompare {
    97  	return &StringCompare{
    98  		StringSearch: StringSearch{
    99  			CompareMethod: stringcompareas.Contains,
   100  			Search:        search,
   101  			BaseIsIgnoreCase: corecomparator.BaseIsIgnoreCase{
   102  				IsIgnoreCase: isIgnoreCase,
   103  			},
   104  		},
   105  		Content: content,
   106  	}
   107  }
   108  
   109  func (it *StringCompare) IsInvalid() bool {
   110  	return it == nil
   111  }
   112  
   113  func (it *StringCompare) IsDefined() bool {
   114  	return it != nil
   115  }
   116  
   117  func (it *StringCompare) IsMatch() bool {
   118  	if it == nil {
   119  		return true
   120  	}
   121  
   122  	return it.CompareMethod.IsCompareSuccess(
   123  		it.IsIgnoreCase,
   124  		it.Content,
   125  		it.Search,
   126  	)
   127  }
   128  
   129  func (it *StringCompare) IsMatchFailed() bool {
   130  	return !it.IsMatch()
   131  }
   132  
   133  func (it *StringCompare) VerifyError() error {
   134  	if it == nil {
   135  		return nil
   136  	}
   137  
   138  	if it.CompareMethod.IsRegex() {
   139  		return regexnew.MatchErrorLock(
   140  			it.Search,
   141  			it.Content)
   142  	}
   143  
   144  	return it.CompareMethod.VerifyError(
   145  		it.IsIgnoreCase,
   146  		it.Content,
   147  		it.Search,
   148  	)
   149  }