/// <summary>Writes an element.</summary> /// <param name="complex">The complex.</param> /// <param name="element">The element.</param> private void WriteElement( FhirComplex complex, FhirElement element) { string optionalFlagString = element.IsOptional ? "?" : string.Empty; string arrayFlagString = element.IsArray ? "[]" : string.Empty; Dictionary <string, string> values = element.NamesAndTypesForExport( FhirTypeBase.NamingConvention.CamelCase, FhirTypeBase.NamingConvention.PascalCase, false, string.Empty, complex.Components.ContainsKey(element.Path)); foreach (KeyValuePair <string, string> kvp in values) { if (!string.IsNullOrEmpty(element.Comment)) { WriteIndentedComment(element.Comment); } _writer.WriteLineIndented($"{kvp.Key}{optionalFlagString}: {kvp.Value}{arrayFlagString};"); if (RequiresExtension(kvp.Value)) { _writer.WriteLineIndented($"_{kvp.Key}?: Element;"); } } }
/// <summary>Writes the elements.</summary> /// <param name="complex"> The complex.</param> private void WriteElements( FhirComplex complex) { foreach (FhirElement element in complex.Elements.Values.OrderBy(s => s.FieldOrder)) { WriteElement(complex, element); } }
/// <summary>Writes an extension.</summary> /// <param name="extension">The extension.</param> private void WriteExtension( FhirComplex extension) { _writer.WriteLineIndented($"+{extension.URL}"); if (extension.Elements.Count > 0) { WriteComplex(extension); } }
/// <summary>Writes an element.</summary> /// <param name="complex">The complex.</param> /// <param name="element">The element.</param> private void WriteElement( FhirComplex complex, FhirElement element) { Dictionary <string, string> values = element.NamesAndTypesForExport( FhirTypeBase.NamingConvention.PascalCase, FhirTypeBase.NamingConvention.PascalCase, false, string.Empty, complex.Components.ContainsKey(element.Path)); foreach (KeyValuePair <string, string> kvp in values) { string elementName; if (kvp.Key == complex.Name) { elementName = $"{kvp.Key}Field"; } else { elementName = kvp.Key; } string optionalFlagString = (element.IsOptional && IsNullable(kvp.Value)) ? "?" : string.Empty; if (!string.IsNullOrEmpty(element.Comment)) { WriteIndentedComment(element.Comment); } string elementType = element.IsArray ? $"List<{kvp.Value}{optionalFlagString}>" : $"{kvp.Value}{optionalFlagString}"; string camel = FhirUtils.ToConvention(kvp.Key, string.Empty, FhirTypeBase.NamingConvention.CamelCase); _writer.WriteLineIndented($"[JsonProperty(\"{camel}\")]"); _writer.WriteLineIndented($"public {elementType} {elementName} {{ get; set; }}"); if (RequiresExtension(kvp.Value)) { _writer.WriteLineIndented($"[JsonProperty(\"_{camel}\")]"); if (element.IsArray) { _writer.WriteLineIndented($"public List<Element> _{elementName} {{ get; set; }}"); } else { _writer.WriteLineIndented($"public Element _{elementName} {{ get; set; }}"); } } } }
/// <summary>Writes a complex.</summary> /// <param name="complex">The complex.</param> private void WriteComplex( FhirComplex complex) { bool indented = false; // write this type's line, if it's a root element // (sub-properties are written with cardinality in the prior loop) if (_writer.Indentation == 0) { string experimental = complex.IsExperimental ? " (experimental)" : string.Empty; _writer.WriteLine($"- {complex.Name}: {complex.BaseTypeName}{experimental}"); _writer.IncreaseIndent(); indented = true; } // write elements WriteElements(complex); // check for extensions if (_info.ExtensionsByPath.ContainsKey(complex.Path)) { WriteExtensions(_info.ExtensionsByPath[complex.Path].Values); } // check for search parameters on this object if (complex.SearchParameters != null) { WriteSearchParameters(complex.SearchParameters.Values); } // check for type operations if (complex.TypeOperations != null) { WriteOperations(complex.TypeOperations.Values, true); } // check for instance operations if (complex.InstanceOperations != null) { WriteOperations(complex.TypeOperations.Values, false); } if (_info.ProfilesByBaseType.ContainsKey(complex.Path)) { WriteProfiles(_info.ProfilesByBaseType[complex.Path].Values); } if (indented) { _writer.DecreaseIndent(); } }
/// <summary>Adds a complex to 'complex'.</summary> /// <param name="elements"> The elements.</param> /// <param name="complex"> The complex.</param> /// <param name="complexType">Type of the complex.</param> private void AddComplex( Dictionary <string, CytoElement> elements, FhirComplex complex, FhirNodeInfo.FhirNodeType complexType) { if (elements.ContainsKey(complex.Name)) { elements[complex.Name].Data.Weight += _weightIncrement; if (elements[complex.Name].Data.Weight > _maxNodeWeight) { _maxNodeWeight = (decimal)elements[complex.Name].Data.Weight; } return; } CytoElement cy = new CytoElement() { Group = CytoElement.GroupNodes, Data = new CytoElementDataNode() { Id = complex.Name, Name = complex.Name, Weight = _weightIncrement, Group = _nodeGroupMap[complexType], }, }; elements.Add(complex.Name, cy); TryRecurse( elements, complex.Name, complexType, complex.BaseTypeName); AddElementEdges(elements, complex, complex); if (complex.Components != null) { foreach (FhirComplex component in complex.Components.Values) { AddElementEdges(elements, complex, component); } } }
/// <summary>Writes the elements.</summary> /// <param name="complex"> The complex.</param> /// <param name="elementsWithCodes">[out] The elements with codes.</param> private void WriteElements( FhirComplex complex, out List <FhirElement> elementsWithCodes) { elementsWithCodes = new List <FhirElement>(); foreach (FhirElement element in complex.Elements.Values.OrderBy(s => s.Name)) { if (element.IsInherited) { continue; } WriteElement(complex, element); if ((element.Codes != null) && (element.Codes.Count > 0)) { elementsWithCodes.Add(element); } } }
/// <summary>Writes a complex.</summary> /// <param name="complex"> The complex.</param> /// <param name="isResource">True if is resource, false if not.</param> private void WriteComplex( FhirComplex complex, bool isResource) { // check for nested components if (complex.Components != null) { foreach (FhirComplex component in complex.Components.Values) { WriteComplex(component, false); } } if (!string.IsNullOrEmpty(complex.Comment)) { WriteIndentedComment(complex.Comment); } if (string.IsNullOrEmpty(complex.BaseTypeName) || complex.Name.Equals("Element", StringComparison.Ordinal)) { _writer.WriteLineIndented($"public class {complex.NameForExport(FhirTypeBase.NamingConvention.PascalCase)} {{"); } else if (complex.Name.Equals(complex.BaseTypeName, StringComparison.Ordinal)) { _writer.WriteLineIndented( $"public class" + $" {complex.NameForExport(FhirTypeBase.NamingConvention.PascalCase, true)}" + $" {{"); } else if ((complex.Components != null) && complex.Components.ContainsKey(complex.Path)) { _writer.WriteLineIndented( $"public class" + $" {complex.NameForExport(FhirTypeBase.NamingConvention.PascalCase, true)}" + $" :" + $" {complex.TypeForExport(FhirTypeBase.NamingConvention.PascalCase, _primitiveTypeMap, false)} {{"); } else { _writer.WriteLineIndented( $"public class" + $" {complex.NameForExport(FhirTypeBase.NamingConvention.PascalCase, true)}" + $" :" + $" {complex.TypeForExport(FhirTypeBase.NamingConvention.PascalCase, _primitiveTypeMap)} {{"); } _writer.IncreaseIndent(); if (isResource && ShouldWriteResourceName(complex.Name)) { _exportedResourceNamesAndTypes.Add(complex.Name, complex.Name); _writer.WriteLineIndented("/** Resource Type Name (for serialization) */"); _writer.WriteLineIndented("[JsonProperty(\"resourceType\")]"); _writer.WriteLineIndented($"public string ResourceType => \"{complex.Name}\";"); } // write elements WriteElements(complex, out List <FhirElement> elementsWithCodes); // close interface (type) _writer.DecreaseIndent(); _writer.WriteLineIndented("}"); if (_exportEnums) { foreach (FhirElement element in elementsWithCodes) { WriteCode(element); } } }
/// <summary>Writes an element.</summary> /// <param name="complex">The complex.</param> /// <param name="element">The element.</param> private void WriteElement( FhirComplex complex, FhirElement element) { string propertyType = string.Empty; if (element.ElementTypes != null) { foreach (FhirElementType elementType in element.ElementTypes.Values.OrderBy(e => e.Name)) { string joiner = string.IsNullOrEmpty(propertyType) ? string.Empty : "|"; string profiles = string.Empty; if ((elementType.Profiles != null) && (elementType.Profiles.Count > 0)) { profiles = "(" + string.Join("|", elementType.Profiles.Values) + ")"; } propertyType = $"{propertyType}{joiner}{elementType.Name}{profiles}"; } } if (string.IsNullOrEmpty(propertyType)) { propertyType = element.BaseTypeName; } _writer.WriteLineIndented( $"-" + $" {element.NameForExport(FhirTypeBase.NamingConvention.CamelCase)}[{element.FhirCardinality}]:" + $" {propertyType}"); _writer.IncreaseIndent(); // check for regex if (!string.IsNullOrEmpty(element.ValidationRegEx)) { _writer.WriteLineIndented($"[{element.ValidationRegEx}]"); } // check for default value if (!string.IsNullOrEmpty(element.DefaultFieldName)) { _writer.WriteLineIndented($".{element.DefaultFieldName} = {element.DefaultFieldValue}"); } // check for fixed value if (!string.IsNullOrEmpty(element.FixedFieldName)) { _writer.WriteLineIndented($".{element.FixedFieldName} = {element.FixedFieldValue}"); } if ((element.Codes != null) && (element.Codes.Count > 0)) { string codes = string.Join("|", element.Codes); _writer.WriteLineIndented($"{{{codes}}}"); } // either step into backbone definition OR extensions, don't write both if (complex.Components.ContainsKey(element.Path)) { WriteComplex(complex.Components[element.Path]); } else if (_info.ExtensionsByPath.ContainsKey(element.Path)) { WriteExtensions(_info.ExtensionsByPath[element.Path].Values); } // check for slicing information if (element.Slicing != null) { WriteSlicings(element.Slicing.Values); } _writer.DecreaseIndent(); }
/// <summary>Adds an element edges.</summary> /// <param name="elements">The elements.</param> /// <param name="root"> The root.</param> /// <param name="current"> The current.</param> private void AddElementEdges( Dictionary <string, CytoElement> elements, FhirComplex root, FhirComplex current) { if (current.Elements != null) { foreach (FhirElement element in current.Elements.Values) { if (!string.IsNullOrEmpty(element.BaseTypeName)) { if (!_info.TryGetNodeInfo(element.BaseTypeName, out FhirNodeInfo node)) { continue; } if ((node.SourceType == FhirNodeInfo.FhirNodeType.Component) || (node.SourceType == FhirNodeInfo.FhirNodeType.Primitive)) { continue; } AddEdge( elements, root.Name, FhirNodeInfo.FhirNodeType.Unknown, element.BaseTypeName, FhirNodeInfo.FhirNodeType.Unknown); continue; } if (element.ElementTypes != null) { foreach (FhirElementType elementType in element.ElementTypes.Values) { if (!_info.TryGetNodeInfo(elementType.Name, out FhirNodeInfo node)) { continue; } if ((node.SourceType == FhirNodeInfo.FhirNodeType.Component) || (node.SourceType == FhirNodeInfo.FhirNodeType.Primitive)) { continue; } if ((elementType.Profiles != null) && (elementType.Profiles.Count > 0)) { foreach (FhirElementProfile profile in elementType.Profiles.Values) { AddEdge( elements, root.Name, FhirNodeInfo.FhirNodeType.Unknown, profile.Name, FhirNodeInfo.FhirNodeType.Unknown); } continue; } AddEdge( elements, root.Name, FhirNodeInfo.FhirNodeType.Unknown, elementType.Name, FhirNodeInfo.FhirNodeType.Unknown); continue; } continue; } } } if (current.Components != null) { foreach (FhirComplex complex in current.Components.Values) { AddElementEdges(elements, root, complex); } } }