Skip to content
Merged
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
30 changes: 16 additions & 14 deletions src/Microsoft.Health.Fhir.CodeGen/Language/Firely/CSharpFirely2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1702,7 +1702,7 @@ void writeOneOnOneGetterAndSetter(string propertyType, string propertyName, bool
_writer.WriteLineIndented($"{propertyType} {interfaceExportName}.{propertyName}");
OpenScope();
_writer.WriteLineIndented($"get => {propertyName};");
_writer.WriteLineIndented($"set => {propertyName} = value;");
_writer.WriteLineIndented($"set => {propertyName} = value!;");
CloseScope();
}

Expand Down Expand Up @@ -1986,9 +1986,9 @@ private void WriteComponent(
if (identifierElement != null)
{
if (identifierElement.cgIsArray())
_writer.WriteLineIndented("List<Identifier> IIdentifiable<List<Identifier>>.Identifier { get => Identifier; set => Identifier = value; }");
_writer.WriteLineIndented("List<Identifier> IIdentifiable<List<Identifier>>.Identifier { get => Identifier; set => Identifier = value!; }");
else
_writer.WriteLineIndented("Identifier? IIdentifiable<Identifier?>.Identifier { get => Identifier; set => Identifier = value; }");
_writer.WriteLineIndented("Identifier? IIdentifiable<Identifier?>.Identifier { get => Identifier; set => Identifier = value!; }");

_writer.WriteLine(string.Empty);
}
Expand Down Expand Up @@ -2210,10 +2210,10 @@ private void WriteDictionaryTrySetValue(string exportName, List<WrittenElementIn

foreach (WrittenElementInfo info in exportedElements)
{
writeSetValueCase(info.FhirElementName, null, info.PropertyType, info.PropertyName);
writeSetValueCase(info.FhirElementName, null, info.PropertyType, info.PropertyName, info.Required);
}

void writeSetValueCase(string fhirName, string? when, TypeReference type, string propName)
void writeSetValueCase(string fhirName, string? when, TypeReference type, string propName, bool required)
{
string overflowTypeName = getDynamicTypeForAbstractTypeName(type.PropertyTypeString);

Expand All @@ -2228,7 +2228,7 @@ void writeSetValueCase(string fhirName, string? when, TypeReference type, string

// Because our list properties are never null when you get them, but can be set to null,
// we need a bang after the assignment here for lists, but not for other elements.
_writer.WriteLineIndented($"else {propName} = ({type.PropertyTypeString}?)value{(type is ListTypeReference ? "!" : "")};");
_writer.WriteLineIndented($"else {propName} = ({type.PropertyTypeString}?)value{(type is ListTypeReference || required ? "!" : "")};");
_writer.WriteLineIndented($"return this;");
_writer.DecreaseIndent();
}
Expand Down Expand Up @@ -3087,7 +3087,8 @@ internal static WrittenElementInfo BuildElementInfo(
PropertyType: typeRef,
PrimitiveHelperName: forPrimitiveType
? (pascal == exportedComplexName ? $"{pascal}_" : pascal)
: null // Since properties cannot have the same name as their enclosing types, we'll add a '_' suffix if this happens.
: null, // Since properties cannot have the same name as their enclosing types, we'll add a '_' suffix if this happens.
Required: element.Min > 0
);
}

Expand All @@ -3103,9 +3104,9 @@ private void WriteElementGettersAndSetters(ElementDefinition element, WrittenEle
{ } s => s
};

if(ei.PropertyType is ListTypeReference)
if (ei.PropertyType is ListTypeReference)
_writer.WriteLineIndented("[AllowNull]");
_writer.WriteLineIndented($"public {(ei.PropertyType is ListTypeReference ltr ? ltr.PropertyTypeString : $"{ei.PropertyType.PropertyTypeString}?")} {ei.PropertyName}");
_writer.WriteLineIndented($"public {(ei.PropertyType is ListTypeReference || ei.Required ? ei.PropertyType.PropertyTypeString : $"{ei.PropertyType.PropertyTypeString}?")} {ei.PropertyName}");

OpenScope();

Expand All @@ -3115,7 +3116,7 @@ private void WriteElementGettersAndSetters(ElementDefinition element, WrittenEle
_writer.IncreaseIndent();
_writer.WriteLineIndented($"throw CodedValidationException.FromTypes(typeof({ei.PropertyType.PropertyTypeString}), Overflow[\"{ei.FhirElementName}\"]);");
_writer.DecreaseIndent();
_writer.WriteLineIndented(ei.PropertyType is not ListTypeReference ? $"return _{ei.PropertyName};" : $"return _{ei.PropertyName} ??= [];");
_writer.WriteLineIndented(ei.PropertyType is not ListTypeReference ? $"return _{ei.PropertyName}{(ei.Required ? "!" : "")};" : $"return _{ei.PropertyName} ??= [];");

CloseScope();

Expand Down Expand Up @@ -3178,8 +3179,8 @@ private void WritePrimitiveHelperProperty(string description, WrittenElementInfo
switch (propType)
{
case PrimitiveTypeReference ptr:
string nullableType = WithNullabilityMarking(ptr.ConveniencePropertyTypeString);
_writer.WriteLineIndented($"public {nullableType} {helperPropName}");
string typeString = WithNullabilityMarking(ptr.ConveniencePropertyTypeString);
_writer.WriteLineIndented($"public {typeString} {helperPropName}");

OpenScope();
string propAccess = versionsRemark is not null
Expand All @@ -3189,7 +3190,7 @@ private void WritePrimitiveHelperProperty(string description, WrittenElementInfo

_writer.WriteLineIndented("set");
OpenScope();
_writer.WriteLineIndented($"{ei.PropertyName} = value is null ? null : new {ptr.PropertyTypeString}(value);");
_writer.WriteLineIndented($"{ei.PropertyName} = value is null ? null! : new {ptr.PropertyTypeString}(value);");
_writer.WriteLineIndented($"OnPropertyChanged(\"{helperPropName}\");");
CloseScope(suppressNewline: true);
CloseScope();
Expand Down Expand Up @@ -3801,7 +3802,8 @@ internal record WrittenElementInfo(
string FhirElementPath,
string PropertyName,
TypeReference PropertyType,
string? PrimitiveHelperName);
string? PrimitiveHelperName,
bool Required = false);

/// <summary>Information about the written model.</summary>
internal record WrittenModelInfo(string FhirName, string CsName, bool IsAbstract);
Expand Down