Skip to content

Commit

Permalink
Move field type resolving logic to separate class
Browse files Browse the repository at this point in the history
  • Loading branch information
Havret committed Jun 23, 2022
1 parent ff1fba0 commit 685c523
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 57 deletions.
64 changes: 64 additions & 0 deletions src/Protobuf.System.Text.Json/FieldTypeResolver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using Google.Protobuf.Reflection;
using Google.Protobuf.WellKnownTypes;
using Type = System.Type;

namespace Protobuf.System.Text.Json;

internal static class FieldTypeResolver
{
public static Type ResolverFieldType(FieldDescriptor fieldDescriptor, Dictionary<string, Type> propertyTypeLookup)
{
switch (fieldDescriptor.FieldType)
{
case FieldType.Double:
return typeof(double);
case FieldType.Float:
return typeof(float);
case FieldType.Int32:
case FieldType.SInt32:
case FieldType.SFixed32:
return typeof(int);
case FieldType.Int64:
case FieldType.SInt64:
case FieldType.SFixed64:
return typeof(long);
case FieldType.UInt32:
case FieldType.Fixed32:
return typeof(uint);
case FieldType.UInt64:
case FieldType.Fixed64:
return typeof(ulong);
case FieldType.Bool:
return typeof(bool);
case FieldType.String:
return typeof(string);
case FieldType.Message when fieldDescriptor.MessageType.ClrType is { } clrType:
if (clrType == typeof(DoubleValue))
return typeof(double?);
if (clrType == typeof(FloatValue))
return typeof(float?);
if (clrType == typeof(Int64Value))
return typeof(long?);
if (clrType == typeof(UInt64Value))
return typeof(ulong?);
if (clrType == typeof(Int32Value))
return typeof(int?);
if (clrType == typeof(UInt32Value))
return typeof(uint?);
if (clrType == typeof(BoolValue))
return typeof(bool?);
if (clrType == typeof(StringValue))
return typeof(string);
return clrType;
case FieldType.Enum when fieldDescriptor.IsRepeated:
var fieldType = propertyTypeLookup[fieldDescriptor.PropertyName];
return fieldType.GenericTypeArguments[0];
case FieldType.Enum:
case FieldType.Message:
return propertyTypeLookup[fieldDescriptor.PropertyName];
default:
throw new ArgumentOutOfRangeException(nameof(fieldDescriptor),
$"FieldType: '{fieldDescriptor}' is not supported.");
}
}
}
58 changes: 1 addition & 57 deletions src/Protobuf.System.Text.Json/ProtobufConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Text.Json.Serialization;
using Google.Protobuf;
using Google.Protobuf.Reflection;
using Google.Protobuf.WellKnownTypes;
using Protobuf.System.Text.Json.InternalConverters;
using Type = System.Type;

Expand Down Expand Up @@ -33,7 +32,7 @@ public ProtobufConverter(JsonSerializerOptions jsonSerializerOptions, JsonProtob
Accessor = fieldDescriptor.Accessor,
IsRepeated = fieldDescriptor.IsRepeated,
IsMap = fieldDescriptor.IsMap,
FieldType = GetFieldType(fieldDescriptor, propertyTypeLookup),
FieldType = FieldTypeResolver.ResolverFieldType(fieldDescriptor, propertyTypeLookup),
JsonName = convertNameFunc(fieldDescriptor),
IsOneOf = fieldDescriptor.ContainingOneof != null
}).ToArray();
Expand Down Expand Up @@ -142,59 +141,4 @@ public override void Write(Utf8JsonWriter writer, T? value, JsonSerializerOption

writer.WriteEndObject();
}

private Type GetFieldType(FieldDescriptor fieldDescriptor, Dictionary<string, Type> propertyTypeLookup)
{
switch (fieldDescriptor.FieldType)
{
case FieldType.Double:
return typeof(double);
case FieldType.Float:
return typeof(float);
case FieldType.Int32:
case FieldType.SInt32:
case FieldType.SFixed32:
return typeof(int);
case FieldType.Int64:
case FieldType.SInt64:
case FieldType.SFixed64:
return typeof(long);
case FieldType.UInt32:
case FieldType.Fixed32:
return typeof(uint);
case FieldType.UInt64:
case FieldType.Fixed64:
return typeof(ulong);
case FieldType.Bool:
return typeof(bool);
case FieldType.String:
return typeof(string);
case FieldType.Message when fieldDescriptor.MessageType.ClrType is { } clrType:
if (clrType == typeof(DoubleValue))
return typeof(double?);
if (clrType == typeof(FloatValue))
return typeof(float?);
if (clrType == typeof(Int64Value))
return typeof(long?);
if (clrType == typeof(UInt64Value))
return typeof(ulong?);
if (clrType == typeof(Int32Value))
return typeof(int?);
if (clrType == typeof(UInt32Value))
return typeof(uint?);
if (clrType == typeof(BoolValue))
return typeof(bool?);
if (clrType == typeof(StringValue))
return typeof(string);
return clrType;
case FieldType.Enum when fieldDescriptor.IsRepeated:
var fieldType = propertyTypeLookup[fieldDescriptor.PropertyName];
return fieldType.GenericTypeArguments[0];
case FieldType.Enum:
case FieldType.Message:
return propertyTypeLookup[fieldDescriptor.PropertyName];
default:
throw new ArgumentOutOfRangeException(nameof(fieldDescriptor), $"FieldType: '{fieldDescriptor}' is not supported.");
}
}
}

0 comments on commit 685c523

Please sign in to comment.