@synanetics/fhir-fml-convert
Version:
Converts StructureMaps written in FML to JSON ($convert operation)
121 lines • 4.75 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.StructureMapVisitorSTU3 = void 0;
const structureMapDefinition_json_1 = __importDefault(require("./res/stu3/structureMapDefinition.json"));
const types_json_1 = __importDefault(require("./res/stu3/types.json"));
const StructureMapVisitor_1 = require("./StructureMapVisitor");
class StructureMapVisitorSTU3 extends StructureMapVisitor_1.StructureMapVisitor {
constructor() {
super();
this.fhirVersion = 'STU3';
this.structureMapDefinition = structureMapDefinition_json_1.default;
this.fhirTypes = types_json_1.default
.entry
.map((e) => ({
id: e.resource.id,
kind: e.resource?.kind,
type: e.resource?.type,
snapshot: e.resource?.snapshot,
}));
}
visitRuleDefinition(ctx) {
const sources = ctx.ruleSources()?.accept(this) ?? [];
const targets = ctx.ruleTargets()?.accept(this) ?? [];
const dependent = ctx
.dependent()
?.namedInvocation()
.map((i) => ({
name: i.variableId().text,
variable: i.paramList().param().map((p) => p.text),
}));
const rule = ctx
.dependent()
?.rules()
?.ruleDefinition()
?.map((r) => r.accept(this));
const ruleDef = {
name: this.getRuleName(ctx.ruleName(), sources[0]),
source: sources,
target: targets,
rule,
dependent,
};
return ruleDef;
}
getDefaultValue(ctx) {
return {
key: 'defaultValueString',
value: ctx.fhirPath().text,
};
}
visitRuleTargets(ctx) {
// @ts-ignore the filter ensure that the returned value is not null
return ctx.ruleTarget().map((tgt) => {
const ruleCtx = tgt.ruleCtx()?.text.split('.');
if (!ruleCtx)
return null;
const targetTransform = tgt.transform();
let transform;
const parameter = [];
if (targetTransform?.namedInvocation()) {
transform = targetTransform.namedInvocation()?.variableId().text;
if (targetTransform.namedInvocation()?.paramList()) {
targetTransform.namedInvocation()?.paramList()?.param().forEach((p) => {
let value;
if (p.literal()) {
value = this.getValueFromLiteral(p.literal());
}
else if (p.variableId()) {
value = {
value: p.variableId().text,
type: 'id',
};
}
if (value) {
parameter.push({
[`value${value.type[0].toUpperCase()}${value.type.substring(1)}`]: value.value,
});
}
});
}
}
else if (targetTransform?.literal() || targetTransform?.ruleCtx()) {
transform = 'copy';
if (targetTransform.literal()) {
const value = this.getValueFromLiteral(targetTransform.literal());
if (value) {
parameter.push({
[`value${value.type[0].toUpperCase()}${value.type.substring(1)}`]: value.value,
});
}
}
else if (targetTransform.ruleCtx()) {
parameter.push({
valueId: targetTransform?.ruleCtx()?.text,
});
}
}
const variable = tgt.alias()?.variableId().text;
const mappingRuleTarget = {
context: ruleCtx[0],
element: ruleCtx[1] ?? undefined,
};
if (transform) {
// @ts-ignore
mappingRuleTarget.transform = transform;
}
if (variable) {
mappingRuleTarget.variable = variable;
}
if (parameter.length) {
mappingRuleTarget.parameter = parameter;
}
return mappingRuleTarget;
}).filter(Boolean);
}
}
exports.StructureMapVisitorSTU3 = StructureMapVisitorSTU3;
//# sourceMappingURL=StructureMapVisitorSTU3.js.map