Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#2901 Add SetValue method #42

Merged
merged 1 commit into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 88 additions & 33 deletions src/Microsoft.Health.Fhir.CodeGen/Language/Firely/CSharpFirely2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -1954,7 +1944,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))
Expand Down Expand Up @@ -2093,23 +2084,21 @@ private string DetermineExportedBaseTypeName(string baseTypeName)
return baseTypeName;
}

private void WriteIDictionarySupport(string exportName, IEnumerable<WrittenElementInfo> exportedElements)
private void WriteIDictionarySupport(string exportName, List<WrittenElementInfo> 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<WrittenElementInfo> exportedElements)
private void WriteDictionaryPairs(string exportName, List<WrittenElementInfo> 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<KeyValuePair<string, object>> GetElementPairs() => Enumerable.Empty<KeyValuePair<string, object>>();");
_writer.WriteLine(string.Empty);
return;
}

Expand All @@ -2132,18 +2121,13 @@ private void WriteDictionaryPairs(string exportName, IEnumerable<WrittenElementI
CloseScope();
}

private void WriteDictionaryTryGetValue(string exportName, IEnumerable<WrittenElementInfo> exportedElements)
private void WriteDictionaryTryGetValue(string exportName, List<WrittenElementInfo> 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())
Expand Down Expand Up @@ -2188,6 +2172,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<WrittenElementInfo> 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);");
}

/// <summary>Writes the children of this item.</summary>
/// <param name="exportName">Name of the exported class.</param>
/// <param name="exportedElements">The exported elements.</param>
Expand Down Expand Up @@ -2523,7 +2575,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();
Expand Down Expand Up @@ -2612,7 +2665,8 @@ private void WriteBackboneComponent(
// open class
OpenScope();

WritePropertyTypeName(componentName);
if(complex.Structure.Abstract != true)
WritePropertyTypeName(componentName);

WriteElements(complex, exportName, ref exportedElements, subset);

Expand Down Expand Up @@ -3649,8 +3703,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);
}
Expand Down Expand Up @@ -3764,7 +3818,8 @@ private void WritePrimitiveType(
// open class
OpenScope();

WritePropertyTypeName(primitive.Name);
if(primitive.Abstract != true)
WritePropertyTypeName(primitive.Name);

if (!string.IsNullOrEmpty(primitive.cgpValidationRegEx()))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="System.Text.Json" Version="8.0.4" />
<PackageReference Include="System.Text.Json" Version="8.0.5" />
</ItemGroup>

<ItemGroup>
Expand All @@ -51,5 +51,5 @@
<ItemGroup>
<Folder Include="Polyfill\" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="System.Text.Json" Version="8.0.4" />
<PackageReference Include="System.Text.Json" Version="8.0.5" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="System.Text.Json" Version="8.0.4" />
<PackageReference Include="System.Text.Json" Version="8.0.5" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="System.Text.Json" Version="8.0.4" />
<PackageReference Include="System.Text.Json" Version="8.0.5" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/fhir-codegen/fhir-codegen.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
<PackageReference Include="System.CommandLine.NamingConventionBinder" Version="2.0.0-beta4.22272.1" />
<PackageReference Include="System.Text.Json" Version="8.0.4" />
<PackageReference Include="System.Text.Json" Version="8.0.5" />
</ItemGroup>

<ItemGroup>
Expand Down