Skip to content

Commit 7d7b433

Browse files
committed
Concat(s0, s1, ..sN)
1 parent 71a1135 commit 7d7b433

File tree

5 files changed

+20
-3
lines changed

5 files changed

+20
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ calling a function will be undefined if:
185185
| Function | Description |
186186
| :--- | :--- |
187187
| `Coalesce(p0, p1, ..pN)` | Returns the first defined, non-null argument. |
188+
| `Concat(s0, s1, ..sN)` | Concatenate two or more strings; all strings must be defined and non-null. |
188189
| `Contains(s, t)` | Tests whether the string `s` contains the substring `t`. |
189190
| `ElementAt(x, i)` | Retrieves a property of `x` by name `i`, or array element of `x` by numeric index `i`. |
190191
| `EndsWith(s, t)` | Tests whether the string `s` ends with substring `t`. |

src/Serilog.Expressions/Expressions/Compilation/Variadics/VariadicCallRewriter.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

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

42-
if (Operators.SameOperator(call.OperatorName, Operators.OpCoalesce))
42+
if (Operators.SameOperator(call.OperatorName, Operators.OpCoalesce) ||
43+
Operators.SameOperator(call.OperatorName, Operators.OpConcat))
4344
{
4445
if (call.Operands.Length == 0)
4546
return CallUndefined();
@@ -64,7 +65,7 @@ protected override Expression Transform(CallExpression call)
6465

6566
static CallExpression CallUndefined()
6667
{
67-
return new CallExpression(false, Operators.OpUndefined);
68+
return new(false, Operators.OpUndefined);
6869
}
6970
}
7071
}

src/Serilog.Expressions/Expressions/Operators.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ static class Operators
2929
// RuntimeOp* means runtime only.
3030

3131
public const string OpCoalesce = "Coalesce";
32+
public const string OpConcat = "Concat";
3233
public const string OpContains = "Contains";
3334
public const string OpElementAt = "ElementAt";
3435
public const string OpEndsWith = "EndsWith";

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,16 @@ public static LogEventPropertyValue _Internal_IsNotNull(LogEventPropertyValue? v
461461
return new ScalarValue(str.Substring((int)si, (int)len));
462462
}
463463

464+
public static LogEventPropertyValue? Concat(LogEventPropertyValue? first, LogEventPropertyValue? second)
465+
{
466+
if (Coerce.String(first, out var f) && Coerce.String(second, out var s))
467+
{
468+
return new ScalarValue(f + s);
469+
}
470+
471+
return null;
472+
}
473+
464474
// ReSharper disable once ReturnTypeCanBeNotNullable
465475
public static LogEventPropertyValue? IndexOfMatch(StringComparison sc, LogEventPropertyValue? corpus, LogEventPropertyValue? regex)
466476
{

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,10 @@ ismatch('foo', '^o') ⇶ false
208208
indexofmatch('foo', 'x') ⇶ -1
209209
substring('abcd', 1, 2) ⇶ 'bc'
210210
substring('abcd', 1) ⇶ 'bcd'
211+
concat('a', 'b', 'c') ⇶ 'abc'
212+
concat('a', 42, 'c') ⇶ undefined()
213+
concat('a', undefined()) ⇶ undefined()
214+
concat(undefined(), 'b') ⇶ undefined()
211215

212216
// Conditional
213217
if true then 1 else 2 ⇶ 1

0 commit comments

Comments
 (0)