Skip to content

Commit 29f45f4

Browse files
committed
Move some silliness around is null to the parser
1 parent 9e8781c commit 29f45f4

File tree

12 files changed

+79
-92
lines changed

12 files changed

+79
-92
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
# _Serilog.Expressions_
1+
# _Serilog Expressions_
22

33
An embeddable mini-language for filtering, enriching, and formatting Serilog
4-
events, ideal for use in JSON or XML configuration.
4+
events, ideal for use with JSON or XML configuration.
55

66
## Getting started
77

src/Serilog.Expressions/Expressions/Compilation/ExpressionCompiler.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
using Serilog.Expressions.Ast;
22
using Serilog.Expressions.Compilation.Arrays;
3-
using Serilog.Expressions.Compilation.In;
4-
using Serilog.Expressions.Compilation.Is;
53
using Serilog.Expressions.Compilation.Linq;
64
using Serilog.Expressions.Compilation.Properties;
75
using Serilog.Expressions.Compilation.Text;
@@ -20,9 +18,7 @@ public static CompiledExpression Compile(Expression expression)
2018
actual = LikeSyntaxTransformer.Rewrite(actual);
2119
actual = PropertiesObjectAccessorTransformer.Rewrite(actual);
2220
actual = ConstantArrayEvaluator.Evaluate(actual);
23-
actual = NotInRewriter.Rewrite(actual);
2421
actual = WildcardComprehensionTransformer.Expand(actual);
25-
actual = IsOperatorTransformer.Rewrite(actual);
2622

2723
return LinqExpressionCompiler.Compile(actual);
2824
}

src/Serilog.Expressions/Expressions/Compilation/In/NotInRewriter.cs

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

src/Serilog.Expressions/Expressions/Compilation/Is/IsOperatorTransformer.cs

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

src/Serilog.Expressions/Expressions/Compilation/Text/LikeSyntaxTransformer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ protected override Expression Transform(CallExpression lx)
2121
if (lx.Operands.Length != 2)
2222
return base.Transform(lx);
2323

24-
if (Operators.SameOperator(lx.OperatorName, Operators.IntermediateOpSqlLike))
24+
if (Operators.SameOperator(lx.OperatorName, Operators.IntermediateOpLike))
2525
return TryCompileLikeExpression(lx.Operands[0], lx.Operands[1]);
2626

27-
if (Operators.SameOperator(lx.OperatorName, Operators.IntermediateOpSqlNotLike))
27+
if (Operators.SameOperator(lx.OperatorName, Operators.IntermediateOpNotLike))
2828
return new CallExpression(
2929
Operators.RuntimeOpStrictNot,
3030
TryCompileLikeExpression(lx.Operands[0], lx.Operands[1]));

src/Serilog.Expressions/Expressions/Operators.cs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,10 @@ static class Operators
4848
public const string RuntimeOpIsNull = "_Internal_IsNull";
4949
public const string RuntimeOpIsNotNull = "_Internal_IsNotNull";
5050
public const string OpCoalesce = "Coalesce";
51-
public const string IntermediateOpSqlLike = "_Internal_Like";
52-
public const string IntermediateOpSqlNotLike = "_Internal_NotLike";
53-
public const string IntermediateOpSqlIs = "_Internal_Is";
54-
public const string RuntimeOpSqlIn = "_Internal_In";
55-
public const string IntermediateOpSqlNotIn = "_Internal_NotIn";
51+
public const string IntermediateOpLike = "_Internal_Like";
52+
public const string IntermediateOpNotLike = "_Internal_NotLike";
53+
public const string RuntimeOpIn = "_Internal_In";
54+
public const string RuntimeOpNotIn = "_Internal_NotIn";
5655
public const string RuntimeOpStrictNot = "_Internal_StrictNot";
5756
public const string OpSubstring = "Substring";
5857
public const string OpIndexOfMatch = "IndexOfMatch";
@@ -70,11 +69,9 @@ static class Operators
7069
OpLessThanOrEqual,
7170
OpGreaterThan,
7271
OpGreaterThanOrEqual,
73-
IntermediateOpSqlLike,
74-
IntermediateOpSqlNotLike,
75-
RuntimeOpSqlIn,
76-
IntermediateOpSqlNotIn,
77-
IntermediateOpSqlIs
72+
IntermediateOpLike,
73+
IntermediateOpNotLike,
74+
RuntimeOpIn,
7875
};
7976

