gitlab.com/evatix-go/core@v1.3.55/corevalidator/LineValidator.go (about)

     1  package corevalidator
     2  
     3  import (
     4  	"errors"
     5  
     6  	"gitlab.com/evatix-go/core/coredata/corestr"
     7  	"gitlab.com/evatix-go/core/errcore"
     8  )
     9  
    10  type LineValidator struct {
    11  	LineNumber
    12  	TextValidator
    13  }
    14  
    15  // IsMatch
    16  //
    17  // lineNumber == -1 mean no checking in line number,
    18  //
    19  // having LineValidator.LineNumber = -1 is also means the same
    20  func (it *LineValidator) IsMatch(
    21  	lineNumber int,
    22  	content string,
    23  	isCaseSensitive bool,
    24  ) bool {
    25  	if !it.LineNumber.IsMatch(lineNumber) {
    26  		return false
    27  	}
    28  
    29  	return it.TextValidator.IsMatch(
    30  		content,
    31  		isCaseSensitive)
    32  }
    33  
    34  func (it *LineValidator) IsMatchMany(
    35  	isSkipOnContentsEmpty,
    36  	isCaseSensitive bool,
    37  	contentsWithLine ...corestr.TextWithLineNumber,
    38  ) bool {
    39  	if it == nil {
    40  		return true
    41  	}
    42  
    43  	if len(contentsWithLine) == 0 && isSkipOnContentsEmpty {
    44  		return true
    45  	}
    46  
    47  	for _, textWithLine := range contentsWithLine {
    48  		if !it.IsMatch(
    49  			textWithLine.LineNumber,
    50  			textWithLine.Text,
    51  			isCaseSensitive) {
    52  			return false
    53  		}
    54  	}
    55  
    56  	return true
    57  }
    58  
    59  // VerifyError
    60  //
    61  // lineNumber == -1 mean no checking in line number,
    62  //
    63  // having LineValidator.LineNumber = -1 is also means the same
    64  func (it *LineValidator) VerifyError(
    65  	params *ValidatorParamsBase,
    66  	processingLineNumber int,
    67  	content string,
    68  ) error {
    69  	if !it.LineNumber.IsMatch(processingLineNumber) {
    70  		msg := errcore.GetSearchLineNumberExpectationMessage(
    71  			params.CaseIndex,
    72  			it.LineNumber.LineNumber,
    73  			processingLineNumber,
    74  			content,
    75  			it.Search,
    76  			*it)
    77  
    78  		return errors.New(msg)
    79  	}
    80  
    81  	return it.TextValidator.verifyDetailErrorUsingLineProcessing(
    82  		processingLineNumber,
    83  		params,
    84  		content)
    85  }
    86  
    87  func (it *LineValidator) VerifyMany(
    88  	isContinueOnError bool,
    89  	params *ValidatorParamsBase,
    90  	contentsWithLine ...corestr.TextWithLineNumber,
    91  ) error {
    92  	if isContinueOnError {
    93  		return it.AllVerifyError(
    94  			params,
    95  			contentsWithLine...)
    96  	}
    97  
    98  	return it.VerifyFirstError(
    99  		params,
   100  		contentsWithLine...)
   101  }
   102  
   103  func (it *LineValidator) VerifyFirstError(
   104  	params *ValidatorParamsBase,
   105  	contentsWithLine ...corestr.TextWithLineNumber,
   106  ) error {
   107  	if it == nil {
   108  		return nil
   109  	}
   110  
   111  	length := len(contentsWithLine)
   112  	if length == 0 && params.IsIgnoreCompareOnActualInputEmpty {
   113  		return nil
   114  	}
   115  
   116  	for _, textWithLine := range contentsWithLine {
   117  		err := it.VerifyError(
   118  			params,
   119  			textWithLine.LineNumber,
   120  			textWithLine.Text,
   121  		)
   122  
   123  		if err != nil {
   124  			return err
   125  		}
   126  	}
   127  
   128  	return nil
   129  }
   130  
   131  func (it *LineValidator) AllVerifyError(
   132  	params *ValidatorParamsBase,
   133  	contentsWithLine ...corestr.TextWithLineNumber,
   134  ) error {
   135  	if it == nil {
   136  		return nil
   137  	}
   138  
   139  	length := len(contentsWithLine)
   140  	if length == 0 && params.IsIgnoreCompareOnActualInputEmpty {
   141  		return nil
   142  	}
   143  
   144  	var sliceErr []string
   145  
   146  	for _, textWithLine := range contentsWithLine {
   147  		err := it.VerifyError(
   148  			params,
   149  			textWithLine.LineNumber,
   150  			textWithLine.Text,
   151  		)
   152  
   153  		if err != nil {
   154  			return err
   155  		}
   156  	}
   157  
   158  	return errcore.SliceToError(
   159  		sliceErr)
   160  }