Skip to content

Commit

Permalink
Dont add @person to a stakeholder if the output is in json format…
Browse files Browse the repository at this point in the history
…. Also made the transformer more generic with `ToJsonTransformer` as name
  • Loading branch information
KristofVDB1 committed Mar 13, 2024
1 parent ee023b2 commit e117253
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { writeFile } from 'fs/promises';
import type { IService } from '@oslo-flanders/core';
import type { StakeholdersDocument } from '@oslo-converter-stakeholders/interfaces/StakeholdersDocument';
import type { StakeholdersDocument, Stakeholder } from '@oslo-converter-stakeholders/interfaces/StakeholdersDocument';
import { fetchFileOrUrl, Logger, ServiceIdentifier } from '@oslo-flanders/core';

import { parse } from 'csv-parse';
Expand All @@ -10,7 +10,7 @@ import {
} from '@oslo-converter-stakeholders/config/StakeholdersConversionServiceConfiguration';
import { ContributorType } from '@oslo-converter-stakeholders/enums/ContributorType';
import { context } from '@oslo-converter-stakeholders/utils/JsonLdContext';
import { ToJsonLdTransformer } from '@oslo-converter-stakeholders/utils/ToJsonLdTransformer';
import { ToJsonTransformer } from '@oslo-converter-stakeholders/utils/ToJsonTransformer';
@injectable()
export class StakeholdersConversionService implements IService {
public readonly logger: Logger;
Expand All @@ -29,7 +29,7 @@ export class StakeholdersConversionService implements IService {
}

// helper methods for creating the StakeholdersDocument in the different output formats
private createJsonLdDocument(authors: object[], contributors: object[], editors: object[]): StakeholdersDocument {
private createJsonLdDocument(authors: Stakeholder[], contributors: Stakeholder[], editors: Stakeholder[]): StakeholdersDocument {
const doc: StakeholdersDocument = {};
doc['@context'] = context;
doc.contributors = contributors;
Expand All @@ -38,15 +38,15 @@ export class StakeholdersConversionService implements IService {
return doc;
}

private createJsonDocument(authors: object[], contributors: object[], editors: object[]): StakeholdersDocument {
private createJsonDocument(authors: Stakeholder[], contributors: Stakeholder[], editors: Stakeholder[]): StakeholdersDocument {
const doc: StakeholdersDocument = {};
doc.contributors = contributors;
doc.authors = authors;
doc.editors = editors;
return doc;
}

private createDocument(authors: object[], contributors: object[], editors: object[]): StakeholdersDocument {
private createDocument(authors: Stakeholder[], contributors: Stakeholder[], editors: Stakeholder[]): StakeholdersDocument {
switch (this.configuration.outputFormat) {
case 'application/json':
return this.createJsonDocument(authors, contributors, editors);
Expand All @@ -57,13 +57,13 @@ export class StakeholdersConversionService implements IService {
}
}

private async parseData(data: Buffer): Promise<{ authors: object[], contributors: object[], editors: object[] }> {
private async parseData(data: Buffer): Promise<{ authors: Stakeholder[], contributors: Stakeholder[], editors: Stakeholder[] }> {
const parser = parse({ delimiter: ';', columns: true });
const transformer = new ToJsonLdTransformer();
const transformer = new ToJsonTransformer(this.configuration.outputFormat);

const contributors: object[] = [];
const authors: object[] = [];
const editors: object[] = [];
const contributors: Stakeholder[] = [];
const authors: Stakeholder[] = [];
const editors: Stakeholder[] = [];

await new Promise<void>((resolve, reject) => {
parser.pipe(transformer)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
import type { ContributorType } from '@oslo-converter-stakeholders/enums/ContributorType';

export interface StakeholdersDocument {
'@context'?: object;
contributors?: object[];
authors?: object[];
editors?: object[];
contributors?: Stakeholder[];
authors?: Stakeholder[];
editors?: Stakeholder[];
}

export interface Stakeholder {
'@type'?: string;
firstName: string;
lastName: string;
affiliation: {
affiliationName: string;
homepage?: string;
};
email: string;
contributorType: ContributorType;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import type { TransformCallback } from 'stream';
import { Transform } from 'stream';
import { ContributorType } from '@oslo-converter-stakeholders/enums/ContributorType';

export class ToJsonLdTransformer extends Transform {
export class ToJsonTransformer extends Transform {
private readonly columnNames = ['Voornaam', 'Naam', 'Affiliatie', 'E-mail', 'Website'];
private readonly outputFormat: string;

public constructor() {
public constructor(outputFormat: string) {
super({
objectMode: true,
});
this.outputFormat = outputFormat;
}

public _transform(chunk: any, encoding: string, callback: TransformCallback): void {
Expand All @@ -18,7 +20,11 @@ export class ToJsonLdTransformer extends Transform {
private createContributor(data: any): any {
const contributor: any = {};

contributor['@type'] = 'Person';
// If the output format is JSON-LD, we need to add the @type attribute
if (this.outputFormat === 'application/ld+json') {
contributor['@type'] = 'Person';
}

contributor.firstName = data.Voornaam;
contributor.lastName = data.Naam;
contributor.affiliation = {};
Expand Down

0 comments on commit e117253

Please sign in to comment.