Skip to content

Commit

Permalink
Fix literals from Virtuoso endpoints being seen as URIs, Closes #94
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensworks committed Mar 11, 2020
1 parent be6a7ec commit cba41cd
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 57 deletions.
25 changes: 8 additions & 17 deletions packages/datasource-sparql/lib/datasources/SparqlDatasource.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

var Datasource = require('@ldf/core').datasources.Datasource,
N3 = require('n3'),
SparqlJsonParser = require('sparqljson-parse').SparqlJsonParser,
termToString = require('rdf-string').termToString,
LRU = require('lru-cache');

var DEFAULT_COUNT_ESTIMATE = { totalCount: 1e9, hasExactCount: false };
Expand All @@ -17,6 +19,7 @@ class SparqlDatasource extends Datasource {

this._countCache = new LRU({ max: 1000, maxAge: 1000 * 60 * 60 * 3 });
this._resolvingCountQueries = {};
this._sparqlJsonParser = new SparqlJsonParser();

// Set endpoint URL and default graph
options = options || {};
Expand Down Expand Up @@ -44,11 +47,12 @@ class SparqlDatasource extends Datasource {
catch (e) { return emitError({ message: INVALID_JSON_RESPONSE }); }

response.results.bindings.forEach(function (binding) {
binding = self._sparqlJsonParser.parseJsonBindings(binding);
var triple = {
subject: binding.s ? self._parseJsonEntity(binding.s) : query.subject,
predicate: binding.p ? self._parseJsonEntity(binding.p) : query.predicate,
object: binding.o ? self._parseJsonEntity(binding.o) : query.object,
graph: binding.g ? self._parseJsonEntity(binding.g) : query.graph,
subject: binding.s ? termToString(binding.s) : query.subject,
predicate: binding.p ? termToString(binding.p) : query.predicate,
object: binding.o ? termToString(binding.o) : query.object,
graph: binding.g ? termToString(binding.g) : query.graph,
};
destination._push(triple);
});
Expand Down Expand Up @@ -169,19 +173,6 @@ class SparqlDatasource extends Datasource {

return query.push('}'), query.join('');
}

// Parses an entity from a JSON SPARQL response
_parseJsonEntity(entity) {
if (entity.type === 'literal') {
var suffixes = '';
if (entity.datatype)
suffixes += '^^<' + entity.datatype + '>';
if (entity['xml:lang'])
suffixes += '@' + entity['xml:lang'];
return '"' + entity.value + '"' + suffixes;
}
return entity.value;
}
}

module.exports = SparqlDatasource;
4 changes: 3 additions & 1 deletion packages/datasource-sparql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@
"lint": "eslint bin/* lib test"
},
"dependencies": {
"sparqljson-parse": "^1.5.1",
"lru-cache": "^4.0.1",
"n3": "^0.9.0"
"n3": "^0.9.0",
"rdf-string": "^1.3.1"
},
"peerDependencies": {
"@ldf/core": "2.2.5"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,44 +253,6 @@ describe('SparqlDatasource', function () {
expect(totalCount).to.equal(1e9);
});
});

describe('when a JSON URI value is received', function () {
var component = { type: 'uri', value: 'http://example.org/someuri' };
it('should deserialize it as an N3.js URI', function () {
expect(datasource._parseJsonEntity(component)).to.equal('http://example.org/someuri');
});
});

describe('when a JSON literal value is received', function () {
var component = { type: 'literal', value: 'somevalue' };
it('should deserialize it as an N3.js literal', function () {
expect(datasource._parseJsonEntity(component)).to.equal('"somevalue"');
});
});

describe('when a JSON literal value with a language is received', function () {
var component = { 'type': 'literal', 'value': 'somevalue', 'xml:lang': 'en' };
it('should deserialize it as an N3.js literal with the language', function () {
expect(datasource._parseJsonEntity(component)).to.equal('"somevalue"@en');
});
});

describe('when a JSON literal value with a datatype is received', function () {
var component = { type: 'literal', value: 'somevalue', datatype: 'http://www.w3.org/2001/XMLSchema#integer' };
it('should deserialize it as an N3.js literal with the datatype', function () {
expect(datasource._parseJsonEntity(component))
.to.equal('"somevalue"^^<http://www.w3.org/2001/XMLSchema#integer>');
});
});

describe('when a JSON literal value with a language and datatype is received', function () {
var component = { 'type': 'literal', 'value': 'somevalue', 'xml:lang': 'en',
'datatype': 'http://www.w3.org/2001/XMLSchema#integer' };
it('should deserialize it as an N3.js literal with the language and datatype', function () {
expect(datasource._parseJsonEntity(component))
.to.equal('"somevalue"^^<http://www.w3.org/2001/XMLSchema#integer>@en');
});
});
});
});

Expand Down
Empty file modified packages/server/bin/ldf-server-migrate-config-3x
100644 → 100755
Empty file.
38 changes: 37 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1004,6 +1004,13 @@
dependencies:
"@types/node" ">= 8"

