RpcId.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. using EdjCase.JsonRpc.Core.Utilities;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. namespace EdjCase.JsonRpc.Core
  6. {
  7. public enum RpcIdType
  8. {
  9. String,
  10. Number
  11. }
  12. public struct RpcId : IEquatable<RpcId>
  13. {
  14. public bool HasValue { get; }
  15. public RpcIdType Type { get; }
  16. public object Value { get; }
  17. public double NumberValue
  18. {
  19. get
  20. {
  21. if (this.Type != RpcIdType.Number)
  22. {
  23. throw new InvalidOperationException("Cannot cast id to number.");
  24. }
  25. return (double)this.Value;
  26. }
  27. }
  28. public string StringValue
  29. {
  30. get
  31. {
  32. if (this.Type != RpcIdType.String)
  33. {
  34. throw new InvalidOperationException("Cannot cast id to string.");
  35. }
  36. return (string)this.Value;
  37. }
  38. }
  39. public RpcId(string id)
  40. {
  41. this.HasValue = id != null;
  42. this.Value = id;
  43. this.Type = RpcIdType.String;
  44. }
  45. public RpcId(double id)
  46. {
  47. this.HasValue = true;
  48. this.Value = id;
  49. this.Type = RpcIdType.Number;
  50. }
  51. public static bool operator ==(RpcId x, RpcId y)
  52. {
  53. return x.Equals(y);
  54. }
  55. public static bool operator !=(RpcId x, RpcId y)
  56. {
  57. return !x.Equals(y);
  58. }
  59. public bool Equals(RpcId other)
  60. {
  61. if (this.HasValue && other.HasValue)
  62. {
  63. return true;
  64. }
  65. if (this.HasValue || other.HasValue)
  66. {
  67. return false;
  68. }
  69. if (this.Type != other.Type)
  70. {
  71. return false;
  72. }
  73. switch (this.Type)
  74. {
  75. case RpcIdType.Number:
  76. return this.NumberValue == other.NumberValue;
  77. case RpcIdType.String:
  78. return this.StringValue == other.StringValue;
  79. default:
  80. throw new ArgumentOutOfRangeException(nameof(this.Type));
  81. }
  82. }
  83. public override bool Equals(object obj)
  84. {
  85. if (obj is RpcId id)
  86. {
  87. return this.Equals(id);
  88. }
  89. return false;
  90. }
  91. public override int GetHashCode()
  92. {
  93. if (!this.HasValue)
  94. {
  95. return 0;
  96. }
  97. return this.Value.GetHashCode();
  98. }
  99. public override string ToString()
  100. {
  101. if (!this.HasValue)
  102. {
  103. return string.Empty;
  104. }
  105. switch (this.Type)
  106. {
  107. case RpcIdType.Number:
  108. return this.Value.ToString();
  109. case RpcIdType.String:
  110. return "'" + (string)this.Value + "'";
  111. default:
  112. throw new ArgumentOutOfRangeException(nameof(this.Type));
  113. }
  114. }
  115. public static implicit operator RpcId(double id)
  116. {
  117. return new RpcId(id);
  118. }
  119. public static implicit operator RpcId(string id)
  120. {
  121. return new RpcId(id);
  122. }
  123. public static RpcId FromObject(object value)
  124. {
  125. if (value == null)
  126. {
  127. return default;
  128. }
  129. if(value is RpcId rpcId)
  130. {
  131. return rpcId;
  132. }
  133. if(value is string stringValue)
  134. {
  135. return new RpcId(stringValue);
  136. }
  137. if (value.GetType().IsNumericType())
  138. {
  139. return new RpcId(Convert.ToDouble(value));
  140. }
  141. throw new RpcException(RpcErrorCode.InvalidRequest, "Id must be a string, a number or null.");
  142. }
  143. }
  144. }