Skip to content

Commit f12f9f0

Browse files
authored
Merge branch 'develop' into dependabot/nuget/FluentAssertions-7.2.0
2 parents a0455cb + 8db9552 commit f12f9f0

File tree

9 files changed

+1091
-1045
lines changed

9 files changed

+1091
-1045
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public EvaluationContext(ITypedElement? resource, ITypedElement? rootResource, I
6060
/// <summary>
6161
/// A delegate that handles the output for the <c>trace()</c> function.
6262
/// </summary>
63-
public Action<string, IEnumerable<ITypedElement>>? Tracer { get; set; }
63+
public Action<string?, IEnumerable<ITypedElement>>? Tracer { get; set; }
6464
}
6565

6666
public static class EvaluationContextExtensions

src/Hl7.Fhir.Base/FhirPath/Expressions/Invokee.cs

+182-183
Large diffs are not rendered by default.

src/Hl7.Fhir.Base/FhirPath/Expressions/SymbolTable.cs

+74-130
Original file line numberDiff line numberDiff line change
@@ -6,165 +6,109 @@
66
* available at https://raw.githubusercontent.com/FirelyTeam/firely-net-sdk/master/LICENSE
77
*/
88

9-
using Hl7.Fhir.ElementModel;
10-
using System;
9+
#nullable enable
10+
1111
using System.Collections.Concurrent;
1212
using System.Collections.Generic;
1313
using System.Linq;
1414
using System.Text;
1515