"@rdfjs/data-model@^1.1.1":
version "1.1.2"
resolved "https://registry.yarnpkg.com/@rdfjs/data-model/-/data-model-1.1.2.tgz#e2f48a422c7e837b8a7d96d240732be3287df713"
integrity sha512-pk/G/JLYGaXesoBLvEmoC/ic0H3B79fTyS0Ujjh5YQB2DZW+mn05ZowFFv88rjB9jf7c1XE5XSmf8jzn6U0HHA==
dependencies:
"@types/rdf-js" "^2.0.1"

"@types/color-name@^1.1.1":
version "1.1.1"
resolved "https://registry.yarnpkg.com/@types/color-name/-/color-name-1.1.1.tgz#1c1261bbeaa10a8055bbc5d8ab84b7b2afc846a0"
Expand Down Expand Up @@ -1033,6 +1040,18 @@
resolved "https://registry.yarnpkg.com/@types/node/-/node-13.7.6.tgz#cb734a7c191472ae6a2b3a502b4dfffcea974113"
integrity sha512-eyK7MWD0R1HqVTp+PtwRgFeIsemzuj4gBFSQxfPHY5iMjS7474e5wq+VFgTcdpyHeNxyKSaetYAjdMLJlKoWqA==

"@types/node@^10.12.18":
version "10.17.17"
resolved "https://registry.yarnpkg.com/@types/node/-/node-10.17.17.tgz#7a183163a9e6ff720d86502db23ba4aade5999b8"
integrity sha512-gpNnRnZP3VWzzj5k3qrpRC6Rk3H/uclhAVo1aIvwzK5p5cOrs9yEyQ8H/HBsBY0u5rrWxXEiVPQ0dEB6pkjE8Q==

"@types/rdf-js@^2.0.1", "@types/rdf-js@^2.0.2":
version "2.0.11"
resolved "https://registry.yarnpkg.com/@types/rdf-js/-/rdf-js-2.0.11.tgz#b9e398504ceb9f00eaa3b3036b643dc3490cf362"
integrity sha512-GC5MZU2HbL5JnlrLAzoxSqLprqtKwocz0TNVugqM04t1ZeeNFpZRqqBQc9Jhev35hEwdH84siRLaCesxHHYlmA==
dependencies:
"@types/node" "*"

"@zkochan/cmd-shim@^3.1.0":
version "3.1.0"
resolved "https://registry.yarnpkg.com/@zkochan/cmd-shim/-/cmd-shim-3.1.0.tgz#2ab8ed81f5bb5452a85f25758eb9b8681982fd2e"
Expand All @@ -1042,7 +1061,7 @@
mkdirp-promise "^5.0.1"
mz "^2.5.0"

JSONStream@^1.0.4, JSONStream@^1.3.4:
JSONStream@^1.0.4, JSONStream@^1.3.3, JSONStream@^1.3.4:
version "1.3.5"
resolved "https://registry.yarnpkg.com/JSONStream/-/JSONStream-1.3.5.tgz#3208c1f08d3a4d99261ab64f92302bc15e111ca0"
integrity sha512-E+iruNOY8VV9s4JEbe1aNEm6MiszPRr/UfcHMz0TQh1BXSxHK+ASV1R6W4HpjBhSeS+54PIsAMCBmwD06LLsqQ==
Expand Down Expand Up @@ -5204,6 +5223,13 @@ rc@~1.2.7:
minimist "^1.2.0"
strip-json-comments "~2.0.1"

rdf-string@^1.3.1:
version "1.3.1"
resolved "https://registry.yarnpkg.com/rdf-string/-/rdf-string-1.3.1.tgz#a9f49db4dbe0ff06bb17bf46e0389862f6a77e49"
integrity sha512-Pcw6aZRfto2cZodK5kSqFZW8mz6nfKLxSfjOSrTi5ajb2CSIwzqGx7UniysgKoV2i7tQL5dpPgCgY80upCiRUw==
dependencies:
"@rdfjs/data-model" "^1.1.1"

read-cmd-shim@^1.0.1:
version "1.0.5"
resolved "https://registry.yarnpkg.com/read-cmd-shim/-/read-cmd-shim-1.0.5.tgz#87e43eba50098ba5a32d0ceb583ab8e43b961c16"
Expand Down Expand Up @@ -5772,6 +5798,16 @@ source-map@^0.6.1, source-map@~0.6.1:
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263"
integrity sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==

sparqljson-parse@^1.5.1:
version "1.5.1"
resolved "https://registry.yarnpkg.com/sparqljson-parse/-/sparqljson-parse-1.5.1.tgz#a3f1b08724e31c6c7e96f182ca9685b606f15ebf"
integrity sha512-+hlVthsoADK/gZkNrV6GCz7Ep3GwtY8DNJSjt9Mt32vuTL9QbQlCRalqOQRaz7D/oyRAsNvkW6/KldMA7s2YCQ==
dependencies:
"@rdfjs/data-model" "^1.1.1"
"@types/node" "^10.12.18"
"@types/rdf-js" "^2.0.2"
JSONStream "^1.3.3"

spawn-sync@^1.0.15:
version "1.0.15"
resolved "https://registry.yarnpkg.com/spawn-sync/-/spawn-sync-1.0.15.tgz#b00799557eb7fb0c8376c29d44e8a1ea67e57476"
Expand Down

0 comments on commit cba41cd

Please sign in to comment.