Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
tensor5 committed May 6, 2024
1 parent bd1a599 commit f93e0a9
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 74 deletions.
7 changes: 4 additions & 3 deletions src/app/components/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { useState } from "react";
import { Footer } from "./Foot";
import { validator } from "../validator";
import { set } from "lodash";
import PublicCode from "../contents/publiccode";
import PublicCode, { defaultItaly } from "../contents/publiccode";
import EditorInput from "./EditorInput";
import developmentStatus from "../contents/developmentStatus";
import EditorBoolean from "./EditorBoolean";
Expand Down Expand Up @@ -55,10 +55,11 @@ const resolver: Resolver<PublicCode> = async (values) => {
const defaultValues = {
publiccodeYmlVersion: "0.3",
legal: {},
localisation: {availableLanguages: []},
localisation: { availableLanguages: [] },
maintenance: {},
platforms: [],
categories: []
categories: [],
it: defaultItaly,
};

export default function Editor() {
Expand Down
57 changes: 53 additions & 4 deletions src/app/contents/publiccode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,18 @@ export default interface PublicCode {
it?: Italy;
}

export interface IntendedAudience {
interface IntendedAudience {
countries?: Array<string>;
unsupportedCountries?: Array<string>;
scope?: Array<(typeof scopes)[number]>;
}

export const defaultIntendedAudience: IntendedAudience = {
countries: undefined,
unsupportedCountries: undefined,
scope: undefined,
};

export interface Description {
genericName?: string;
localisedName?: string;
Expand All @@ -62,20 +68,34 @@ interface Maintenance {
contacts?: Array<Contact>;
}

export interface Contact {
interface Contact {
name: string;
email?: string;
phone?: string;
affiliation?: string;
}

export interface Contractor {
export const defaultContact: Contact = {
name: "",
email: undefined,
phone: undefined,
affiliation: undefined,
};

interface Contractor {
name: string;
until: string; // “YYYY-MM-DD”
email?: string;
website?: string;
}

export const defaultContractor: Contractor = {
name: "",
until: "",
email: undefined,
website: undefined,
};

interface Localisation {
localisationReady: boolean;
availableLanguages: Array<string>;
Expand All @@ -87,28 +107,47 @@ interface DependsOn {
hardware?: Array<Dependency>;
}

export interface Dependency {
interface Dependency {
name: string;
versionMin?: string;
versionMax?: string;
version?: string;
optional?: boolean;
}

export const defaultDependency: Dependency = {
name: "",
versionMin: undefined,
versionMax: undefined,
version: undefined,
optional: undefined,
};

export interface Italy {
countryExtensionVersion: "1.0";
conforme?: Conforme;
piattaforme?: Piattaforme;
riuso?: Riuso;
}

export const defaultItaly: Italy = {
countryExtensionVersion: "1.0",
};

interface Conforme {
lineeGuidaDesign?: boolean;
modelloInteroperabilita?: boolean;
misureMinimeSicurezza?: boolean;
gdpr?: boolean;
}

export const defaultConforme: Conforme = {
lineeGuidaDesign: undefined,
modelloInteroperabilita: undefined,
misureMinimeSicurezza: undefined,
gdpr: undefined,
};

interface Piattaforme {
spid?: boolean;
cie?: boolean;
Expand All @@ -117,6 +156,16 @@ interface Piattaforme {
io?: boolean;
}

export const defaultPiattaforme: Piattaforme = {
spid: undefined,
cie: undefined,
anpr: undefined,
pagopa: undefined,
io: undefined,
};

interface Riuso {
codiceIPA: string;
}

export const defaultRiuso: Riuso = { codiceIPA: "" };
107 changes: 40 additions & 67 deletions src/app/linter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,24 @@ import {
cloneDeep,
isArray,
isEmpty,
isEqual,
isObjectLike,
isUndefined,
mapValues,
} from "lodash";
import PublicCode, {
Contact,
Contractor,
Dependency,
Description,
IntendedAudience,
Italy,
defaultConforme,
defaultContact,
defaultContractor,
defaultDependency,
defaultIntendedAudience,
defaultItaly,
defaultPiattaforme,
defaultRiuso,
} from "./contents/publiccode";

// PublicCode keys sorted as in the specs
export const publicCodeKeys = [
"publiccodeYmlVersion",
"name",
"applicationSuite",
"url",
"landingURL",
"isBasedOn",
"softwareVersion",
"releaseDate",
"logo",
"platforms",
"categories",
"usedBy",
"roadmap",
"developmentStatus",
"softwareType",
"intendedAudience",
"description",
"legal",
"maintenance",
"localisation",
"dependsOn",
"it",
] as const;

function sortDescription({
genericName,
localisedName,
Expand All @@ -68,32 +47,6 @@ function sortDescription({
};
}

function sortIntendedAudience({
countries,
unsupportedCountries,
scope,
}: IntendedAudience) {
return { countries, unsupportedCountries, scope };
}

function sortContact({ name, email, phone, affiliation }: Contact) {
return { name, email, phone, affiliation };
}

function sortContractor({ name, until, email, website }: Contractor) {
return { name, until, email, website };
}

function sortDependency({
name,
versionMin,
versionMax,
version,
optional,
}: Dependency) {
return { name, versionMin, versionMax, version, optional };
}

export default function linter({
publiccodeYmlVersion,
name,
Expand Down Expand Up @@ -135,26 +88,31 @@ export default function linter({
developmentStatus,
softwareType,
intendedAudience: intendedAudience
? sortIntendedAudience(intendedAudience)
? sortAs(defaultIntendedAudience, intendedAudience)
: undefined,
description: mapValues(description, sortDescription),
legal: { license, mainCopyrightOwner, repoOwner, authorsFile },
maintenance: {
type,
contractors: contractors?.map(sortContractor),
contacts: contacts?.map(sortContact),
contractors: contractors?.map((c) => sortAs(defaultContractor, c)),
contacts: contacts?.map((c) => sortAs(defaultContact, c)),
},
localisation: {
localisationReady,
availableLanguages: clone(availableLanguages),
},
dependsOn: dependsOn
? mapValues(dependsOn, (v) => (v ? v.map(sortDependency) : undefined))
? mapValues(dependsOn, (v) =>
v ? v.map((d) => sortAs(defaultDependency, d)) : undefined
)
: undefined,
it,
it:
it === undefined || isEqual(removeEmpty(it), defaultItaly)
? undefined
: lintItaly(it),
};

return removeEmpty(cloneDeep(sortedPC));
return removeEmpty(sortedPC);
}

function removeEmpty<T>(obj: T): T {
Expand All @@ -175,11 +133,26 @@ function removeEmpty<T>(obj: T): T {
return ret;
}

function lintItaly({ conforme, piattaforme, riuso }: Italy) {
function lintItaly({
countryExtensionVersion,
conforme,
piattaforme,
riuso,
}: Italy): Italy {
return {
countryExtensionVersion: "1.0",
conforme: conforme ? {} : undefined,
piattaforme,
riuso,
countryExtensionVersion,
conforme: conforme ? sortAs(defaultConforme, conforme) : undefined,
piattaforme: piattaforme
? sortAs(defaultPiattaforme, piattaforme)
: undefined,
riuso: riuso ? sortAs(defaultRiuso, riuso) : undefined,
};
}

function sortAs<T>(template: T, obj: T): T {
const ret = cloneDeep(template);
for (const key in template) {
ret[key] = obj[key];
}
return ret;
}

0 comments on commit f93e0a9

Please sign in to comment.