fix collision property issues

main
Laurin Weger 3 weeks ago
parent bdce4700f6
commit c27f939a37
No known key found for this signature in database
GPG Key ID: 9B372BB0B792770F
  1. 26
      packages/schema-converter-shex/src/typing/ShexJTypingTransformerCompact.ts
  2. 8
      packages/schema-converter-shex/test/testData/pluralAnonymous.ts
  3. 27
      packages/schema-converter-shex/test/testData/propertyCollision.ts

@ -65,22 +65,29 @@ const predicateIriByProp = new WeakMap<dom.PropertyDeclaration, string>();
function resolveCollisions(props: dom.PropertyDeclaration[]): void { function resolveCollisions(props: dom.PropertyDeclaration[]): void {
const groups = new Map<string, dom.PropertyDeclaration[]>(); const groups = new Map<string, dom.PropertyDeclaration[]>();
props.forEach((p) => { props.forEach((p) => {
const base = p.name.replace(/\d+$/, ""); const name = p.name;
if (!groups.has(base)) groups.set(base, []); if (!groups.has(name)) groups.set(name, []);
groups.get(base)!.push(p); groups.get(name)!.push(p);
}); });
groups.forEach((list) => { groups.forEach((list) => {
if (list.length < 2) return; if (list.length < 2) return;
const predicateIris = new Set(
list.map((p) => predicateIriByProp.get(p)).filter(Boolean)
);
if (predicateIris.size < 2) {
return;
}
// First pass rename using second last segment // First pass rename using second last segment
list.forEach((prop) => { list.forEach((prop) => {
const iri = predicateIriByProp.get(prop) || prop.name; const iri = predicateIriByProp.get(prop);
if (!iri) return;
const segs = iri.split(/[#:\/]/).filter(Boolean); const segs = iri.split(/[#:\/]/).filter(Boolean);
if (!segs.length) return; if (segs.length < 2) return;
const local = segs.at(-1)!; const local = segs.at(-1)!;
const secondLast = segs.length > 1 ? segs.at(-2)! : undefined; const secondLast = segs.at(-2)!;
if (secondLast) {
prop.name = `${secondLast}_${local}`; prop.name = `${secondLast}_${local}`;
}
}); });
// Detect any remaining duplicates after first pass // Detect any remaining duplicates after first pass
const nameCounts = new Map<string, number>(); const nameCounts = new Map<string, number>();
@ -89,7 +96,8 @@ function resolveCollisions(props: dom.PropertyDeclaration[]): void {
); );
list.forEach((p) => { list.forEach((p) => {
if (nameCounts.get(p.name)! > 1) { if (nameCounts.get(p.name)! > 1) {
p.name = predicateIriByProp.get(p) || p.name; const iri = predicateIriByProp.get(p);
if (iri) p.name = iri;
} }
}); });
}); });

@ -3,7 +3,7 @@ import type { TestData } from "./testData.js";
export const pluralAnonymous: TestData = { export const pluralAnonymous: TestData = {
name: "plural anonymous", name: "plural anonymous",
shexc: ` shexc: `
PREFIX ex: <http://ex/> PREFIX ex: <http://ex.org/>
ex:ConfigHolderShape { ex:configs { ex:key . ; ex:val . }* } ex:ConfigHolderShape { ex:configs { ex:key . ; ex:val . }* }
`, `,
sampleTurtle: ``, sampleTurtle: ``,
@ -15,16 +15,16 @@ export const pluralAnonymous: TestData = {
export interface ConfigHolder { export interface ConfigHolder {
id?: IRI; id?: IRI;
/** /**
* Original IRI: http://ex/configs * Original IRI: http://ex.org/configs
*/ */
configs?: Record<IRI, { configs?: Record<IRI, {
id?: IRI; id?: IRI;
/** /**
* Original IRI: http://ex/key * Original IRI: http://ex.org/key
*/ */
key: any; key: any;
/** /**
* Original IRI: http://ex/val * Original IRI: http://ex.org/val
*/ */
val: any; val: any;
}>; }>;

@ -14,5 +14,30 @@ export const propertyCollision: TestData = {
baseNode: "http://ex/c1", baseNode: "http://ex/c1",
successfulContext: {} as any, successfulContext: {} as any,
successfulTypings: "", successfulTypings: "",
successfulCompactTypings: `export type IRI = string;\n\nexport interface C {\n id?: IRI;\n /**\n * Original IRI: http://ex/label\n */\n ex_label: any;\n /**\n * Original IRI: http://ex2/label\n */\n ex2_label: any;\n /**\n * Original IRI: http://xmlns.com/foaf/0.1/label\n */\n "0.1_label": any;\n /**\n * Original IRI: http://example.com/v1#label\n */\n v1_label: any;\n /**\n * Original IRI: http://api.example.com/v2.1:label\n */\n "v2.1:label": any;\n}\n\n`, successfulCompactTypings: `export type IRI = string;
export interface C {
id?: IRI;
/**
* Original IRI: http://ex/label
*/
label: any;
/**
* Original IRI: http://ex2/label
*/
label2: any;
/**
* Original IRI: http://xmlns.com/foaf/0.1/label
*/
label3: any;
/**
* Original IRI: http://example.com/v1#label
*/
label4: any;
/**
* Original IRI: http://api.example.com/v2.1:label
*/
"v2.1:label": any;
}
`,
}; };

Loading…
Cancel
Save