Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

174 changes: 87 additions & 87 deletions examples/python-r4-us-core/fhir_types/README.md

Large diffs are not rendered by default.

176 changes: 88 additions & 88 deletions examples/typescript-r4-us-core/fhir-types/README.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"minimatch": ">=10.2.3",
"rollup": ">=4.59.0",
"smol-toml": ">=1.6.1",
"brace-expansion": ">=5.0.6",
"brace-expansion": ">=5.0.7",
"picomatch": ">=4.0.4",
"esbuild": ">=0.28.1"
}
Expand Down
6 changes: 6 additions & 0 deletions src/api/writer-generator/introspection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,17 @@ export class IntrospectionWriter extends FileSystemWriter<IntrospectionWriterOpt
);
}

// Dump only schemas that survived IR transformations (e.g. tree shaking),
// mirroring the typeSchemas output.
const indexUrls = new Set<string>(tsIndex.schemas.map((ts) => ts.identifier.url));

if (this.opts.fhirSchemas && tsIndex.register) {
const outputPath = this.opts.fhirSchemas;
const allFs = tsIndex.register.allFs();
// Deduplicate FHIR schemas by URL (same schema can appear from different packages)
const seenUrls = new Set<string>();
const fhirSchemas = allFs.filter((fs) => {
if (!indexUrls.has(fs.url)) return false;
if (seenUrls.has(fs.url)) return false;
seenUrls.add(fs.url);
return true;
Expand All @@ -142,6 +147,7 @@ export class IntrospectionWriter extends FileSystemWriter<IntrospectionWriterOpt
// Deduplicate SDs by URL (same SD can appear multiple times from different packages)
const seenUrls = new Set<string>();
const structureDefinitions = allSd.filter((sd) => {
if (!indexUrls.has(sd.url)) return false;
if (seenUrls.has(sd.url)) return false;
seenUrls.add(sd.url);
return true;
Expand Down
31 changes: 31 additions & 0 deletions src/typeschema/collision-order.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
type CollisionSource = {
sourceCanonical: string;
sourcePackage: string;
};

type CollisionVariant = {
schemaHash: string;
sources: readonly CollisionSource[];
};

const compareStrings = (left: string, right: string): number => {
if (left < right) return -1;
if (left > right) return 1;
return 0;
};

export const compareCollisionSources = (left: CollisionSource, right: CollisionSource): number =>
compareStrings(left.sourcePackage, right.sourcePackage) ||
compareStrings(left.sourceCanonical, right.sourceCanonical);

const collisionSourcesKey = (sources: readonly CollisionSource[]): string =>
JSON.stringify(
[...sources]
.sort(compareCollisionSources)
.map(({ sourcePackage, sourceCanonical }) => [sourcePackage, sourceCanonical]),
);

export const compareCollisionVariants = (left: CollisionVariant, right: CollisionVariant): number =>
right.sources.length - left.sources.length ||
compareStrings(collisionSourcesKey(left.sources), collisionSourcesKey(right.sources)) ||
compareStrings(left.schemaHash, right.schemaHash);
9 changes: 8 additions & 1 deletion src/typeschema/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*/

import type { CodegenLog } from "@root/utils/log";
import { compareCollisionSources, compareCollisionVariants } from "./collision-order";
import { transformFhirSchema, transformValueSet } from "./core/transformer";
import type { ResolveCollisionsConf, TypeSchemaCollisions } from "./ir/types";
import type { Register } from "./register";
Expand Down Expand Up @@ -54,7 +55,13 @@ const deduplicateSchemas = (
const collisions: TypeSchemaCollisions = {};

for (const versions of Object.values(groups)) {
const sorted = Object.values(versions).sort((a, b) => b.sources.length - a.sources.length);
const sorted = Object.entries(versions)
.map(([schemaHash, version]) => ({
...version,
schemaHash,
sources: [...version.sources].sort(compareCollisionSources),
}))
.sort(compareCollisionVariants);
const best = sorted[0];
if (!best) continue;

Expand Down
14 changes: 11 additions & 3 deletions src/typeschema/ir/report.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { CanonicalUrl, PkgName } from "@root/typeschema/types";
import { extractNameFromCanonical } from "@root/typeschema/types";
import { extractNameFromCanonical, hashSchema } from "@root/typeschema/types";
import { compareCollisionSources, compareCollisionVariants } from "../collision-order";
import type {
CollisionResolution,
IrReport,
Expand Down Expand Up @@ -68,12 +69,19 @@ type VersionGroup = { entries: CollisionEntry[]; mark: VersionMark };
const groupCollisionVersions = (entries: CollisionEntry[], resolution?: CollisionResolution): VersionGroup[] => {
const uniqueSchemas = new Map<string, CollisionEntry[]>();
for (const entry of entries) {
const key = JSON.stringify(entry.typeSchema);
const key = hashSchema(entry.typeSchema);
if (!uniqueSchemas.has(key)) uniqueSchemas.set(key, []);
uniqueSchemas.get(key)?.push(entry);
}

const sorted = [...uniqueSchemas.values()].sort((a, b) => b.length - a.length);
const sorted = [...uniqueSchemas.entries()]
.map(([schemaHash, group]) => ({
entries: [...group].sort(compareCollisionSources),
schemaHash,
sources: group,
}))
.sort(compareCollisionVariants)
.map((group) => group.entries);
const markVersion = (group: CollisionEntry[], i: number): VersionMark => {
if (resolution)
return group.some(
Expand Down
4 changes: 2 additions & 2 deletions src/typeschema/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,8 @@ export const registerFromManager = async (
if (!sd.package_name) {
return {
...sd,
package_name: pkgIndex.pkg.name,
package_version: pkgIndex.pkg.version,
package_name: r.pkg.name,
package_version: r.pkg.version,
};
}
return sd;
Expand Down
Loading
Loading