16-
namespace Hl7.FhirPath.Expressions
17-
{
16+
namespace Hl7.FhirPath.Expressions;
1817

19-
public class SymbolTable
18+
/// <summary>
19+
/// Holds the functions and constants available for the FhirPath engine to bind to.
20+
/// </summary>
21+
public class SymbolTable
22+
{
23+
/// <summary>
24+
/// An empty symbol table.
25+
/// </summary>
26+
public SymbolTable()
2027
{
21-
public SymbolTable()
22-
{
23-
24-
}
28+
// Nothing
29+
}
2530

26-
public SymbolTable(SymbolTable parent)
27-
{
28-
Parent = parent;
29-
}
31+
/// <summary>
32+
/// A local symbol table inside a parent scope.
33+
/// </summary>
34+
public SymbolTable(SymbolTable parent)
35+
{
36+
Parent = parent;
37+
}
3038

31-
public int Count()
32-
{
33-
var cnt = _entries.Count;
34-
if (Parent != null) cnt += Parent.Count();
39+
/// <summary>
40+
/// The number of entries in the symbol table, including the parent scope (if any).
41+
/// </summary>
42+
public int Count()
43+
{
44+
var cnt = _entries.Count;
45+
if (Parent != null) cnt += Parent.Count();
3546

36-
return cnt;
37-
}
47+
return cnt;
48+
}
3849

39-
internal Invokee First()
40-
{
41-
return _entries.Any() ? _entries.First().Body : (Parent?.First());
42-
}
50+
internal Invokee? First() => _entries.Any() ? _entries.First().Body : (Parent?.First());
4351

44-
public SymbolTable Parent { get; private set; }
52+
/// <summary>
53+
/// The parent scope for this symbol table.
54+
/// </summary>
55+
public SymbolTable? Parent { get; private set; }
4556

46-
[System.Diagnostics.DebuggerDisplay(@"\{{DebuggerDisplayValue()}}")]
47-
private class TableEntry
57+
[System.Diagnostics.DebuggerDisplay(@"\{{DebuggerDisplayValue()}}")]
58+
private class TableEntry(CallSignature signature, Invokee body)
59+
{
60+
public string DebuggerDisplayValue()
4861
{
49-
public string DebuggerDisplayValue()
50-
{
51-
if (Signature != null)
52-
{
53-
var sb = new StringBuilder();
54-
sb.Append(Signature.ReturnType.Name);
55-
sb.Append(' ');
56-
sb.Append(Signature.Name);
57-
sb.Append(" (");
58-
bool b = false;
59-
foreach (var item in Signature.ArgumentTypes)
60-
{
61-
if (b)
62-
sb.Append(", ");
63-
sb.Append(item.Name);
64-
b = true;
65-
}
66-
sb.Append(')');
67-
return sb.ToString();
68-
}
69-
return null;
70-
}
71-
72-
public CallSignature Signature { get; private set; }
73-
public Invokee Body { get; private set; }
62+
var sb = new StringBuilder();
63+
sb.Append(Signature.ReturnType.Name);
64+
sb.Append(' ');
65+
sb.Append(Signature.Name);
66+
sb.Append(" (");
67+
bool b = false;
7468

75-
public TableEntry(CallSignature signature, Invokee body)
69+
foreach (var item in Signature.ArgumentTypes)
7670
{
77-
Signature = signature;
78-
Body = body;
71+
if (b)
72+
sb.Append(", ");
73+
sb.Append(item.Name);
74+
b = true;
7975
}
80-
}
81-
82-
private ConcurrentBag<TableEntry> _entries = new();
76+
sb.Append(')');
8377

84-
internal void Add(CallSignature signature, Invokee body)
85-
{
86-
_entries.Add(new TableEntry(signature, body));
78+
return sb.ToString();
8779
}
8880

89-
public SymbolTable Filter(string name, int argCount)
90-
{
91-
var result = new SymbolTable
92-
{
93-
_entries = new(_entries.Where(e => e.Signature.Matches(name, argCount)))
94-
};
95-
96-
if (Parent != null)
97-
result.Parent = Parent.Filter(name, argCount);
98-
99-
return result;
100-
}
101-
102-
internal Invokee DynamicGet(string name, IEnumerable<object> args)
103-
{
104-
var exactMatches = _entries.Where(e => e.Signature.DynamicExactMatches(name, args));
105-
TableEntry entry = exactMatches.Union(_entries.Where(e => e.Signature.DynamicMatches(name, args))).FirstOrDefault();
106-
107-
if (entry == null && Parent != null) return Parent.DynamicGet(name, args);
108-
109-
return entry?.Body;
110-
}
81+
public CallSignature Signature { get; } = signature;
82+
public Invokee Body { get; } = body;
11183
}
11284

85+
private ConcurrentBag<TableEntry> _entries = [];
11386

114-
public static class SymbolTableExtensions
87+
internal void Add(CallSignature signature, Invokee body)
11588
{
116-
public static void Add<R>(this SymbolTable table, string name, Func<R> func)
117-
{
118-
table.Add(new CallSignature(name, typeof(R)), InvokeeFactory.Wrap(func));
119-
}
120-
121-
public static void Add<A, R>(this SymbolTable table, string name, Func<A, R> func, bool doNullProp = false)
122-
{
123-
if (typeof(A) != typeof(EvaluationContext))
124-
table.Add(new CallSignature(name, typeof(R), typeof(A)), InvokeeFactory.Wrap(func, doNullProp));
125-
else
126-
table.Add(new CallSignature(name, typeof(R)), InvokeeFactory.Wrap(func, doNullProp));
127-
}
128-
129-
public static void Add<A, B, R>(this SymbolTable table, string name, Func<A, B, R> func, bool doNullProp = false)
130-
{
131-
if (typeof(B) != typeof(EvaluationContext))
132-
table.Add(new CallSignature(name, typeof(R), typeof(A), typeof(B)), InvokeeFactory.Wrap(func, doNullProp));
133-
else
134-
table.Add(new CallSignature(name, typeof(R), typeof(A)), InvokeeFactory.Wrap(func, doNullProp));
135-
}
89+
_entries.Add(new TableEntry(signature, body));
90+
}
13691

137-
public static void Add<A, B, C, R>(this SymbolTable table, string name, Func<A, B, C, R> func, bool doNullProp = false)
92+
public SymbolTable Filter(string name, int argCount)
93+
{
94+
var result = new SymbolTable
13895
{
139-
if (typeof(C) != typeof(EvaluationContext))
140-
table.Add(new CallSignature(name, typeof(R), typeof(A), typeof(B), typeof(C)), InvokeeFactory.Wrap(func, doNullProp));
141-
else
142-
table.Add(new CallSignature(name, typeof(R), typeof(A), typeof(B)), InvokeeFactory.Wrap(func, doNullProp));
143-
}
96+
_entries = new ConcurrentBag<TableEntry>(_entries.Where(e => e.Signature.Matches(name, argCount)))
97+
};
14498

145-
public static void Add<A, B, C, D, R>(this SymbolTable table, string name, Func<A, B, C, D, R> func, bool doNullProp = false)
146-
{
147-
if (typeof(D) != typeof(EvaluationContext))
148-
table.Add(new CallSignature(name, typeof(R), typeof(A), typeof(B), typeof(C), typeof(D)), InvokeeFactory.Wrap(func, doNullProp));
149-
else
150-
table.Add(new CallSignature(name, typeof(R), typeof(A), typeof(B), typeof(C)), InvokeeFactory.Wrap(func, doNullProp));
99+
if (Parent != null)
100+
result.Parent = Parent.Filter(name, argCount);
151101

152-
}
102+
return result;
103+
}
153104

154-
public static void AddLogic(this SymbolTable table, string name, Func<Func<bool?>, Func<bool?>, bool?> func)
155-
{
156-
table.Add(new CallSignature(name, typeof(bool?), typeof(object), typeof(Func<bool?>), typeof(Func<bool?>)),
157-
InvokeeFactory.WrapLogic(func));
158-
}
105+
internal Invokee? DynamicGet(string name, IEnumerable<object> args)
106+
{
107+
var exactMatches = _entries.Where(e => e.Signature.DynamicExactMatches(name, args));
108+
var entry = exactMatches.Union(_entries.Where(e => e.Signature.DynamicMatches(name, args))).FirstOrDefault();
159109

160-
public static void AddVar(this SymbolTable table, string name, object value)
161-
{
162-
table.AddVar(name, ElementNode.ForPrimitive(value));
163-
}
110+
if (entry == null && Parent != null) return Parent.DynamicGet(name, args);
164111

165-
public static void AddVar(this SymbolTable table, string name, ITypedElement value)
166-
{
167-
table.Add(new CallSignature(name, typeof(string)), InvokeeFactory.Return(value));
168-
}
112+
return entry?.Body;
169113
}
170-
}
114+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright (c) 2015, Firely ([email protected]) and contributors
3+
* See the file CONTRIBUTORS for details.
4+
*
5+
* This file is licensed under the BSD 3-Clause license
6+
* available at https://raw.githubusercontent.com/FirelyTeam/firely-net-sdk/master/LICENSE
7+
*/
8+
9+
#nullable enable
10+
11+
using Hl7.Fhir.ElementModel;
12+
using System;
13+
14+
namespace Hl7.FhirPath.Expressions;
15+
16+
public static class SymbolTableExtensions
17+
{
18+
public static void Add<R>(this SymbolTable table, string name, Func<R> func)
19+
{
20+
table.Add(new CallSignature(name, typeof(R)), InvokeeFactory.Wrap(func));
21+
}
22+
23+
public static void Add<A, R>(this SymbolTable table, string name, Func<A, R> func, bool doNullProp = false)
24+
{
25+
table.Add(
26+
typeof(A) != typeof(EvaluationContext)
27+
? new CallSignature(name, typeof(R), typeof(A))
28+
: new CallSignature(name, typeof(R)), InvokeeFactory.Wrap(func, doNullProp));
29+
}
30+
31+
public static void Add<A, B, R>(this SymbolTable table, string name, Func<A, B, R> func, bool doNullProp = false)
32+
{
33+
if (typeof(B) != typeof(EvaluationContext))
34+
table.Add(new CallSignature(name, typeof(R), typeof(A), typeof(B)), InvokeeFactory.Wrap(func, doNullProp));
35+
else
36+
table.Add(new CallSignature(name, typeof(R), typeof(A)), InvokeeFactory.Wrap(func, doNullProp));
37+
}
38+
39+
public static void Add<A, B, C, R>(this SymbolTable table, string name, Func<A, B, C, R> func,
40+
bool doNullProp = false)
41+
{
42+
if (typeof(C) != typeof(EvaluationContext))
43+
table.Add(new CallSignature(name, typeof(R), typeof(A), typeof(B), typeof(C)),
44+
InvokeeFactory.Wrap(func, doNullProp));
45+
else
46+
table.Add(new CallSignature(name, typeof(R), typeof(A), typeof(B)), InvokeeFactory.Wrap(func, doNullProp));
47+
}
48+
49+
public static void Add<A, B, C, D, R>(this SymbolTable table, string name, Func<A, B, C, D, R> func,
50+
bool doNullProp = false)
51+
{
52+
if (typeof(D) != typeof(EvaluationContext))
53+
table.Add(new CallSignature(name, typeof(R), typeof(A), typeof(B), typeof(C), typeof(D)),
54+
InvokeeFactory.Wrap(func, doNullProp));
55+
else
56+
table.Add(new CallSignature(name, typeof(R), typeof(A), typeof(B), typeof(C)),
57+
InvokeeFactory.Wrap(func, doNullProp));
58+
}
59+
60+
public static void AddLogic(this SymbolTable table, string name, Func<Func<bool?>, Func<bool?>, bool?> func)
61+
{
62+
table.Add(new CallSignature(name, typeof(bool?), typeof(object), typeof(Func<bool?>), typeof(Func<bool?>)),
63+
InvokeeFactory.WrapLogic(func));
64+
}
65+
66+
public static void AddVar(this SymbolTable table, string name, object value)
67+
{
68+
table.AddVar(name, ElementNode.ForPrimitive(value));
69+
}
70+
71+
public static void AddVar(this SymbolTable table, string name, ITypedElement value)
72+
{
73+
table.Add(new CallSignature(name, typeof(string)), InvokeeFactory.Return(value));
74+
}
75+
}

0 commit comments

Comments
 (0)