8077
public static bool SameOperator(string op1, string op2)

src/Serilog.Expressions/Expressions/Parsing/ExpressionTokenParsers.cs

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,31 @@ public static TokenListParserResult<ExpressionToken, Expression> TryPartialParse
4040
static readonly TokenListParser<ExpressionToken, string> Neq = Token.EqualTo(ExpressionToken.NotEqual).Value(Operators.OpNotEqual);
4141
static readonly TokenListParser<ExpressionToken, string> Negate = Token.EqualTo(ExpressionToken.Minus).Value(Operators.OpNegate);
4242
static readonly TokenListParser<ExpressionToken, string> Not = Token.EqualTo(ExpressionToken.Not).Value(Operators.OpNot);
43-
static readonly TokenListParser<ExpressionToken, string> Like = Token.EqualTo(ExpressionToken.Like).Value(Operators.IntermediateOpSqlLike);
44-
static readonly TokenListParser<ExpressionToken, string> NotLike = Not.IgnoreThen(Like).Value(Operators.IntermediateOpSqlNotLike);
45-
static readonly TokenListParser<ExpressionToken, string> In = Token.EqualTo(ExpressionToken.In).Value(Operators.RuntimeOpSqlIn);
46-
static readonly TokenListParser<ExpressionToken, string> NotIn = Not.IgnoreThen(In).Value(Operators.IntermediateOpSqlNotIn);
47-
static readonly TokenListParser<ExpressionToken, string> Is = Token.EqualTo(ExpressionToken.Is).Value(Operators.IntermediateOpSqlIs);
43+
44+
static readonly TokenListParser<ExpressionToken, string> Like = Token.EqualTo(ExpressionToken.Like).Value(Operators.IntermediateOpLike);
45+
46+
static readonly TokenListParser<ExpressionToken, string> NotLike =
47+
Token.EqualTo(ExpressionToken.Not)
48+
.IgnoreThen(Token.EqualTo(ExpressionToken.Like))
49+
.Value(Operators.IntermediateOpNotLike);
50+
51+
static readonly TokenListParser<ExpressionToken, string> In = Token.EqualTo(ExpressionToken.In).Value(Operators.RuntimeOpIn);
52+
53+
static readonly TokenListParser<ExpressionToken, string> NotIn =
54+
Token.EqualTo(ExpressionToken.Not)
55+
.IgnoreThen(Token.EqualTo(ExpressionToken.In))
56+
.Value(Operators.RuntimeOpNotIn);
57+
58+
static readonly TokenListParser<ExpressionToken, string> IsNull =
59+
Token.EqualTo(ExpressionToken.Is)
60+
.IgnoreThen(Token.EqualTo(ExpressionToken.Null))
61+
.Value(Operators.RuntimeOpIsNull);
62+
63+
static readonly TokenListParser<ExpressionToken, string> IsNotNull =
64+
Token.EqualTo(ExpressionToken.Is)
65+
.IgnoreThen(Token.EqualTo(ExpressionToken.Not))
66+
.IgnoreThen(Token.EqualTo(ExpressionToken.Null))
67+
.Value(Operators.RuntimeOpIsNotNull);
4868

4969
static readonly TokenListParser<ExpressionToken, Func<Expression, Expression>> PropertyPathStep =
5070
Token.EqualTo(ExpressionToken.Period)
@@ -152,15 +172,28 @@ from path in PropertyPathStep.Or(PropertyPathIndexerStep).Many()
152172
static readonly TokenListParser<ExpressionToken, Expression> Operand =
153173
(from op in Negate.Or(Not)
154174
from path in Path
155-
select MakeUnary(op, path)).Or(Path).Named("expression");
175+
select MakeUnary(op, path))
176+
.Or(Path)
177+
.Then(operand => Token.EqualTo(ExpressionToken.Is).Try()
178+
.IgnoreThen(
179+
Token.EqualTo(ExpressionToken.Null).Value(Operators.RuntimeOpIsNull)
180+
.Or(Token.EqualTo(ExpressionToken.Not).IgnoreThen(Token.EqualTo(ExpressionToken.Null)).Value(Operators.RuntimeOpIsNotNull)))
181+
.Select(op => (Expression)new CallExpression(op, operand))
182+
.OptionalOrDefault(operand))
183+
.Named("expression");
156184

