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

Removed IDeepCopyable #2996

Open
wants to merge 9 commits into
base: develop-6.0
Choose a base branch
from
Open
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
35 changes: 35 additions & 0 deletions src/Hl7.Fhir.Base/CompatibilitySuppressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@
<Right>lib/net8.0/Hl7.Fhir.Base.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
<Suppression>
<DiagnosticId>CP0001</DiagnosticId>
<Target>T:Hl7.Fhir.Model.IDeepCopyable</Target>
<Left>lib/net8.0/Hl7.Fhir.Base.dll</Left>
<Right>lib/net8.0/Hl7.Fhir.Base.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
<Suppression>
<DiagnosticId>CP0001</DiagnosticId>
<Target>T:Hl7.Fhir.Model.ListCopyExtensions</Target>
<Left>lib/net8.0/Hl7.Fhir.Base.dll</Left>
<Right>lib/net8.0/Hl7.Fhir.Base.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
<Suppression>
<DiagnosticId>CP0001</DiagnosticId>
<Target>T:Hl7.Fhir.Serialization.DefaultModelFactory</Target>
Expand Down Expand Up @@ -295,6 +309,20 @@
<Right>lib/net8.0/Hl7.Fhir.Base.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>M:Hl7.Fhir.Model.Base.CopyTo(Hl7.Fhir.Model.IDeepCopyable)</Target>
<Left>lib/net8.0/Hl7.Fhir.Base.dll</Left>
<Right>lib/net8.0/Hl7.Fhir.Base.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>M:Hl7.Fhir.Model.Base.DeepCopy</Target>
<Left>lib/net8.0/Hl7.Fhir.Base.dll</Left>
<Right>lib/net8.0/Hl7.Fhir.Base.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>M:Hl7.Fhir.Model.Base.get_Children</Target>
Expand Down Expand Up @@ -631,6 +659,13 @@
<Right>lib/net8.0/Hl7.Fhir.Base.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
<Suppression>
<DiagnosticId>CP0005</DiagnosticId>
<Target>M:Hl7.Fhir.Model.Base.DeepCopyInternal</Target>
<Left>lib/net8.0/Hl7.Fhir.Base.dll</Left>
<Right>lib/net8.0/Hl7.Fhir.Base.dll</Right>
<IsBaselineSuppression>true</IsBaselineSuppression>
</Suppression>
<Suppression>
<DiagnosticId>CP0006</DiagnosticId>
<Target>M:Hl7.Fhir.ElementModel.ITypedElement.Children(System.String)</Target>
Expand Down
25 changes: 24 additions & 1 deletion src/Hl7.Fhir.Base/Model/Base.Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace Hl7.Fhir.Model;

Expand All @@ -25,12 +26,34 @@ 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);

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 @@ -54,6 +54,13 @@ static Code()
throw new ArgumentException("T must be an enumerated type");
}

protected internal override Base DeepCopyInternal()
{
var instance = new Code<T>();
CopyToInternal(instance);
return instance;
}

public override string TypeName => "code";

public Code() : this(null) { }
Expand Down
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();
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();
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();
CopyToInternal(instance);
return instance;
}
}
45 changes: 23 additions & 22 deletions src/Hl7.Fhir.Base/Model/Generated/Attachment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ public string Url
if (value == null)
UrlElement = null;
else
UrlElement = new Hl7.Fhir.Model.FhirUri(value);
UrlElement = new Hl7.Fhir.Model.FhirUrl(value);
OnPropertyChanged("Url");
}
}
Expand Down 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
Loading