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. 28
      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 {
const groups = new Map<string, dom.PropertyDeclaration[]>();
props.forEach((p) => {
const base = p.name.replace(/\d+$/, "");
if (!groups.has(base)) groups.set(base, []);
groups.get(base)!.push(p);
const name = p.name;
if (!groups.has(name)) groups.set(name, []);
groups.get(name)!.push(p);
});
groups.forEach((list) => {
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
list.forEach((prop) => {
const iri = predicateIriByProp.get(prop) || prop.name;
const iri = predicateIriByProp.get(prop);
if (!iri) return;
const segs = iri.split(/[#:\/]/).filter(Boolean);
if (!segs.length) return;
if (segs.length < 2) return;
const local = segs.at(-1)!;
const secondLast = segs.length > 1 ? segs.at(-2)! : undefined;
if (secondLast) {
prop.name = `${secondLast}_${local}`;
}
const secondLast = segs.at(-2)!;
prop.name = `${secondLast}_${local}`;
});
// Detect any remaining duplicates after first pass
const nameCounts = new Map<string, number>();
@ -89,7 +96,8 @@ function resolveCollisions(props: dom.PropertyDeclaration[]): void {
);
list.forEach((p) => {
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 = {
name: "plural anonymous",
shexc: `
PREFIX ex: <http://ex/>
PREFIX ex: <http://ex.org/>
ex:ConfigHolderShape { ex:configs { ex:key . ; ex:val . }* }
`,
sampleTurtle: ``,
@ -15,16 +15,16 @@ export const pluralAnonymous: TestData = {
export interface ConfigHolder {
id?: IRI;
/**
* Original IRI: http://ex/configs
* Original IRI: http://ex.org/configs
*/
configs?: Record<IRI, {
id?: IRI;
/**
* Original IRI: http://ex/key
* Original IRI: http://ex.org/key
*/
key: any;
/**
* Original IRI: http://ex/val
* Original IRI: http://ex.org/val
*/
val: any;
}>;

@ -14,5 +14,30 @@ export const propertyCollision: TestData = {
baseNode: "http://ex/c1",
successfulContext: {} as any,
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