157185
static readonly TokenListParser<ExpressionToken, Expression> InnerTerm = Parse.Chain(Power, Operand, MakeBinary);
158186

159187
static readonly TokenListParser<ExpressionToken, Expression> Term = Parse.Chain(Multiply.Or(Divide).Or(Modulo), InnerTerm, MakeBinary);
160188

161189
static readonly TokenListParser<ExpressionToken, Expression> Comparand = Parse.Chain(Add.Or(Subtract), Term, MakeBinary);
162190

163-
static readonly TokenListParser<ExpressionToken, Expression> Comparison = Parse.Chain(Is.Or(NotLike.Try().Or(Like)).Or(NotIn.Try().Or(In)).Or(Lte.Or(Neq).Or(Lt)).Or(Gte.Or(Gt)).Or(Eq), Comparand, MakeBinary);
191+
static readonly TokenListParser<ExpressionToken, Expression> Comparison = Parse.Chain(
192+
NotLike.Try().Or(Like)
193+
.Or(NotIn.Try().Or(In))
194+
.Or(Lte.Or(Neq).Or(Lt))
195+
.Or(Gte.Or(Gt))
196+
.Or(Eq), Comparand, MakeBinary);
164197

165198
static readonly TokenListParser<ExpressionToken, Expression> Conjunction = Parse.Chain(And, Comparison, MakeBinary);
166199

src/Serilog.Expressions/Expressions/Runtime/RuntimeOperators.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,11 @@ static bool UnboxedEqualHelper(LogEventPropertyValue? left, LogEventPropertyValu
196196
return null;
197197
}
198198

199+
public static LogEventPropertyValue? _Internal_NotIn(LogEventPropertyValue? item, LogEventPropertyValue? collection)
200+
{
201+
return _Internal_StrictNot(_Internal_In(item, collection));
202+
}
203+
199204
public static LogEventPropertyValue? NotEqual(LogEventPropertyValue? left, LogEventPropertyValue? right)
200205
{
201206
if (left == null || right == null)

src/Serilog.Expressions/Serilog.Expressions.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<Description>Expression evaluation over Serilog events.</Description>
4+
<Description>An embeddable mini-language for filtering, enriching, and formatting Serilog
5+
events, ideal for use with JSON or XML configuration.</Description>
56
<VersionPrefix>1.0.0</VersionPrefix>
67
<Authors>Serilog Contributors</Authors>
78
<TargetFramework>netstandard2.1</TargetFramework>
89
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
910
<GenerateDocumentationFile>true</GenerateDocumentationFile>
10-
<AssemblyName>Serilog.Expressions</AssemblyName>
1111
<RootNamespace>Serilog</RootNamespace>
1212
<AssemblyOriginatorKeyFile>../../assets/Serilog.snk</AssemblyOriginatorKeyFile>
1313
<SignAssembly>true</SignAssembly>
@@ -23,7 +23,7 @@
2323
</PropertyGroup>
2424

2525
<ItemGroup>
26-
<PackageReference Include="Serilog" Version="2.9.0" />
26+
<PackageReference Include="Serilog" Version="2.10.0" />
2727
<PackageReference Include="Superpower" Version="2.3.0" />
2828
</ItemGroup>
2929

test/Serilog.Expressions.Tests/Cases/ephemeral-expression-evaluation-cases.asv renamed to test/Serilog.Expressions.Tests/Cases/expression-evaluation-cases.asv

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,21 @@ null >= null ⇶ undefined
160160
undefined >= null ⇶ undefined
161161
undefined >= undefined ⇶ undefined
162162

163+
// Comparators - in/not in
164+
1 in [1, 2, 3] ⇶ true
165+
5 in [1, 2, 3] ⇶ false
166+
1 not in [1, 2, 3] ⇶ false
167+
5 not in [1, 2, 3] ⇶ true
168+
undefined in [1, 2, 3] ⇶ undefined
169+
undefined not in [1, 2, 3] ⇶ undefined
170+
1 in undefined ⇶ undefined
171+
null in [1, null, 3] ⇶ true
172+
173+
// Comparators - is null/is not null
174+
null is null ⇶ true
175+
null is not null ⇶ false
176+
undefined is null ⇶ true
177+
163178
// Property names and accessors
164179
[5, 6, 7][1] ⇶ 6
165180
User.Name ⇶ 'nblumhardt'

0 commit comments

Comments
 (0)