Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Concat(s0, s1, ..sN) #45

Merged
merged 2 commits into from
Jun 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ calling a function will be undefined if:

| Function | Description |
| :--- | :--- |
| `Coalesce(p0, p1, ..pN)` | Returns the first defined, non-null argument. |
| `Coalesce(p0, p1, [..pN])` | Returns the first defined, non-null argument. |
| `Concat(s0, s1, [..sN])` | Concatenate two or more strings. |
| `Contains(s, t)` | Tests whether the string `s` contains the substring `t`. |
| `ElementAt(x, i)` | Retrieves a property of `x` by name `i`, or array element of `x` by numeric index `i`. |
| `EndsWith(s, t)` | Tests whether the string `s` ends with substring `t`. |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

namespace Serilog.Expressions.Compilation.Variadics
{
// Now a bit of a misnomer - handles variadic `coalesce()`, as well as optional arguments for other functions.
// Handles variadic `coalesce()` and `concat()`, as well as optional arguments for other functions.
class VariadicCallRewriter : IdentityTransformer
{
static readonly VariadicCallRewriter Instance = new VariadicCallRewriter();
Expand All @@ -39,7 +39,8 @@ protected override Expression Transform(CallExpression call)
return new CallExpression(call.IgnoreCase, call.OperatorName, operands);
}

if (Operators.SameOperator(call.OperatorName, Operators.OpCoalesce))
if (Operators.SameOperator(call.OperatorName, Operators.OpCoalesce) ||
Operators.SameOperator(call.OperatorName, Operators.OpConcat))
{
if (call.Operands.Length == 0)
return CallUndefined();
Expand All @@ -64,7 +65,7 @@ protected override Expression Transform(CallExpression call)

static CallExpression CallUndefined()
{
return new CallExpression(false, Operators.OpUndefined);
return new(false, Operators.OpUndefined);
}
}
}
1 change: 1 addition & 0 deletions src/Serilog.Expressions/Expressions/Operators.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ static class Operators
// RuntimeOp* means runtime only.

public const string OpCoalesce = "Coalesce";
public const string OpConcat = "Concat";
public const string OpContains = "Contains";
public const string OpElementAt = "ElementAt";
public const string OpEndsWith = "EndsWith";
Expand Down
10 changes: 10 additions & 0 deletions src/Serilog.Expressions/Expressions/Runtime/RuntimeOperators.cs
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,16 @@ public static LogEventPropertyValue _Internal_IsNotNull(LogEventPropertyValue? v
return new ScalarValue(str.Substring((int)si, (int)len));
}

public static LogEventPropertyValue? Concat(LogEventPropertyValue? first, LogEventPropertyValue? second)
{
if (Coerce.String(first, out var f) && Coerce.String(second, out var s))
{
return new ScalarValue(f + s);
}

return null;
}

// ReSharper disable once ReturnTypeCanBeNotNullable
public static LogEventPropertyValue? IndexOfMatch(StringComparison sc, LogEventPropertyValue? corpus, LogEventPropertyValue? regex)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,10 @@ ismatch('foo', '^o') ⇶ false
indexofmatch('foo', 'x') ⇶ -1
substring('abcd', 1, 2) ⇶ 'bc'
substring('abcd', 1) ⇶ 'bcd'
concat('a', 'b', 'c') ⇶ 'abc'
concat('a', 42, 'c') ⇶ undefined()
concat('a', undefined()) ⇶ undefined()
concat(undefined(), 'b') ⇶ undefined()

// Conditional
if true then 1 else 2 ⇶ 1
Expand Down