Skip to content

Commit

Permalink
Improve header value.
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianStehle committed Jan 18, 2025
1 parent b97d00d commit 709397f
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 59 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<PackageProjectUrl>https://github.com/squidex/squidex</PackageProjectUrl>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<Version>6.27.0</Version>
<Version>6.28.0</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(GITHUB_ACTIONS)' == 'true'">
Expand Down
30 changes: 19 additions & 11 deletions events/Squidex.Events.Mongo/HeaderValueSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,18 @@ public override HeaderValue Deserialize(BsonDeserializationContext context, Bson
switch (reader.CurrentBsonType)
{
case BsonType.String:
return new HeaderStringValue(reader.ReadString());
return reader.ReadString();
case BsonType.Int32:
return new HeaderNumberValue(reader.ReadInt32());
return reader.ReadInt32();
case BsonType.Int64:
return new HeaderNumberValue(reader.ReadInt64());
return reader.ReadInt64();
case BsonType.Double:
return reader.ReadDouble();
case BsonType.Boolean:
return new HeaderBooleanValue(reader.ReadBoolean());
return reader.ReadBoolean();
case BsonType.Null:
reader.ReadNull();
return default;
default:
throw new BsonSerializationException($"Unsupported token '{reader.CurrentBsonType}'.");
}
Expand All @@ -34,16 +39,19 @@ public override HeaderValue Deserialize(BsonDeserializationContext context, Bson
public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, HeaderValue value)
{
var writer = context.Writer;
switch (value)
switch (value.Value)
{
case HeaderStringValue s:
writer.WriteString(s.Value);
case string s:
writer.WriteString(s);
break;
case HeaderNumberValue n:
writer.WriteInt64(n.Value);
case double n:
writer.WriteDouble(n);
break;
case HeaderBooleanValue b:
writer.WriteBoolean(b.Value);
case bool b:
writer.WriteBoolean(b);
break;
case null:
writer.WriteNull();
break;
default:
throw new BsonSerializationException($"Unsupported value type '{value.GetType()}'.");
Expand Down
31 changes: 29 additions & 2 deletions events/Squidex.Events.Tests/EnvelopeHeadersTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// ==========================================================================

using FakeItEasy;
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using Squidex.Events.Mongo;
using Xunit;
Expand Down Expand Up @@ -211,7 +212,8 @@ public void Should_serialize_and_deserialize_to_json_bytes()
["key1"] = 13,
["key2"] = "Hello World",
["key3"] = true,
["key4"] = false
["key4"] = false,
["key5"] = default
};

var json = source.SerializeToJsonBytes();
Expand All @@ -229,14 +231,39 @@ public void Should_serialize_and_deserialize_bson()
["key1"] = 13,
["key2"] = "Hello World",
["key3"] = true,
["key4"] = false
["key4"] = false,
["key5"] = default
};

var deserialized = source.SerializeAndDeserializeBson();

CompareHeaders(deserialized, source);
}

[Fact]
public void Should_serialize_and_deserialize_bson_numbers()
{
var source = new BsonDocument
{
["number1"] = 100,
["number2"] = 200L,
["number3"] = 300.5f,
["number4"] = 400.5d
};

var expected = new EnvelopeHeaders
{
["number1"] = 100,
["number2"] = 200,
["number3"] = 300.5,
["number4"] = 400.5
};

var deserialized = source.SerializeAndDeserializeBson<BsonDocument, EnvelopeHeaders>();

CompareHeaders(deserialized, expected);
}

private static void CompareHeaders(EnvelopeHeaders lhs, EnvelopeHeaders rhs)
{
foreach (var key in lhs.Keys.Concat(rhs.Keys).Distinct())
Expand Down
9 changes: 7 additions & 2 deletions events/Squidex.Events.Tests/TestHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,24 @@ public sealed class ObjectHolder<T>
}

public static T SerializeAndDeserializeBson<T>(this T value)
{
return SerializeAndDeserializeBson<T, T>(value);
}

