Skip to content

Commit

Permalink
Merge pull request #18 from jamesofnet/master
Browse files Browse the repository at this point in the history
Fixed #17, short patterns not replaced with regex
  • Loading branch information
vjeantet authored Mar 4, 2017
2 parents 83bfdfd + dd02d42 commit d73e972
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
15 changes: 8 additions & 7 deletions grok.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ import (
)

var (
defn = regexp.MustCompile(`%{(\w+):?(\w+)?:?(\w+)?}`)
normal = regexp.MustCompile(`%{(\w+:?\w+:?\w+)}`)
normal = regexp.MustCompile(`%{(\w+(?::\w+(?::\w+)?)?)}`)
)

// A Config structure is used to configure a Grok parser.
Expand Down Expand Up @@ -122,13 +121,15 @@ func (g *Grok) addPatternsFromMap(m map[string]string) error {
patternDeps := graph{}
for k, v := range m {
keys := []string{}
for _, key := range defn.FindAllStringSubmatch(v, -1) {
if g.patterns[key[1]] == nil {
if _, ok := m[key[1]]; !ok {
return fmt.Errorf("no pattern found for %%{%s}", key[1])
for _, key := range normal.FindAllStringSubmatch(v, -1) {
names := strings.Split(key[1], ":")
syntax := names[0]
if g.patterns[syntax] == nil {
if _, ok := m[syntax]; !ok {
return fmt.Errorf("no pattern found for %%{%s}", syntax)
}
}
keys = append(keys, key[1])
keys = append(keys, syntax)
}
patternDeps[k] = keys
}
Expand Down
13 changes: 13 additions & 0 deletions grok_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,19 @@ func TestErrorMatch(t *testing.T) {

}

func TestShortName(t *testing.T) {
g, _ := New()
g.AddPattern("A", "a")

m, err := g.Match("%{A}", "a")
if err != nil {
t.Fatal("a should match %%{A}: err=%s", err.Error())
}
if !m {
t.Fatal("%%{A} didn't match 'a'")
}
}

func TestDayCompile(t *testing.T) {
g, _ := New()
g.AddPattern("DAY", "(?:Mon(?:day)?|Tue(?:sday)?|Wed(?:nesday)?|Thu(?:rsday)?|Fri(?:day)?|Sat(?:urday)?|Sun(?:day)?)")
Expand Down

0 comments on commit d73e972

Please sign in to comment.