From 66f7b426369c3111e9a8c0b85c55946588e28378 Mon Sep 17 00:00:00 2001 From: Ewout Kramer Date: Tue, 9 Jul 2024 17:54:00 +0200 Subject: [PATCH 1/3] Now generating code for Base, DataType and PrimitiveType. --- src/Hl7.Fhir.Base/Model/Base.cs | 61 +-------- src/Hl7.Fhir.Base/Model/Generated/Base.cs | 118 ++++++++++++++++++ src/Hl7.Fhir.Base/Model/Generated/DataType.cs | 18 --- .../Model/Generated/PrimitiveType.cs | 98 +++++++++++++++ .../Model/Generated/_GeneratorLog.cs | 2 + src/Hl7.Fhir.Base/Model/PrimitiveType.cs | 58 +-------- 6 files changed, 221 insertions(+), 134 deletions(-) create mode 100644 src/Hl7.Fhir.Base/Model/Generated/Base.cs create mode 100644 src/Hl7.Fhir.Base/Model/Generated/PrimitiveType.cs diff --git a/src/Hl7.Fhir.Base/Model/Base.cs b/src/Hl7.Fhir.Base/Model/Base.cs index 2090c5534d..8b17c44b88 100644 --- a/src/Hl7.Fhir.Base/Model/Base.cs +++ b/src/Hl7.Fhir.Base/Model/Base.cs @@ -40,37 +40,9 @@ POSSIBILITY OF SUCH DAMAGE. namespace Hl7.Fhir.Model { - [Serializable] - [FhirType("Base", "http://hl7.org/fhir/StructureDefinition/Base")] - [DataContract] - public abstract class Base : IDeepCopyable, IDeepComparable, + public abstract partial class Base : IDeepCopyable, IDeepComparable, IAnnotated, IAnnotatable, IValidatableObject, INotifyPropertyChanged, IReadOnlyDictionary { - public virtual bool IsExactly(IDeepComparable other) => other is Base; - - public virtual bool Matches(IDeepComparable other) => other is Base; - - /// - /// - /// - /// - /// Does a deep-copy of all elements, except UserData - /// - public virtual IDeepCopyable CopyTo(IDeepCopyable other) - { - if (other is Base dest) - { - if (_annotations is not null) - dest.annotations.AddRange(annotations); - - return dest; - } - else - throw new ArgumentException("Can only copy to an object of the same type", nameof(other)); - } - - public abstract IDeepCopyable DeepCopy(); - public virtual IEnumerable Validate(ValidationContext validationContext) => Enumerable.Empty(); #region << Annotations >> @@ -95,27 +67,6 @@ protected virtual void OnPropertyChanged(String property) => #endregion - public virtual string TypeName => "Base"; - - /// - /// Enumerate all child nodes. - /// Return a sequence of child elements, components and/or properties. - /// Child nodes are returned in the order defined by the FHIR specification. - /// First returns child nodes inherited from any base class(es), recursively. - /// Finally returns child nodes defined by the current class. - /// - public virtual IEnumerable Children { get { return Enumerable.Empty(); } } - - /// - /// Enumerate all child nodes. - /// Return a sequence of child elements, components and/or properties. - /// Child nodes are returned as tuples with the name and the node itself, in the order defined - /// by the FHIR specification. - /// First returns child nodes inherited from any base class(es), recursively. - /// Finally returns child nodes defined by the current class. - /// - public virtual IEnumerable NamedChildren => Enumerable.Empty(); - public IReadOnlyDictionary AsReadOnlyDictionary() => this; #region IReadOnlyDictionary @@ -135,13 +86,5 @@ protected virtual void OnPropertyChanged(String property) => bool IReadOnlyDictionary.TryGetValue(string key, out object value) => TryGetValue(key, out value); #endregion - - protected virtual bool TryGetValue(string key, out object value) - { - value = default; - return false; - } - - protected virtual IEnumerable> GetElementPairs() => Enumerable.Empty>(); } -} +} \ No newline at end of file diff --git a/src/Hl7.Fhir.Base/Model/Generated/Base.cs b/src/Hl7.Fhir.Base/Model/Generated/Base.cs new file mode 100644 index 0000000000..6e15e5f2d9 --- /dev/null +++ b/src/Hl7.Fhir.Base/Model/Generated/Base.cs @@ -0,0 +1,118 @@ +// +// Contents of: hl7.fhir.r5.expansions#5.0.0, hl7.fhir.r5.core#5.0.0 + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.Serialization; +using Hl7.Fhir.Introspection; +using Hl7.Fhir.Serialization; +using Hl7.Fhir.Specification; +using Hl7.Fhir.Utility; +using Hl7.Fhir.Validation; +using SystemPrimitive = Hl7.Fhir.ElementModel.Types; + +/* + Copyright (c) 2011+, HL7, Inc. + All rights reserved. + + Redistribution and use in source and binary forms, with or without modification, + are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + * Neither the name of HL7 nor the names of its contributors may be used to + endorse or promote products derived from this software without specific + prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. + +*/ + +namespace Hl7.Fhir.Model +{ + /// + /// Base for all types and resources + /// + /// + /// Base definition for all types defined in FHIR type system. + /// + [Serializable] + [DataContract] + [FhirType("Base","http://hl7.org/fhir/StructureDefinition/Base")] + public abstract partial class Base + { + /// + /// FHIR Type Name + /// + public virtual string TypeName { get { return "Base"; } } + + public virtual IDeepCopyable CopyTo(IDeepCopyable other) + { + var dest = other as Base; + + if (dest == null) + { + throw new ArgumentException("Can only copy to an object of the same type", "other"); + } + + if (_annotations is not null) + dest.annotations.AddRange(annotations); + + return dest; + } + + public virtual IDeepCopyable DeepCopy() => + CopyTo((IDeepCopyable)Activator.CreateInstance(GetType())!); + + /// + public virtual bool Matches(IDeepComparable other) => other is Base; + + public virtual bool IsExactly(IDeepComparable other) => other is Base; + + /// + /// Enumerate all child nodes. + /// Return a sequence of child elements, components and/or properties. + /// Child nodes are returned in the order defined by the FHIR specification. + /// First returns child nodes inherited from any base class(es), recursively. + /// Finally returns child nodes defined by the current class. + /// + [IgnoreDataMember] + public virtual IEnumerable Children => Enumerable.Empty(); + + /// + /// Enumerate all child nodes. + /// Return a sequence of child elements, components and/or properties. + /// Child nodes are returned as tuples with the name and the node itself, in the order defined + /// by the FHIR specification. + /// First returns child nodes inherited from any base class(es), recursively. + /// Finally returns child nodes defined by the current class. + /// + [IgnoreDataMember] + public virtual IEnumerable NamedChildren => Enumerable.Empty(); + + protected virtual bool TryGetValue(string key, out object value) + { + value = default; + return false; + } + + protected virtual IEnumerable> GetElementPairs() => Enumerable.Empty>(); + + } + +} + +// end of file diff --git a/src/Hl7.Fhir.Base/Model/Generated/DataType.cs b/src/Hl7.Fhir.Base/Model/Generated/DataType.cs index 527d1ca957..455d463204 100644 --- a/src/Hl7.Fhir.Base/Model/Generated/DataType.cs +++ b/src/Hl7.Fhir.Base/Model/Generated/DataType.cs @@ -93,24 +93,6 @@ public override bool IsExactly(IDeepComparable other) return true; } - [IgnoreDataMember] - public override IEnumerable Children - { - get - { - foreach (var item in base.Children) yield return item; - } - } - - [IgnoreDataMember] - public override IEnumerable NamedChildren - { - get - { - foreach (var item in base.NamedChildren) yield return item; - } - } - } } diff --git a/src/Hl7.Fhir.Base/Model/Generated/PrimitiveType.cs b/src/Hl7.Fhir.Base/Model/Generated/PrimitiveType.cs new file mode 100644 index 0000000000..1cf3c9ce81 --- /dev/null +++ b/src/Hl7.Fhir.Base/Model/Generated/PrimitiveType.cs @@ -0,0 +1,98 @@ +// +// Contents of: hl7.fhir.r5.expansions#5.0.0, hl7.fhir.r5.core#5.0.0 + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.Serialization; +using Hl7.Fhir.Introspection; +using Hl7.Fhir.Serialization; +using Hl7.Fhir.Specification; +using Hl7.Fhir.Utility; +using Hl7.Fhir.Validation; +using SystemPrimitive = Hl7.Fhir.ElementModel.Types; + +/* + Copyright (c) 2011+, HL7, Inc. + All rights reserved. + + Redistribution and use in source and binary forms, with or without modification, + are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + * Neither the name of HL7 nor the names of its contributors may be used to + endorse or promote products derived from this software without specific + prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. + +*/ + +namespace Hl7.Fhir.Model +{ + /// + /// Parent type for DataTypes with a simple value + /// + /// + /// The base type for all re-useable types defined that have a simple property. + /// + [Serializable] + [DataContract] + [FhirType("PrimitiveType","http://hl7.org/fhir/StructureDefinition/PrimitiveType")] + public abstract partial class PrimitiveType : Hl7.Fhir.Model.DataType + { + /// + /// FHIR Type Name + /// + public override string TypeName { get { return "PrimitiveType"; } } + + public override IDeepCopyable CopyTo(IDeepCopyable other) + { + var dest = other as PrimitiveType; + + if (dest == null) + { + throw new ArgumentException("Can only copy to an object of the same type", "other"); + } + + base.CopyTo(dest); + if (ObjectValue != null) dest.ObjectValue = ObjectValue; + return dest; + } + + /// + public override bool Matches(IDeepComparable other) => IsExactly(other); + + public override bool IsExactly(IDeepComparable other) + { + var otherT = other as PrimitiveType; + if(otherT == null) return false; + + if(!base.IsExactly(otherT)) return false; + + var otherValue = otherT.ObjectValue; + if (ObjectValue is byte[] bytes && otherValue is byte[] bytesOther) + return Enumerable.SequenceEqual(bytes, bytesOther); + else + return Equals(ObjectValue, otherT.ObjectValue); + + } + + } + +} + +// end of file diff --git a/src/Hl7.Fhir.Base/Model/Generated/_GeneratorLog.cs b/src/Hl7.Fhir.Base/Model/Generated/_GeneratorLog.cs index 378af93b7b..15a964b948 100644 --- a/src/Hl7.Fhir.Base/Model/Generated/_GeneratorLog.cs +++ b/src/Hl7.Fhir.Base/Model/Generated/_GeneratorLog.cs @@ -329,6 +329,7 @@ // Attachment.cs // BackboneElement.cs // BackboneType.cs +// Base.cs // CodeableConcept.cs // CodeableReference.cs // Coding.cs @@ -340,6 +341,7 @@ // Meta.cs // Narrative.cs // Period.cs +// PrimitiveType.cs // Quantity.cs // Range.cs // ResourceReference.cs diff --git a/src/Hl7.Fhir.Base/Model/PrimitiveType.cs b/src/Hl7.Fhir.Base/Model/PrimitiveType.cs index 4deb2f2173..5ba104e979 100644 --- a/src/Hl7.Fhir.Base/Model/PrimitiveType.cs +++ b/src/Hl7.Fhir.Base/Model/PrimitiveType.cs @@ -18,19 +18,8 @@ namespace Hl7.Fhir.Model { - /// - /// The base class for all FHIR primitives, which are defined in the datatype section of FHIR. - /// These FHIR primitives all contain a "real" primitive value and optionally an id or one or more - /// extensions. - /// - [Serializable] - [FhirType("PrimitiveType", "http://hl7.org/fhir/StructureDefinition/PrimitiveType")] - [DataContract] - public abstract class PrimitiveType : DataType + public partial class PrimitiveType { - /// - public override string TypeName { get { return "PrimitiveType"; } } - /// /// The value of the primitive, stored as an object. Will generally contain the same value as the /// `Value` property and allows the user to retrieve a primitive value regardless of actual type. @@ -68,51 +57,6 @@ protected virtual void OnObjectValueChanged() return ObjectValue is null ? null : PrimitiveTypeConverter.ConvertTo(ObjectValue); } - /// - public override IDeepCopyable CopyTo(IDeepCopyable other) - { - if (other is PrimitiveType dest) - { - base.CopyTo(dest); - if (ObjectValue != null) dest.ObjectValue = ObjectValue; - return dest; - } - else - throw new ArgumentException("Can only copy to an object of the same type", nameof(other)); - } - - /// - public override IDeepCopyable DeepCopy() - { - return CopyTo((IDeepCopyable)Activator.CreateInstance(GetType())!); - } - - /// - public override bool Matches(IDeepComparable other) => IsExactly(other); - - /// - public override bool IsExactly(IDeepComparable other) - { - if (other is not PrimitiveType otherT) return false; - - if (!base.IsExactly(other)) return false; - - var otherValue = otherT.ObjectValue; - - if (ObjectValue is byte[] bytes && otherValue is byte[] bytesOther) - return Enumerable.SequenceEqual(bytes, bytesOther); - else - return Equals(ObjectValue, otherT.ObjectValue); - } - - /// - [IgnoreDataMember] - public override IEnumerable Children => base.Children; - - /// - [IgnoreDataMember] - public override IEnumerable NamedChildren => base.NamedChildren; - /// protected override bool TryGetValue(string key, [NotNullWhen(true)] out object? value) { From 3b8ffa922b29cd6003732619c8bbd5aa61fe4b30 Mon Sep 17 00:00:00 2001 From: Ewout Kramer Date: Wed, 10 Jul 2024 11:37:54 +0200 Subject: [PATCH 2/3] Now generating code for Extension --- src/Hl7.Fhir.Base/Model/Extension.cs | 150 ++------------ .../Model/Generated/Extension.cs | 185 ++++++++++++++++++ .../Model/Generated/_GeneratorLog.cs | 1 + 3 files changed, 201 insertions(+), 135 deletions(-) create mode 100644 src/Hl7.Fhir.Base/Model/Generated/Extension.cs diff --git a/src/Hl7.Fhir.Base/Model/Extension.cs b/src/Hl7.Fhir.Base/Model/Extension.cs index 3d3bb72a9a..801f932518 100644 --- a/src/Hl7.Fhir.Base/Model/Extension.cs +++ b/src/Hl7.Fhir.Base/Model/Extension.cs @@ -40,142 +40,22 @@ POSSIBILITY OF SUCH DAMAGE. using System.Runtime.Serialization; using SystemPrimitive = Hl7.Fhir.ElementModel.Types; -namespace Hl7.Fhir.Model +namespace Hl7.Fhir.Model; + +/// +/// Optional Extensions Element +/// +[System.Diagnostics.DebuggerDisplay(@"\{Value={Value} Url={_url}}")] +[Bindable(true)] +public partial class Extension { - /// - /// Optional Extensions Element - /// - [Serializable] - [System.Diagnostics.DebuggerDisplay(@"\{Value={Value} Url={_url}}")] - [FhirType("Extension", "http://hl7.org/fhir/StructureDefinition/Extension")] - [DataContract] - [Bindable(true)] - public class Extension : DataType + public Extension() { - public Extension() - { - } - - public Extension(string url, DataType value) - { - this.Url = url; - this.Value = value; - } - - public override string TypeName { get { return "Extension"; } } - - /// - /// identifies the meaning of the extension - /// - [FhirElement("url", XmlSerialization = XmlRepresentation.XmlAttr, InSummary = true, Order = 30)] - [DeclaredType(Type = typeof(SystemPrimitive.String))] - [Cardinality(Min = 1, Max = 1)] - [UriPattern] - [DataMember] - public string? Url - { - get { return _url; } - set { _url = value; OnPropertyChanged("Url"); } - } - - private string? _url; - - /// - /// Value of extension - /// - [FhirElement("value", InSummary = true, Order = 40, Choice = ChoiceType.DatatypeChoice)] - [DataMember] - public DataType? Value - { - get { return _value; } - set { _value = value; OnPropertyChanged("Value"); } - } - - private DataType? _value; - - public override IDeepCopyable CopyTo(IDeepCopyable other) - { - if (other is Extension dest) - { - base.CopyTo(dest); - if (Url != null) dest.Url = Url; - if (Value != null) dest.Value = (Hl7.Fhir.Model.DataType)Value.DeepCopy(); - return dest; - } - else - throw new ArgumentException("Can only copy to an object of the same type", nameof(other)); - } - - public override IDeepCopyable DeepCopy() - { - return CopyTo(new Extension()); - } - - public override bool Matches(IDeepComparable other) - { - var otherT = other as Extension; - if (otherT == null) return false; - - if (!base.Matches(otherT)) return false; - if (Url != otherT.Url) return false; - if (!DeepComparable.Matches(Value, otherT.Value)) return false; - - return true; - } - - public override bool IsExactly(IDeepComparable other) - { - var otherT = other as Extension; - if (otherT == null) return false; - - if (!base.IsExactly(otherT)) return false; - if (Url != otherT.Url) return false; - if (!DeepComparable.IsExactly(Value, otherT.Value)) return false; - - return true; - } - - public override IEnumerable Children - { - get - { - foreach (var item in base.Children) yield return item; - if (Url != null) yield return new FhirUri(Url); - if (Value != null) yield return Value; - } - } - - public override IEnumerable NamedChildren - { - get - { - // Extension elements - foreach (var item in base.NamedChildren) yield return item; - if (Url != null) yield return new ElementValue("url", new FhirUri(Url)); - if (Value != null) yield return new ElementValue("value", Value); - } - } - - protected override bool TryGetValue(string key, [NotNullWhen(true)]out object? value) - { - switch (key) - { - case "url": - value = Url; - return Url is not null; - case "value": - value = Value; - return Value is not null; - default: - return base.TryGetValue(key, out value); - } - } + } - protected override IEnumerable> GetElementPairs() - { - if (Url is not null) yield return new KeyValuePair("url", Url); - if (Value is not null) yield return new KeyValuePair("value", Value); - foreach (var kvp in base.GetElementPairs()) yield return kvp; - } + public Extension(string url, DataType value) + { + this.Url = url; + this.Value = value; } -} +} \ No newline at end of file diff --git a/src/Hl7.Fhir.Base/Model/Generated/Extension.cs b/src/Hl7.Fhir.Base/Model/Generated/Extension.cs new file mode 100644 index 0000000000..1db72f29e7 --- /dev/null +++ b/src/Hl7.Fhir.Base/Model/Generated/Extension.cs @@ -0,0 +1,185 @@ +// +// Contents of: hl7.fhir.r5.expansions#5.0.0, hl7.fhir.r5.core#5.0.0 + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.Serialization; +using Hl7.Fhir.Introspection; +using Hl7.Fhir.Serialization; +using Hl7.Fhir.Specification; +using Hl7.Fhir.Utility; +using Hl7.Fhir.Validation; +using SystemPrimitive = Hl7.Fhir.ElementModel.Types; + +/* + Copyright (c) 2011+, HL7, Inc. + All rights reserved. + + Redistribution and use in source and binary forms, with or without modification, + are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + * Neither the name of HL7 nor the names of its contributors may be used to + endorse or promote products derived from this software without specific + prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. + +*/ + +namespace Hl7.Fhir.Model +{ + /// + /// Optional Extensions Element + /// + /// + /// Optional Extension Element - found in all resources. + /// + [Serializable] + [DataContract] + [FhirType("Extension","http://hl7.org/fhir/StructureDefinition/Extension")] + public partial class Extension : Hl7.Fhir.Model.DataType + { + /// + /// FHIR Type Name + /// + public override string TypeName { get { return "Extension"; } } + + /// + /// identifies the meaning of the extension + /// + [FhirElement("url", XmlSerialization = XmlRepresentation.XmlAttr, InSummary=true, Order=30)] + [DeclaredType(Type = typeof(SystemPrimitive.String))] + [UriPattern] + [Cardinality(Min=1,Max=1)] + [DataMember] + public string Url + { + get { return _Url; } + set { _Url = value; OnPropertyChanged("Url"); } + } + + private string _Url; + + /// + /// Value of extension + /// + [FhirElement("value", InSummary=true, Order=40, Choice=ChoiceType.DatatypeChoice)] + [DataMember] + public Hl7.Fhir.Model.DataType Value + { + get { return _Value; } + set { _Value = value; OnPropertyChanged("Value"); } + } + + private Hl7.Fhir.Model.DataType _Value; + + public override IDeepCopyable CopyTo(IDeepCopyable other) + { + var dest = other as Extension; + + if (dest == null) + { + throw new ArgumentException("Can only copy to an object of the same type", "other"); + } + + base.CopyTo(dest); + if(Url != null) dest.Url = Url; + if(Value != null) dest.Value = (Hl7.Fhir.Model.DataType)Value.DeepCopy(); + return dest; + } + + public override IDeepCopyable DeepCopy() + { + return CopyTo(new Extension()); + } + + /// + public override bool Matches(IDeepComparable other) + { + var otherT = other as Extension; + if(otherT == null) return false; + + if(!base.Matches(otherT)) return false; + if( Url != otherT.Url ) return false; + if( !DeepComparable.Matches(Value, otherT.Value)) return false; + + return true; + } + + public override bool IsExactly(IDeepComparable other) + { + var otherT = other as Extension; + if(otherT == null) return false; + + if(!base.IsExactly(otherT)) return false; + if(Url != otherT.Url) return false; + if( !DeepComparable.IsExactly(Value, otherT.Value)) return false; + + return true; + } + + [IgnoreDataMember] + public override IEnumerable Children + { + get + { + foreach (var item in base.Children) yield return item; + if (Url != null) yield return new FhirUri(Url); + if (Value != null) yield return Value; + } + } + + [IgnoreDataMember] + public override IEnumerable NamedChildren + { + get + { + foreach (var item in base.NamedChildren) yield return item; + if (Url != null) yield return new ElementValue("url", new FhirUri(Url)); + if (Value != null) yield return new ElementValue("value", Value); + } + } + + protected override bool TryGetValue(string key, out object value) + { + switch (key) + { + case "url": + value = Url; + return Url is not null; + case "value": + value = Value; + return Value is not null; + default: + return base.TryGetValue(key, out value); + } + + } + + protected override IEnumerable> GetElementPairs() + { + foreach (var kvp in base.GetElementPairs()) yield return kvp; + if (Url is not null) yield return new KeyValuePair("url",Url); + if (Value is not null) yield return new KeyValuePair("value",Value); + } + + } + +} + +// end of file diff --git a/src/Hl7.Fhir.Base/Model/Generated/_GeneratorLog.cs b/src/Hl7.Fhir.Base/Model/Generated/_GeneratorLog.cs index 15a964b948..1dd8034863 100644 --- a/src/Hl7.Fhir.Base/Model/Generated/_GeneratorLog.cs +++ b/src/Hl7.Fhir.Base/Model/Generated/_GeneratorLog.cs @@ -337,6 +337,7 @@ // ContactPoint.cs // DataType.cs // Element.cs +// Extension.cs // Identifier.cs // Meta.cs // Narrative.cs From 0872939548476ef64359bccdd7d8a8f2e0819d61 Mon Sep 17 00:00:00 2001 From: Ewout Kramer Date: Wed, 10 Jul 2024 15:39:40 +0200 Subject: [PATCH 3/3] Made sure elements serialized as attributes are serialized first. --- .../BaseFhirXmlPocoSerializer.cs | 34 ++++++++++++++----- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/src/Hl7.Fhir.Base/Serialization/BaseFhirXmlPocoSerializer.cs b/src/Hl7.Fhir.Base/Serialization/BaseFhirXmlPocoSerializer.cs index 93115f4e18..dc0bca302d 100644 --- a/src/Hl7.Fhir.Base/Serialization/BaseFhirXmlPocoSerializer.cs +++ b/src/Hl7.Fhir.Base/Serialization/BaseFhirXmlPocoSerializer.cs @@ -98,25 +98,28 @@ private void serializeInternal( private void serializeElement(IReadOnlyDictionary members, XmlWriter writer, SerializationFilter? filter, ClassMapping? mapping) { - foreach (var member in members) - { - var propertyMapping = mapping?.FindMappedElementByName(member.Key); + // Make sure that elements with attributes are serialized first. + var orderedMembers = members + .Select(m => (m, mapping: mapping?.FindMappedElementByName(m.Key))) + .OrderBy(p => p.mapping?.SerializationHint != XmlRepresentation.XmlAttr); - if (filter?.TryEnterMember(member.Key, member.Value, propertyMapping) == false) + foreach (var ((mKey, mValue), propertyMapping) in orderedMembers) + { + if (filter?.TryEnterMember(mKey, mValue, propertyMapping) == false) continue; var elementName = propertyMapping?.Choice == ChoiceType.DatatypeChoice ? - addSuffixToElementName(member.Key, member.Value) : member.Key; + addSuffixToElementName(mKey, mValue) : mKey; - if (member.Value is ICollection coll && member.Value is not byte[]) + if (mValue is ICollection coll and not byte[]) { foreach (var value in coll) serializeMemberValue(elementName, value, writer, filter); } else - serializeMemberValue(elementName, member.Value, writer, filter); + serializeMemberValue(elementName, mValue, writer, filter); - filter?.LeaveMember(member.Key, member.Value, propertyMapping); + filter?.LeaveMember(mKey, mValue, propertyMapping); } } @@ -186,4 +189,17 @@ protected virtual void SerializePrimitiveValue(string elementName, object value, } } -#nullable restore +#if NETSTANDARD + +file static class KvpExtensions +{ + public static void Deconstruct(this KeyValuePair kvp, out TKey key, out TValue value) + { + key = kvp.Key; + value = kvp.Value; + } +} + +#endif + +#nullable restore \ No newline at end of file