@@ -28,6 +28,7 @@ POSSIBILITY OF SUCH DAMAGE.
2828
2929*/
3030
31+ #nullable enable
3132
3233using Hl7 . Fhir . Introspection ;
3334using Hl7 . Fhir . Specification ;
@@ -40,81 +41,76 @@ POSSIBILITY OF SUCH DAMAGE.
4041using COVE = Hl7 . Fhir . Validation . CodedValidationException ;
4142using S = Hl7 . Fhir . ElementModel . Types ;
4243
43- namespace Hl7 . Fhir . Model
44+ namespace Hl7 . Fhir . Model ;
45+
46+ /// <summary>
47+ /// A <see cref="Code"/> that has a limited set of values and which <see cref="Code.Value"/> can therefore
48+ /// be represented as an enumerated type.
49+ /// </summary>
50+ [ Serializable ]
51+ [ FhirType ( "codeOfT" ) ]
52+ [ DataContract ]
53+ [ System . Diagnostics . DebuggerDisplay ( @"\{Value={Value}}" ) ]
54+ public class Code < T > : Code , INullableValue < T > where T : struct , Enum
4455{
45- /// <summary>
46- /// A <see cref="Code"/> that has a limited set of values and which <see cref="Code.Value"/> can therefore
47- /// be represented as an enumerated type.
48- /// </summary>
49- [ Serializable ]
50- [ FhirType ( "codeOfT" ) ]
51- [ DataContract ]
52- [ System . Diagnostics . DebuggerDisplay ( @"\{Value={Value}}" ) ]
53- public class Code < T > : Code , INullableValue < T > , ISystemAndCode where T : struct , Enum
56+ static Code ( )
5457 {
55- static Code ( )
56- {
57- if ( ! typeof ( T ) . IsEnum ( ) )
58- throw new ArgumentException ( "T must be an enumerated type" ) ;
59- }
58+ if ( ! typeof ( T ) . IsEnum ( ) )
59+ throw new ArgumentException ( "T must be an enumerated type" ) ;
60+ }
6061
61- public override string TypeName => "code" ;
62+ public override string TypeName => "code" ;
6263
63- public Code ( ) : this ( null ) { }
64+ public Code ( ) : this ( null ) { }
6465
65- public Code ( T ? value )
66- {
67- Value = value ;
68- }
66+ public Code ( T ? value )
67+ {
68+ Value = value ;
69+ }
6970
70- // Primitive value of element
71- [ FhirElement ( "value" , IsPrimitiveValue = true , XmlSerialization = XmlRepresentation . XmlAttr , InSummary = true , Order = 30 ) ]
72- [ DataMember ]
73- new public T ? Value
71+ // Primitive value of element
72+ [ FhirElement ( "value" , IsPrimitiveValue = true , XmlSerialization = XmlRepresentation . XmlAttr , InSummary = true , Order = 30 ) ]
73+ [ DataMember ]
74+ new public T ? Value
75+ {
76+ get => TryParseObjectValue ( out var value )
77+ ? value
78+ : throw new InvalidCastException ( $ "Value '{ ObjectValue } ' cannot be cast to a member of enumeration { typeof ( T ) . Name } .") ;
79+ set
7480 {
75- get => TryParseObjectValue ( out var value )
76- ? value
77- : throw new InvalidCastException ( $ "Value '{ ObjectValue } ' cannot be cast to a member of enumeration { typeof ( T ) . Name } .") ;
78- set
79- {
80- ObjectValue = value ? . GetLiteral ( ) ;
81- OnPropertyChanged ( "Value" ) ;
82- }
81+ ObjectValue = value ? . GetLiteral ( ) ;
82+ OnPropertyChanged ( "Value" ) ;
8383 }
84+ }
85+
86+ internal bool TryParseObjectValue ( out T ? value )
87+ {
88+ value = default ;
8489
85- internal bool TryParseObjectValue ( out T ? value )
90+ if ( ObjectValue is string s && EnumUtility . ParseLiteral < T > ( s ) is { } parsed )
8691 {
87- value = default ;
88-
89- if ( ObjectValue is string s && EnumUtility . ParseLiteral < T > ( s ) is { } parsed )
90- {
91- value = parsed ;
92- return true ;
93- }
94- else return ObjectValue is null ;
92+ value = parsed ;
93+ return true ;
9594 }
95+ else return ObjectValue is null ;
96+ }
9697
97- string ISystemAndCode . System => Value ? . GetSystem ( ) ;
98+ public override IEnumerable < Coding > ToCodings ( ) => [ new ( Value ? . GetSystem ( ) , Value ? . GetLiteral ( ) ) ] ;
9899
99- string ISystemAndCode . Code => Value ? . GetLiteral ( ) ;
100+ public override S . Code ToSystemCode ( ) =>
101+ new ( Value ? . GetSystem ( ) ,
102+ Value ? . GetLiteral ( ) ?? throw new InvalidOperationException ( "Code must have a value in order to be useable to construct a System.Code." ) ,
103+ display : null ,
104+ version : null ) ;
100105
101- public override S . Code ToSystemCode ( ) =>
102- new ( Value ? . GetSystem ( ) ,
103- Value ? . GetLiteral ( ) ?? throw new InvalidOperationException ( "Code must have a value in order to be useable to construct a System.Code." ) ,
104- display : null ,
105- version : null ) ;
106+ public override IEnumerable < ValidationResult > Validate ( ValidationContext validationContext )
107+ {
108+ var baseResults = base . Validate ( validationContext ) ;
106109
107- public override IEnumerable < ValidationResult > Validate ( ValidationContext validationContext )
108- {
109- var baseResults = base . Validate ( validationContext ) ;
110-
111- if ( TryParseObjectValue ( out _ ) )
112- return baseResults ;
113- else
114- {
115- var result = COVE . INVALID_CODED_VALUE ( validationContext , ObjectValue , EnumUtility . GetName < T > ( ) ) . AsResult ( validationContext ) ;
116- return baseResults . Append ( result ) ;
117- }
118- }
110+ if ( TryParseObjectValue ( out _ ) )
111+ return baseResults ;
112+
113+ var result = COVE . INVALID_CODED_VALUE ( validationContext , ObjectValue , EnumUtility . GetName < T > ( ) ) . AsResult ( validationContext ) ;
114+ return baseResults . Append ( result ) ;
119115 }
120116}
0 commit comments