diff --git a/sql/ast.go b/sql/ast.go index 009ddca1..21a7ea50 100644 --- a/sql/ast.go +++ b/sql/ast.go @@ -674,7 +674,7 @@ type Thing struct { func ParseThing(val string) *Thing { r := strings.NewReader(val) s := newScanner(r) - if t, _, v := s.scanIdiom(); t == THING { + if t, _, v := s.scanThing(); t == THING { return v.(*Thing) } return nil diff --git a/sql/scanner.go b/sql/scanner.go index b10f4d3a..362bfadd 100644 --- a/sql/scanner.go +++ b/sql/scanner.go @@ -756,7 +756,7 @@ func (s *scanner) scanNumber(chp ...rune) (tok Token, lit string, val interface{ if chn := s.next(); chn == '+' { tok = DOUBLE buf.WriteRune(chn) - } else if ch == '-' { + } else if chn == '-' { tok = DOUBLE buf.WriteRune(chn) } else { @@ -765,7 +765,10 @@ func (s *scanner) scanNumber(chp ...rune) (tok Token, lit string, val interface{ case 's', 'h', 'd', 'w': tok = DURATION case 'n', 'u', 'ยต', 'm': - if chn := s.next(); chn == 's' { + tok = DURATION + if chn := s.next(); chn == eof { + break + } else if chn == 's' { tok = DURATION buf.WriteRune(chn) } else if ch == 'm' { @@ -975,7 +978,7 @@ func (s *scanner) part() (tok Token, lit string, val interface{}) { tok = ILLEGAL } - if tok != IDENT && tok != NUMBER && tok != DOUBLE { + if tok != IDENT && tok != NUMBER && tok != DOUBLE && tok != DURATION { tok = ILLEGAL } diff --git a/sql/sql_test.go b/sql/sql_test.go index 1b429416..3335a92a 100644 --- a/sql/sql_test.go +++ b/sql/sql_test.go @@ -180,6 +180,46 @@ func TestMain(t *testing.T) { } +func Test_Parse_IDs(t *testing.T) { + + Convey("All IDs should parse correctly", t, func() { + So(ParseThing("test:tester"), ShouldResemble, &Thing{ + TB: "test", + ID: string("tester"), + }) + So(ParseThing("test:123456"), ShouldResemble, &Thing{ + TB: "test", + ID: float64(123456), + }) + So(ParseThing("test:abcdef"), ShouldResemble, &Thing{ + TB: "test", + ID: string("abcdef"), + }) + So(ParseThing("test:a1b2c3"), ShouldResemble, &Thing{ + TB: "test", + ID: string("a1b2c3"), + }) + So(ParseThing("test:1a2b3c"), ShouldResemble, &Thing{ + TB: "test", + ID: string("1a2b3c"), + }) + So(ParseThing("test:4m9sms"), ShouldResemble, &Thing{ + TB: "test", + ID: string("4m9sms"), + }) + So(ParseThing("test:4m98m85ms"), ShouldResemble, &Thing{ + TB: "test", + ID: string("4m98m85ms"), + }) + So(ParseThing("test:00s5w36sm"), ShouldResemble, &Thing{ + TB: "test", + ID: string("00s5w36sm"), + }) + }) + +} + + // Ensure the parser can parse a multi-statement query. func Test_Parse_General(t *testing.T) {