refactor: Rename RepresentationPreference into ValuePreferences.

This commit is contained in:
Ruben Verborgh
2021-01-04 23:28:17 +01:00
parent 4828912593
commit 09ae959333
8 changed files with 47 additions and 39 deletions

View File

@@ -1,4 +1,5 @@
import type { Representation } from '../../ldp/representation/Representation';
import type { ValuePreferences } from '../../ldp/representation/RepresentationPreferences';
import { getLoggerFor } from '../../logging/LogUtil';
import { supportsConversion, matchesMediaType } from './ConversionUtil';
import type { RepresentationConverterArgs } from './RepresentationConverter';
@@ -34,24 +35,24 @@ export class ChainedConverter extends TypedRepresentationConverter {
return this.converters[this.converters.length - 1];
}
public async getInputTypes(): Promise<Record<string, number>> {
public async getInputTypes(): Promise<ValuePreferences> {
return this.first.getInputTypes();
}
public async getOutputTypes(): Promise<Record<string, number>> {
public async getOutputTypes(): Promise<ValuePreferences> {
return this.last.getOutputTypes();
}
public async canHandle(input: RepresentationConverterArgs): Promise<void> {
// We assume a chain can be constructed, otherwise there would be a configuration issue
// So we only check if the input can be parsed and the preferred type can be written
const inTypes = this.filterTypes(await this.first.getInputTypes());
const outTypes = this.filterTypes(await this.last.getOutputTypes());
const inTypes = this.getAcceptableTypes(await this.first.getInputTypes());
const outTypes = this.getAcceptableTypes(await this.last.getOutputTypes());
supportsConversion(input, inTypes, outTypes);
}
private filterTypes(typeVals: Record<string, number>): string[] {
return Object.keys(typeVals).filter((name): boolean => typeVals[name] > 0);
private getAcceptableTypes(preferences: ValuePreferences): string[] {
return Object.keys(preferences).filter((name): boolean => preferences[name] > 0);
}
public async handle(input: RepresentationConverterArgs): Promise<Representation> {

View File

@@ -2,8 +2,10 @@ import type { Readable } from 'stream';
import rdfSerializer from 'rdf-serialize';
import type { Representation } from '../../ldp/representation/Representation';
import { RepresentationMetadata } from '../../ldp/representation/RepresentationMetadata';
import type { RepresentationPreference } from '../../ldp/representation/RepresentationPreference';
import type { RepresentationPreferences } from '../../ldp/representation/RepresentationPreferences';
import type {
ValuePreferences,
RepresentationPreferences,
} from '../../ldp/representation/RepresentationPreferences';
import { INTERNAL_QUADS } from '../../util/ContentTypes';
import { guardStream } from '../../util/GuardedStream';
import { CONTENT_TYPE } from '../../util/Vocabularies';
@@ -15,11 +17,11 @@ import { TypedRepresentationConverter } from './TypedRepresentationConverter';
* Converts `internal/quads` to most major RDF serializations.
*/
export class QuadToRdfConverter extends TypedRepresentationConverter {
public async getInputTypes(): Promise<RepresentationPreference> {
public async getInputTypes(): Promise<ValuePreferences> {
return { [INTERNAL_QUADS]: 1 };
}
public async getOutputTypes(): Promise<RepresentationPreference> {
public async getOutputTypes(): Promise<ValuePreferences> {
return rdfSerializer.getContentTypesPrioritized();
}

View File

@@ -2,6 +2,7 @@ import { PassThrough } from 'stream';
import rdfParser from 'rdf-parse';
import type { Representation } from '../../ldp/representation/Representation';
import { RepresentationMetadata } from '../../ldp/representation/RepresentationMetadata';
import type { ValuePreferences } from '../../ldp/representation/RepresentationPreferences';
import { INTERNAL_QUADS } from '../../util/ContentTypes';
import { BadRequestHttpError } from '../../util/errors/BadRequestHttpError';
import { pipeSafely } from '../../util/StreamUtil';
@@ -13,11 +14,11 @@ import { TypedRepresentationConverter } from './TypedRepresentationConverter';
* Converts most major RDF serializations to `internal/quads`.
*/
export class RdfToQuadConverter extends TypedRepresentationConverter {
public async getInputTypes(): Promise<Record<string, number>> {
public async getInputTypes(): Promise<ValuePreferences> {
return rdfParser.getContentTypesPrioritized();
}
public async getOutputTypes(): Promise<Record<string, number>> {
public async getOutputTypes(): Promise<ValuePreferences> {
return { [INTERNAL_QUADS]: 1 };
}

View File

@@ -1,4 +1,4 @@
import type { RepresentationPreference } from '../../ldp/representation/RepresentationPreference';
import type { ValuePreferences } from '../../ldp/representation/RepresentationPreferences';
import { supportsConversion } from './ConversionUtil';
import { RepresentationConverter } from './RepresentationConverter';
import type { RepresentationConverterArgs } from './RepresentationConverter';
@@ -10,12 +10,12 @@ export abstract class TypedRepresentationConverter extends RepresentationConvert
/**
* Gets the supported input content types for this converter, mapped to a numerical priority.
*/
public abstract getInputTypes(): Promise<RepresentationPreference>;
public abstract getInputTypes(): Promise<ValuePreferences>;
/**
* Gets the supported output content types for this converter, mapped to a numerical quality.
*/
public abstract getOutputTypes(): Promise<RepresentationPreference>;
public abstract getOutputTypes(): Promise<ValuePreferences>;
/**
* Verifies whether this converter supports the input.