diff --git a/src/Microsoft.Health.Fhir.CodeGen/Language/Firely/CSharpFirely2.cs b/src/Microsoft.Health.Fhir.CodeGen/Language/Firely/CSharpFirely2.cs index dd4d280db..cd2de2d07 100644 --- a/src/Microsoft.Health.Fhir.CodeGen/Language/Firely/CSharpFirely2.cs +++ b/src/Microsoft.Health.Fhir.CodeGen/Language/Firely/CSharpFirely2.cs @@ -1294,16 +1294,6 @@ private void WriteInterface( GenSubset subset) { string exportName = "I" + complex.Name.ToPascalCase(); - - //writtenModels.Add( - // complex.Name, - // new WrittenModelInfo() - // { - // FhirName = complex.Name, - // CsName = $"{Namespace}.{exportName}", - // IsAbstract = complex.Abstract == true, - // }); - string filename = Path.Combine(_exportDirectory, "Generated", $"{exportName}.cs"); _modelWriter.WriteLineIndented($"// {exportName}.cs"); @@ -1545,8 +1535,6 @@ private void WriteInterfaceComponent( string fhirTypeConstructor = $"\"{complexName}\",\"{complex.cgUrl()}\""; - //_writer.WriteLineIndented($"[FhirType({fhirTypeConstructor}, IsResource=true)]"); - StructureDefinition? parentInterface = _info.GetParentInterface(complex.Structure); if (parentInterface == null) @@ -1878,15 +1866,7 @@ private void WriteComponent( WriteSerializable(); string fhirTypeConstructor = $"\"{complexName}\",\"{complex.cgUrl()}\""; - - if (isResource) - { - _writer.WriteLineIndented($"[FhirType({fhirTypeConstructor}, IsResource=true)]"); - } - else - { - _writer.WriteLineIndented($"[FhirType({fhirTypeConstructor})]"); - } + _writer.WriteLineIndented($"[FhirType({fhirTypeConstructor})]"); var isPatientClass = false; @@ -1954,7 +1934,8 @@ private void WriteComponent( // open class OpenScope(); - WritePropertyTypeName(complex.cgName()); + if(complex.Structure.Abstract != true) + WritePropertyTypeName(complex.cgName()); string validationRegEx = complex.cgValidationRegEx(); if (!string.IsNullOrEmpty(validationRegEx)) @@ -2093,23 +2074,21 @@ private string DetermineExportedBaseTypeName(string baseTypeName) return baseTypeName; } - private void WriteIDictionarySupport(string exportName, IEnumerable exportedElements) + private void WriteIDictionarySupport(string exportName, List exportedElements) { WriteDictionaryTryGetValue(exportName, exportedElements); + WriteDictionaryTrySetValue(exportName, exportedElements); WriteDictionaryPairs(exportName, exportedElements); } - private string NullCheck(string propertyName, bool isList) => propertyName + (isList ? "?.Any() == true" : " is not null"); - private void WriteDictionaryPairs(string exportName, IEnumerable exportedElements) + private void WriteDictionaryPairs(string exportName, List exportedElements) { - // Base implementation differs from subclasses. + // Base implementation differs from subclasses and is hand-written code in a separate partical class if (exportName == "Base") { - _writer.WriteLineIndented("protected virtual IEnumerable> GetElementPairs() => Enumerable.Empty>();"); - _writer.WriteLine(string.Empty); return; } @@ -2132,18 +2111,13 @@ private void WriteDictionaryPairs(string exportName, IEnumerable exportedElements) + private void WriteDictionaryTryGetValue(string exportName, List exportedElements) { - // Base implementation differs from subclasses. - if (exportName == "Base") - { - _writer.WriteLineIndented("protected virtual bool TryGetValue(string key, out object value)"); - OpenScope(); - _writer.WriteLineIndented("value = default;"); - _writer.WriteLineIndented("return false;"); - CloseScope(); - return; - } + // Base implementation differs from subclasses and is hand-written code in a separate partical class + if (exportName == "Base") + { + return; + } // Don't override anything if there are no additional elements. if (!exportedElements.Any()) @@ -2188,6 +2162,74 @@ void writeCase(string key, string propName, bool isList) void writeBaseTryGetValue() => _writer.WriteLineIndented("return base.TryGetValue(key, out value);"); } + + private void WriteDictionaryTrySetValue(string exportName, List exportedElements) + { + // Base implementation differs from subclasses and is hand-written code in a separate partical class + if (exportName == "Base") + { + return; + } + + // Don't override anything if there are no additional elements. + if (!exportedElements.Any()) + { + return; + } + + _writer.WriteLineIndented("protected override Base SetValue(string key, object value)"); + OpenScope(); + + // switch + _writer.WriteLineIndented("switch (key)"); + OpenScope(); + + foreach (WrittenElementInfo info in exportedElements) + { + writeSetValueCase(info.FhirElementName, null, + $"{info.PropertyName} = ({info.PropertyType.PropertyTypeString})value;"); + + // if (info.PropertyType is ListTypeReference ltr) + // { + // writeSetValueCase(info.FhirElementName, $"value is IEnumerable<{ltr.Element.PropertyTypeString}> v", + // $"{info.PropertyName} = new {info.PropertyType.PropertyTypeString}(v);"); + // } + // else + // { + // writeSetValueCase(info.FhirElementName, $"value is {info.PropertyType.PropertyTypeString} v", + // $"{info.PropertyName} = v;"); + // } + // + // writeSetValueCase(info.FhirElementName, "value is null", + // $"{info.PropertyName} = null;"); + } + + void writeSetValueCase(string name, string? when, string statement) + { + _writer.WriteLineIndented(when is not null ? $"case \"{name}\" when {when}:" : $"case \"{name}\":"); + + _writer.IncreaseIndent(); + + _writer.WriteLineIndented(statement); + //_writer.WriteLineIndented($"return true;"); + _writer.WriteLineIndented($"return this;"); + _writer.DecreaseIndent(); + } + + _writer.WriteLineIndented("default:"); + _writer.IncreaseIndent(); + writeBaseTrySetValue(); + + _writer.DecreaseIndent(); + + // end switch + CloseScope(includeSemicolon: false); + + CloseScope(); + + void writeBaseTrySetValue() => _writer.WriteLineIndented("return base.SetValue(key, value);"); + } + /// Writes the children of this item. /// Name of the exported class. /// The exported elements. @@ -2523,7 +2565,8 @@ private void WriteConstrainedQuantity( // open class OpenScope(); - WritePropertyTypeName(complex.Structure.Name); + if(complex.Structure.Abstract != true) + WritePropertyTypeName(complex.Structure.Name); _writer.WriteLineIndented("public override IDeepCopyable DeepCopy()"); OpenScope(); @@ -2600,7 +2643,7 @@ private void WriteBackboneComponent( string componentName = parentExportName + "#" + explicitNamePart; WriteSerializable(); - _writer.WriteLineIndented($"[FhirType(\"{componentName}\", IsNestedType=true)]"); + _writer.WriteLineIndented($"[FhirType(\"{componentName}\")]"); _writer.WriteLineIndented($"[BackboneType(\"{complex.Element.Path}\")]"); @@ -2612,7 +2655,8 @@ private void WriteBackboneComponent( // open class OpenScope(); - WritePropertyTypeName(componentName); + if(complex.Structure.Abstract != true) + WritePropertyTypeName(componentName); WriteElements(complex, exportName, ref exportedElements, subset); @@ -3649,8 +3693,8 @@ internal static void BuildElementOptionalFlags( private void WritePropertyTypeName(string name) { WriteIndentedComment("FHIR Type Name"); - var specifier = name == "Base" ? "virtual" : "override"; - _writer.WriteLineIndented($"public {specifier} string TypeName {{ get {{ return \"{name}\"; }} }}"); + + _writer.WriteLineIndented($"public override string TypeName {{ get {{ return \"{name}\"; }} }}"); _writer.WriteLine(string.Empty); } @@ -3764,7 +3808,8 @@ private void WritePrimitiveType( // open class OpenScope(); - WritePropertyTypeName(primitive.Name); + if(primitive.Abstract != true) + WritePropertyTypeName(primitive.Name); if (!string.IsNullOrEmpty(primitive.cgpValidationRegEx())) { diff --git a/src/Microsoft.Health.Fhir.CodeGen/Microsoft.Health.Fhir.CodeGen.csproj b/src/Microsoft.Health.Fhir.CodeGen/Microsoft.Health.Fhir.CodeGen.csproj index cef55dd98..f5018a55c 100644 --- a/src/Microsoft.Health.Fhir.CodeGen/Microsoft.Health.Fhir.CodeGen.csproj +++ b/src/Microsoft.Health.Fhir.CodeGen/Microsoft.Health.Fhir.CodeGen.csproj @@ -33,7 +33,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - + @@ -51,5 +51,5 @@ - + diff --git a/src/Microsoft.Health.Fhir.CodeGenCommon/Microsoft.Health.Fhir.CodeGenCommon.csproj b/src/Microsoft.Health.Fhir.CodeGenCommon/Microsoft.Health.Fhir.CodeGenCommon.csproj index 2ed73f60f..7fbee7921 100644 --- a/src/Microsoft.Health.Fhir.CodeGenCommon/Microsoft.Health.Fhir.CodeGenCommon.csproj +++ b/src/Microsoft.Health.Fhir.CodeGenCommon/Microsoft.Health.Fhir.CodeGenCommon.csproj @@ -10,7 +10,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - + diff --git a/src/Microsoft.Health.Fhir.CrossVersion/Microsoft.Health.Fhir.CrossVersion.csproj b/src/Microsoft.Health.Fhir.CrossVersion/Microsoft.Health.Fhir.CrossVersion.csproj index 7c434ab8b..735bed944 100644 --- a/src/Microsoft.Health.Fhir.CrossVersion/Microsoft.Health.Fhir.CrossVersion.csproj +++ b/src/Microsoft.Health.Fhir.CrossVersion/Microsoft.Health.Fhir.CrossVersion.csproj @@ -19,7 +19,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - + diff --git a/src/Microsoft.Health.Fhir.MappingLanguage/Microsoft.Health.Fhir.MappingLanguage.csproj b/src/Microsoft.Health.Fhir.MappingLanguage/Microsoft.Health.Fhir.MappingLanguage.csproj index 594c46597..68c146ff8 100644 --- a/src/Microsoft.Health.Fhir.MappingLanguage/Microsoft.Health.Fhir.MappingLanguage.csproj +++ b/src/Microsoft.Health.Fhir.MappingLanguage/Microsoft.Health.Fhir.MappingLanguage.csproj @@ -16,7 +16,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - + diff --git a/src/fhir-codegen/fhir-codegen.csproj b/src/fhir-codegen/fhir-codegen.csproj index 270a5021f..574351448 100644 --- a/src/fhir-codegen/fhir-codegen.csproj +++ b/src/fhir-codegen/fhir-codegen.csproj @@ -37,7 +37,7 @@ - +