Skip to content

Commit 02bd0a1

Browse files
gkelloggdavidlehn
authored andcommitted
Adds support for @included in compaction, expansion and flattening.
Coded added for framing, but not enabled due to big backlog on framing in general.
1 parent edae3d9 commit 02bd0a1

File tree

7 files changed

+59
-36
lines changed

7 files changed

+59
-36
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535

3636
## 2.0.1 - 2019-12-10
3737

38+
### Added
39+
- Added support for `@included` blocks
40+
3841
### Fixed
3942
- JSON literal value handling issues.
4043

lib/compact.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,10 @@ api.compact = async ({
343343
continue;
344344
}
345345

346-
// skip array processing for keywords that aren't @graph or @list
346+
// skip array processing for keywords that aren't
347+
// @graph, @list, or @included
347348
if(expandedProperty !== '@graph' && expandedProperty !== '@list' &&
349+
expandedProperty !== '@included' &&
348350
_isKeyword(expandedProperty)) {
349351
// use keyword alias and add value as is
350352
const alias = api.compactIri({

lib/context.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,6 +1230,7 @@ api.isKeyword = v => {
12301230
case '@explicit':
12311231
case '@graph':
12321232
case '@id':
1233+
case '@included':
12331234
case '@index':
12341235
case '@json':
12351236
case '@language':

lib/expand.js

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ const {
1616
const {
1717
isList: _isList,
1818
isValue: _isValue,
19-
isGraph: _isGraph
19+
isGraph: _isGraph,
20+
isSubject: _isSubject
2021
} = require('./graphTypes');
2122

2223
const {
@@ -251,8 +252,8 @@ api.expand = async ({
251252
expandedParent: rval,
252253
options,
253254
insideList,
254-
typeScopedContext,
255255
typeKey,
256+
typeScopedContext,
256257
expansionMap});
257258

258259
// get property count on expanded output
@@ -392,6 +393,7 @@ api.expand = async ({
392393
* @param expandedParent the expanded result into which to add values.
393394
* @param options the expansion options.
394395
* @param insideList true if the element is a list, false if not.
396+
* @param typeKey first key found expanding to @type.
395397
* @param typeScopedContext the context before reverting.
396398
* @param expansionMap(info) a function that can be used to custom map
397399
* unmappable values (or to throw an error when they are detected);
@@ -406,8 +408,8 @@ async function _expandObject({
406408
expandedParent,
407409
options = {},
408410
insideList,
409-
typeScopedContext,
410411
typeKey,
412+
typeScopedContext,
411413
expansionMap
412414
}) {
413415
const keys = Object.keys(element).sort();
@@ -459,8 +461,9 @@ async function _expandObject({
459461
{code: 'invalid reverse property map', value});
460462
}
461463
if(expandedProperty in expandedParent &&
462-
expandedProperty !== '@included' &&
463-
expandedProperty !== '@type') {
464+
expandedProperty !== '@included' &&
465+
expandedProperty !== '@type')
466+
{
464467
throw new JsonLdError(
465468
'Invalid JSON-LD syntax; colliding keywords detected.',
466469
'jsonld.SyntaxError',
@@ -519,6 +522,31 @@ async function _expandObject({
519522
continue;
520523
}
521524

525+
// Included blocks are treated as an array of separate object nodes sharing
526+
// the same referencing active_property.
527+
// For 1.0, it is skipped as are other unknown keywords
528+
if(expandedProperty === '@included' && _processingMode(activeCtx, 1.1)) {
529+
const includedResult = _asArray(await api.expand({
530+
activeCtx,
531+
activeProperty,
532+
element: value,
533+
options,
534+
expansionMap
535+
}));
536+
537+
// Expanded values must be node objects
538+
if(!includedResult.every(v => _isSubject(v))) {
539+
throw new JsonLdError(
540+
'Invalid JSON-LD syntax; ' +
541+
'values of @included must expand to node objects.',
542+
'jsonld.SyntaxError', {code: 'invalid @included value', value});
543+
}
544+
545+
_addValue(
546+
expandedParent, '@included', includedResult, {propertyIsArray: true});
547+
continue;
548+
}
549+
522550
// @graph must be an array or an object
523551
if(expandedProperty === '@graph' &&
524552
!(_isObject(value) || _isArray(value))) {
@@ -632,6 +660,11 @@ async function _expandObject({
632660
continue;
633661
}
634662

663+
// skip unknown keywords
664+
//if(expandedProperty.startsWith('@')) {
665+
// continue;
666+
//}
667+
635668
// use potential scoped context for key
636669
let termCtx = activeCtx;
637670
const ctx = _getContextValue(activeCtx, key, '@context');
@@ -825,6 +858,7 @@ async function _expandObject({
825858
insideList,
826859
typeScopedContext,
827860
typeKey,
861+
typeScopedContext,
828862
expansionMap});
829863
}
830864
}

lib/frame.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,13 @@ api.frame = (state, subjects, frame, parent, property = null) => {
167167
}
168168
}
169169

170+
// if frame has @included, recurse over its sub-frame
171+
if('@included' in frame) {
172+
api.frame(
173+
state,
174+
subjects, frame['@included'], output, '@included');
175+
}
176+
170177
// iterate over subject properties
171178
for(const prop of Object.keys(subject).sort()) {
172179
// copy keywords to output

lib/nodeMap.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,12 @@ api.createNodeMap = (input, graphs, graph, issuer, name, list) => {
145145
continue;
146146
}
147147

148+
// recurse into included
149+
if(property === '@included') {
150+
api.createNodeMap(input[property], graphs, graph, issuer);
151+
continue;
152+
}
153+
148154
// copy non-@type keywords
149155
if(property !== '@type' && isKeyword(property)) {
150156
if(property === '@index' && property in subject &&

tests/test-common.js

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,6 @@ const TEST_TYPES = {
3333
specVersion: ['json-ld-1.0'],
3434
// FIXME
3535
idRegex: [
36-
// included
37-
/compact-manifest.jsonld#tin01$/,
38-
/compact-manifest.jsonld#tin02$/,
39-
/compact-manifest.jsonld#tin03$/,
40-
/compact-manifest.jsonld#tin04$/,
41-
/compact-manifest.jsonld#tin05$/,
4236
// direction
4337
/compact-manifest.jsonld#tdi01$/,
4438
/compact-manifest.jsonld#tdi02$/,
@@ -105,16 +99,6 @@ const TEST_TYPES = {
10599
/remote-doc-manifest.jsonld#t0013$/, // HTML
106100
// colliding keywords
107101
/expand-manifest.jsonld#t0114$/,
108-
// included
109-
/expand-manifest.jsonld#tin01$/,
110-
/expand-manifest.jsonld#tin02$/,
111-
/expand-manifest.jsonld#tin03$/,
112-
/expand-manifest.jsonld#tin04$/,
113-
/expand-manifest.jsonld#tin05$/,
114-
/expand-manifest.jsonld#tin06$/,
115-
/expand-manifest.jsonld#tin07$/,
116-
/expand-manifest.jsonld#tin08$/,
117-
/expand-manifest.jsonld#tin09$/,
118102
// keywords
119103
/expand-manifest.jsonld#tpr30$/,
120104
/expand-manifest.jsonld#tpr31$/,
@@ -159,13 +143,6 @@ const TEST_TYPES = {
159143
/html-manifest.jsonld#tf002$/,
160144
/html-manifest.jsonld#tf003$/,
161145
/html-manifest.jsonld#tf004$/,
162-
// included
163-
/flatten-manifest.jsonld#tin01$/,
164-
/flatten-manifest.jsonld#tin02$/,
165-
/flatten-manifest.jsonld#tin03$/,
166-
/flatten-manifest.jsonld#tin04$/,
167-
/flatten-manifest.jsonld#tin05$/,
168-
/flatten-manifest.jsonld#tin06$/,
169146
]
170147
},
171148
fn: 'flatten',
@@ -331,13 +308,6 @@ const TEST_TYPES = {
331308
/toRdf-manifest.jsonld#te112$/,
332309
// colliding keyword
333310
/toRdf-manifest.jsonld#te114$/,
334-
// included
335-
/toRdf-manifest.jsonld#tin01$/,
336-
/toRdf-manifest.jsonld#tin02$/,
337-
/toRdf-manifest.jsonld#tin03$/,
338-
/toRdf-manifest.jsonld#tin04$/,
339-
/toRdf-manifest.jsonld#tin05$/,
340-
/toRdf-manifest.jsonld#tin06$/,
341311
// keywords
342312
/toRdf-manifest.jsonld#tpr30$/,
343313
/toRdf-manifest.jsonld#tpr31$/,

0 commit comments

Comments
 (0)