-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathCSharpFirelyCommon.cs
More file actions
265 lines (229 loc) · 8.15 KB
/
CSharpFirelyCommon.cs
File metadata and controls
265 lines (229 loc) · 8.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// <copyright file="CSharpFirelyCommon.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.ComponentModel;
using System.Text;
using Hl7.Fhir.Model;
using Microsoft.Health.Fhir.CodeGen.FhirExtensions;
using Microsoft.Health.Fhir.CodeGenCommon.Extensions;
using Microsoft.Health.Fhir.CodeGenCommon.Packaging;
using Microsoft.Health.Fhir.CodeGenCommon.Utils;
#if NETSTANDARD2_0
using Microsoft.Health.Fhir.CodeGenCommon.Polyfill;
#endif
namespace Microsoft.Health.Fhir.CodeGen.Language.Firely;
public static class CSharpFirelyCommon
{
/// <summary>Dictionary mapping FHIR primitive types to language equivalents (see Template-Model.tt#1252).</summary>
public static readonly Dictionary<string, string> PrimitiveTypeMap = new()
{
{ "base64Binary", "byte[]" },
{ "boolean", "bool?" },
{ "canonical", "string" },
{ "code", "string" },
{ "date", "string" },
{ "dateTime", "string" },
{ "decimal", "decimal?" },
{ "id", "string" },
{ "instant", "DateTimeOffset?" },
{ "integer", "int?" },
{ "integer64", "long?" },
{ "oid", "string" },
{ "positiveInt", "int?" },
{ "string", "string" },
{ "time", "string" },
{ "unsignedInt", "int?" },
{ "uri", "string" },
{ "url", "string" },
{ "uuid", "string" },
{ "xhtml", "string" },
{ "markdown", "string" }
};
/// <summary>Types that have non-standard names or formatting (see Template-Model.tt#1252).</summary>
public static readonly Dictionary<string, string> TypeNameMappings = new()
{
{ "boolean", "FhirBoolean" },
{ "dateTime", "FhirDateTime" },
{ "decimal", "FhirDecimal" },
{ "Reference", "ResourceReference" },
{ "string", "FhirString" },
{ "uri", "FhirUri" },
{ "url", "FhirUrl" },
{ "xhtml", "XHtml" },
};
/// <summary>Context types that need to be remapped for use.</summary>
public static readonly Dictionary<string, string> ContextTypeMappings = new()
{
{ "Resource", "DomainResource" },
};
/// <summary>
/// Determines the subset of code to generate.
/// </summary>
[Flags]
public enum GenSubset
{
// Subset of datatypes and resources used in R3 and later
[Description("Subset of datatypes and resources used in R3 and later.")]
Base = 1,
// Subset of conformance resources used by the SDK
[Description("Subset of conformance resources used by the SDK.")]
Conformance = 2,
// Subset of model classes that are not part of Base or Conformance
[Description("Subset of model classes that are not part of Base or Conformance.")]
Satellite = 4,
}
/// <summary>Writes an indented comment.</summary>
/// <param name="writer"> The writer to write the comment to.</param>
/// <param name="value"> The value.</param>
/// <param name="isSummary"> (Optional) True if is summary, false if not.</param>
/// <param name="singleLine">(Optional) True if this is a short comment using a single line
/// comment prefix. Implies isSummary = false.</param>
/// <param name="isRemarks"> (Optional) True if is remarks, false if not.</param>
public static void WriteIndentedComment(
this ExportStreamWriter writer,
string value,
bool isSummary = true,
bool singleLine = false,
bool isRemarks = false)
{
if (string.IsNullOrEmpty(value))
{
return;
}
if (singleLine)
{
isSummary = false;
}
if (isSummary)
{
writer.WriteLineIndented("/// <summary>");
}
if (isRemarks)
{
writer.WriteLineIndented("/// <remarks>");
}
string comment = value
.Replace('\r', '\n')
.Replace("\r\n", "\n", StringComparison.Ordinal)
.Replace("\n\n", "\n", StringComparison.Ordinal)
.Replace("&", "&", StringComparison.Ordinal)
.Replace("<", "<", StringComparison.Ordinal)
.Replace(">", ">", StringComparison.Ordinal);
string[] lines = comment.Split('\n');
foreach (string line in lines)
{
writer.WriteIndented(singleLine ? "// " : "/// ");
writer.WriteLine(line);
}
if (isSummary)
{
writer.WriteLineIndented("/// </summary>");
}
if (isRemarks)
{
writer.WriteLineIndented("/// </remarks>");
}
}
/// <summary>Opens the scope.</summary>
/// <param name="writer">The writer to write the comment to.</param>
public static void OpenScope(ExportStreamWriter writer)
{
writer.WriteLineIndented("{");
writer.IncreaseIndent();
}
/// <summary>Closes the scope.</summary>
/// <param name="writer"> The writer to write the comment to.</param>
/// <param name="includeSemicolon">(Optional) True to include, false to exclude the semicolon.</param>
/// <param name="suppressNewline"> (Optional) True to suppress, false to allow the newline.</param>
public static void CloseScope(ExportStreamWriter writer, bool includeSemicolon = false, bool suppressNewline = false)
{
writer.DecreaseIndent();
if (includeSemicolon)
{
writer.WriteLineIndented("};");
}
else
{
writer.WriteLineIndented("}");
}
if (!suppressNewline)
{
writer.WriteLine(string.Empty);
}
}
/// <summary>Convert enum value - see Template-Model.tt#2061.</summary>
/// <param name="name">The name.</param>
/// <returns>The enum converted value.</returns>
public static string ConvertEnumValue(string name)
{
// remove a leading underscore
if (name.StartsWith('_'))
{
name = name.Substring(1);
}
// expand common literals
switch (name)
{
case "=":
return "Equal";
case "!=":
return "NotEqual";
case "<":
return "LessThan";
case "<=":
return "LessOrEqual";
case ">=":
return "GreaterOrEqual";
case ">":
return "GreaterThan";
}
string[] bits = name.Split([' ', '-']);
string result = string.Empty;
foreach (string bit in bits)
{
result += bit.Substring(0, 1).ToUpperInvariant();
result += bit.Substring(1);
}
result = result
.Replace('.', '_')
.Replace(')', '_')
.Replace('(', '_')
.Replace('/', '_')
.Replace('+', '_');
if (char.IsDigit(result[0]))
{
result = "N" + result;
}
return result;
}
/// <summary>Gets an order.</summary>
/// <param name="element">The element.</param>
/// <returns>The order.</returns>
public static int GetOrder(ElementDefinition element)
{
//return (element.cgFieldOrder() * 10) + 10;
return (element.cgComponentFieldOrder() * 10) + 10;
}
public static int GetOrder(int relativeOrder)
{
return (relativeOrder * 10) + 10;
}
public static string BuildOpenAllowedTypesAttribute() => "[AllowedTypes(OpenChoice = true)]";
public static string BuildAllowedTypesAttribute(IEnumerable<TypeReference> types, FhirReleases.FhirSequenceCodes? since)
{
StringBuilder sb = new();
sb.Append("[AllowedTypes(");
string typesList = string.Join(",",
types.Select(t => $"typeof({t.PropertyTypeString})"));
sb.Append(typesList);
if (since is not null)
sb.Append($", Since = FhirRelease.{since}");
sb.Append(")]");
return sb.ToString();
}
}
public static class StringHelpers
{
public static string EnsurePeriod(this string s) => s.EndsWith('.') ? s : s + ".";
}