1- #nullable enable
2-
31using Microsoft . Health . Fhir . CodeGenCommon . Extensions ;
42using Microsoft . Health . Fhir . CodeGenCommon . Utils ;
53
64namespace Microsoft . Health . Fhir . CodeGen . Language . Firely ;
75
86public abstract record TypeReference ( string Name )
97{
8+ public static TypeReference BuildFromFhirTypeName ( string name , string ? vsName = null , string ? vsClass = null )
9+ {
10+ // Elements of type Code or Code<T> have their own naming/types, so handle those separately.
11+ if ( name == "code" && vsName is not null )
12+ return new CodedTypeReference ( vsName , vsClass ) ;
13+
14+ if ( PrimitiveTypeReference . IsFhirPrimitiveType ( name ) )
15+ return PrimitiveTypeReference . GetTypeReference ( name ) ;
16+
17+ // Otherwise, this is a "normal" name for a complex type.
18+ return new ComplexTypeReference ( name , MapTypeName ( name ) ) ;
19+ }
20+
1021 public abstract string PropertyTypeString { get ; }
1122
1223 internal static string MapTypeName ( string name )
@@ -43,27 +54,42 @@ public record PrimitiveTypeReference(string Name, string PocoTypeName, Type Conv
4354 public static PrimitiveTypeReference ForTypeName ( string name , Type propertyType ) =>
4455 new ( name , MapTypeName ( name ) , propertyType ) ;
4556
57+ public static readonly PrimitiveTypeReference PrimitiveType = ForTypeName ( "PrimitiveType" , typeof ( object ) ) ;
58+ public static readonly PrimitiveTypeReference Boolean = ForTypeName ( "boolean" , typeof ( bool ) ) ;
59+ public static readonly PrimitiveTypeReference Base64Binary = ForTypeName ( "base64Binary" , typeof ( byte [ ] ) ) ;
60+ public static readonly PrimitiveTypeReference Canonical = ForTypeName ( "canonical" , typeof ( string ) ) ;
61+ public static readonly PrimitiveTypeReference Code = ForTypeName ( "code" , typeof ( string ) ) ;
62+ public static readonly PrimitiveTypeReference Date = ForTypeName ( "date" , typeof ( string ) ) ;
63+ public static readonly PrimitiveTypeReference DateTime = ForTypeName ( "dateTime" , typeof ( string ) ) ;
64+ public static readonly PrimitiveTypeReference Decimal = ForTypeName ( "decimal" , typeof ( decimal ) ) ;
65+ public static readonly PrimitiveTypeReference Id = ForTypeName ( "id" , typeof ( string ) ) ;
66+ public static readonly PrimitiveTypeReference Instant = ForTypeName ( "instant" , typeof ( DateTimeOffset ) ) ;
67+ public static readonly PrimitiveTypeReference Integer = ForTypeName ( "integer" , typeof ( int ) ) ;
68+ public static readonly PrimitiveTypeReference Integer64 = ForTypeName ( "integer64" , typeof ( long ) ) ;
69+ public static readonly PrimitiveTypeReference Oid = ForTypeName ( "oid" , typeof ( string ) ) ;
70+ public static readonly PrimitiveTypeReference PositiveInt = ForTypeName ( "positiveInt" , typeof ( int ) ) ;
71+ public static readonly PrimitiveTypeReference String = ForTypeName ( "string" , typeof ( string ) ) ;
72+ public static readonly PrimitiveTypeReference Time = ForTypeName ( "time" , typeof ( string ) ) ;
73+ public static readonly PrimitiveTypeReference UnsignedInt = ForTypeName ( "unsignedInt" , typeof ( int ) ) ;
74+ public static readonly PrimitiveTypeReference Uri = ForTypeName ( "uri" , typeof ( string ) ) ;
75+ public static readonly PrimitiveTypeReference Url = ForTypeName ( "url" , typeof ( string ) ) ;
76+ public static readonly PrimitiveTypeReference Xhtml = ForTypeName ( "xhtml" , typeof ( string ) ) ;
77+ public static readonly PrimitiveTypeReference Markdown = ForTypeName ( "markdown" , typeof ( string ) ) ;
78+
4679 public static readonly IReadOnlyCollection < PrimitiveTypeReference > PrimitiveList =
4780 [
48- ForTypeName ( "base64Binary" , typeof ( byte [ ] ) ) , ForTypeName ( "boolean" , typeof ( bool ) ) ,
49- ForTypeName ( "canonical" , typeof ( string ) ) , ForTypeName ( "code" , typeof ( string ) ) ,
50- ForTypeName ( "date" , typeof ( string ) ) , ForTypeName ( "dateTime" , typeof ( string ) ) ,
51- ForTypeName ( "decimal" , typeof ( decimal ) ) , ForTypeName ( "id" , typeof ( string ) ) ,
52- ForTypeName ( "instant" , typeof ( DateTimeOffset ) ) , ForTypeName ( "integer" , typeof ( int ) ) ,
53- ForTypeName ( "integer64" , typeof ( long ) ) , ForTypeName ( "oid" , typeof ( string ) ) ,
54- ForTypeName ( "positiveInt" , typeof ( int ) ) , ForTypeName ( "string" , typeof ( string ) ) ,
55- ForTypeName ( "time" , typeof ( string ) ) , ForTypeName ( "unsignedInt" , typeof ( int ) ) ,
56- ForTypeName ( "uri" , typeof ( string ) ) , ForTypeName ( "url" , typeof ( string ) ) ,
57- ForTypeName ( "xhtml" , typeof ( string ) ) , ForTypeName ( "markdown" , typeof ( string ) )
81+ Boolean , Base64Binary , Canonical , Code , Date , DateTime , Decimal , Id ,
82+ Instant , Integer , Integer64 , Oid , PositiveInt , String , Time , UnsignedInt ,
83+ Uri , Url , Xhtml , Markdown
5884 ] ;
5985
60- private static readonly Dictionary < string , PrimitiveTypeReference > s_primitiveDictionary =
86+ private static readonly Dictionary < string , PrimitiveTypeReference > _primitiveDictionary =
6187 PrimitiveList . ToDictionary ( ptr => ptr . Name ) ;
6288
63- public static bool IsFhirPrimitiveType ( string name ) => s_primitiveDictionary . ContainsKey ( name ) ;
89+ public static bool IsFhirPrimitiveType ( string name ) => _primitiveDictionary . ContainsKey ( name ) ;
6490
6591 public static PrimitiveTypeReference GetTypeReference ( string name ) =>
66- s_primitiveDictionary . TryGetValue ( name , out var tr )
92+ _primitiveDictionary . TryGetValue ( name , out var tr )
6793 ? tr
6894 : throw new InvalidOperationException ( $ "Unknown FHIR primitive { name } ") ;
6995
@@ -83,10 +109,12 @@ public record CqlTypeReference(string Name, Type PropertyType) : TypeReference(N
83109
84110public record ComplexTypeReference ( string Name , string PocoTypeName ) : TypeReference ( Name )
85111{
112+ public ComplexTypeReference ( string name ) : this ( name , name ) { }
113+
86114 public override string PropertyTypeString => $ "Hl7.Fhir.Model.{ PocoTypeName } ";
87- }
88115
89- public record ChoiceTypeReference ( ) : ComplexTypeReference ( "DataType" , "DataType" ) ;
116+ public static readonly ComplexTypeReference DataTypeReference = new ( "DataType" ) ;
117+ }
90118
91119public record CodedTypeReference ( string EnumName , string ? EnumClassName )
92120 : PrimitiveTypeReference ( "code" , EnumName , typeof ( Enum ) )
0 commit comments