deiu / rdf2go

Native golang library for RDF (includes parser/serializer for Turtle and JSON-LD)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Escaping characters throws an exception

BoraBak opened this issue · comments

Hello,

There's a problem (bug) in the current library code regarding the escpaction of special characters.
I'm trying to parse a RDF literal (string) which has dobule slash: "aaa\\bbb".
As a result of that, an exception is being thrown: panic: strconv.ParseInt: parsing "ser": invalid syntax

Program's code:

package main

import (
	"github.com/deiu/rdf2go"
	"log"
	"os"
)

func main() {
	if err := convertTtlToJsonLd(); err != nil {
		log.Fatalln(err)
	}
}

func convertTtlToJsonLd() error {
	r, _ := os.OpenFile("test.ttl", os.O_CREATE|os.O_RDONLY, 0664)
	w, _ := os.OpenFile("test.json", os.O_CREATE|os.O_WRONLY, 0644)
	g := rdf2go.NewGraph("https://oxfordsemantic.tech/RDFox/getting-started/")
	if err := g.Parse(r, "text/turtle"); err != nil {
		return err
	}
	return g.Serialize(w, "application/ld+json")
}

RDF (Turtle) file - test.ttl:

@prefix : <https://oxfordsemantic.tech/RDFox/getting-started/> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfox: <http://oxfordsemantic.tech/RDFox#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix swrl: <http://www.w3.org/2003/11/swrl#> .
@prefix swrlb: <http://www.w3.org/2003/11/swrlb#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

:Username :Asset_account :Account_159 ;
    :Str_username "w10apps01\\user" ;
    rdf:type :Username .

Exception being thrown:

panic: strconv.ParseInt: parsing "ser": invalid syntax

goroutine 1 [running]:
github.com/deiu/gon3.unescapeUChar({0xc0001c41f0?, 0x12a0120?})
        /Users/bora/go/pkg/mod/github.com/deiu/gon3@v0.0.0-20170627184619-f84eb1e0bd62/rdf.go:208 +0x2be
github.com/deiu/gon3.lexicalForm({0xc0001c41ef, 0x11})
        /Users/bora/go/pkg/mod/github.com/deiu/gon3@v0.0.0-20170627184619-f84eb1e0bd62/rdf.go:155 +0xd2
github.com/deiu/gon3.(*Parser).parseRDFLiteral(0xc000129e68?)
        /Users/bora/go/pkg/mod/github.com/deiu/gon3@v0.0.0-20170627184619-f84eb1e0bd62/parser.go:560 +0x7e
github.com/deiu/gon3.(*Parser).parseLiteral(0xc000129e68?)
        /Users/bora/go/pkg/mod/github.com/deiu/gon3@v0.0.0-20170627184619-f84eb1e0bd62/parser.go:508 +0x5e
github.com/deiu/gon3.(*Parser).parseObject(0xc000129e68)
        /Users/bora/go/pkg/mod/github.com/deiu/gon3@v0.0.0-20170627184619-f84eb1e0bd62/parser.go:469 +0x9c5
github.com/deiu/gon3.(*Parser).parseObjectList(0xc000129e68?)
        /Users/bora/go/pkg/mod/github.com/deiu/gon3@v0.0.0-20170627184619-f84eb1e0bd62/parser.go:381 +0x25
github.com/deiu/gon3.(*Parser).parsePredicateObjectList(0xc000129e68?)
        /Users/bora/go/pkg/mod/github.com/deiu/gon3@v0.0.0-20170627184619-f84eb1e0bd62/parser.go:344 +0xa9
github.com/deiu/gon3.(*Parser).parseTriples(0xc000129e68)
        /Users/bora/go/pkg/mod/github.com/deiu/gon3@v0.0.0-20170627184619-f84eb1e0bd62/parser.go:273 +0xb8
github.com/deiu/gon3.(*Parser).parseStatement(0x1343660?)
        /Users/bora/go/pkg/mod/github.com/deiu/gon3@v0.0.0-20170627184619-f84eb1e0bd62/parser.go:168 +0x105
github.com/deiu/gon3.(*Parser).Parse(0xc000129e68, {0x1343660?, 0xc000010230?})
        /Users/bora/go/pkg/mod/github.com/deiu/gon3@v0.0.0-20170627184619-f84eb1e0bd62/parser.go:51 +0x11b
github.com/deiu/rdf2go.(*Graph).Parse(0xc00007dc50, {0x1343660, 0xc000010230}, {0x12c8083?, 0x300000002?})
        /Users/bora/go/pkg/mod/github.com/deiu/rdf2go@v0.0.0-20180504135839-3c24cc9e7afa/graph.go:201 +0x2f7
main.convertTtlToJsonLd()
        /Users/bora/converter/main.go:19 +0x1c7
main.main()
        /Users/bora/converter/main.go:10 +0x19

Process finished with the exit code 2

Hey @BoraBak, thanks for raising this issue. Unfortunately I am not able to spend time on it at the moment, so any PRs would be welcome!