Pipeline: add default value if regex doesn't match (#3646)

This commit is contained in:
andig 2022-06-16 09:06:03 +02:00 • committed by GitHub
parent 703e1362f7
commit f618cdf980
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 9 deletions

View file

@ -18,6 +18,7 @@ import (
type Pipeline struct {
re *regexp.Regexp
jq *gojq.Query
dflt string
unpack string
decode string
vm *otto.Otto
@ -25,12 +26,13 @@ type Pipeline struct {
}
type Settings struct {
Regex string
Jq string
Unpack string
Decode string
VM string
Script string
Regex string
Default string
Jq string
Unpack string
Decode string
VM string
Script string
}
func New(cc Settings) (*Pipeline, error) {
@ -38,7 +40,7 @@ func New(cc Settings) (*Pipeline, error) {
var err error
if err == nil && cc.Regex != "" {
_, err = p.WithRegex(cc.Regex)
_, err = p.WithRegex(cc.Regex, cc.Default)
}
if err == nil && cc.Jq != "" {
@ -61,13 +63,14 @@ func New(cc Settings) (*Pipeline, error) {
}
// WithRegex adds a regex query applied to the mqtt listener payload
func (p *Pipeline) WithRegex(regex string) (*Pipeline, error) {
func (p *Pipeline) WithRegex(regex, dflt string) (*Pipeline, error) {
re, err := regexp.Compile(regex)
if err != nil {
return nil, fmt.Errorf("invalid regex '%s': %w", re, err)
}
p.re = re
p.dflt = dflt
return p, nil
}
@ -185,6 +188,8 @@ func (p *Pipeline) Process(in []byte) ([]byte, error) {
b = m[0] // full match
} else if len(m) > 1 {
b = m[1] // first submatch
} else if len(p.dflt) > 0 {
return []byte(p.dflt), nil
}
}

View file

@ -7,7 +7,7 @@ import (
func TestRegex(t *testing.T) {
for _, re := range []string{`([0-9.]+)`, `[0-9.]+`} {
p, err := new(Pipeline).WithRegex(re)
p, err := new(Pipeline).WithRegex(re, "")
if err != nil {
t.Error(err)
}
@ -22,3 +22,19 @@ func TestRegex(t *testing.T) {
}
}
}
func TestRegexDefault(t *testing.T) {
p, err := new(Pipeline).WithRegex(`\d+`, "123")
if err != nil {
t.Error(err)
}
res, err := p.Process([]byte("xxx"))
if err != nil {
t.Error(err)
}
if exp := []byte("123"); !bytes.Equal(res, exp) {
t.Errorf("Expected %s, got %s", exp, res)
}
}