feat(batch15): complete group 1 msgblock/consumerfilestore
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
using System.Text.Json;
|
||||
|
||||
namespace ZB.MOM.NatsNet.Server;
|
||||
|
||||
/// <summary>
|
||||
/// String/JSON helpers for store enums that mirror Go enum methods.
|
||||
/// </summary>
|
||||
public static class StoreEnumExtensions
|
||||
{
|
||||
public static string String(this StoreCipher cipher)
|
||||
=> cipher switch
|
||||
{
|
||||
StoreCipher.ChaCha => "ChaCha20-Poly1305",
|
||||
StoreCipher.Aes => "AES-GCM",
|
||||
StoreCipher.NoCipher => "None",
|
||||
_ => "Unknown StoreCipher",
|
||||
};
|
||||
|
||||
public static string String(this StoreCompression alg)
|
||||
=> alg switch
|
||||
{
|
||||
StoreCompression.NoCompression => "None",
|
||||
StoreCompression.S2Compression => "S2",
|
||||
_ => "Unknown StoreCompression",
|
||||
};
|
||||
|
||||
public static byte[] MarshalJSON(this StoreCompression alg)
|
||||
=> alg switch
|
||||
{
|
||||
StoreCompression.NoCompression => JsonSerializer.SerializeToUtf8Bytes("none"),
|
||||
StoreCompression.S2Compression => JsonSerializer.SerializeToUtf8Bytes("s2"),
|
||||
_ => throw new InvalidOperationException("unknown compression algorithm"),
|
||||
};
|
||||
|
||||
public static void UnmarshalJSON(this ref StoreCompression alg, ReadOnlySpan<byte> b)
|
||||
{
|
||||
var parsed = JsonSerializer.Deserialize<string>(b);
|
||||
if (parsed == null)
|
||||
throw new InvalidDataException("compression value must be a JSON string");
|
||||
|
||||
alg = parsed switch
|
||||
{
|
||||
"none" => StoreCompression.NoCompression,
|
||||
"s2" => StoreCompression.S2Compression,
|
||||
_ => throw new InvalidOperationException("unknown compression algorithm"),
|
||||
};
|
||||
}
|
||||
|
||||
public static void UnmarshalJSON(this ref StoreCompression alg, byte[] b)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(b);
|
||||
UnmarshalJSON(ref alg, b.AsSpan());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user