Skip to content

Commit

Permalink
Merge pull request #56 from josegonzalez/patch-1
Browse files Browse the repository at this point in the history
feat: export Format and Regexp so we can reuse the Parser logic
  • Loading branch information
satyrius committed Sep 18, 2023
2 parents 2efce0b + b53c679 commit 0911513
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
6 changes: 3 additions & 3 deletions parser.go
Expand Up @@ -15,8 +15,8 @@ type StringParser interface {

// Parser is a log record parser. Use specific constructors to initialize it.
type Parser struct {
format string
regexp *regexp.Regexp
Format string
Regexp *regexp.Regexp
}

// NewParser returns a new Parser, use given log format to create its internal
Expand Down Expand Up @@ -45,7 +45,7 @@ func NewParser(format string) *Parser {
// ParseString parses a log file line using internal format regexp. If a line
// does not match the given format an error will be returned.
func (parser *Parser) ParseString(line string) (entry *Entry, err error) {
re := parser.regexp
re := parser.Regexp
fields := re.FindStringSubmatch(line)
if fields == nil {
err = fmt.Errorf("access log line '%v' does not match given format '%v'", line, re)
Expand Down
14 changes: 7 additions & 7 deletions parser_test.go
Expand Up @@ -13,11 +13,11 @@ func TestParser(t *testing.T) {
parser := NewParser(format)

Convey("Ensure parser format is ok", func() {
So(parser.format, ShouldEqual, format)
So(parser.Format, ShouldEqual, format)
})

Convey("Test format to regexp translation", func() {
So(parser.regexp.String(), ShouldEqual,
So(parser.Regexp.String(), ShouldEqual,
`^(?P<remote_addr>[^ ]*) \[(?P<time_local>[^]]*)\] "(?P<request>[^"]*)" (?P<status>[^ ]*)`)
})

Expand Down Expand Up @@ -59,9 +59,9 @@ func TestParser(t *testing.T) {
parser := NewParser(format)

Convey("Ensure two fields concatenated toggle regexp ok", func() {
So(parser.format, ShouldEqual, format)
So(parser.Format, ShouldEqual, format)
So(
parser.regexp.String(),
parser.Regexp.String(),
ShouldEqual,
`^(?P<remote_addr>[^ ]*) \[(?P<time_local>[^]]*)\] "(?P<host>[^"]*)(?P<request_uri>[^"]*)" (?P<status>[^ ]*)`,
)
Expand All @@ -70,9 +70,9 @@ func TestParser(t *testing.T) {
format = `$remote_addr [$time_local] "$host$request_uri$demo" $status`
parser = NewParser(format)
Convey("Ensure three fields concatenated toggle regexp ok", func() {
So(parser.format, ShouldEqual, format)
So(parser.Format, ShouldEqual, format)
So(
parser.regexp.String(),
parser.Regexp.String(),
ShouldEqual,
`^(?P<remote_addr>[^ ]*) \[(?P<time_local>[^]]*)\] "(?P<host>[^"]*)(?P<request_uri>[^"]*)(?P<demo>[^"]*)" (?P<status>[^ ]*)`,
)
Expand All @@ -96,7 +96,7 @@ func TestParser(t *testing.T) {
`)
parser, err := NewNginxParser(conf, "main")
So(err, ShouldBeNil)
So(parser.format, ShouldEqual, expected)
So(parser.Format, ShouldEqual, expected)
})
})
}

0 comments on commit 0911513

Please sign in to comment.