Skip to content

Expand expansion map to support calling when a relative IRI is detected #452

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Jun 16, 2021
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 78 additions & 9 deletions lib/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -1041,19 +1041,88 @@ function _expandIri(activeCtx, value, relativeTo, localCtx, defined, options) {
}
}

// prepend vocab
if(relativeTo.vocab && '@vocab' in activeCtx) {
return activeCtx['@vocab'] + value;
}
// prepend vocab
const prependedResult = activeCtx['@vocab'] + value;
let expansionMapResult = undefined;
if(options && options.expansionMap) {
// if we are about to expand the value by prepending
// @vocab then call the expansion map to inform
// interested callers that this is occurring

// TODO: use `await` to support async
expansionMapResult = options.expansionMap({
prependedIri: {
type: '@vocab',
vocab: activeCtx['@vocab'],
value,
result: prependedResult
},
activeCtx,
options
});

// prepend base
if(relativeTo.base && '@base' in activeCtx) {
if(activeCtx['@base']) {
// The null case preserves value as potentially relative
return prependBase(prependBase(options.base, activeCtx['@base']), value);
}
if(expansionMapResult !== undefined) {
value = expansionMapResult;
} else {
// the null case preserves value as potentially relative
value = prependedResult;
}
} else if(relativeTo.base) {
return prependBase(options.base, value);
// prepend base
let prependedResult;
let expansionMapResult;
let base;
if('@base' in activeCtx) {
if(activeCtx['@base']) {
base = prependBase(options.base, activeCtx['@base']);
prependedResult = prependBase(base, value);
}
} else {
base = options.base;
prependedResult = prependBase(options.base, value);
}
if(options && options.expansionMap) {
// if we are about to expand the value by pre-pending
// @base then call the expansion map to inform
// interested callers that this is occurring

// TODO: use `await` to support async
expansionMapResult = options.expansionMap({
prependedIri: {
type: '@base',
base,
value,
result: prependedResult
},
activeCtx,
options
});
}
if(expansionMapResult !== undefined) {
value = expansionMapResult;
} else if(prependedResult !== undefined) {
// the null case preserves value as potentially relative
value = prependedResult;
}
}

if(!_isAbsoluteIri(value) && options && options.expansionMap) {
// if the result of the expansion is not an absolute iri then
// call the expansion map to inform interested callers that
// the resulting value is a relative iri, which can result in
// it being dropped when converting to other RDF representations

// TODO: use `await` to support async
const expandedResult = options.expansionMap({
relativeIri: value,
activeCtx,
options
});
if(expandedResult !== undefined) {
value = expandedResult;
}
}

return value;
Expand Down
7 changes: 7 additions & 0 deletions lib/expand.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ api.expand = async ({
typeScopedContext = null,
expansionMap = () => undefined
}) => {

// add expansion map to the processing options
options = {...options, expansionMap};

// nothing to expand
if(element === null || element === undefined) {
return null;
Expand Down Expand Up @@ -420,6 +424,9 @@ async function _expandObject({
const nests = [];
let unexpandedValue;

// add expansion map to the processing options
options = {...options, expansionMap};

// Figure out if this is the type for a JSON literal
const isJsonType = element[typeKey] &&
_expandIri(activeCtx,
Expand Down
2 changes: 1 addition & 1 deletion lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ api.parseLinkHeader = header => {
while((match = REGEX_LINK_HEADER_PARAMS.exec(params))) {
result[match[1]] = (match[2] === undefined) ? match[3] : match[2];
}
const rel = result['rel'] || '';
const rel = result.rel || '';
if(Array.isArray(rval[rel])) {
rval[rel].push(result);
} else if(rval.hasOwnProperty(rel)) {
Expand Down
Loading