Skip to content

Commit

Permalink
refactor: update to functional options pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
iret-kawashima committed Oct 23, 2023
1 parent fcf825e commit 9f03ae2
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 13 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Usage
func main() {
// Instantiate Parser with fields and patterns set
// The default is to return each matched line in NDJSON (newline-separated JSON) format
p := parser.New(fields, patterns, nil, nil)
p := parser.New(fields, patterns)

// Parses from a string passed
log := `dummy string`
Expand Down Expand Up @@ -69,7 +69,7 @@ Customize
Processing of each matched line and metadata output can be overridden when Parser instantiation.

```go
p := parser.New(fields, patterns, customLineHandler, customMetadataHandler)
p := parser.New(fields, patterns, WithLineHandler(customLineHandler), WithMetadataHandler(customMetadataHandler))
```

If you want to pretty-print json, you can wrap the default handler:
Expand Down Expand Up @@ -99,7 +99,7 @@ func prettyJSONMetadataHandler(m *parser.Metadata) (string, error) {
return prettyJSON(s)
}

p := parser.New(fields, patterns, prettyJSONLineHandler, prettyJSONMetadataHandler)
p := parser.New(fields, patterns, WithLineHandler(prettyJSONLineHandler), WithMetadataHandler(prettyJSONMetadataHandler))
```

Author
Expand Down
33 changes: 24 additions & 9 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,35 @@ type LineHandler func(matches []string, fields []string, index int) (string, err
// MetadataHandler is a type for functions that process metadata.
type MetadataHandler func(metadata *Metadata) (string, error)

// New creates a new parser with the specified configurations.
func New(fields []string, patterns []*regexp.Regexp, lineHandler LineHandler, metadataHandler MetadataHandler) *Parser {
if lineHandler == nil {
lineHandler = DefaultLineHandler
// Option is an option to replace the behavior of LineHandler and MetadataHandler.
type Option func(*Parser)

// WithLineHandler overrides the behavior of DefaultLineHandler.
func WithLineHandler(handler LineHandler) Option {
return func(p *Parser) {
p.LineHandler = handler
}
if metadataHandler == nil {
metadataHandler = DefaultMetadataHandler
}

// WithMetadataHandler overrides the behavior of DefaultMetadataHandler.
func WithMetadataHandler(handler MetadataHandler) Option {
return func(p *Parser) {
p.MetadataHandler = handler
}
return &Parser{
}

// New creates a new parser with the specified configurations.
func New(fields []string, patterns []*regexp.Regexp, opts ...Option) *Parser {
p := &Parser{
Fields: fields,
Patterns: patterns,
LineHandler: lineHandler,
MetadataHandler: metadataHandler,
LineHandler: DefaultLineHandler,
MetadataHandler: DefaultMetadataHandler,
}
for _, opt := range opts {
opt(p)
}
return p
}

// parse reads from the input, processes each line and returns parsed data and metadata.
Expand Down
9 changes: 8 additions & 1 deletion parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,14 @@ func TestNew(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := New(tt.args.fields, tt.args.patterns, tt.args.lineHandler, tt.args.metadataHandler)
opts := []Option{}
if tt.args.lineHandler != nil {
opts = append(opts, WithLineHandler(tt.args.lineHandler))
}
if tt.args.metadataHandler != nil {
opts = append(opts, WithMetadataHandler(tt.args.metadataHandler))
}
p := New(tt.args.fields, tt.args.patterns, opts...)
if !reflect.DeepEqual(p.Fields, tt.want.parser.Fields) {
t.Errorf("got: %v, want: %v", p.Fields, tt.want.parser.Fields)
}
Expand Down

0 comments on commit 9f03ae2

Please sign in to comment.