/// <summary>Writes a polymorphic helpers.</summary> private void WritePolymorphicHelpers() { _writer.WriteLineIndented("public class ResourceConverter : JsonConverter"); _writer.WriteLineIndented("{"); _writer.IncreaseIndent(); // function CanConvert _writer.WriteLineIndented("public override bool CanConvert(Type objectType)"); _writer.WriteLineIndented("{"); _writer.IncreaseIndent(); _writer.WriteLineIndented("return typeof(Resource).IsAssignableFrom(objectType);"); _writer.DecreaseIndent(); _writer.WriteLineIndented("}"); // property CanWrite _writer.WriteLineIndented("public override bool CanWrite"); _writer.WriteLineIndented("{"); _writer.IncreaseIndent(); _writer.WriteLineIndented("get { return false; }"); _writer.DecreaseIndent(); _writer.WriteLineIndented("}"); // function WriteJson _writer.WriteLineIndented("public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)"); _writer.WriteLineIndented("{"); _writer.IncreaseIndent(); _writer.WriteLineIndented("throw new NotImplementedException();"); _writer.DecreaseIndent(); _writer.WriteLineIndented("}"); // property CanRead _writer.WriteLineIndented("public override bool CanRead"); _writer.WriteLineIndented("{"); _writer.IncreaseIndent(); _writer.WriteLineIndented("get { return true; }"); _writer.DecreaseIndent(); _writer.WriteLineIndented("}"); // function ReadJson _writer.WriteLineIndented("public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)"); _writer.WriteLineIndented("{"); _writer.IncreaseIndent(); _writer.WriteLineIndented("JObject jObject = JObject.Load(reader);"); _writer.WriteLineIndented("string resourceType = jObject[\"resourceType\"].Value<string>();"); _writer.WriteLineIndented("object target = null;"); _writer.WriteLineIndented("switch (resourceType)"); _writer.WriteLineIndented("{"); _writer.IncreaseIndent(); // loop through our types foreach (KeyValuePair <string, string> kvp in _exportedResourceNamesAndTypes) { _writer.WriteLineIndented($"case \"{kvp.Key}\":"); _writer.IncreaseIndent(); _writer.WriteLineIndented($"target = new {kvp.Value}();"); _writer.WriteLineIndented("break;"); _writer.DecreaseIndent(); } // default case returns a Resource object _writer.WriteLineIndented("default:"); _writer.IncreaseIndent(); _writer.WriteLineIndented("target = new Resource();"); _writer.WriteLineIndented("break;"); _writer.DecreaseIndent(); // close switch _writer.DecreaseIndent(); _writer.WriteLineIndented("}"); // populate _writer.WriteLineIndented("serializer.Populate(jObject.CreateReader(), target);"); // return/close ReadJson _writer.WriteLineIndented("return target;"); _writer.DecreaseIndent(); _writer.WriteLineIndented("}"); // close class _writer.DecreaseIndent(); _writer.WriteLineIndented("}"); }
/// <summary>Writes a value sets.</summary> /// <param name="valueSets"> Sets the value belongs to.</param> /// <param name="headerHint">(Optional) The header hint.</param> private void WriteValueSets( IEnumerable <FhirValueSetCollection> valueSets, string headerHint = null) { if (!string.IsNullOrEmpty(headerHint)) { _writer.WriteLineIndented($"{headerHint}: {valueSets.Count()} (unversioned)"); } foreach (FhirValueSetCollection collection in valueSets.OrderBy(c => c.URL)) { foreach (FhirValueSet vs in collection.ValueSetsByVersion.Values.OrderBy(v => v.Version)) { _writer.WriteLineIndented($"- ValueSet: {vs.URL}|{vs.Version}"); _writer.IncreaseIndent(); _writer.WriteLineIndented( $" references: {vs.ReferencedByPaths.Count}," + $" strongest binding: {vs.StrongestBinding}"); foreach (FhirConcept value in vs.Concepts.OrderBy(c => c.Code)) { _writer.WriteLineIndented($"- #{value.Code}: {value.Display}"); } _writer.DecreaseIndent(); } } }
/// <summary>Export the passed FHIR version into the specified directory.</summary> /// <param name="info"> The information.</param> /// <param name="serverInfo"> Information describing the server.</param> /// <param name="options"> Options for controlling the operation.</param> /// <param name="exportDirectory">Directory to write files.</param> void ILanguage.Export( FhirVersionInfo info, FhirServerInfo serverInfo, ExporterOptions options, string exportDirectory) { // set internal vars so we don't pass them to every function // this is ugly, but the interface patterns get bad quickly because we need the type map to copy the FHIR info _info = info; _options = options; if (options.OptionalClassTypesToExport.Contains(ExporterOptions.FhirExportClassType.Enum)) { _exportEnums = true; } else { _exportEnums = false; } if ((options.LanguageOptions != null) && (options.LanguageOptions.Count > 0)) { foreach (KeyValuePair <string, string> kvp in options.LanguageOptions) { string key = kvp.Key.ToUpperInvariant(); switch (key) { case "NAMESPACE": _namespace = kvp.Value; break; } } } _exportedResourceNamesAndTypes = new Dictionary <string, string>(); _exportedCodes = new HashSet <string>(); // create a filename for writing (single file for now) string filename = Path.Combine(exportDirectory, $"R{info.MajorVersion}.cs"); using (FileStream stream = new FileStream(filename, FileMode.Create)) using (ExportStreamWriter writer = new ExportStreamWriter(stream)) { _writer = writer; WriteHeader(); // open namespace _writer.WriteLineIndented($"namespace {_namespace}"); _writer.WriteLineIndented("{"); _writer.IncreaseIndent(); WriteComplexes(_info.ComplexTypes.Values, false); WriteComplexes(_info.Resources.Values, true); if (_exportEnums) { WriteValueSets(_info.ValueSetsByUrl.Values); } WritePolymorphicHelpers(); // close namespace _writer.DecreaseIndent(); _writer.WriteLineIndented("}"); WriteFooter(); } }
/// <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(); }