MessageTypeId.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System;
  2. using JetBrains.Annotations;
  3. using ProtoBuf;
  4. namespace Abc.Zebus
  5. {
  6. [ProtoContract(Surrogate = typeof(ProtobufSurrogate))]
  7. public readonly struct MessageTypeId : IEquatable<MessageTypeId>
  8. {
  9. public static readonly MessageTypeId EndOfStream = new MessageTypeId("Abc.Zebus.Transport.EndOfStream");
  10. public static readonly MessageTypeId EndOfStreamAck = new MessageTypeId("Abc.Zebus.Transport.EndOfStreamAck");
  11. public static readonly MessageTypeId PersistenceStopping = new MessageTypeId("Abc.Zebus.PersistentTransport.PersistenceStopping");
  12. public static readonly MessageTypeId PersistenceStoppingAck = new MessageTypeId("Abc.Zebus.PersistentTransport.PersistenceStoppingAck");
  13. private readonly MessageTypeDescriptor? _descriptor;
  14. public MessageTypeId(Type? messageType)
  15. => _descriptor = MessageTypeDescriptorCache.GetMessageTypeDescriptor(messageType);
  16. public MessageTypeId(string? fullName)
  17. => _descriptor = MessageTypeDescriptorCache.GetMessageTypeDescriptor(fullName);
  18. private MessageTypeId(MessageTypeDescriptor descriptor)
  19. => _descriptor = descriptor;
  20. public string? FullName => Descriptor.FullName;
  21. [System.Diagnostics.Contracts.Pure]
  22. public Type? GetMessageType() => Descriptor.MessageType;
  23. [System.Diagnostics.Contracts.Pure]
  24. public bool IsInfrastructure() => Descriptor.IsInfrastructure;
  25. [System.Diagnostics.Contracts.Pure]
  26. public bool IsPersistent() => Descriptor.IsPersistent;
  27. internal MessageTypeDescriptor Descriptor => _descriptor ?? MessageTypeDescriptor.Null;
  28. public override string ToString()
  29. {
  30. if (FullName is null)
  31. return "(unknown type)";
  32. var lastDotIndex = FullName.LastIndexOf('.');
  33. return lastDotIndex != -1 ? FullName.Substring(lastDotIndex + 1) : FullName;
  34. }
  35. internal static MessageTypeId GetMessageTypeIdBypassCache(Type? messageType)
  36. => new MessageTypeId(MessageTypeDescriptorCache.GetMessageTypeDescriptorBypassCache(messageType));
  37. public bool Equals(MessageTypeId other) => _descriptor == other._descriptor;
  38. public override bool Equals(object? obj) => obj is MessageTypeId messageTypeId && Equals(messageTypeId);
  39. public override int GetHashCode() => Descriptor.GetHashCode();
  40. public static bool operator ==(MessageTypeId left, MessageTypeId right) => left.Equals(right);
  41. public static bool operator !=(MessageTypeId left, MessageTypeId right) => !left.Equals(right);
  42. [ProtoContract]
  43. [UsedImplicitly(ImplicitUseTargetFlags.WithMembers)]
  44. internal struct ProtobufSurrogate
  45. {
  46. [ProtoMember(1, IsRequired = true)]
  47. private string? FullName { get; set; }
  48. private ProtobufSurrogate(MessageTypeId typeId)
  49. => FullName = typeId.FullName;
  50. public static implicit operator MessageTypeId(ProtobufSurrogate value) => new MessageTypeId(value.FullName);
  51. public static implicit operator ProtobufSurrogate(MessageTypeId value) => new ProtobufSurrogate(value);
  52. }
  53. }
  54. }