processor_match.go 816 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package executor
  2. import (
  3. "fmt"
  4. "regexp"
  5. "strings"
  6. schemepkg "git.buran.team/main/cep/scheme"
  7. )
  8. type MatchProcessor struct {
  9. expression *regexp.Regexp
  10. count int
  11. }
  12. func NewMatchProcessor(processorScheme schemepkg.Processor) (*MatchProcessor, error) {
  13. e, err := regexp.Compile(processorScheme.KindMatch.Expression)
  14. if err != nil {
  15. return nil, fmt.Errorf("can't create match processor: %w", err)
  16. }
  17. return &MatchProcessor{
  18. expression: e,
  19. count: processorScheme.KindMatch.Count,
  20. }, nil
  21. }
  22. func (this *MatchProcessor) Process(buffer []byte) bool {
  23. lines := strings.Split(string(buffer), "\n")
  24. count := 0
  25. for _, line := range lines {
  26. if !this.expression.Match([]byte(line)) {
  27. continue
  28. }
  29. count += 1
  30. }
  31. if this.count == -1 {
  32. return count > 0
  33. }
  34. return count == this.count
  35. }