public static TOut SerializeAndDeserializeBson<TIn, TOut>(this TIn value)
{
using var stream = new MemoryStream();

using (var writer = new BsonBinaryWriter(stream))
{
BsonSerializer.Serialize(writer, new ObjectHolder<T> { Value = value });
BsonSerializer.Serialize(writer, new ObjectHolder<TIn> { Value = value });
}

stream.Position = 0;

using (var reader = new BsonBinaryReader(stream))
{
return BsonSerializer.Deserialize<ObjectHolder<T>>(reader).Value;
return BsonSerializer.Deserialize<ObjectHolder<TOut>>(reader).Value;
}
}
}
12 changes: 6 additions & 6 deletions events/Squidex.Events/EnvelopeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static DateTime GetDateTime(this EnvelopeHeaders obj, string key)
{
if (obj.TryGetValue(key, out var found))
{
if (found is HeaderStringValue s && DateTime.TryParse(s.Value, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, out var dateTime))
if (found.Value is string s && DateTime.TryParse(s, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, out var dateTime))
{
return dateTime;
}
Expand All @@ -33,12 +33,12 @@ public static long GetLong(this EnvelopeHeaders obj, string key)
{
if (obj.TryGetValue(key, out var found))
{
if (found is HeaderNumberValue n)
if (found.Value is double n)
{
return n.Value;
return (long)n;
}

if (found is HeaderStringValue s && double.TryParse(s.Value, NumberStyles.Any, CultureInfo.InvariantCulture, out var result))
if (found.Value is string s && double.TryParse(s, NumberStyles.Any, CultureInfo.InvariantCulture, out var result))
{
return (long)result;
}
Expand All @@ -59,9 +59,9 @@ public static string GetString(this EnvelopeHeaders obj, string key)

public static bool GetBoolean(this EnvelopeHeaders obj, string key)
{
if (obj.TryGetValue(key, out var found) && found is HeaderBooleanValue b)
if (obj.TryGetValue(key, out var found) && found.Value is bool b)
{
return b.Value;
return b;
}

return false;
Expand Down
45 changes: 21 additions & 24 deletions events/Squidex.Events/EventHeaderValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,48 +6,45 @@
// ==========================================================================

#pragma warning disable MA0048 // File name must match type name
#pragma warning disable SA1313 // Parameter names should begin with lower-case letter

namespace Squidex.Events;

public abstract record HeaderValue
public readonly record struct HeaderValue
{
public static implicit operator HeaderValue(string source)
{
return new HeaderStringValue(source);
}
public object? Value { get; }

public static implicit operator HeaderValue(long source)
private HeaderValue(object? value)
{
return new HeaderNumberValue(source);
Value = value;
}

public static implicit operator HeaderValue(bool source)
public static implicit operator HeaderValue(string source)
{
return new HeaderBooleanValue(source);
return new HeaderValue(source);
}
}

public record HeaderBooleanValue(bool Value) : HeaderValue
{
public override string ToString()
public static implicit operator HeaderValue(double source)
{
return Value ? "true" : "false";
return new HeaderValue(source);
}
}

public record HeaderNumberValue(long Value) : HeaderValue
{
public override string ToString()
public static implicit operator HeaderValue(bool source)
{
return Value.ToString();
return new HeaderValue(source);
}
}

public record HeaderStringValue(string Value) : HeaderValue
{
public override string ToString()
{
return Value;
switch (Value)
{
case null:
return "null";
case true:
return "true";
case false:
return "false";
default:
return Value.ToString()!;
}
}
}
31 changes: 18 additions & 13 deletions events/Squidex.Events/Utils/HeaderValueConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,38 +12,43 @@ namespace Squidex.Events.Utils;

public sealed class HeaderValueConverter : JsonConverter<HeaderValue>
{
public override HeaderValue? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
public override HeaderValue Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
switch (reader.TokenType)
{
case JsonTokenType.String:
return new HeaderStringValue(reader.GetString()!);
return reader.GetString()!;
case JsonTokenType.Number:
return new HeaderNumberValue(reader.GetInt64());
return reader.GetDouble();
case JsonTokenType.Null:
return default;
case JsonTokenType.True:
return new HeaderBooleanValue(true);
return true;
case JsonTokenType.False:
return new HeaderBooleanValue(false);
return false;
default:
throw new JsonException($"Unsupported token '{reader.TokenType}'.");
}
}

public override void Write(Utf8JsonWriter writer, HeaderValue value, JsonSerializerOptions options)
{
switch (value)
switch (value.Value)
{
case HeaderStringValue s:
writer.WriteStringValue(s.Value);
case string s:
writer.WriteStringValue(s);
break;
case HeaderNumberValue n:
writer.WriteNumberValue(n.Value);
case double n:
writer.WriteNumberValue(n);
break;
case HeaderBooleanValue b:
writer.WriteBooleanValue(b.Value);
case bool b:
writer.WriteBooleanValue(b);
break;
case null:
writer.WriteNullValue();
break;
default:
throw new JsonException($"Unsupported value type '{value.GetType()}'.");
throw new JsonException($"Unsupported value type '{value.Value.GetType()}'.");
}
}
}

0 comments on commit 709397f

Please sign in to comment.