Skip to content

Commit

Permalink
feat(fhir-ts-codegen): add resourceType element
Browse files Browse the repository at this point in the history
  • Loading branch information
tangdrew committed Oct 25, 2019
1 parent ef06551 commit 48a2fa7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
11 changes: 8 additions & 3 deletions packages/fhir-ts-codegen/lib/code-generater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,12 @@ const generateImports = ({
map(({ elements }) => elements),
flatten,
filter(
({ backbone, contentReference, primitive, recursive }) =>
!primitive && !backbone && !contentReference && !recursive
({ backbone, contentReference, primitive, recursive, name }) =>
!primitive &&
!backbone &&
!contentReference &&
!recursive &&
name !== "resourceType"
),
map(({ type }) => type),
// Ensure Element is imported
Expand Down Expand Up @@ -108,12 +112,13 @@ const generateProperty = ({
array,
comment,
iotsType,
literal,
name,
required,
type
}: AnalyzedElement) =>
pipe(
["boolean", "string"].includes(type) ? type : iotsType,
["boolean", "string"].includes(type) ? type : literal ? type : iotsType,
typeName => `/** ${comment} */
${name}${required ? "" : "?"}: ${typeName}${array ? "[]" : ""};`
);
Expand Down
34 changes: 30 additions & 4 deletions packages/fhir-ts-codegen/lib/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ export const parse = (structureDefinition: StructureDefinition) =>
resourceName: structureDefinition.name
},
groupElementDefinitions,
analyzeElementDefinitionGroups,
groups => ({ name: structureDefinition.name, groups })
analyzeElementDefinitionGroups(structureDefinition.type),
groups => ({
name: structureDefinition.name,
groups
})
);

export interface ElementGroup {
Expand All @@ -40,6 +43,7 @@ export interface AnalyzedElement {
comment?: string;
contentReference: boolean;
iotsType: string;
literal: boolean;
name: string;
primitive: boolean;
recursive: boolean;
Expand All @@ -52,7 +56,7 @@ interface ElementDefinitionGroup {
elements: ElementDefinition[];
}

const analyzeElementDefinitionGroups = (groups: {
const analyzeElementDefinitionGroups = (resourceType: string) => (groups: {
[key: string]: ElementDefinitionGroup;
}) =>
Object.keys(groups).reduce((accum: { [key: string]: ElementGroup }, key) => {
Expand All @@ -71,7 +75,11 @@ const analyzeElementDefinitionGroups = (groups: {
map(elementDefinition =>
analyzeElementDefinition({ elementDefinition, resourceType: name })
),
expandPrimitiveElements
expandPrimitiveElements,
elements =>
isResourceDefinition(resourceType, rootElement)
? [creatResourceTypeAnalyzedElement(resourceType), ...elements]
: elements
)
};
return {
Expand Down Expand Up @@ -118,6 +126,23 @@ const expandPrimitiveElements = (elements: AnalyzedElement[]) =>
flatten
);

const creatResourceTypeAnalyzedElement = (
resourceType: string
): AnalyzedElement => ({
array: false,
backbone: false,
choice: false,
comment: "The type of resource",
contentReference: false,
iotsType: `t.literal("${resourceType}")`,
literal: true,
name: "resourceType",
primitive: false,
recursive: false,
required: false,
type: `"${resourceType}"`
});

const analyzeElementDefinition = ({
elementDefinition,
resourceType
Expand All @@ -136,6 +161,7 @@ const analyzeElementDefinition = ({
comment: elementDefinition.short,
contentReference: isContentReferenceElement(elementDefinition),
iotsType: primitive ? `primitives.R4.${type}` : type,
literal: false,
name: elementName(elementDefinition),
primitive,
recursive: type === resourceType,
Expand Down

0 comments on commit 48a2fa7

Please sign in to comment.