Skip to content

Commit a935d3c

Browse files
authored
Merge pull request #2921 from FirelyTeam/spike/define-Iscopednode
Defined IScopedNode
2 parents 4fbe347 + 77a5f18 commit a935d3c

File tree

12 files changed

+458
-238
lines changed

12 files changed

+458
-238
lines changed

src/Hl7.Fhir.Base/CompatibilitySuppressions.xml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@
88
<Right>lib/net8.0/Hl7.Fhir.Base.dll</Right>
99
<IsBaselineSuppression>true</IsBaselineSuppression>
1010
</Suppression>
11+
<Suppression>
12+
<DiagnosticId>CP0001</DiagnosticId>
13+
<Target>T:Hl7.Fhir.ElementModel.ScopedNodeExtensions</Target>
14+
<Left>lib/net8.0/Hl7.Fhir.Base.dll</Left>
15+
<Right>lib/net8.0/Hl7.Fhir.Base.dll</Right>
16+
<IsBaselineSuppression>true</IsBaselineSuppression>
17+
</Suppression>
1118
<Suppression>
1219
<DiagnosticId>CP0001</DiagnosticId>
1320
<Target>T:Hl7.Fhir.ElementModel.Types.MetricConfiguration</Target>
@@ -22,6 +29,20 @@
2229
<Right>lib/net8.0/Hl7.Fhir.Base.dll</Right>
2330
<IsBaselineSuppression>true</IsBaselineSuppression>
2431
</Suppression>
32+
<Suppression>
33+
<DiagnosticId>CP0002</DiagnosticId>
34+
<Target>F:Hl7.Fhir.ElementModel.ScopedNode.Parent</Target>
35+
<Left>lib/net8.0/Hl7.Fhir.Base.dll</Left>
36+
<Right>lib/net8.0/Hl7.Fhir.Base.dll</Right>
37+
<IsBaselineSuppression>true</IsBaselineSuppression>
38+
</Suppression>
39+
<Suppression>
40+
<DiagnosticId>CP0002</DiagnosticId>
41+
<Target>M:Hl7.Fhir.ElementModel.ScopedNode.Children(System.String)</Target>
42+
<Left>lib/net8.0/Hl7.Fhir.Base.dll</Left>
43+
<Right>lib/net8.0/Hl7.Fhir.Base.dll</Right>
44+
<IsBaselineSuppression>true</IsBaselineSuppression>
45+
</Suppression>
2546
<Suppression>
2647
<DiagnosticId>CP0002</DiagnosticId>
2748
<Target>M:Hl7.Fhir.ElementModel.TypedElementExtensions.IsExactlyEqualTo``1(``0,``0,System.Boolean)</Target>
@@ -85,6 +106,13 @@
85106
<Right>lib/net8.0/Hl7.Fhir.Base.dll</Right>
86107
<IsBaselineSuppression>true</IsBaselineSuppression>
87108
</Suppression>
109+
<Suppression>
110+
<DiagnosticId>CP0002</DiagnosticId>
111+
<Target>M:Hl7.Fhir.FhirPath.FhirEvaluationContext.get_ElementResolver</Target>
112+
<Left>lib/net8.0/Hl7.Fhir.Base.dll</Left>
113+
<Right>lib/net8.0/Hl7.Fhir.Base.dll</Right>
114+
<IsBaselineSuppression>true</IsBaselineSuppression>
115+
</Suppression>
88116
<Suppression>
89117
<DiagnosticId>CP0002</DiagnosticId>
90118
<Target>M:Hl7.Fhir.FhirPath.FhirEvaluationContext.get_TerminologyService</Target>

src/Hl7.Fhir.Base/ElementModel/ScopedNode.cs

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,20 @@
66
* available at https://raw.githubusercontent.com/FirelyTeam/firely-net-sdk/master/LICENSE
77
*/
88

9+
using Hl7.Fhir.Model;
910
using Hl7.Fhir.Specification;
11+
using Hl7.Fhir.Support.Poco;
1012
using Hl7.Fhir.Utility;
1113
using System;
12-
using System.Collections;
1314
using System.Collections.Generic;
15+
using System.Diagnostics.CodeAnalysis;
1416
using System.Linq;
1517

1618
#nullable enable
1719

1820
namespace 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
}

src/Hl7.Fhir.Base/ElementModel/ScopedNodeExtensions.cs

Lines changed: 0 additions & 145 deletions
This file was deleted.

src/Hl7.Fhir.Base/FhirPath/ElementNavFhirExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ public static void PrepareFhirSymbolTableFunctions()
4040
public static SymbolTable AddFhirExtensions(this SymbolTable t)
4141
{
4242
t.Add("hasValue", (ITypedElement f) => f.HasValue(), doNullProp: false);
43-
t.Add("resolve", (ITypedElement f, EvaluationContext ctx) => resolver(f, ctx), doNullProp: false);
44-
t.Add("resolve", (IEnumerable<ITypedElement> f, EvaluationContext ctx) => f.Select(fi => resolver(fi, ctx)), doNullProp: false);
43+
t.Add("resolve", (IScopedNode f, EvaluationContext ctx) => resolver(f, ctx), doNullProp: false);
44+
t.Add("resolve", (IEnumerable<IScopedNode> f, EvaluationContext ctx) => f.Select(fi => resolver(fi, ctx)), doNullProp: false);
4545

4646
t.Add("memberOf", (ITypedElement input, string valueset, EvaluationContext ctx) => MemberOf(input, valueset, ctx), doNullProp: false);
4747

@@ -69,7 +69,7 @@ public static SymbolTable AddFhirExtensions(this SymbolTable t)
6969

7070
return t;
7171

72-
static ITypedElement? resolver(ITypedElement f, EvaluationContext ctx)
72+
static IScopedNode? resolver(IScopedNode f, EvaluationContext ctx)
7373
{
7474
return ctx is FhirEvaluationContext fctx ? f.Resolve(fctx.ElementResolver) : f.Resolve();
7575
}

src/Hl7.Fhir.Base/FhirPath/FhirEvaluationContext.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88

99
using Hl7.Fhir.ElementModel;
10+
using Hl7.Fhir.Model;
1011
using Hl7.Fhir.Specification.Terminology;
1112
using Hl7.FhirPath;
1213
using System;
@@ -78,9 +79,9 @@ private static ITypedElement toNearestResource(ScopedNode node)
7879
return scan;
7980
}
8081

81-
private Func<string, ITypedElement>? _elementResolver;
82+
private Func<string, IScopedNode>? _elementResolver;
8283

83-
public Func<string, ITypedElement>? ElementResolver
84+
public Func<string, IScopedNode>? ElementResolver
8485
{
8586
get { return _elementResolver; }
8687
set { _elementResolver = value; }

src/Hl7.Fhir.Base/Hl7.Fhir.Base.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
</PropertyGroup>
1212

1313
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.1'">
14+
<PackageReference Include="System.Buffers" Version="4.5.1" />
1415
<PackageReference Include="System.Text.Json" Version="8.0.5" />
1516
</ItemGroup>
1617

0 commit comments

Comments
 (0)