From 48dadf01602eb90540571eeed6381616f1b9d110 Mon Sep 17 00:00:00 2001 From: Gregg Kellogg Date: Sat, 4 Jan 2020 16:15:59 -0800 Subject: [PATCH 1/3] More support for "@type": "@none" --- CHANGELOG.md | 1 + lib/compact.js | 9 ++++----- lib/context.js | 14 +++++++++++++- lib/expand.js | 2 +- tests/test-common.js | 8 -------- 5 files changed, 19 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 970d234a..65defd3f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ ### Fixed - JSON literal value handling issues. +- More support for "@type": "@none" ## 2.0.0 - 2019-12-09 diff --git a/lib/compact.js b/lib/compact.js index 58145f89..4f12bb63 100644 --- a/lib/compact.js +++ b/lib/compact.js @@ -952,7 +952,7 @@ api.compactValue = ({activeCtx, activeProperty, value, options}) => { const preserveIndex = '@index' in value && !container.includes('@index'); // if there's no @index to preserve ... - if(!preserveIndex) { + if(!preserveIndex && type !== '@none') { // matching @type or @language specified in context, compact value if(value['@type'] === type || value['@language'] === language) { return value['@value']; @@ -971,6 +971,7 @@ api.compactValue = ({activeCtx, activeProperty, value, options}) => { const hasNullMapping = (activeCtx.mappings.has(activeProperty) && activeCtx.mappings.get(activeProperty)['@language'] === null); if(isValueOnlyKey && + type !== '@none' && (!hasDefaultLanguage || !isValueString || hasNullMapping)) { return value['@value']; } @@ -1169,17 +1170,15 @@ function _selectTerm( prefs.push('@none'); const containerMap = activeCtx.inverse[iri]; - for(let ci = 0; ci < containers.length; ++ci) { + for(const container of containers) { // if container not available in the map, continue - const container = containers[ci]; if(!(container in containerMap)) { continue; } const typeOrLanguageValueMap = containerMap[container][typeOrLanguage]; - for(let pi = 0; pi < prefs.length; ++pi) { + for(const pref of prefs) { // if type/language option not available in the map, continue - const pref = prefs[pi]; if(!(pref in typeOrLanguageValueMap)) { continue; } diff --git a/lib/context.js b/lib/context.js index 8d24b481..c802253a 100644 --- a/lib/context.js +++ b/lib/context.js @@ -527,7 +527,15 @@ api.createTermDefinition = ({ {code: 'invalid type mapping', context: localCtx}); } - if(type !== '@id' && type !== '@vocab' && type !== '@json') { + if((type === '@json' || type === '@none')) { + if(api.processingMode(activeCtx, 1.0)) { + throw new JsonLdError( + 'Invalid JSON-LD syntax; an @context @type value not be ' + + '#{type} in JSON-LD 1.0 mode.', + 'jsonld.SyntaxError', + {code: 'invalid type mapping', context: localCtx}); + } + } else if(type !== '@id' && type !== '@vocab') { // expand @type to full IRI type = _expandIri( activeCtx, type, {vocab: true, base: false}, localCtx, defined, @@ -959,6 +967,10 @@ api.getInitialContext = options => { if(mapping.reverse) { // term is preferred for values using @reverse _addPreferredTerm(term, entry['@type'], '@reverse'); + } else if(mapping['@type'] === '@none') { + _addPreferredTerm(term, entry['@any'], '@none'); + _addPreferredTerm(term, entry['@language'], '@none'); + _addPreferredTerm(term, entry['@type'], '@none'); } else if('@type' in mapping) { // term is preferred for values using specific type _addPreferredTerm(term, entry['@type'], mapping['@type']); diff --git a/lib/expand.js b/lib/expand.js index c8e985b3..7cf27bf7 100644 --- a/lib/expand.js +++ b/lib/expand.js @@ -855,7 +855,7 @@ function _expandValue({activeCtx, activeProperty, value, options}) { const rval = {}; - if(type && !['@id', '@vocab'].includes(type)) { + if(type && !['@id', '@vocab', '@none'].includes(type)) { // other type rval['@type'] = type; } else if(_isString(value)) { diff --git a/tests/test-common.js b/tests/test-common.js index 81149cc1..296e5a5d 100644 --- a/tests/test-common.js +++ b/tests/test-common.js @@ -32,10 +32,6 @@ const TEST_TYPES = { specVersion: ['json-ld-1.0'], // FIXME idRegex: [ - // @type: @none - /compact-manifest.jsonld#ttn01$/, - /compact-manifest.jsonld#ttn02$/, - /compact-manifest.jsonld#ttn03$/, // included /compact-manifest.jsonld#tin01$/, /compact-manifest.jsonld#tin02$/, @@ -106,8 +102,6 @@ const TEST_TYPES = { /expand-manifest.jsonld#thc03$/, /expand-manifest.jsonld#thc04$/, /expand-manifest.jsonld#thc05$/, - // @type: @none - /expand-manifest.jsonld#ttn02$/, // misc /expand-manifest.jsonld#te043$/, /expand-manifest.jsonld#te044$/, @@ -404,8 +398,6 @@ const TEST_TYPES = { /toRdf-manifest.jsonld#tso08$/, /toRdf-manifest.jsonld#tso10$/, /toRdf-manifest.jsonld#tso11$/, - // type:none - /toRdf-manifest.jsonld#ttn02$/, // colliding keyword /toRdf-manifest.jsonld#te114$/, // included From e01b76e2955ec75e5f0b9bde34a62b4e040eca40 Mon Sep 17 00:00:00 2001 From: Gregg Kellogg Date: Mon, 6 Jan 2020 12:02:04 -0800 Subject: [PATCH 2/3] Update CHANGELOG --- CHANGELOG.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 65defd3f..abdf87de 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,10 +1,12 @@ # jsonld ChangeLog +### Fixed +- More support for "@type": "@none" + ## 2.0.1 - 2019-12-10 ### Fixed - JSON literal value handling issues. -- More support for "@type": "@none" ## 2.0.0 - 2019-12-09 From 32c0e87b35e00332e7e815f2c08fc1bae6b15324 Mon Sep 17 00:00:00 2001 From: Gregg Kellogg Date: Mon, 6 Jan 2020 12:05:18 -0800 Subject: [PATCH 3/3] Apply suggestions from @davidlehn code review Co-Authored-By: David I. Lehn --- lib/context.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/context.js b/lib/context.js index c802253a..09aedfd9 100644 --- a/lib/context.js +++ b/lib/context.js @@ -530,8 +530,8 @@ api.createTermDefinition = ({ if((type === '@json' || type === '@none')) { if(api.processingMode(activeCtx, 1.0)) { throw new JsonLdError( - 'Invalid JSON-LD syntax; an @context @type value not be ' + - '#{type} in JSON-LD 1.0 mode.', + 'Invalid JSON-LD syntax; an @context @type value must not be ' + + `"${type}" in JSON-LD 1.0 mode.`, 'jsonld.SyntaxError', {code: 'invalid type mapping', context: localCtx}); }