66 * available at https://raw.githubusercontent.com/FirelyTeam/firely-net-sdk/master/LICENSE
77 */
88
9+ using Hl7 . Fhir . Model ;
910using Hl7 . Fhir . Specification ;
11+ using Hl7 . Fhir . Support . Poco ;
1012using Hl7 . Fhir . Utility ;
1113using System ;
12- using System . Collections ;
1314using System . Collections . Generic ;
15+ using System . Diagnostics . CodeAnalysis ;
1416using System . Linq ;
1517
1618#nullable enable
1719
1820namespace Hl7 . Fhir . ElementModel
1921{
20- public class ScopedNode : ITypedElement , IAnnotated , IExceptionSource
22+ public class ScopedNode : IScopedNode , IAnnotated , IExceptionSource
2123 {
2224 private class Cache
2325 {
@@ -54,7 +56,6 @@ private ScopedNode(ScopedNode parentNode, ScopedNode? parentResource, ITypedElem
5456
5557 if ( Current . Name == "entry" )
5658 _fullUrl = Current . Children ( "fullUrl" ) . FirstOrDefault ( ) ? . Value as string ?? _fullUrl ;
57-
5859 }
5960
6061 public ExceptionNotificationHandler ? ExceptionHandler { get ; set ; }
@@ -72,7 +73,7 @@ private ScopedNode(ScopedNode parentNode, ScopedNode? parentResource, ITypedElem
7273 /// <summary>
7374 /// The resource or element which is the direct parent of this node.
7475 /// </summary>
75- public readonly ScopedNode ? Parent ;
76+ public IScopedNode ? Parent { get ; }
7677
7778 /// <summary>
7879 /// Returns the location of the current element within its most direct parent resource or datatype.
@@ -87,6 +88,19 @@ private ScopedNode(ScopedNode parentNode, ScopedNode? parentResource, ITypedElem
8788 /// <inheritdoc/>
8889 public string Name => Current . Name ;
8990
91+ /// <summary>
92+ /// Will be replaced by a different implementation in the future.
93+ /// </summary>
94+ public NodeType Type => this switch
95+ {
96+ { AtResource : true } when Current . Children ( "contained" ) . Any ( ) => NodeType . DomainResource | NodeType . Resource ,
97+ { InstanceType : FhirTypeConstants . BUNDLE } => NodeType . Bundle | NodeType . Resource ,
98+ { AtResource : true } => NodeType . Resource ,
99+ { InstanceType : FhirTypeConstants . REFERENCE or FhirTypeConstants . CANONICAL or FhirTypeConstants . CODEABLEREFERENCE } => NodeType . Reference ,
100+ { Value : not null } => NodeType . Primitive ,
101+ _ => 0
102+ } ;
103+
90104 /// <inheritdoc/>
91105 public string ? InstanceType => Current . InstanceType ;
92106
@@ -96,6 +110,12 @@ private ScopedNode(ScopedNode parentNode, ScopedNode? parentResource, ITypedElem
96110 /// <inheritdoc/>
97111 public string Location => Current . Location ;
98112
113+ public bool TryResolveBundleEntry ( string fullUrl , [ NotNullWhen ( true ) ] out IScopedNode ? result )
114+ => ( result = ( ( ReferencedResourceCache ) this . BundledResources ( ) ) . ResolveReference ( fullUrl ) ) is not null ;
115+
116+ public bool TryResolveContainedEntry ( string id , [ NotNullWhen ( true ) ] out IScopedNode ? result )
117+ => ( result = ( this . ContainedResourcesWithId ( ) ) . ResolveReference ( id ) ) is not null ;
118+
99119 /// <summary>
100120 /// Whether this node is a root element of a Resource.
101121 /// </summary>
@@ -264,7 +284,13 @@ private set
264284 public IEnumerable < object > Annotations ( Type type ) => type == typeof ( ScopedNode ) ? ( new [ ] { this } ) : Current . Annotations ( type ) ;
265285
266286 /// <inheritdoc />
267- public IEnumerable < ITypedElement > Children ( string ? name = null ) =>
287+ IEnumerable < ITypedElement > ITypedElement . Children ( string ? name ) =>
268288 Current . Children ( name ) . Select ( c => new ScopedNode ( this , ParentResource , c , _fullUrl ) ) ;
289+
290+ /// <inheritdoc />
291+ public IEnumerable < IScopedNode > Children ( string ? name = null ) =>
292+ Current . Children ( name ) . Select ( c => new ScopedNode ( this , ParentResource , c , _fullUrl ) ) ;
293+
294+ public string ShortPath => Current is ElementNode en ? en . ShortPath : Current . Location ;
269295 }
270296}
0 commit comments