ProtoHelper - made generic 'get_oneof_case<TOneofEnum>' method (returns oneof-case of an IMessage obj)
This commit is contained in:
1 parent
b09ad7748f
commit
fa95ac6bfd
1 file changed
+134
-115
@@ -1,115 +1,134 @@
|
||||
using mbts;
|
||||
using Google.Protobuf;
|
||||
using Google.Protobuf.Reflection;
|
||||
using mbts.ui.utility;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using mbts.utils;
|
||||
using bts.api;
|
||||
using bts.api.orderman;
|
||||
using System.Reflection;
|
||||
|
||||
namespace bts.utils {
|
||||
static class ProtoHelper {
|
||||
const string
|
||||
_case_suffix = "Case",
|
||||
_oneof_case_suffix = "OneofCase";
|
||||
|
||||
public static MessageDescriptor Descriptor<T>() where T : IMessage, new() =>
|
||||
_DescriptorImpl<T>.value;
|
||||
|
||||
public static OneofDescriptor get_oneof_descriptor<T>(string prop_name) where T : IMessage, new() {
|
||||
prop_name = prop_name.iremove_suffix(_case_suffix);
|
||||
return Descriptor<T>().Oneofs.FirstOrDefault(d => d.Name.iequals(prop_name));
|
||||
}
|
||||
|
||||
public static IReadOnlyDictionary<TOneofCase, (FieldDescriptor field_descriptor, Type field_type)> get_oneof_descriptor_index<T, TOneofCase>(string prop_name)
|
||||
where T : IMessage, new()
|
||||
where TOneofCase : struct =>
|
||||
_DescriptorImpl<T>.OneofImpl<TOneofCase>.get_descriptor_case_index(prop_name);
|
||||
|
||||
public static FieldDescriptor get_oneof_field_descriptor<T, TOneofCase>(TOneofCase oneof_case) where T : IMessage, new() where TOneofCase : struct {
|
||||
string oneof_name = oneof_case.ToString();
|
||||
return Descriptor<T>().Fields.InFieldNumberOrder().FirstOrDefault(fd => fd.Name.Replace("_", "").iequals(oneof_name));
|
||||
}
|
||||
|
||||
public static TImmutable immutable_factory<TMutable, TImmutable>(TMutable m) where TMutable : IMessage, new() where TImmutable : IImmutableMessage =>
|
||||
(TImmutable)Activator.CreateInstance(typeof(TImmutable), m);
|
||||
|
||||
public static IMessage mutable_factory(Type type) =>
|
||||
MutableFactoryStore.get(type)();
|
||||
|
||||
static class MutableFactoryStore {
|
||||
[ThreadStatic]
|
||||
static readonly Dictionary<Type, Func<IMessage>> _factories = new Dictionary<Type, Func<IMessage>>();
|
||||
|
||||
public static Func<IMessage> get(Type type) =>
|
||||
_factories.GetOrAdd(type,
|
||||
t => typeof(MutableFactory<>).MakeGenericType(t)
|
||||
.GetField(nameof(MutableFactory<LimitOrderDefinition>.anonymous_factory), BindingFlags.Static | BindingFlags.Public)
|
||||
.GetValue(null) as Func<IMessage>);
|
||||
}
|
||||
|
||||
static class MutableFactory<T> where T : IMessage<T>, new() {
|
||||
public static readonly Func<IMessage> anonymous_factory = () => new T();
|
||||
}
|
||||
|
||||
public static Type get_field_type(FieldDescriptor field_descriptor) {
|
||||
switch (field_descriptor.FieldType) {
|
||||
case FieldType.Bool: return typeof(bool);
|
||||
case FieldType.Bytes: return typeof(ByteString);
|
||||
case FieldType.Double: return typeof(double);
|
||||
case FieldType.Float: return typeof(float);
|
||||
case FieldType.UInt32: return typeof(uint);
|
||||
case FieldType.UInt64: return typeof(ulong);
|
||||
// int
|
||||
case FieldType.SFixed32:
|
||||
case FieldType.Fixed32:
|
||||
case FieldType.SInt32:
|
||||
case FieldType.Int32: return typeof(int);
|
||||
// long
|
||||
case FieldType.SFixed64:
|
||||
case FieldType.Fixed64:
|
||||
case FieldType.SInt64:
|
||||
case FieldType.Int64: return typeof(long);
|
||||
//
|
||||
case FieldType.String: return typeof(string);
|
||||
case FieldType.Enum: return field_descriptor.EnumType.ClrType;
|
||||
case FieldType.Message: return field_descriptor.MessageType.ClrType;
|
||||
default: throw ExceptionBuilder.Argument.unexpected_case(field_descriptor.FieldType, nameof(field_descriptor));
|
||||
}
|
||||
}
|
||||
|
||||
public static IReadOnlyDictionary<TOneofCase, FieldDescriptor> get_oneof_descriptor_index<TOneofCase>(OneofDescriptor descriptor) where TOneofCase : struct =>
|
||||
descriptor.Fields.ToDictionary(fd => EnumHelper.parse<TOneofCase>(fd.Name.Replace("_", "")));
|
||||
|
||||
static class _DescriptorImpl<T> where T : IMessage, new() {
|
||||
public static readonly MessageDescriptor value = new T().Descriptor;
|
||||
|
||||
public static class OneofImpl<TOneofCase> where TOneofCase : struct {
|
||||
static Dictionary<string, IReadOnlyDictionary<TOneofCase, (FieldDescriptor field_descriptor, Type field_type)>> _descriptor_case_indices =
|
||||
new Dictionary<string, IReadOnlyDictionary<TOneofCase, (FieldDescriptor field_descriptor, Type field_type)>>();
|
||||
|
||||
public static IReadOnlyDictionary<TOneofCase, (FieldDescriptor field_descriptor, Type field_type)> get_descriptor_case_index(string prop_name) {
|
||||
/// Use local copy because <see cref="_descriptor_case_indices"/> is updated atomically.
|
||||
var dci = _descriptor_case_indices;
|
||||
if (!dci.TryGetValue(prop_name, out IReadOnlyDictionary<TOneofCase, (FieldDescriptor field_descriptor, Type field_type)> rv)) {
|
||||
/// Update <see cref="_descriptor_case_indices"/> atomically (lock-free).
|
||||
/// 1) clone the local copy.
|
||||
/// 2) add the new KeyValue to the clone.
|
||||
/// 3) assign the clone back to <see cref="_descriptor_case_indices"/>.
|
||||
rv = get_oneof_descriptor_index<TOneofCase>(get_oneof_descriptor<T>(prop_name))
|
||||
.ToDictionary(kv => kv.Key, kv => (kv.Value, get_field_type(kv.Value)));
|
||||
dci = dci.ToDictionary();
|
||||
dci.Add(prop_name, rv);
|
||||
/// It is possible for add ops to be overwritten.
|
||||
/// This is perfectly safe, just means that the 'clone-add-assign' will be repeated the next time a caller requests this key.
|
||||
_descriptor_case_indices = dci;
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
using bts.api;
|
||||
using bts.api.orderman;
|
||||
using Google.Protobuf;
|
||||
using Google.Protobuf.Reflection;
|
||||
using mbts;
|
||||
using mbts.ui.utilities;
|
||||
using mbts.ui.utility;
|
||||
using mbts.utils;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
|
||||
namespace bts.utils {
|
||||
static class ProtoHelper {
|
||||
const string
|
||||
_case_suffix = "Case",
|
||||
_oneof_case_suffix = "OneofCase";
|
||||
|
||||
public static MessageDescriptor Descriptor<T>() where T : IMessage, new() =>
|
||||
_DescriptorImpl<T>.value;
|
||||
|
||||
public static OneofDescriptor get_oneof_descriptor<T>(string prop_name) where T : IMessage, new() {
|
||||
prop_name = prop_name.iremove_suffix(_case_suffix);
|
||||
return Descriptor<T>().Oneofs.FirstOrDefault(d => d.Name.iequals(prop_name));
|
||||
}
|
||||
|
||||
public static IReadOnlyDictionary<TOneofCase, (FieldDescriptor field_descriptor, Type field_type)> get_oneof_descriptor_index<T, TOneofCase>(string prop_name)
|
||||
where T : IMessage, new()
|
||||
where TOneofCase : struct =>
|
||||
_DescriptorImpl<T>.OneofImpl<TOneofCase>.get_descriptor_case_index(prop_name);
|
||||
|
||||
public static FieldDescriptor get_oneof_field_descriptor<T, TOneofCase>(TOneofCase oneof_case) where T : IMessage, new() where TOneofCase : struct {
|
||||
string oneof_name = oneof_case.ToString();
|
||||
return Descriptor<T>().Fields.InFieldNumberOrder().FirstOrDefault(fd => fd.Name.Replace("_", "").iequals(oneof_name));
|
||||
}
|
||||
|
||||
public static TOneofCase get_oneof_case<TOneofCase>(OneofDescriptor oneof_descriptor, IMessage value) where TOneofCase : struct {
|
||||
var fd = oneof_descriptor.Accessor.GetCaseFieldDescriptor(value);
|
||||
if (fd == null)
|
||||
return default;
|
||||
string enum_type_name = DisplayNameHelper.snake_to_title_case(oneof_descriptor.Name) + "OneofCase";
|
||||
var enum_type = oneof_descriptor.ContainingType.ClrType.GetNestedType(enum_type_name);
|
||||
if (enum_type == null) {
|
||||
var all_types = oneof_descriptor.ContainingType.ClrType.GetNestedTypes();
|
||||
enum_type = all_types.FirstOrDefault(t => t.Name == enum_type_name);
|
||||
if (enum_type == null)
|
||||
return default;
|
||||
}
|
||||
string adj_name = fd.Name.Replace("_", "");
|
||||
var result = Enum.Parse(enum_type, adj_name, true);
|
||||
var converted = ConvertUtils.try_convert<TOneofCase>(result);
|
||||
return converted;
|
||||
}
|
||||
|
||||
public static TImmutable immutable_factory<TMutable, TImmutable>(TMutable m) where TMutable : IMessage, new() where TImmutable : IImmutableMessage =>
|
||||
(TImmutable)Activator.CreateInstance(typeof(TImmutable), m);
|
||||
|
||||
public static IMessage mutable_factory(Type type) =>
|
||||
MutableFactoryStore.get(type)();
|
||||
|
||||
static class MutableFactoryStore {
|
||||
[ThreadStatic]
|
||||
static readonly Dictionary<Type, Func<IMessage>> _factories = new Dictionary<Type, Func<IMessage>>();
|
||||
|
||||
public static Func<IMessage> get(Type type) =>
|
||||
_factories.GetOrAdd(type,
|
||||
t => typeof(MutableFactory<>).MakeGenericType(t)
|
||||
.GetField(nameof(MutableFactory<LimitOrderDefinition>.anonymous_factory), BindingFlags.Static | BindingFlags.Public)
|
||||
.GetValue(null) as Func<IMessage>);
|
||||
}
|
||||
|
||||
static class MutableFactory<T> where T : IMessage<T>, new() {
|
||||
public static readonly Func<IMessage> anonymous_factory = () => new T();
|
||||
}
|
||||
|
||||
public static Type get_field_type(FieldDescriptor field_descriptor) {
|
||||
switch (field_descriptor.FieldType) {
|
||||
case FieldType.Bool: return typeof(bool);
|
||||
case FieldType.Bytes: return typeof(ByteString);
|
||||
case FieldType.Double: return typeof(double);
|
||||
case FieldType.Float: return typeof(float);
|
||||
case FieldType.UInt32: return typeof(uint);
|
||||
case FieldType.UInt64: return typeof(ulong);
|
||||
// int
|
||||
case FieldType.SFixed32:
|
||||
case FieldType.Fixed32:
|
||||
case FieldType.SInt32:
|
||||
case FieldType.Int32: return typeof(int);
|
||||
// long
|
||||
case FieldType.SFixed64:
|
||||
case FieldType.Fixed64:
|
||||
case FieldType.SInt64:
|
||||
case FieldType.Int64: return typeof(long);
|
||||
//
|
||||
case FieldType.String: return typeof(string);
|
||||
case FieldType.Enum: return field_descriptor.EnumType.ClrType;
|
||||
case FieldType.Message: return field_descriptor.MessageType.ClrType;
|
||||
default: throw ExceptionBuilder.Argument.unexpected_case(field_descriptor.FieldType, nameof(field_descriptor));
|
||||
}
|
||||
}
|
||||
|
||||
public static IReadOnlyDictionary<TOneofCase, FieldDescriptor> get_oneof_descriptor_index<TOneofCase>(OneofDescriptor descriptor) where TOneofCase : struct =>
|
||||
descriptor.Fields.ToDictionary(fd => EnumHelper.parse<TOneofCase>(fd.Name.Replace("_", "")));
|
||||
|
||||
static class _DescriptorImpl<T> where T : IMessage, new() {
|
||||
public static readonly MessageDescriptor value = new T().Descriptor;
|
||||
|
||||
public static class OneofImpl<TOneofCase> where TOneofCase : struct {
|
||||
static Dictionary<string, IReadOnlyDictionary<TOneofCase, (FieldDescriptor field_descriptor, Type field_type)>> _descriptor_case_indices =
|
||||
new Dictionary<string, IReadOnlyDictionary<TOneofCase, (FieldDescriptor field_descriptor, Type field_type)>>();
|
||||
|
||||
public static IReadOnlyDictionary<TOneofCase, (FieldDescriptor field_descriptor, Type field_type)> get_descriptor_case_index(string prop_name) {
|
||||
/// Use local copy because <see cref="_descriptor_case_indices"/> is updated atomically.
|
||||
var dci = _descriptor_case_indices;
|
||||
if (!dci.TryGetValue(prop_name, out IReadOnlyDictionary<TOneofCase, (FieldDescriptor field_descriptor, Type field_type)> rv)) {
|
||||
/// Update <see cref="_descriptor_case_indices"/> atomically (lock-free).
|
||||
/// 1) clone the local copy.
|
||||
/// 2) add the new KeyValue to the clone.
|
||||
/// 3) assign the clone back to <see cref="_descriptor_case_indices"/>.
|
||||
rv = get_oneof_descriptor_index<TOneofCase>(get_oneof_descriptor<T>(prop_name))
|
||||
.ToDictionary(kv => kv.Key, kv => (kv.Value, get_field_type(kv.Value)));
|
||||
dci = dci.ToDictionary();
|
||||
dci.Add(prop_name, rv);
|
||||
/// It is possible for add ops to be overwritten.
|
||||
/// This is perfectly safe, just means that the 'clone-add-assign' will be repeated the next time a caller requests this key.
|
||||
_descriptor_case_indices = dci;
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user