diff --git a/grok.go b/grok.go index c7358f4..2fa659b 100644 --- a/grok.go +++ b/grok.go @@ -76,12 +76,8 @@ func NewWithConfig(config *Config) (*Grok, error) { if err != nil { return nil, err } - return g, nil -} -// Patterns return a map of the loaded patterns. -func (g *Grok) Patterns() map[string]*gPattern { - return g.patterns + return g, nil } // AddPattern adds a new pattern to the list of loaded patterns. @@ -95,6 +91,7 @@ func (g *Grok) addPattern(name, pattern string) error { return nil } +// AddPattern adds a named pattern to grok func (g *Grok) AddPattern(name, pattern string) error { g.serviceMu.Lock() defer g.serviceMu.Unlock() @@ -103,6 +100,7 @@ func (g *Grok) AddPattern(name, pattern string) error { return nil } +// AddPatternsFromMap loads a map of named patterns func (g *Grok) AddPatternsFromMap(m map[string]string) error { g.serviceMu.Lock() defer g.serviceMu.Unlock() @@ -217,7 +215,7 @@ func (g *Grok) Parse(pattern, text string) (map[string]string, error) { return captures, nil } -// Parse returns a inteface{} map with captured fields based on provided pattern over the text +// ParseTyped returns a inteface{} map with typed captured fields based on provided pattern over the text func (g *Grok) ParseTyped(pattern string, text string) (map[string]interface{}, error) { gr, err := g.compile(pattern) if err != nil { diff --git a/grok_test.go b/grok_test.go index caec37b..d527c3f 100644 --- a/grok_test.go +++ b/grok_test.go @@ -4,12 +4,12 @@ import "testing" func TestNew(t *testing.T) { g, _ := New() - if len(g.Patterns()) == 0 { + if len(g.patterns) == 0 { t.Fatal("the Grok object should have some patterns pre loaded") } g, _ = NewWithConfig(&Config{NamedCapturesOnly: true}) - if len(g.Patterns()) == 0 { + if len(g.patterns) == 0 { t.Fatal("the Grok object should have some patterns pre loaded") } } @@ -59,7 +59,7 @@ func TestMultiParseWithDefaultCaptureMode(t *testing.T) { func TestNewWithNoDefaultPatterns(t *testing.T) { g, _ := NewWithConfig(&Config{SkipDefaultPatterns: true}) - if len(g.Patterns()) != 0 { + if len(g.patterns) != 0 { t.Fatal("Using SkipDefaultPatterns the Grok object should not have any patterns pre loaded") } } @@ -429,16 +429,16 @@ func TestConcurentParse(t *testing.T) { func TestPatterns(t *testing.T) { g, _ := NewWithConfig(&Config{SkipDefaultPatterns: true}) - if len(g.Patterns()) != 0 { - t.Fatalf("Patterns should return 0, have '%d'", len(g.Patterns())) + if len(g.patterns) != 0 { + t.Fatalf("Patterns should return 0, have '%d'", len(g.patterns)) } name := "DAY0" pattern := "(?:Mon(?:day)?|Tue(?:sday)?|Wed(?:nesday)?|Thu(?:rsday)?|Fri(?:day)?|Sat(?:urday)?|Sun(?:day)?)" g.AddPattern(name, pattern) g.AddPattern(name+"1", pattern) - if len(g.Patterns()) != 2 { - t.Fatalf("Patterns should return 2, have '%d'", len(g.Patterns())) + if len(g.patterns) != 2 { + t.Fatalf("Patterns should return 2, have '%d'", len(g.patterns)) } } @@ -454,7 +454,7 @@ func TestParseTypedWithDefaultCaptureMode(t *testing.T) { t.Fatalf("%s should be '%d' have '%d'", "status", 200, captures["status"]) } else { if captures["duration"] != 0.8 { - t.Fatalf("%s should be '%d' have '%d'", "duration", 0.8, captures["duration"]) + t.Fatalf("%s should be '%f' have '%f'", "duration", 0.8, captures["duration"]) } } } @@ -470,7 +470,7 @@ func TestParseTypedWithNoTypeInfo(t *testing.T) { t.Fatalf("%s should be '%s' have '%s'", "timestamp", "23/Apr/2014:22:58:32 +0200", captures["timestamp"]) } if captures["TIME"] != nil { - t.Fatalf("%s should be '%s' have '%s'", "TIME", nil, captures["TIME"]) + t.Fatalf("%s should be nil have '%s'", "TIME", captures["TIME"]) } }