Skip to content

Commit

Permalink
fix: Sort preferences by descending weight.
Browse files Browse the repository at this point in the history
  • Loading branch information
RubenVerborgh committed Jan 3, 2021
1 parent de939ac commit 98bf8c1
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 13 deletions.
17 changes: 10 additions & 7 deletions src/storage/RepresentationConvertingStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,19 @@ export class RepresentationConvertingStore<T extends ResourceStore = ResourceSto
* Helper function that converts a Representation using the given args and converter,
* if the conversion is necessary and there is a converter.
*/
private async convertRepresentation(args: RepresentationConverterArgs, converter?: RepresentationConverter):
private async convertRepresentation(input: RepresentationConverterArgs, converter?: RepresentationConverter):
Promise<Representation> {
if (!converter || !args.preferences.type || this.matchesPreferences(args.representation, args.preferences)) {
return args.representation;
if (!converter || !input.preferences.type || this.matchesPreferences(input.representation, input.preferences)) {
return input.representation;
}
this.logger.debug(`Conversion needed for ${input.identifier
.path} from ${input.representation.metadata.contentType} to satisfy ${input.preferences.type
.map((pref): string => `${pref.value};q=${pref.weight}`).join(', ')}`);

const typeStr = args.preferences.type.map((pref): string => `${pref.value};q=${pref.weight}`).join(', ');
this.logger.info(`Convert ${args.identifier.path} from ${args.representation.metadata.contentType} to ${typeStr}`);

return converter.handleSafe(args);
const converted = await converter.handleSafe(input);
this.logger.info(`Converted representation for ${input.identifier
.path} from ${input.representation.metadata.contentType} to ${converted.metadata.contentType}`);
return converted;
}

/**
Expand Down
5 changes: 4 additions & 1 deletion src/storage/conversion/ConversionUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ RepresentationPreference[] => {
return { value: type, weight };
});

return weightedSupported.filter((preference): boolean => preference.weight !== 0);
// Return all non-zero preferences in descending order of weight
return weightedSupported
.filter((pref): boolean => pref.weight !== 0)
.sort((prefA, prefB): number => prefB.weight - prefA.weight);
};

/**
Expand Down
12 changes: 7 additions & 5 deletions test/unit/storage/RepresentationConvertingStore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ describe('A RepresentationConvertingStore', (): void => {
let source: ResourceStore;
let inConverter: RepresentationConverter;
let outConverter: RepresentationConverter;
const convertedIn = { metadata: {}};
const convertedOut = { metadata: {}};
const inType = 'text/turtle';
const metadata = new RepresentationMetadata({ [CONTENT_TYPE]: 'text/turtle' });
let representation: Representation;
Expand All @@ -22,8 +24,8 @@ describe('A RepresentationConvertingStore', (): void => {
setRepresentation: jest.fn(),
} as any;

inConverter = { handleSafe: jest.fn(async(): Promise<any> => 'inConvert') } as any;
outConverter = { handleSafe: jest.fn(async(): Promise<any> => 'outConvert') } as any;
inConverter = { handleSafe: jest.fn(async(): Promise<any> => convertedIn) } as any;
outConverter = { handleSafe: jest.fn(async(): Promise<any> => convertedOut) } as any;

store = new RepresentationConvertingStore(source, { inType, inConverter, outConverter });
representation = { binary: true, data: 'data', metadata } as any;
Expand Down Expand Up @@ -64,7 +66,7 @@ describe('A RepresentationConvertingStore', (): void => {
it('calls the converter if another output is preferred.', async(): Promise<void> => {
await expect(store.getRepresentation({ path: 'path' }, { type: [
{ value: 'text/plain', weight: 1 }, { value: 'text/turtle', weight: 0 },
]})).resolves.toEqual('outConvert');
]})).resolves.toEqual(convertedOut);
expect(source.getRepresentation).toHaveBeenCalledTimes(1);
expect(outConverter.handleSafe).toHaveBeenCalledTimes(1);
expect(outConverter.handleSafe).toHaveBeenLastCalledWith({
Expand Down Expand Up @@ -95,11 +97,11 @@ describe('A RepresentationConvertingStore', (): void => {

await expect(store.addResource(id, representation, 'conditions' as any)).resolves.toBeUndefined();
expect(inConverter.handleSafe).toHaveBeenCalledTimes(1);
expect(source.addResource).toHaveBeenLastCalledWith(id, 'inConvert', 'conditions');
expect(source.addResource).toHaveBeenLastCalledWith(id, convertedIn, 'conditions');

await expect(store.setRepresentation(id, representation, 'conditions' as any)).resolves.toBeUndefined();
expect(inConverter.handleSafe).toHaveBeenCalledTimes(2);
expect(source.setRepresentation).toHaveBeenLastCalledWith(id, 'inConvert', 'conditions');
expect(source.setRepresentation).toHaveBeenLastCalledWith(id, convertedIn, 'conditions');
});

it('throws an error if no content-type is provided.', async(): Promise<void> => {
Expand Down
7 changes: 7 additions & 0 deletions test/unit/storage/conversion/ConversionUtil.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ describe('ConversionUtil', (): void => {
expect(matchingMediaTypes(preferences, [ 'b/x', 'c/x' ])).toEqual([{ value: 'b/x', weight: 0.5 }]);
});

it('sorts by descending weight.', async(): Promise<void> => {
const preferences: RepresentationPreferences = { type:
[{ value: 'a/x', weight: 1 }, { value: 'b/x', weight: 0.5 }, { value: 'c/x', weight: 0.8 }]};
expect(matchingMediaTypes(preferences, [ 'a/x', 'b/x', 'c/x' ]))
.toEqual([{ value: 'a/x', weight: 1 }, { value: 'c/x', weight: 0.8 }, { value: 'b/x', weight: 0.5 }]);
});

it('errors if there are duplicate preferences.', async(): Promise<void> => {
const preferences: RepresentationPreferences =
{ type: [{ value: 'b/x', weight: 1 }, { value: 'b/x', weight: 0 }]};
Expand Down

0 comments on commit 98bf8c1

Please sign in to comment.