Skip to content
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
26 changes: 25 additions & 1 deletion src/Hl7.Fhir.Base/Model/Base.Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,36 @@ public static IEnumerable<Base> Children(this Base instance)
foreach (var item in list)
yield return item;
break;
case("value", _) when instance is PrimitiveType:
case ("value", _) when instance is PrimitiveType:
yield break;
default:
yield return (Base)element.Value;
break;
}
}
}

public static T DeepCopy<T>(this T source) where T : Base => (T)source.DeepCopyInternal();

public static void CopyTo<T>(this T source, T target) where T : Base => source.CopyToInternal(target);

public static IEnumerable<T> DeepCopy<T>(this IEnumerable<T> source) where T : Base => source.DeepCopyInternal();

internal static IEnumerable<T> DeepCopyInternal<T>(this IEnumerable<T> source) where T : Base
{
return source.Select(item => item.DeepCopy()).ToList();
}

internal static void CopyToInternal(this Dictionary<string, object> source, Dictionary<string, object> target)
{
foreach ((string key, object value) in source)
{
target[key] = value switch
{
Base baseValue => baseValue.DeepCopy(),
IEnumerable<Base> baseList => baseList.DeepCopyInternal(),
_ => throw new InvalidOperationException($"Unexpected type in overflow: key {key} is of type {value.GetType()}, but either Base or IEnumerable<Base> was expected.")
};
}
}
}
16 changes: 8 additions & 8 deletions src/Hl7.Fhir.Base/Model/Base.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ POSSIBILITY OF SUCH DAMAGE.

namespace Hl7.Fhir.Model;

public abstract partial class Base : IDeepCopyable, IAnnotatable,
public abstract partial class Base : IAnnotatable,
IValidatableObject, INotifyPropertyChanged
{
/// <summary>
Expand All @@ -64,13 +64,13 @@ public abstract partial class Base : IDeepCopyable, IAnnotatable,

private AnnotationList annotations => LazyInitializer.EnsureInitialized(ref _annotations, () => [])!;

public IEnumerable<object> Annotations(Type type)
{
if (type == typeof(IFhirValueProvider))
return [this];
else
return annotations.OfType(type);
}
public IEnumerable<object> Annotations(Type type)
{
if (type == typeof(IFhirValueProvider))
return [this];
else
return annotations.OfType(type);
}

public void AddAnnotation(object annotation) => annotations.AddAnnotation(annotation);

Expand Down
7 changes: 7 additions & 0 deletions src/Hl7.Fhir.Base/Model/CodeOfT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,11 @@ public override IEnumerable<ValidationResult> Validate(ValidationContext validat
var result = COVE.INVALID_CODED_VALUE(validationContext, ObjectValue, EnumUtility.GetName<T>()).AsResult(validationContext);
return baseResults.Append(result);
}

protected internal override Base DeepCopyInternal()
{
var instance = new Code<T>();
CopyToInternal(instance);
return instance;
}
}
21 changes: 21 additions & 0 deletions src/Hl7.Fhir.Base/Model/DynamicDataType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ public class DynamicDataType : DataType, IDynamicType
public string? DynamicTypeName { get; set; }

public override string TypeName => DynamicTypeName ?? base.TypeName;

protected internal override Base DeepCopyInternal()
{
var instance = new DynamicDataType { DynamicTypeName = DynamicTypeName };
CopyToInternal(instance);
return instance;
}
}


Expand All @@ -39,6 +46,13 @@ public class DynamicResource : Resource, IDynamicType
public string? DynamicTypeName { get; set; }

public override string TypeName => DynamicTypeName ?? base.TypeName;

protected internal override Base DeepCopyInternal()
{
var instance = new DynamicResource { DynamicTypeName = DynamicTypeName };
CopyToInternal(instance);
return instance;
}
}


Expand All @@ -53,4 +67,11 @@ public class DynamicPrimitive : PrimitiveType, IDynamicType
public string? DynamicTypeName { get; set; }

public override string TypeName => DynamicTypeName ?? base.TypeName;

protected internal override Base DeepCopyInternal()
{
var instance = new DynamicPrimitive { DynamicTypeName = DynamicTypeName };
CopyToInternal(instance);
return instance;
}
}
43 changes: 22 additions & 21 deletions src/Hl7.Fhir.Base/Model/Generated/Attachment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ public int? Pages
}
}

public override IDeepCopyable CopyTo(IDeepCopyable other)
protected internal override void CopyToInternal(Base other)
{
var dest = other as Attachment;

Expand All @@ -545,26 +545,27 @@ public override IDeepCopyable CopyTo(IDeepCopyable other)
throw new ArgumentException("Can only copy to an object of the same type", "other");
}

