Fix Thing parsing for record IDs

This commit is contained in:
Tobie Morgan Hitchcock 2021-05-12 16:37:50 +01:00
parent 1390c56501
commit 97513cdb65
3 changed files with 47 additions and 4 deletions

View file

@ -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

View file

@ -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
}

View file

@ -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) {