Destutter nquads
This commit is contained in:
parent
40f3363cde
commit
177059cc16
3 changed files with 20 additions and 20 deletions
|
|
@ -106,7 +106,7 @@ func Repl(ts graph.TripleStore, queryLanguage string, cfg *config.Config) {
|
||||||
}
|
}
|
||||||
if strings.HasPrefix(line, ":a") {
|
if strings.HasPrefix(line, ":a") {
|
||||||
var tripleStmt = line[3:]
|
var tripleStmt = line[3:]
|
||||||
triple := nquads.ParseLineToTriple(tripleStmt)
|
triple := nquads.Parse(tripleStmt)
|
||||||
if triple == nil {
|
if triple == nil {
|
||||||
fmt.Println("Not a valid triple.")
|
fmt.Println("Not a valid triple.")
|
||||||
line = ""
|
line = ""
|
||||||
|
|
@ -118,7 +118,7 @@ func Repl(ts graph.TripleStore, queryLanguage string, cfg *config.Config) {
|
||||||
}
|
}
|
||||||
if strings.HasPrefix(line, ":d") {
|
if strings.HasPrefix(line, ":d") {
|
||||||
var tripleStmt = line[3:]
|
var tripleStmt = line[3:]
|
||||||
triple := nquads.ParseLineToTriple(tripleStmt)
|
triple := nquads.Parse(tripleStmt)
|
||||||
if triple == nil {
|
if triple == nil {
|
||||||
fmt.Println("Not a valid triple.")
|
fmt.Println("Not a valid triple.")
|
||||||
line = ""
|
line = ""
|
||||||
|
|
|
||||||
|
|
@ -27,7 +27,7 @@ import (
|
||||||
func isWhitespace(s uint8) bool {
|
func isWhitespace(s uint8) bool {
|
||||||
return (s == '\t' || s == '\r' || s == ' ')
|
return (s == '\t' || s == '\r' || s == ' ')
|
||||||
}
|
}
|
||||||
func ParseLineToTriple(str string) *graph.Triple {
|
func Parse(str string) *graph.Triple {
|
||||||
// Skip leading whitespace.
|
// Skip leading whitespace.
|
||||||
str = skipWhitespace(str)
|
str = skipWhitespace(str)
|
||||||
// Check for a comment
|
// Check for a comment
|
||||||
|
|
@ -184,7 +184,7 @@ func ReadNQuadsFromReader(c chan *graph.Triple, reader io.Reader) {
|
||||||
if pre {
|
if pre {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
triple := ParseLineToTriple(line)
|
triple := Parse(line)
|
||||||
line = ""
|
line = ""
|
||||||
if triple != nil {
|
if triple != nil {
|
||||||
nTriples++
|
nTriples++
|
||||||
|
|
|
||||||
|
|
@ -25,61 +25,61 @@ import (
|
||||||
func TestParsingNTriples(t *testing.T) {
|
func TestParsingNTriples(t *testing.T) {
|
||||||
Convey("When parsing", t, func() {
|
Convey("When parsing", t, func() {
|
||||||
Convey("It should not parse invalid triples", func() {
|
Convey("It should not parse invalid triples", func() {
|
||||||
x := ParseLineToTriple("invalid")
|
x := Parse("invalid")
|
||||||
So(x, ShouldBeNil)
|
So(x, ShouldBeNil)
|
||||||
})
|
})
|
||||||
Convey("It should not parse comments", func() {
|
Convey("It should not parse comments", func() {
|
||||||
x := ParseLineToTriple("# nominally valid triple .")
|
x := Parse("# nominally valid triple .")
|
||||||
So(x, ShouldBeNil)
|
So(x, ShouldBeNil)
|
||||||
})
|
})
|
||||||
Convey("It should parse simple triples", func() {
|
Convey("It should parse simple triples", func() {
|
||||||
x := ParseLineToTriple("this is valid .")
|
x := Parse("this is valid .")
|
||||||
So(x, ShouldNotBeNil)
|
So(x, ShouldNotBeNil)
|
||||||
So(x.Sub, ShouldEqual, "this")
|
So(x.Sub, ShouldEqual, "this")
|
||||||
})
|
})
|
||||||
Convey("It should parse quoted triples", func() {
|
Convey("It should parse quoted triples", func() {
|
||||||
x := ParseLineToTriple("this is \"valid too\" .")
|
x := Parse("this is \"valid too\" .")
|
||||||
So(x, ShouldNotBeNil)
|
So(x, ShouldNotBeNil)
|
||||||
So(x.Obj, ShouldEqual, "valid too")
|
So(x.Obj, ShouldEqual, "valid too")
|
||||||
So(x.Provenance, ShouldEqual, "")
|
So(x.Provenance, ShouldEqual, "")
|
||||||
})
|
})
|
||||||
Convey("It should parse escaped quoted triples", func() {
|
Convey("It should parse escaped quoted triples", func() {
|
||||||
x := ParseLineToTriple("he said \"\\\"That's all folks\\\"\" .")
|
x := Parse("he said \"\\\"That's all folks\\\"\" .")
|
||||||
So(x, ShouldNotBeNil)
|
So(x, ShouldNotBeNil)
|
||||||
So(x.Obj, ShouldEqual, "\"That's all folks\"")
|
So(x.Obj, ShouldEqual, "\"That's all folks\"")
|
||||||
So(x.Provenance, ShouldEqual, "")
|
So(x.Provenance, ShouldEqual, "")
|
||||||
})
|
})
|
||||||
|
|
||||||
Convey("It should parse an example real triple", func() {
|
Convey("It should parse an example real triple", func() {
|
||||||
x := ParseLineToTriple("\":/guid/9202a8c04000641f80000000010c843c\" \"name\" \"George Morris\" .")
|
x := Parse("\":/guid/9202a8c04000641f80000000010c843c\" \"name\" \"George Morris\" .")
|
||||||
So(x, ShouldNotBeNil)
|
So(x, ShouldNotBeNil)
|
||||||
So(x.Obj, ShouldEqual, "George Morris")
|
So(x.Obj, ShouldEqual, "George Morris")
|
||||||
So(x.Provenance, ShouldEqual, "")
|
So(x.Provenance, ShouldEqual, "")
|
||||||
})
|
})
|
||||||
|
|
||||||
Convey("It should parse a pathologically spaced triple", func() {
|
Convey("It should parse a pathologically spaced triple", func() {
|
||||||
x := ParseLineToTriple("foo is \"\\tA big tough\\r\\nDeal\\\\\" .")
|
x := Parse("foo is \"\\tA big tough\\r\\nDeal\\\\\" .")
|
||||||
So(x, ShouldNotBeNil)
|
So(x, ShouldNotBeNil)
|
||||||
So(x.Obj, ShouldEqual, "\tA big tough\r\nDeal\\")
|
So(x.Obj, ShouldEqual, "\tA big tough\r\nDeal\\")
|
||||||
So(x.Provenance, ShouldEqual, "")
|
So(x.Provenance, ShouldEqual, "")
|
||||||
})
|
})
|
||||||
|
|
||||||
Convey("It should parse a simple quad", func() {
|
Convey("It should parse a simple quad", func() {
|
||||||
x := ParseLineToTriple("this is valid quad .")
|
x := Parse("this is valid quad .")
|
||||||
So(x, ShouldNotBeNil)
|
So(x, ShouldNotBeNil)
|
||||||
So(x.Obj, ShouldEqual, "valid")
|
So(x.Obj, ShouldEqual, "valid")
|
||||||
So(x.Provenance, ShouldEqual, "quad")
|
So(x.Provenance, ShouldEqual, "quad")
|
||||||
})
|
})
|
||||||
|
|
||||||
Convey("It should parse a quoted quad", func() {
|
Convey("It should parse a quoted quad", func() {
|
||||||
x := ParseLineToTriple("this is valid \"quad thing\" .")
|
x := Parse("this is valid \"quad thing\" .")
|
||||||
So(x, ShouldNotBeNil)
|
So(x, ShouldNotBeNil)
|
||||||
So(x.Obj, ShouldEqual, "valid")
|
So(x.Obj, ShouldEqual, "valid")
|
||||||
So(x.Provenance, ShouldEqual, "quad thing")
|
So(x.Provenance, ShouldEqual, "quad thing")
|
||||||
})
|
})
|
||||||
|
|
||||||
Convey("It should parse crazy escaped quads", func() {
|
Convey("It should parse crazy escaped quads", func() {
|
||||||
x := ParseLineToTriple("\"\\\"this\" \"\\\"is\" \"\\\"valid\" \"\\\"quad thing\".")
|
x := Parse("\"\\\"this\" \"\\\"is\" \"\\\"valid\" \"\\\"quad thing\".")
|
||||||
So(x, ShouldNotBeNil)
|
So(x, ShouldNotBeNil)
|
||||||
So(x.Sub, ShouldEqual, "\"this")
|
So(x.Sub, ShouldEqual, "\"this")
|
||||||
So(x.Pred, ShouldEqual, "\"is")
|
So(x.Pred, ShouldEqual, "\"is")
|
||||||
|
|
@ -93,27 +93,27 @@ func TestParsingNTriplesOfficial(t *testing.T) {
|
||||||
Convey("When using some public test cases...", t, func() {
|
Convey("When using some public test cases...", t, func() {
|
||||||
Convey("It should handle some simple cases with comments", func() {
|
Convey("It should handle some simple cases with comments", func() {
|
||||||
var x *graph.Triple
|
var x *graph.Triple
|
||||||
x = ParseLineToTriple("<http://example/s> <http://example/p> <http://example/o> . # comment")
|
x = Parse("<http://example/s> <http://example/p> <http://example/o> . # comment")
|
||||||
So(x, ShouldNotBeNil)
|
So(x, ShouldNotBeNil)
|
||||||
So(x.Sub, ShouldEqual, "http://example/s")
|
So(x.Sub, ShouldEqual, "http://example/s")
|
||||||
So(x.Pred, ShouldEqual, "http://example/p")
|
So(x.Pred, ShouldEqual, "http://example/p")
|
||||||
So(x.Obj, ShouldEqual, "http://example/o")
|
So(x.Obj, ShouldEqual, "http://example/o")
|
||||||
So(x.Provenance, ShouldEqual, "")
|
So(x.Provenance, ShouldEqual, "")
|
||||||
x = ParseLineToTriple("<http://example/s> <http://example/p> _:o . # comment")
|
x = Parse("<http://example/s> <http://example/p> _:o . # comment")
|
||||||
So(x, ShouldNotBeNil)
|
So(x, ShouldNotBeNil)
|
||||||
So(x.Sub, ShouldEqual, "http://example/s")
|
So(x.Sub, ShouldEqual, "http://example/s")
|
||||||
So(x.Pred, ShouldEqual, "http://example/p")
|
So(x.Pred, ShouldEqual, "http://example/p")
|
||||||
So(x.Obj, ShouldEqual, "_:o")
|
So(x.Obj, ShouldEqual, "_:o")
|
||||||
So(x.Provenance, ShouldEqual, "")
|
So(x.Provenance, ShouldEqual, "")
|
||||||
x = ParseLineToTriple("<http://example/s> <http://example/p> \"o\" . # comment")
|
x = Parse("<http://example/s> <http://example/p> \"o\" . # comment")
|
||||||
So(x, ShouldNotBeNil)
|
So(x, ShouldNotBeNil)
|
||||||
So(x.Obj, ShouldEqual, "o")
|
So(x.Obj, ShouldEqual, "o")
|
||||||
So(x.Provenance, ShouldEqual, "")
|
So(x.Provenance, ShouldEqual, "")
|
||||||
x = ParseLineToTriple("<http://example/s> <http://example/p> \"o\"^^<http://example/dt> . # comment")
|
x = Parse("<http://example/s> <http://example/p> \"o\"^^<http://example/dt> . # comment")
|
||||||
So(x, ShouldNotBeNil)
|
So(x, ShouldNotBeNil)
|
||||||
So(x.Obj, ShouldEqual, "o")
|
So(x.Obj, ShouldEqual, "o")
|
||||||
So(x.Provenance, ShouldEqual, "")
|
So(x.Provenance, ShouldEqual, "")
|
||||||
x = ParseLineToTriple("<http://example/s> <http://example/p> \"o\"@en . # comment")
|
x = Parse("<http://example/s> <http://example/p> \"o\"@en . # comment")
|
||||||
So(x, ShouldNotBeNil)
|
So(x, ShouldNotBeNil)
|
||||||
So(x.Obj, ShouldEqual, "o")
|
So(x.Obj, ShouldEqual, "o")
|
||||||
So(x.Provenance, ShouldEqual, "")
|
So(x.Provenance, ShouldEqual, "")
|
||||||
|
|
@ -123,7 +123,7 @@ func TestParsingNTriplesOfficial(t *testing.T) {
|
||||||
|
|
||||||
func BenchmarkParser(b *testing.B) {
|
func BenchmarkParser(b *testing.B) {
|
||||||
for n := 0; n < b.N; n++ {
|
for n := 0; n < b.N; n++ {
|
||||||
x := ParseLineToTriple("<http://example/s> <http://example/p> \"object of some real\\tlength\"@en . # comment")
|
x := Parse("<http://example/s> <http://example/p> \"object of some real\\tlength\"@en . # comment")
|
||||||
if x.Obj != "object of some real\tlength" {
|
if x.Obj != "object of some real\tlength" {
|
||||||
b.Fail()
|
b.Fail()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue