Skip to content

Commit 327885b

Browse files
committed
Simplify expression compilation
1 parent b8f985d commit 327885b

File tree

6 files changed

+160
-196
lines changed

6 files changed

+160
-196
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using System.Linq;
2+
using System.Text.RegularExpressions;
3+
using Serilog.Events;
4+
using Serilog.Expressions.Runtime;
5+
6+
namespace Serilog.Expressions.Compilation.Linq
7+
{
8+
static class Intrinsics
9+
{
10+
static readonly LogEventPropertyValue NegativeOne = new ScalarValue(-1);
11+
12+
public static LogEventPropertyValue ConstructSequenceValue(LogEventPropertyValue[] elements)
13+
{
14+
// Avoid upsetting Serilog's (currently) fragile `SequenceValue.Render()`.
15+
if (elements.Any(el => el == null))
16+
return null;
17+
return new SequenceValue(elements);
18+
}
19+
20+
public static bool CoerceToScalarBoolean(LogEventPropertyValue value)
21+
{
22+
if (value is ScalarValue sv && sv.Value is bool b)
23+
return b;
24+
return false;
25+
}
26+
27+
public static LogEventPropertyValue IndexOfMatch(LogEventPropertyValue value, Regex regex)
28+
{
29+
if (value is ScalarValue scalar &&
30+
scalar.Value is string s)
31+
{
32+
var m = regex.Match(s);
33+
if (m.Success)
34+
return new ScalarValue(m.Index);
35+
return NegativeOne;
36+
}
37+
38+
return null;
39+
}
40+
41+
public static LogEventPropertyValue GetPropertyValue(LogEvent context, string propertyName)
42+
{
43+
if (!context.Properties.TryGetValue(propertyName, out var value))
44+
return null;
45+
46+
return value;
47+
}
48+
49+
public static LogEventPropertyValue TryGetStructurePropertyValue(LogEventPropertyValue maybeStructure, string name)
50+
{
51+
if (maybeStructure is StructureValue sv)
52+
{
53+
foreach (var prop in sv.Properties)
54+
{
55+
if (prop.Name == name)
56+
{
57+
return prop.Value;
58+
}
59+
}
60+
}
61+
62+
return null;
63+
}
64+
}
65+
}

0 commit comments

Comments
 (0)