base.CopyTo(dest);
if(ContentTypeElement != null) dest.ContentTypeElement = (Hl7.Fhir.Model.Code)ContentTypeElement.DeepCopy();
if(LanguageElement != null) dest.LanguageElement = (Hl7.Fhir.Model.Code)LanguageElement.DeepCopy();
if(DataElement != null) dest.DataElement = (Hl7.Fhir.Model.Base64Binary)DataElement.DeepCopy();
if(UrlElement != null) dest.UrlElement = (Hl7.Fhir.Model.PrimitiveType)UrlElement.DeepCopy();
if(SizeElement != null) dest.SizeElement = (Hl7.Fhir.Model.PrimitiveType)SizeElement.DeepCopy();
if(HashElement != null) dest.HashElement = (Hl7.Fhir.Model.Base64Binary)HashElement.DeepCopy();
if(TitleElement != null) dest.TitleElement = (Hl7.Fhir.Model.FhirString)TitleElement.DeepCopy();
if(CreationElement != null) dest.CreationElement = (Hl7.Fhir.Model.FhirDateTime)CreationElement.DeepCopy();
if(HeightElement != null) dest.HeightElement = (Hl7.Fhir.Model.PositiveInt)HeightElement.DeepCopy();
if(WidthElement != null) dest.WidthElement = (Hl7.Fhir.Model.PositiveInt)WidthElement.DeepCopy();
if(FramesElement != null) dest.FramesElement = (Hl7.Fhir.Model.PositiveInt)FramesElement.DeepCopy();
if(DurationElement != null) dest.DurationElement = (Hl7.Fhir.Model.FhirDecimal)DurationElement.DeepCopy();
if(PagesElement != null) dest.PagesElement = (Hl7.Fhir.Model.PositiveInt)PagesElement.DeepCopy();
return dest;
}

public override IDeepCopyable DeepCopy()
{
return CopyTo(new Attachment());
base.CopyToInternal(dest);
if(ContentTypeElement != null) dest.ContentTypeElement = (Hl7.Fhir.Model.Code)ContentTypeElement.DeepCopyInternal();
if(LanguageElement != null) dest.LanguageElement = (Hl7.Fhir.Model.Code)LanguageElement.DeepCopyInternal();
if(DataElement != null) dest.DataElement = (Hl7.Fhir.Model.Base64Binary)DataElement.DeepCopyInternal();
if(UrlElement != null) dest.UrlElement = (Hl7.Fhir.Model.PrimitiveType)UrlElement.DeepCopyInternal();
if(SizeElement != null) dest.SizeElement = (Hl7.Fhir.Model.PrimitiveType)SizeElement.DeepCopyInternal();
if(HashElement != null) dest.HashElement = (Hl7.Fhir.Model.Base64Binary)HashElement.DeepCopyInternal();
if(TitleElement != null) dest.TitleElement = (Hl7.Fhir.Model.FhirString)TitleElement.DeepCopyInternal();
if(CreationElement != null) dest.CreationElement = (Hl7.Fhir.Model.FhirDateTime)CreationElement.DeepCopyInternal();
if(HeightElement != null) dest.HeightElement = (Hl7.Fhir.Model.PositiveInt)HeightElement.DeepCopyInternal();
if(WidthElement != null) dest.WidthElement = (Hl7.Fhir.Model.PositiveInt)WidthElement.DeepCopyInternal();
if(FramesElement != null) dest.FramesElement = (Hl7.Fhir.Model.PositiveInt)FramesElement.DeepCopyInternal();
if(DurationElement != null) dest.DurationElement = (Hl7.Fhir.Model.FhirDecimal)DurationElement.DeepCopyInternal();
if(PagesElement != null) dest.PagesElement = (Hl7.Fhir.Model.PositiveInt)PagesElement.DeepCopyInternal();
}

protected internal override Base DeepCopyInternal()
{
var instance = new Attachment();
CopyToInternal(instance);
return instance;
}

public override bool CompareChildren(Base other, IEqualityComparer<Base> comparer)
Expand Down
7 changes: 3 additions & 4 deletions src/Hl7.Fhir.Base/Model/Generated/BackboneElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public List<Hl7.Fhir.Model.Extension> ModifierExtension

private List<Hl7.Fhir.Model.Extension> _ModifierExtension;

public override IDeepCopyable CopyTo(IDeepCopyable other)
protected internal override void CopyToInternal(Base other)
{
var dest = other as BackboneElement;

Expand All @@ -77,9 +77,8 @@ public override IDeepCopyable CopyTo(IDeepCopyable other)
throw new ArgumentException("Can only copy to an object of the same type", "other");
}

