Initial commit
This commit is contained in:
152
DynamicCodec.cs
Normal file
152
DynamicCodec.cs
Normal file
@@ -0,0 +1,152 @@
|
||||
using Google.Protobuf;
|
||||
using Google.Protobuf.Collections;
|
||||
|
||||
namespace DynamicProtobuf.Runtime;
|
||||
|
||||
public static class DynamicCodec
|
||||
{
|
||||
public static FieldCodec<string> ForString(string fullName, string fieldName) => ForString(fullName, fieldName, "");
|
||||
|
||||
public static FieldCodec<ByteString> ForBytes(string fullName, string fieldName) => ForBytes(fullName, fieldName, ByteString.Empty);
|
||||
|
||||
public static FieldCodec<bool> ForBool(string fullName, string fieldName) => ForBool(fullName, fieldName, false);
|
||||
|
||||
public static FieldCodec<int> ForInt32(string fullName, string fieldName) => ForInt32(fullName, fieldName, 0);
|
||||
|
||||
public static FieldCodec<int> ForSInt32(string fullName, string fieldName) => ForSInt32(fullName, fieldName, 0);
|
||||
|
||||
public static FieldCodec<uint> ForFixed32(string fullName, string fieldName) => ForFixed32(fullName, fieldName, 0);
|
||||
|
||||
public static FieldCodec<int> ForSFixed32(string fullName, string fieldName) => ForSFixed32(fullName, fieldName, 0);
|
||||
|
||||
public static FieldCodec<uint> ForUInt32(string fullName, string fieldName) => ForUInt32(fullName, fieldName, 0);
|
||||
|
||||
public static FieldCodec<long> ForInt64(string fullName, string fieldName) => ForInt64(fullName, fieldName, 0);
|
||||
|
||||
|
||||
public static FieldCodec<long> ForSInt64(string fullName, string fieldName) => ForSInt64(fullName, fieldName, 0);
|
||||
|
||||
public static FieldCodec<ulong> ForFixed64(string fullName, string fieldName) => ForFixed64(fullName, fieldName, 0);
|
||||
|
||||
public static FieldCodec<long> ForSFixed64(string fullName, string fieldName) => ForSFixed64(fullName, fieldName, 0);
|
||||
|
||||
public static FieldCodec<ulong> ForUInt64(string fullName, string fieldName) => ForUInt64(fullName, fieldName, 0);
|
||||
|
||||
public static FieldCodec<float> ForFloat(string fullName, string fieldName) => ForFloat(fullName, fieldName, 0);
|
||||
|
||||
public static FieldCodec<double> ForDouble(string fullName, string fieldName) => ForDouble(fullName, fieldName, 0);
|
||||
|
||||
public static FieldCodec<T?> ForEnum<T>(string fullName, string fieldName, Func<T?, int> toInt32, Func<int, T> fromInt32) =>
|
||||
ForEnum(fullName, fieldName, toInt32, fromInt32, default);
|
||||
|
||||
public static FieldCodec<string> ForString(string fullName, string fieldName, string defaultValue)
|
||||
{
|
||||
uint tag = DynamicFieldRegistry.GetTag(fullName, fieldName);
|
||||
return FieldCodec.ForString(tag, defaultValue);
|
||||
}
|
||||
|
||||
public static FieldCodec<ByteString> ForBytes(string fullName, string fieldName, ByteString defaultValue)
|
||||
{
|
||||
uint tag = DynamicFieldRegistry.GetTag(fullName, fieldName);
|
||||
return FieldCodec.ForBytes(tag, defaultValue);
|
||||
}
|
||||
|
||||
public static FieldCodec<bool> ForBool(string fullName, string fieldName, bool defaultValue)
|
||||
{
|
||||
uint tag = DynamicFieldRegistry.GetTag(fullName, fieldName);
|
||||
return FieldCodec.ForBool(tag, defaultValue);
|
||||
}
|
||||
|
||||
public static FieldCodec<int> ForInt32(string fullName, string fieldName, int defaultValue)
|
||||
{
|
||||
uint tag = DynamicFieldRegistry.GetTag(fullName, fieldName);
|
||||
return FieldCodec.ForInt32(tag, defaultValue);
|
||||
}
|
||||
|
||||
public static FieldCodec<int> ForSInt32(string fullName, string fieldName, int defaultValue)
|
||||
{
|
||||
uint tag = DynamicFieldRegistry.GetTag(fullName, fieldName);
|
||||
return FieldCodec.ForSInt32(tag, defaultValue);
|
||||
}
|
||||
|
||||
public static FieldCodec<uint> ForFixed32(string fullName, string fieldName, uint defaultValue)
|
||||
{
|
||||
uint tag = DynamicFieldRegistry.GetTag(fullName, fieldName);
|
||||
return FieldCodec.ForFixed32(tag);
|
||||
}
|
||||
|
||||
public static FieldCodec<int> ForSFixed32(string fullName, string fieldName, int defaultValue)
|
||||
{
|
||||
uint tag = DynamicFieldRegistry.GetTag(fullName, fieldName);
|
||||
return FieldCodec.ForSFixed32(tag, defaultValue);
|
||||
}
|
||||
|
||||
public static FieldCodec<uint> ForUInt32(string fullName, string fieldName, uint defaultValue)
|
||||
{
|
||||
uint tag = DynamicFieldRegistry.GetTag(fullName, fieldName);
|
||||
return FieldCodec.ForUInt32(tag, defaultValue);
|
||||
}
|
||||
|
||||
public static FieldCodec<long> ForInt64(string fullName, string fieldName, long defaultValue)
|
||||
{
|
||||
uint tag = DynamicFieldRegistry.GetTag(fullName, fieldName);
|
||||
return FieldCodec.ForInt64(tag, defaultValue);
|
||||
}
|
||||
|
||||
public static FieldCodec<long> ForSInt64(string fullName, string fieldName, long defaultValue)
|
||||
{
|
||||
uint tag = DynamicFieldRegistry.GetTag(fullName, fieldName);
|
||||
return FieldCodec.ForSInt64(tag, defaultValue);
|
||||
}
|
||||
|
||||
public static FieldCodec<ulong> ForFixed64(string fullName, string fieldName, ulong defaultValue)
|
||||
{
|
||||
uint tag = DynamicFieldRegistry.GetTag(fullName, fieldName);
|
||||
return FieldCodec.ForFixed64(tag, defaultValue);
|
||||
}
|
||||
|
||||
public static FieldCodec<long> ForSFixed64(string fullName, string fieldName, long defaultValue)
|
||||
{
|
||||
uint tag = DynamicFieldRegistry.GetTag(fullName, fieldName);
|
||||
return FieldCodec.ForSFixed64(tag, defaultValue);
|
||||
}
|
||||
|
||||
public static FieldCodec<ulong> ForUInt64(string fullName, string fieldName, ulong defaultValue)
|
||||
{
|
||||
uint tag = DynamicFieldRegistry.GetTag(fullName, fieldName);
|
||||
return FieldCodec.ForUInt64(tag, defaultValue);
|
||||
}
|
||||
|
||||
|
||||
public static FieldCodec<float> ForFloat(string fullName, string fieldName, float defaultValue)
|
||||
{
|
||||
uint tag = DynamicFieldRegistry.GetTag(fullName, fieldName);
|
||||
return FieldCodec.ForFloat(tag, defaultValue);
|
||||
}
|
||||
|
||||
public static FieldCodec<double> ForDouble(string fullName, string fieldName, double defaultValue)
|
||||
{
|
||||
uint tag = DynamicFieldRegistry.GetTag(fullName, fieldName);
|
||||
return FieldCodec.ForDouble(tag, defaultValue);
|
||||
}
|
||||
|
||||
public static FieldCodec<T> ForEnum<T>(string fullName, string fieldName, Func<T, int> toInt32, Func<int, T> fromInt32, T defaultValue)
|
||||
{
|
||||
uint tag = DynamicFieldRegistry.GetTag(fullName, fieldName);
|
||||
return FieldCodec.ForEnum(tag, toInt32, fromInt32, defaultValue);
|
||||
}
|
||||
|
||||
public static MapField<TKey, TValue>.Codec ForMap<TKey, TValue>(string fullName, string fieldName, FieldCodec<TKey> keyCodec,
|
||||
FieldCodec<TValue> valueCodec)
|
||||
{
|
||||
uint tag = DynamicFieldRegistry.GetTag(fullName, fieldName);
|
||||
return new MapField<TKey, TValue>.Codec(keyCodec, valueCodec, tag);
|
||||
}
|
||||
|
||||
public static FieldCodec<T> ForMessage<T>(string fullName, string fieldName, MessageParser<T> parser)
|
||||
where T : class, IMessage<T>
|
||||
{
|
||||
uint tag = DynamicFieldRegistry.GetTag(fullName, fieldName);
|
||||
return FieldCodec.ForMessage(tag, parser);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user