Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Versioned instances #1495

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
12 changes: 11 additions & 1 deletion src/export/InstanceExporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,13 @@ export class InstanceExporter implements Fishable {
}

exportInstance(fshDefinition: Instance): InstanceDefinition {
if (this.pkg.instances.some(i => i._instanceMeta.name === fshDefinition.name)) {
if (
this.pkg.instances.some(
i =>
i._instanceMeta.name === fshDefinition.name &&
i._instanceMeta.versionId === fshDefinition.versionId
)
) {
return;
}

Expand Down Expand Up @@ -815,6 +821,9 @@ export class InstanceExporter implements Fishable {
if (fshDefinition.title) {
instanceDef._instanceMeta.title = fshDefinition.title;
}
if (fshDefinition.versionId) {
instanceDef._instanceMeta.versionId = fshDefinition.versionId;
}
if (fshDefinition.description) {
instanceDef._instanceMeta.description = fshDefinition.description;
}
Expand Down Expand Up @@ -952,6 +961,7 @@ export class InstanceExporter implements Fishable {
instanceDef.resourceType === instance.resourceType &&
(instanceDef.id ?? instanceDef._instanceMeta.name) ===
(instance.id ?? instance._instanceMeta.name) &&
instanceDef._instanceMeta.versionId === instance._instanceMeta.versionId &&
instanceDef !== instance
)
) {
Expand Down
5 changes: 4 additions & 1 deletion src/fhirtypes/InstanceDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ export class InstanceDefinition {
getFileName(): string {
// Logical instances should use Binary type. See: https://fshschool.org/docs/sushi/tips/#instances-of-logical-models
const type = this._instanceMeta.sdKind === 'logical' ? 'Binary' : this.resourceType;
return sanitize(`${type}-${this.id ?? this._instanceMeta.name}.json`, {
const versionString = this._instanceMeta.versionId ? `_v${this._instanceMeta.versionId}` : '';
const filename = `${type}-${this.id ?? this._instanceMeta.name}${versionString}.json`;
return sanitize(filename, {
replacement: '-'
});
}
Expand Down Expand Up @@ -61,6 +63,7 @@ type InstanceMeta = {
sdType?: string;
sdKind?: string;
instanceOfUrl?: string;
versionId?: string;
};

// eslint-disable-next-line @typescript-eslint/no-empty-interface
Expand Down
2 changes: 2 additions & 0 deletions src/fshtypes/Instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ export class Instance extends FshEntity {
instanceOf: string;
description?: string;
usage?: InstanceUsage;
versionId?: string;
rules: (AssignmentRule | InsertRule | PathRule)[];

constructor(public name: string) {
super();
this.id = name; // init same as name
this.rules = [];
this.usage = 'Example'; // init to Example (default)
this.versionId = undefined; // init to undefined (default)
}

get constructorName() {
Expand Down
31 changes: 24 additions & 7 deletions src/import/FSHImporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -448,15 +448,29 @@ export class FSHImporter extends FSHVisitor {
const instance = new Instance(ctx.name().getText())
.withLocation(location)
.withFile(this.currentFile);
if (this.docs.some(doc => doc.instances.has(instance.name))) {
logger.error(`Skipping Instance: an Instance named ${instance.name} already exists.`, {
file: this.currentFile,
location
});
} else {
{
try {
this.parseInstance(instance, location, ctx.instanceMetadata(), ctx.instanceRule());
this.currentDoc.instances.set(instance.name, instance);
if (
this.docs.some(
doc =>
doc.instances.has(instance.name) &&
Array.from(doc.instances.values()).some(
entity => entity.name === instance.name && entity.versionId === instance.versionId
)
)
) {
const versionString = instance.versionId ? `with versionId ${instance.versionId} ` : '';
logger.error(
`Skipping Instance: an Instance named ${instance.name} ${versionString}already exists.`,
{
file: this.currentFile,
location
}
);
} else {
this.currentDoc.instances.set(instance.name, instance);
}
} catch (e) {
logger.error(e.message, instance.sourceInfo);
if (e.stack) {
Expand Down Expand Up @@ -518,6 +532,9 @@ export class FSHImporter extends FSHVisitor {
const rule = this.visitInstanceRule(instanceRule);
if (rule) {
instance.rules.push(rule);
if (rule instanceof AssignmentRule && rule.path === 'meta.versionId') {
instance.versionId = rule.value.toString();
}
}
});
}
Expand Down
4 changes: 3 additions & 1 deletion src/utils/Processing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,7 @@ export function writeFHIRResources(
toJSON: (snapshot: boolean) => any;
url?: string;
id?: string;
versionId?: string;
resourceType?: string;
}[]
) => {
Expand All @@ -560,7 +561,8 @@ export function writeFHIRResources(
predef =>
predef.url === resource.url &&
predef.resourceType === resource.resourceType &&
predef.id === resource.id
predef.id === resource.id &&
predef.versionId === resource.versionId
)
) {
checkNullValuesOnArray(resource);
Expand Down
40 changes: 40 additions & 0 deletions test/import/FSHImporter.Instance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,46 @@ describe('FSHImporter', () => {
);
expect(loggerSpy.getLastMessage('error')).toMatch(/File: File2\.fsh.*Line: 2 - 4\D*/s);
});

it('should not produce an error when encountering an instance with a name used by another instance in another file but has a unique versionId', () => {
const input1 = `
Instance: SomeInstance
InstanceOf: Patient
Title: "Instance"
* meta
* meta.versionId = "1"
`;

const input2 = `
Instance: SomeInstance
InstanceOf: Patient
Title: "Instance"
* meta
* meta.versionId = "2"
`;

const input3 = `
Instance: SomeInstance
InstanceOf: Patient
Title: "Instance"
`;

const result = importText([
new RawFSH(input1, 'File1.fsh'),
new RawFSH(input2, 'File2.fsh'),
new RawFSH(input3, 'File2.fsh')
]);
expect(result.reduce((sum, d2) => sum + d2.instances.size, 0)).toBe(3);
const instance1 = result[0].instances.get('SomeInstance');
const instance2 = result[1].instances.get('SomeInstance');
const instance3 = result[2].instances.get('SomeInstance');
expect(instance1.title).toBe('Instance');
expect(instance1.versionId).toBe('1');
expect(instance1.title).toBe('Instance');
expect(instance2.versionId).toBe('2');
expect(instance3.title).toBe('Instance');
expect(instance3.versionId).toBe(undefined);
});
});
});
});
Loading