base.CopyTo(dest);
if(ModifierExtension.Any()) dest.ModifierExtension = new List<Hl7.Fhir.Model.Extension>(ModifierExtension.DeepCopy());
return dest;
base.CopyToInternal(dest);
if(ModifierExtension.Any()) dest.ModifierExtension = new List<Hl7.Fhir.Model.Extension>(ModifierExtension.DeepCopyInternal());
}

public override bool CompareChildren(Base other, IEqualityComparer<Base> comparer)
Expand Down
7 changes: 3 additions & 4 deletions src/Hl7.Fhir.Base/Model/Generated/BackboneType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public List<Hl7.Fhir.Model.Extension> ModifierExtension

private List<Hl7.Fhir.Model.Extension> _ModifierExtension;

public override IDeepCopyable CopyTo(IDeepCopyable other)
protected internal override void CopyToInternal(Base other)
{
var dest = other as BackboneType;

Expand All @@ -77,9 +77,8 @@ public override IDeepCopyable CopyTo(IDeepCopyable other)
throw new ArgumentException("Can only copy to an object of the same type", "other");
}

base.CopyTo(dest);
if(ModifierExtension.Any()) dest.ModifierExtension = new List<Hl7.Fhir.Model.Extension>(ModifierExtension.DeepCopy());
return dest;
base.CopyToInternal(dest);
if(ModifierExtension.Any()) dest.ModifierExtension = new List<Hl7.Fhir.Model.Extension>(ModifierExtension.DeepCopyInternal());
}

public override bool CompareChildren(Base other, IEqualityComparer<Base> comparer)
Expand Down
8 changes: 4 additions & 4 deletions src/Hl7.Fhir.Base/Model/Generated/Base.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ namespace Hl7.Fhir.Model
[FhirType("Base","http://hl7.org/fhir/StructureDefinition/Base")]
public abstract partial class Base
{
public virtual IDeepCopyable CopyTo(IDeepCopyable other)
protected internal virtual void CopyToInternal(Base other)
{
var dest = other as Base;

Expand All @@ -66,11 +66,11 @@ public virtual IDeepCopyable CopyTo(IDeepCopyable other)
if (_annotations is not null)
dest.annotations.AddRange(annotations);

return dest;
if (Overflow.Any())
Overflow.CopyToInternal(dest.Overflow);
}

public virtual IDeepCopyable DeepCopy() =>
CopyTo((IDeepCopyable)Activator.CreateInstance(GetType())!);
protected internal abstract Base DeepCopyInternal();

}

Expand Down
7 changes: 7 additions & 0 deletions src/Hl7.Fhir.Base/Model/Generated/Base64Binary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ public byte[] Value
set { ObjectValue = value; OnPropertyChanged("Value"); }
}

protected internal override Base DeepCopyInternal()
{
var instance = new Base64Binary();
CopyToInternal(instance);
return instance;
}

}

}
Expand Down
19 changes: 10 additions & 9 deletions src/Hl7.Fhir.Base/Model/Generated/Binary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public byte[] Data
}
}

public override IDeepCopyable CopyTo(IDeepCopyable other)
protected internal override void CopyToInternal(Base other)
{
var dest = other as Binary;

Expand All @@ -189,17 +189,18 @@ public override IDeepCopyable CopyTo(IDeepCopyable other)
throw new ArgumentException("Can only copy to an object of the same type", "other");
}

base.CopyTo(dest);
if(ContentTypeElement != null) dest.ContentTypeElement = (Hl7.Fhir.Model.Code)ContentTypeElement.DeepCopy();
if(SecurityContext != null) dest.SecurityContext = (Hl7.Fhir.Model.ResourceReference)SecurityContext.DeepCopy();
if(ContentElement != null) dest.ContentElement = (Hl7.Fhir.Model.Base64Binary)ContentElement.DeepCopy();
if(DataElement != null) dest.DataElement = (Hl7.Fhir.Model.Base64Binary)DataElement.DeepCopy();
return dest;
base.CopyToInternal(dest);
if(ContentTypeElement != null) dest.ContentTypeElement = (Hl7.Fhir.Model.Code)ContentTypeElement.DeepCopyInternal();
if(SecurityContext != null) dest.SecurityContext = (Hl7.Fhir.Model.ResourceReference)SecurityContext.DeepCopyInternal();
if(ContentElement != null) dest.ContentElement = (Hl7.Fhir.Model.Base64Binary)ContentElement.DeepCopyInternal();
if(DataElement != null) dest.DataElement = (Hl7.Fhir.Model.Base64Binary)DataElement.DeepCopyInternal();
}

public override IDeepCopyable DeepCopy()
protected internal override Base DeepCopyInternal()
{
return CopyTo(new Binary());
var instance = new Binary();
CopyToInternal(instance);
return instance;
}

public override bool CompareChildren(Base other, IEqualityComparer<Base> comparer)
Expand Down
Loading