Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions query_string_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ package querystr
import (
"fmt"
"log"
"os"
"strconv"
"strings"
"time"
Expand All @@ -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
Expand Down
15 changes: 15 additions & 0 deletions query_string_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package querystr

import (
"io/ioutil"
"reflect"
"strings"
"testing"
Expand Down Expand Up @@ -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

Expand Down