Skip to content
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
10 changes: 8 additions & 2 deletions src/Seq.Syntax/Expressions/Runtime/LevelValue.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © Serilog Contributors
// Copyright © Datalust and Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -12,16 +12,22 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System.Text.Json.Serialization;

namespace Seq.Syntax.Expressions.Runtime;

/// <summary>
/// The typed value of the <c>@Level</c> keyword property: the level name exactly as spelled in
/// the document's <c>@l</c> (<c>Information</c> when absent). Distinguishes levels from plain
/// strings so that fixed-width moniker formats apply.
/// </summary>
/// <remarks>Serializes as its name, so that a level handed to a caller through
/// <see cref="EvaluationResult"/> degrades to a JSON string when cloned or written, rather than
/// to an object carrying the wrapper's own fields.</remarks>
[JsonConverter(typeof(LevelValueJsonConverter))]
sealed class LevelValue(string name)
{
public string Name { get; } = name;

public override string ToString() => Name;
}
}
31 changes: 31 additions & 0 deletions src/Seq.Syntax/Expressions/Runtime/LevelValueJsonConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright © Datalust and Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System.Text.Json;
using System.Text.Json.Serialization;

namespace Seq.Syntax.Expressions.Runtime;

sealed class LevelValueJsonConverter : JsonConverter<LevelValue>
{
public override LevelValue Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return LevelMapping.ToLevelValue(reader.GetString());
}

public override void Write(Utf8JsonWriter writer, LevelValue value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.Name);
}
}
6 changes: 3 additions & 3 deletions src/Seq.Syntax/Expressions/Runtime/Values.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ static class Values
{
switch (Underlying(value))
{
// Typed scalars degrade to their JSON string forms on insertion; `DeepClone()` does
// this for the serializer-supported ones, but a `LevelValue` would clone as a
// `{"Name": ...}` object.
// Typed scalars degrade to their JSON string forms on insertion. `DeepClone()` does
// this too, via each type's serializer support, but for a `LevelValue` the direct
// conversion avoids a serializer round-trip.
case LevelValue level:
return JsonValue.Create(level.Name);
// `unsafe()` output has no JSON form to degrade to: `DeepClone()` would serialize
Expand Down
15 changes: 15 additions & 0 deletions test/Seq.Syntax.Tests/Expressions/ValueMechanicsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,21 @@ public void ClrBackedValuesRoundTrip()
Assert.Equal(TimeSpan.FromMinutes(90), tsBack);
}

[Fact]
public void LevelValuesHandedToCallersCloneAsStrings()
{
// A caller re-parenting a `@Level` result, e.g. `seqcli search --column @Level`, sees a
// JSON string rather than the wrapper's fields.
var evt = new JsonObject { ["@l"] = "Warning" };
var expr = SeqExpression.Compile("@Level");

Assert.True(expr(evt).TryGetValue(out var level));
var enriched = new JsonObject { ["Column"] = level!.DeepClone() };

Assert.Equal("Warning", (string)enriched["Column"]!);
Assert.Equal("""{"Column":"Warning"}""", enriched.ToJsonString());
}

[Fact]
public void CallablesCanBeWrappedAndRecovered()
{
Expand Down
Loading