/
pattern.go
45 lines (37 loc) · 1.06 KB
/
pattern.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
package eval
import (
"fmt"
"regexp"
)
func patternToRegexp(pattern string) (*regexp.Regexp, error) {
// do not accept full wildcard value
if matched, err := regexp.Match(`[a-zA-Z0-9\.]+`, []byte(pattern)); err != nil || !matched {
return nil, &ErrInvalidPattern{Pattern: pattern}
}
// quote eveything except wilcard
re := regexp.MustCompile(`[\.*+?()|\[\]{}^$]`)
quoted := re.ReplaceAllStringFunc(pattern, func(s string) string {
if s != "*" {
return "\\" + s
}
return ".*"
})
return regexp.Compile("^" + quoted + "$")
}
func toPattern(se *StringEvaluator) error {
if se.isRegexp {
return nil
}
reg, err := patternToRegexp(se.Value)
if err != nil {
return fmt.Errorf("invalid pattern '%s': %s", se.Value, err)
}
se.valueType = PatternValueType
se.isRegexp = true
se.regexp = reg
return nil
}