From b0f12b6f8eb8e0d27ac467edec17a99ce6e97044 Mon Sep 17 00:00:00 2001 From: Akshay Shekher Date: Tue, 5 Jan 2021 18:19:00 -0800 Subject: [PATCH] Added default logger for when logging is enabled but logger is not specified. fixes #1 --- query_string_parser.go | 9 +++++++++ query_string_parser_test.go | 15 +++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/query_string_parser.go b/query_string_parser.go index 91fbab9..4ffd7d3 100644 --- a/query_string_parser.go +++ b/query_string_parser.go @@ -29,6 +29,7 @@ package querystr import ( "fmt" "log" + "os" "strconv" "strings" "time" @@ -43,27 +44,35 @@ type QueryStringOptions struct { logger *log.Logger } +var defaultLogger = log.New(os.Stderr, "", log.LstdFlags) + +// DefaultOptions creates default options with debug features disabled func DefaultOptions() QueryStringOptions { return QueryStringOptions{ dateFormat: time.RFC3339, + logger: defaultLogger, } } +// WithDebugParser will make parser print debug messages to logger in options or stderr by default func (o QueryStringOptions) WithDebugParser(debug bool) QueryStringOptions { o.debugParser = debug return o } +// WithDebugLexer will make lexer print debug messages to logger in options or stderr by default func (o QueryStringOptions) WithDebugLexer(debug bool) QueryStringOptions { o.debugLexer = debug return o } +// WithDateFormat changes the default date format (RFC 3339) to the specified one func (o QueryStringOptions) WithDateFormat(dateFormat string) QueryStringOptions { o.dateFormat = dateFormat return o } +// WithLogger changes the log destination to the specified one, default is os.Stderr func (o QueryStringOptions) WithLogger(logger *log.Logger) QueryStringOptions { o.logger = logger return o diff --git a/query_string_parser_test.go b/query_string_parser_test.go index 3145dba..f18e94c 100644 --- a/query_string_parser_test.go +++ b/query_string_parser_test.go @@ -15,6 +15,7 @@ package querystr import ( + "io/ioutil" "reflect" "strings" "testing" @@ -420,6 +421,20 @@ func TestQuerySyntaxParserInvalid(t *testing.T) { } } +func TestParserDebugWithNoLogger(t *testing.T) { + // don't print logs in tests + defaultLogger.SetOutput(ioutil.Discard) + + r, err := ParseQueryString("text", DefaultOptions().WithDebugParser(true)) + if err != nil { + t.Errorf("unexpected error: %+v", err) + } + expectedQuery := bluge.NewBooleanQuery().AddShould(bluge.NewMatchQuery("text")) + if !reflect.DeepEqual(r, expectedQuery) { + t.Error("unexpected query returned") + } +} + var extTokenTypes []int var extTokens []yySymType