-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathComponentDefinition.cs
More file actions
230 lines (194 loc) · 9.15 KB
/
ComponentDefinition.cs
File metadata and controls
230 lines (194 loc) · 9.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// <copyright file="ComponentDefinition.cs" company="Microsoft Corporation">
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
// </copyright>
using System.Diagnostics.CodeAnalysis;
using Hl7.Fhir.Model;
using Microsoft.Health.Fhir.CodeGen.FhirExtensions;
using static Microsoft.Health.Fhir.CodeGenCommon.Extensions.FhirNameConventionExtensions;
namespace Microsoft.Health.Fhir.CodeGen.Models;
/// <summary>
/// A component definition.
/// </summary>
public record class ComponentDefinition
{
/// <summary>
/// Gets or sets the structure definition.
/// </summary>
public required StructureDefinition Structure { get; init; }
/// <summary>
/// Gets or sets the element definition.
/// </summary>
public required ElementDefinition Element { get; init; }
/// <summary>
/// Gets or sets a value indicating whether this object is the root of the structure.
/// </summary>
public required bool IsRootOfStructure { get; init; }
/// <summary>
/// Initializes a new instance of the <see cref="ComponentDefinition"/> class.
/// </summary>
/// <remarks>
/// This constructor is used to create a new instance of the ComponentDefinition class.
/// </remarks>
public ComponentDefinition() { }
/// <summary>
/// Initializes a new instance of the <see cref="ComponentDefinition"/> class.
/// </summary>
/// <param name="sd">The structure definition.</param>
[SetsRequiredMembers]
public ComponentDefinition(StructureDefinition sd)
{
Structure = sd;
Element = sd.cgRootElement()
?? throw new InvalidOperationException($"StructureDefinition {sd.Url} does not have a root element.");
IsRootOfStructure = true;
}
/// <summary>
/// Gets the URL for code generation.
/// </summary>
/// <returns>A string representing the URL.</returns>
public string cgUrl() => Element?.cgUrl(Structure) ?? Structure.Url;
/// <summary>Cg explicit name.</summary>
/// <returns>A string.</returns>
public string? cgExplicitName() => Element.cgExplicitName();
/// <summary>Gets the code generation name.</summary>
/// <remarks>Note: Firely generation uses this version.</remarks>
/// <param name="convention"> (Optional) The naming convention.</param>
/// <param name="concatenatePath"> (Optional) True to concatenate path.</param>
/// <param name="skipStructureNameInConcatenation">(Optional) True to skip structure name in
/// concatenation.</param>
/// <returns>A string representing the code generation name.</returns>
public string cgName(
NamingConvention convention = NamingConvention.PascalCase,
bool concatenatePath = false,
bool skipStructureNameInConcatenation = false) => Element.cgNameForExport(
convention,
concatenatePath: concatenatePath,
skipStructureNameInConcatenation: skipStructureNameInConcatenation);
/// <summary>Get a short description for a component.</summary>
/// <returns>A string.</returns>
public string cgShort() =>
(!string.IsNullOrEmpty(Element?.Short) && !Element!.cgHasCodes()) ? Element!.Short
: !string.IsNullOrEmpty(Element?.Definition) ? Element!.Definition
: !string.IsNullOrEmpty(Structure?.Description) ? Structure!.Description
: !string.IsNullOrEmpty(Structure?.Purpose) ? Structure!.Purpose
: string.Empty;
/// <summary>Get a description for a component</summary>
/// <returns>A string.</returns>
public string cgDefinition() =>
!string.IsNullOrEmpty(Element?.Definition) ? Element!.Definition
: !string.IsNullOrEmpty(Structure?.Description) ? Structure!.Description
: !string.IsNullOrEmpty(Structure?.Purpose) ? Structure!.Purpose
: string.Empty;
public string cgComment() =>
!string.IsNullOrEmpty(Element?.Comment) ? Element!.Comment
: !string.IsNullOrEmpty(Structure?.Purpose) ? Structure!.Purpose
: string.Empty;
/// <summary>Cg name rooted.</summary>
/// <param name="convention">(Optional) The convention.</param>
/// <returns>A string.</returns>
/// <remarks>TypeScript uses this version</remarks>
public string cgNameRooted(NamingConvention convention = NamingConvention.PascalCase) => IsRootOfStructure
? Structure.Name.ToConvention(convention)
: Element.cgNameForExport(convention, true);
/// <summary>
/// Gets the base type name for code generation.
/// </summary>
/// <param name="dc">The device context.</param>
/// <returns>A string representing the base type name.</returns>
public string cgBaseTypeName(DefinitionCollection dc, bool usePathForParents)
{
string tn = Structure.cgBaseTypeName();
if (IsRootOfStructure && !string.IsNullOrEmpty(tn))
{
return Structure.cgBaseTypeName();
}
return Element?.cgBaseTypeName(dc, usePathForParents) ?? tn;
}
/// <summary>
/// Gets the child element definition for code generation.
/// </summary>
/// <param name="name">The name of the child element.</param>
/// <returns>An ElementDefinition representing the child element.</returns>
public ElementDefinition? cgGetChild(string name)
{
string path = Element.Path + (name.StartsWith(".") ? name : "." + name);
if ((Structure.Snapshot != null) && (Structure.Snapshot.Element.Count != 0))
{
return Structure.Snapshot.Element.FirstOrDefault(e => e.Path == path);
}
if ((Structure.Differential != null) && (Structure.Differential.Element.Count != 0))
{
return Structure.Differential.Element.FirstOrDefault(e => e.Path == path);
}
return null;
}
/// <summary>
/// Gets the child component definitions for code generation.
/// </summary>
/// <param name="dc">The definition collection.</param>
/// <param name="includeDescendants">(Optional) True to include, false to exclude the descendants.</param>
/// <returns>An enumerator that allows foreach to be used to process child component definitions in this collection.</returns>
public IEnumerable<ComponentDefinition> cgChildComponents(DefinitionCollection dc, bool includeDescendants = false) =>
Structure.cgComponents(dc, Element, false, !includeDescendants);
/// <summary>
/// Gets the validation regular expression for code generation.
/// </summary>
/// <returns>A string representing the validation regular expression.</returns>
public string cgValidationRegEx() => Element?.cgValidationRegEx() ?? string.Empty;
/// <summary>
/// Enumerates the elements contained in this component.
/// </summary>
/// <param name="includeDescendants">(Optional) True to include, false to exclude the descendants.</param>
/// <returns>An enumerator that allows foreach to be used to process cg get children in this collection.</returns>
public IEnumerable<ElementDefinition> cgGetChildren(
bool includeDescendants = false,
bool skipSlices = true)
{
IEnumerable<ElementDefinition> source;
int dotCount = Element.Path.Count(c => c == '.');
int colonCount = Element.ElementId.Count(c => c == ':');
// filter based on the backbone path we want
// we want child elements of the requested path, so append an additional dot
source = (Structure.Snapshot != null) && (Structure.Snapshot.Element.Count != 0)
? Structure.Snapshot.Element.Where(e => e.ElementId.StartsWith(Element.ElementId + ".", StringComparison.Ordinal))
: Structure.Differential.Element.Where(e => e.ElementId.StartsWith(Element.ElementId + ".", StringComparison.Ordinal));
// traverse our filtered elements
foreach (ElementDefinition e in source)
{
// skip slices and their children
if (skipSlices && (e.ElementId.Count(c => c == ':') > colonCount))
{
continue;
}
// if top level only, we need exactly one dot more than the forBackbonePath
if (!includeDescendants)
{
if (e.Path.Count(c => c == '.') != dotCount + 1)
{
continue;
}
}
yield return e;
}
}
public bool DerivesFromDataType(DefinitionCollection dc)
{
string btName = cgBaseTypeName(dc, false);
if ((btName == "DataType") || (btName == "Hl7.Fhir.Model.DataType"))
{
return true;
}
return dc.PrimitiveTypesByName.ContainsKey(btName) || dc.ComplexTypesByName.ContainsKey(btName);
}
public bool DerivesFromElement(DefinitionCollection dc)
{
string btName = cgBaseTypeName(dc, false);
if ((btName == "Element") || (btName == "Hl7.Fhir.Model.Element") ||
(btName == "BackboneElement") || (btName == "Hl7.Fhir.Model.BackboneElement"))
{
return true;
}
return !dc.PrimitiveTypesByName.ContainsKey(btName) && !dc.ComplexTypesByName.ContainsKey(btName);
}
}