Skip to content

fix support of unquoted value on time criteria #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
9 changes: 9 additions & 0 deletions query_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"strings"
)

var validUnquotedTimeRegexp = regexp.MustCompile(`^\d+(ns|u|ms|s|m|h|d|w)?$`)

// Duration Duration interface
type Duration interface {
Nanoseconds(uint) Duration
Expand Down Expand Up @@ -315,6 +317,7 @@ func (q *Query) Build() string {
}

var functionMatcher = regexp.MustCompile(`.+\(.+\)$`)
var mathMatcher = regexp.MustCompile(`^(.+?)((\s*)([\-\+\/\*])(\s*)(-?\d+)(\.\d+)?)+`)

func (q *Query) buildFields() string {
if q.fields == nil {
Expand All @@ -338,6 +341,8 @@ func (q *Query) buildFields() string {

if functionMatcher.MatchString(selectField) {
fields[i] = selectField
} else if mathMatcher.MatchString(selectField) {
fields[i] = selectField
} else {
fields[i] = fmt.Sprintf("\"%s\"", selectField)
}
Expand Down Expand Up @@ -511,6 +516,10 @@ func getCriteriaTemplate(tag Tag) string {
case bool:
return fmt.Sprintf(`"%s" %s %t`, tag.key, tag.op, tag.value)
default:
if tag.key == "time" && validUnquotedTimeRegexp.MatchString(tag.value.(string)) {
// 'time' key accepts non-quoted string value (eg: 1535313431000ns)
return fmt.Sprintf(`%s %s %s`, tag.key, tag.op, tag.value)
}
return fmt.Sprintf(`"%s" %s '%s'`, tag.key, tag.op, tag.value)
}
}