Skip to content

Commit 3198122

Browse files
authored
Merge pull request #117 from nblumhardt/v4-updates
Update to Serilog 4, tidy up
2 parents ed917a8 + fde5bfb commit 3198122

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+190
-151
lines changed

serilog-expressions.sln.DotSettings

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
2+
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=CI/@EntryIndexedValue">CI</s:String>
23
<s:Boolean x:Key="/Default/UserDictionary/Words/=Acerola/@EntryIndexedValue">True</s:Boolean>
34
<s:Boolean x:Key="/Default/UserDictionary/Words/=Comparand/@EntryIndexedValue">True</s:Boolean>
45
<s:Boolean x:Key="/Default/UserDictionary/Words/=delim/@EntryIndexedValue">True</s:Boolean>

src/Serilog.Expressions/Expressions/Ast/AccessorExpression.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@
1414

1515
namespace Serilog.Expressions.Ast;
1616

17+
/// <summary>
18+
/// An accessor retrieves a property from a (structured) object. For example, in the expression
19+
/// <code>Headers.ContentType</code> the <code>.</code> operator forms an accessor expression that
20+
/// retrieves the <code>ContentType</code> property from the <code>Headers</code> object.
21+
/// </summary>
22+
/// <remarks>Note that the AST type can represent accessors that cannot be validly written using
23+
/// <code>.</code> notation. In these cases, if the accessor is formatted back out as an expression,
24+
/// <see cref="IndexerExpression"/> notation will be used.</remarks>
1725
class AccessorExpression : Expression
1826
{
1927
public AccessorExpression(Expression receiver, string memberName)

src/Serilog.Expressions/Expressions/Ast/AmbientNameExpression.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414

1515
namespace Serilog.Expressions.Ast;
1616

17+
/// <summary>
18+
/// An ambient name is generally a property name or built-in that appears standalone in an expression. For example,
19+
/// in <code>Headers.ContentType</code>, <code>Headers</code> is an ambient name that produces an
20+
/// <see cref="AmbientNameExpression"/>. Built-ins like <code>@Level</code> are also parsed as ambient names.
21+
/// </summary>
1722
class AmbientNameExpression : Expression
1823
{
1924
readonly bool _requiresEscape;

src/Serilog.Expressions/Expressions/Ast/ArrayExpression.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@
1414

1515
namespace Serilog.Expressions.Ast;
1616

17+
/// <summary>
18+
/// An array expression constructs an array from a list of elements. For example, <code>[1, 2, 3]</code> is an
19+
/// array expression. The items in an array expression can be literal values or expressions, like in the
20+
/// above example, or they can be spread expressions that describe zero or more elements to include in the
21+
/// list. Whether included via regular elements or spread expressions, undefined values are ignored and won't
22+
/// appear in the resulting array value.
23+
/// </summary>
1724
class ArrayExpression : Expression
1825
{
1926
public ArrayExpression(Element[] elements)

src/Serilog.Expressions/Expressions/Ast/CallExpression.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414

1515
namespace Serilog.Expressions.Ast;
1616

17+
/// <summary>
18+
/// A <see cref="CallExpression"/> is a function call made up of the function name, parenthesised argument
19+
/// list, and optional postfix <code>ci</code> modifier. For example, <code>Substring(RequestPath, 0, 5)</code>.
20+
/// </summary>
1721
class CallExpression : Expression
1822
{
1923
public CallExpression(bool ignoreCase, string operatorName, params Expression[] operands)

src/Serilog.Expressions/Expressions/Ast/ConstantExpression.cs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
namespace Serilog.Expressions.Ast;
1919

20+
/// <summary>
21+
/// A constant such as <code>'hello'</code>, <code>true</code>, <code>null</code>, or <code>123.45</code>.
22+
/// </summary>
2023
class ConstantExpression : Expression
2124
{
2225
public ConstantExpression(LogEventPropertyValue constant)
@@ -30,19 +33,14 @@ public override string ToString()
3033
{
3134
if (Constant is ScalarValue sv)
3235
{
33-
switch (sv.Value)
36+
return sv.Value switch
3437
{
35-
case string s:
36-
return "'" + s.Replace("'", "''") + "'";
37-
case true:
38-
return "true";
39-
case false:
40-
return "false";
41-
case IFormattable formattable:
42-
return formattable.ToString(null, CultureInfo.InvariantCulture);
43-
default:
44-
return (sv.Value ?? "null").ToString() ?? "<ToString() returned null>";
45-
}
38+
string s => "'" + s.Replace("'", "''") + "'",
39+
true => "true",
40+
false => "false",
41+
IFormattable formattable => formattable.ToString(null, CultureInfo.InvariantCulture),
42+
_ => (sv.Value ?? "null").ToString() ?? "<ToString() returned null>"
43+
};
4644
}
4745

4846
return Constant.ToString();

src/Serilog.Expressions/Expressions/Ast/Element.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
namespace Serilog.Expressions.Ast;
1616

17-
abstract class Element
18-
{
19-
}
17+
/// <summary>
18+
/// An element in an <see cref="ArrayExpression"/>.
19+
/// </summary>
20+
abstract class Element;

src/Serilog.Expressions/Expressions/Ast/Expression.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,15 @@
1414

1515
namespace Serilog.Expressions.Ast;
1616

17+
/// <summary>
18+
/// An AST node.
19+
/// </summary>
1720
abstract class Expression
1821
{
19-
// Used only as an enabler for testing and debugging.
22+
/// <summary>
23+
/// The <see cref="ToString"/> representation of an <see cref="Expression"/> is <strong>not</strong>
24+
/// guaranteed to be syntactically valid: this is provided for debugging purposes only.
25+
/// </summary>
26+
/// <returns>A textual representation of the expression.</returns>
2027
public abstract override string ToString();
2128
}

src/Serilog.Expressions/Expressions/Ast/IndexOfMatchExpression.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616

1717
namespace Serilog.Expressions.Ast;
1818

19+
/// <summary>
20+
/// A non-syntax expression tree node used when compiling the <see cref="Operators.OpIndexOfMatch"/>,
21+
/// <see cref="Operators.OpIsMatch"/>, and SQL-style <code>like</code> expressions.
22+
/// </summary>
1923
class IndexOfMatchExpression : Expression
2024
{
2125
public Expression Corpus { get; }

src/Serilog.Expressions/Expressions/Ast/IndexerExpression.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414

1515
namespace Serilog.Expressions.Ast;
1616

17+
/// <summary>
18+
/// An <see cref="IndexerExpression"/> retrieves a property from an object, by name, or an item from an array
19+
/// by zero-based numeric index. For example, <code>Headers['Content-Type']</code> and <code>Items[2]</code> are
20+
/// parsed as indexer expressions.
21+
/// </summary>
1722
class IndexerExpression : Expression
1823
{
1924
public Expression Receiver { get; }

src/Serilog.Expressions/Expressions/Ast/IndexerWildcard.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,7 @@
1414

1515
namespace Serilog.Expressions.Ast;
1616

17+
/// <summary>
18+
/// Describes the wildcard in a <see cref="IndexerWildcardExpression"/>.
19+
/// </summary>
1720
enum IndexerWildcard { Undefined, Any, All }

src/Serilog.Expressions/Expressions/Ast/IndexerWildcardExpression.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414

1515
namespace Serilog.Expressions.Ast;
1616

17+
/// <summary>
18+
/// An indexer wildcard is a placeholder in a property path expression. For example,
19+
/// in <code>Headers[?] = 'test'</code>, the question-mark token is a wildcard that means "any property of
20+
/// the <code>Headers</code> object". The other wildcard indexer is the asterisk, meaning "all".
21+
/// </summary>
1722
class IndexerWildcardExpression : Expression
1823
{
1924
public IndexerWildcardExpression(IndexerWildcard wildcard)

src/Serilog.Expressions/Expressions/Ast/ItemElement.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414

1515
namespace Serilog.Expressions.Ast;
1616

17+
/// <summary>
18+
/// A single item in an <see cref="ArrayExpression"/>.
19+
/// </summary>
1720
class ItemElement : Element
1821
{
1922
public Expression Value { get; }

src/Serilog.Expressions/Expressions/Ast/LambdaExpression.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414

1515
namespace Serilog.Expressions.Ast;
1616

17+
/// <summary>
18+
/// A non-syntax expression tree node used in the compilation of <see cref="IndexerWildcardExpression"/>. Only
19+
/// very limited support for lambda expressions is currently present.
20+
/// </summary>
1721
class LambdaExpression : Expression
1822
{
1923
public LambdaExpression(ParameterExpression[] parameters, Expression body)

src/Serilog.Expressions/Expressions/Ast/LocalNameExpression.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414

1515
namespace Serilog.Expressions.Ast;
1616

17+
/// <summary>
18+
/// Represents the iteration variable in template <code>#each</code> directives.
19+
/// </summary>
1720
class LocalNameExpression : Expression
1821
{
1922
public LocalNameExpression(string name)

src/Serilog.Expressions/Expressions/Ast/Member.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
namespace Serilog.Expressions.Ast;
1616

17-
abstract class Member
18-
{
19-
}
17+
/// <summary>
18+
/// A member in an <see cref="ObjectExpression"/>.
19+
/// </summary>
20+
abstract class Member;

src/Serilog.Expressions/Expressions/Ast/ObjectExpression.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414

1515
namespace Serilog.Expressions.Ast;
1616

17+
/// <summary>
18+
/// Constructs an object given a list of members. Members can be <code>name: value</code> pairs, or spread
19+
/// expressions that include members from another object. Where names conflict, the rightmost appearance of
20+
/// a name wins. Members that evaluate to an undefined value do not appear in the resulting object.
21+
/// </summary>
1722
class ObjectExpression : Expression
1823
{
1924
public ObjectExpression(Member[] members)

src/Serilog.Expressions/Expressions/Ast/ParameterExpression.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414

1515
namespace Serilog.Expressions.Ast;
1616

17+
/// <summary>
18+
/// Non-syntax expression type used to represent parameters in <see cref="LambdaExpression"/> bodies.
19+
/// </summary>
1720
class ParameterExpression : Expression
1821
{
1922
public ParameterExpression(string parameterName)

src/Serilog.Expressions/Expressions/Ast/PropertyMember.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616

1717
namespace Serilog.Expressions.Ast;
1818

19+
/// <summary>
20+
/// An <see cref="ObjectExpression"/> member comprising an optionally-quoted name and a value, for example
21+
/// the object <code>{Username: 'alice'}</code> includes a single <see cref="PropertyMember"/> with name
22+
/// <code>Username</code> and value <code>'alice'</code>.
23+
/// </summary>
1924
class PropertyMember : Member
2025
{
2126
public string Name { get; }

src/Serilog.Expressions/Expressions/Ast/SpreadElement.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@
1414

1515
namespace Serilog.Expressions.Ast;
1616

17+
/// <summary>
18+
/// An element in an <see cref="ArrayExpression"/> that describes zero or more items to include in the array.
19+
/// Spread elements are written with two dots preceding an expression that evaluates to an array of elements to
20+
/// insert into the result array at the position of the spread element, for example, in <code>[1, 2, ..Others]</code>,
21+
/// the <code>..Others</code> expression is a spread element. If the value of the array in the spread is
22+
/// undefined, no items will be added to the list.
23+
/// </summary>
1724
class SpreadElement : Element
1825
{
1926
public Expression Content { get; }

src/Serilog.Expressions/Expressions/Ast/SpreadMember.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414

1515
namespace Serilog.Expressions.Ast;
1616

17+
/// <summary>
18+
/// An <see cref="ObjectExpression"/> member that designates another object from which to copy members into the
19+
/// current object. Spread member expressions comprise two dots preceding an expression that is expected to
20+
/// evaluate to an object.
21+
/// </summary>
1722
class SpreadMember : Member
1823
{
1924
public Expression Content { get; }

src/Serilog.Expressions/Expressions/Compilation/DefaultFunctionNameResolver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public static NameResolver Build(NameResolver? additionalNameResolver)
2222
{
2323
var defaultResolver = new StaticMemberNameResolver(typeof(RuntimeOperators));
2424
return additionalNameResolver == null
25-
? (NameResolver) defaultResolver
25+
? defaultResolver
2626
: new OrderedNameResolver(new[] {defaultResolver, additionalNameResolver });
2727
}
2828
}

src/Serilog.Expressions/Expressions/Compilation/Linq/LinqExpressionCompiler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ static ExpressionBody CompileLogical(Func<ExpressionBody, ExpressionBody, Expres
176176
{
177177
return LX.Convert(
178178
LX.New(
179-
typeof(ScalarValue).GetConstructor(new[]{typeof(object)})!,
179+
typeof(ScalarValue).GetConstructor([typeof(object)])!,
180180
LX.Convert(apply(
181181
LX.Call(CoerceToScalarBooleanMethod, lhs),
182182
LX.Call(CoerceToScalarBooleanMethod, rhs)), typeof(object))),
@@ -253,7 +253,7 @@ protected override ExpressionBody Transform(Ast.LambdaExpression lmx)
253253
var lambda = LX.Lambda(delegateType, rewritten!, parameters.Select(px => px.Item2).ToArray());
254254

255255
// Unfortunately, right now, functions need to be threaded through in constant scalar values :-D
256-
return LX.New(typeof(ScalarValue).GetConstructor(new[] {typeof(object)})!,
256+
return LX.New(typeof(ScalarValue).GetConstructor([typeof(object)])!,
257257
LX.Convert(lambda, typeof(object)));
258258
}
259259

src/Serilog.Expressions/Expressions/Compilation/Linq/ParameterReplacementVisitor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class ParameterReplacementVisitor : ExpressionVisitor
2323
public static Expression ReplaceParameters(LambdaExpression lambda, params ParameterExpression[] newParameters)
2424
{
2525
var v = new ParameterReplacementVisitor(lambda.Parameters.ToArray(), newParameters);
26-
return v.Visit(lambda.Body);
26+
return v.Visit(lambda.Body)!;
2727
}
2828

2929
ParameterReplacementVisitor(ParameterExpression[] from, ParameterExpression[] to)

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,7 @@ protected override Expression Transform(CallExpression call)
4848

4949
Expression TryCompileLikeExpression(bool ignoreCase, Expression corpus, Expression like)
5050
{
51-
if (like is ConstantExpression cx &&
52-
cx.Constant is ScalarValue scalar &&
53-
scalar.Value is string s)
51+
if (like is ConstantExpression { Constant: ScalarValue { Value: string s } })
5452
{
5553
var regex = LikeToRegex(s);
5654
var opts = RegexOptions.Compiled | RegexOptions.ExplicitCapture;

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,7 @@ protected override Expression Transform(CallExpression call)
4949

5050
Expression TryCompileIndexOfMatch(bool ignoreCase, Expression corpus, Expression regex)
5151
{
52-
if (regex is ConstantExpression cx &&
53-
cx.Constant is ScalarValue scalar &&
54-
scalar.Value is string s)
52+
if (regex is ConstantExpression { Constant: ScalarValue { Value: string s } })
5553
{
5654
var opts = RegexOptions.Compiled | RegexOptions.ExplicitCapture;
5755
if (ignoreCase)

src/Serilog.Expressions/Expressions/Compilation/Wildcards/WildcardComprehensionTransformer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ protected override Expression Transform(CallExpression lx)
6868
comparisonArgs[indexerOperand] = nestedComparand;
6969
var body = new CallExpression(lx.IgnoreCase, lx.OperatorName, comparisonArgs);
7070

71-
var lambda = new LambdaExpression(new[] { px }, body);
71+
var lambda = new LambdaExpression([px], body);
7272

7373
var op = Operators.ToRuntimeWildcardOperator(wc);
7474
var call = new CallExpression(false, op, coll, lambda);
@@ -84,7 +84,7 @@ protected override Expression Transform(IndexerExpression ix)
8484

8585
var px = new ParameterExpression("p" + _nextParameter++);
8686
var coll = Transform(ix.Receiver);
87-
var lambda = new LambdaExpression(new[] { px }, px);
87+
var lambda = new LambdaExpression([px], px);
8888
var op = Operators.ToRuntimeWildcardOperator(wx.Wildcard);
8989
return new CallExpression(false, op, coll, lambda);
9090
}

src/Serilog.Expressions/Expressions/Helpers.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ namespace Serilog.Expressions
1919
/// <summary>
2020
/// Helper methods.
2121
/// </summary>
22-
internal static class Helpers
22+
static class Helpers
2323
{
2424
/// <summary>
2525
/// Backport .NET Standard 2.1 additions to maintain .NET Standard 2.0 compatibility.
@@ -35,7 +35,7 @@ internal static class Helpers
3535
/// <returns></returns>
3636
public static bool Contains(this string source, string value, StringComparison comparisonType)
3737
{
38-
return source?.IndexOf(value, comparisonType) >= 0;
38+
return source.IndexOf(value, comparisonType) >= 0;
3939
}
4040
}
4141
}

src/Serilog.Expressions/Expressions/Parsing/ExpressionTextParsers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ static class ExpressionTextParsers
2929

3030
public static readonly TextParser<string> HexInteger =
3131
Span.EqualTo("0x")
32-
.IgnoreThen(Character.Digit.Or(Character.Matching(ch => ch >= 'a' && ch <= 'f' || ch >= 'A' && ch <= 'F', "a-f"))
32+
.IgnoreThen(Character.Digit.Or(Character.Matching(ch => ch is >= 'a' and <= 'f' or >= 'A' and <= 'F', "a-f"))
3333
.Named("hex digit")
3434
.AtLeastOnce())
3535
.Select(chars => new string(chars));

0 commit comments

Comments
 (0)