Skip to content

Commit 0434150

Browse files
authored
Allow hash keys to be URIs that dereference to a string
Closes #115
1 parent f3df62f commit 0434150

File tree

4 files changed

+52
-19
lines changed

4 files changed

+52
-19
lines changed

lib/construction/argument/ArgumentConstructorHandlerHash.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,16 @@ export class ArgumentConstructorHandlerHash implements IArgumentConstructorHandl
2929
if (!entry.property.key) {
3030
throw new ErrorResourcesContext(`Missing key in fields entry`, { entry, argument });
3131
}
32-
if (entry.property.key.type !== 'Literal') {
33-
throw new ErrorResourcesContext(`Illegal non-literal key (${entry.property.key.value} as ${entry.property.key.type}) in fields entry`, { entry, argument });
32+
33+
const key = await argsCreator.getArgumentValues(entry.properties.key, settings);
34+
if (typeof key !== 'string') {
35+
throw new ErrorResourcesContext(`Illegal non-string key (${entry.property.key.value} as ${entry.property.key.type}) in fields entry`, { entry, argument });
3436
}
3537

3638
// Recursively get value arg value
3739
if (entry.property.value) {
3840
const subValue = await argsCreator.getArgumentValues(entry.properties.value, settings);
39-
return { key: entry.property.key.value, value: subValue };
41+
return { key, value: subValue };
4042
}
4143

4244
// Ignore cases where value may not be set, because params may be optional

lib/construction/strategy/ConstructionStrategyCommonJsString.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ export class ConstructionStrategyCommonJsString implements IConstructionStrategy
9494
}
9595
sb.push('\n');
9696
sb.push(' ');
97-
sb.push(`'${entry.key}'`);
97+
sb.push(`${entry.key}`);
9898
sb.push(': ');
9999
sb.push(entry.value);
100100
}

test/unit/construction/ConfigConstructor-test.ts

+34-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const DF = new DataFactory();
1212
describe('ConfigConstructor', () => {
1313
let objectLoader: RdfObjectLoader;
1414
let componentResources: Record<string, Resource>;
15-
let configConstructorPool: ConfigConstructorPool<any>;
15+
let configConstructorPool: jest.Mocked<ConfigConstructorPool<any>>;
1616
let constructor: ConfigConstructor<any>;
1717
let constructionStrategy: IConstructionStrategy<any>;
1818
let moduleState: IModuleState;
@@ -224,7 +224,7 @@ describe('ConfigConstructor', () => {
224224
.toThrowError(/^Missing key in fields entry/u);
225225
});
226226

227-
it('should throw on IRI key', async() => {
227+
it('can use dereferenced IRI values as keys', async() => {
228228
const resource = objectLoader.createCompactedResource({
229229
fields: {
230230
list: [
@@ -235,8 +235,39 @@ describe('ConfigConstructor', () => {
235235
],
236236
},
237237
});
238+
expect(await constructor.getArgumentValue(resource, settings)).toEqual({
239+
entries: [
240+
{
241+
key: 'INSTANCE',
242+
value: 'ABC',
243+
},
244+
],
245+
});
246+
expect(constructionStrategy.createHash).toHaveBeenCalledWith({
247+
settings,
248+
entries: [
249+
{
250+
key: 'INSTANCE',
251+
value: 'ABC',
252+
},
253+
],
254+
});
255+
});
256+
257+
it('should throw on non-string keys', async() => {
258+
configConstructorPool.instantiate.mockResolvedValueOnce(new Error('this is an object'));
259+
const resource = objectLoader.createCompactedResource({
260+
fields: {
261+
list: [
262+
{
263+
key: `ex:abc`,
264+
value: '"ABC"',
265+
},
266+
],
267+
},
268+
});
238269
await expect(constructor.getArgumentValue(resource, settings)).rejects
239-
.toThrowError(/^Illegal non-literal key \(ex:abc as NamedNode\) in fields entry/u);
270+
.toThrowError(/^Illegal non-string key \(ex:abc as NamedNode\) in fields entry/u);
240271
});
241272

242273
it('should ignore fields without value', async() => {

test/unit/construction/strategy/ConstructionStrategyCommonJsString-test.ts

+12-12
Original file line numberDiff line numberDiff line change
@@ -290,39 +290,39 @@ module.exports = urn_comunica_sparqlinit;
290290
expect(constructionStrategy.createHash({
291291
settings,
292292
entries: [
293-
{ key: 'a', value: '1' },
294-
{ key: 'b', value: '2' },
295-
{ key: 'c', value: '3' },
293+
{ key: '"a"', value: '1' },
294+
{ key: '"b"', value: '2' },
295+
{ key: '"c"', value: '3' },
296296
],
297297
})).toEqual(`{
298-
'a': 1,
299-
'b': 2,
300-
'c': 3
298+
"a": 1,
299+
"b": 2,
300+
"c": 3
301301
}`);
302302
});
303303

304304
it('for defined and undefined entries', () => {
305305
expect(constructionStrategy.createHash({
306306
settings,
307307
entries: [
308-
{ key: 'a', value: '1' },
308+
{ key: '"a"', value: '1' },
309309
undefined,
310-
{ key: 'c', value: '3' },
310+
{ key: '"c"', value: '3' },
311311
],
312312
})).toEqual(`{
313-
'a': 1,
314-
'c': 3
313+
"a": 1,
314+
"c": 3
315315
}`);
316316
});
317317

318318
it('for defined entries with array values', () => {
319319
expect(constructionStrategy.createHash({
320320
settings,
321321
entries: [
322-
{ key: 'a', value: '[\n a,\n b\n]' },
322+
{ key: '"a"', value: '[\n a,\n b\n]' },
323323
],
324324
})).toEqual(`{
325-
'a': [
325+
"a": [
326326
a,
327327
b
328328
]

0 commit comments

Comments
 (0)