SortOrder.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Linq.Expressions;
  6. using System.Runtime.Serialization;
  7. using System.Text.Json;
  8. namespace InABox.Core
  9. {
  10. public enum SortDirection
  11. {
  12. Ascending,
  13. Descending
  14. }
  15. public interface ISortOrder : ISerializeBinary
  16. {
  17. SortDirection Direction { get; set; }
  18. Expression Expression { get; set; }
  19. IEnumerable<ISortOrder> Thens { get; }
  20. ISortOrder ThenBy(ISortOrder sortOrder);
  21. IEnumerable<String> ColumnNames();
  22. string AsOData();
  23. void SerializeBinary(CoreBinaryWriter writer);
  24. void DeserializeBinary(CoreBinaryReader reader);
  25. }
  26. public static class SortOrder
  27. {
  28. public static ISortOrder Create<T>(Type concrete, Expression<Func<T,object>> expression, SortDirection direction = SortDirection.Ascending)
  29. {
  30. if (!typeof(T).IsAssignableFrom(concrete))
  31. throw new Exception($"Columns: {concrete.EntityName()} does not implement {typeof(T).EntityName()}");
  32. var type = typeof(SortOrder<>).MakeGenericType(concrete);
  33. var property = CoreUtils.GetFullPropertyName(expression,".");
  34. var result = Activator.CreateInstance(type, property, direction );
  35. return (result as ISortOrder)!;
  36. }
  37. }
  38. public class SortOrder<T> : SerializableExpression<T>, ISortOrder // where T : Entity
  39. {
  40. public SortDirection Direction { get; set; }
  41. public List<SortOrder<T>> Thens { get; private set; }
  42. IEnumerable<ISortOrder> ISortOrder.Thens => Thens;
  43. //public SortOrder<T> Ascending()
  44. //{
  45. // Direction = SortOrder.Ascending;
  46. // return this;
  47. //}
  48. //public SortOrder<T> Descending()
  49. //{
  50. // Direction = SortOrder.Descending;
  51. // return this;
  52. //}
  53. ISortOrder ISortOrder.ThenBy(ISortOrder sortOrder)
  54. {
  55. if(sortOrder is SortOrder<T> order)
  56. {
  57. Thens.Add(order);
  58. }
  59. return this;
  60. }
  61. public SortOrder<T> ThenBy(Expression<Func<T, object?>> expression, SortDirection direction = SortDirection.Ascending)
  62. {
  63. var thenby = new SortOrder<T>(expression, direction);
  64. Thens.Add(thenby);
  65. return this;
  66. }
  67. #region Constructors
  68. public SortOrder()
  69. {
  70. Thens = new List<SortOrder<T>>();
  71. Direction = SortDirection.Ascending;
  72. }
  73. public SortOrder(Expression<Func<T, object?>> expression, SortDirection direction = SortDirection.Ascending)
  74. : base(expression)
  75. {
  76. Thens = new List<SortOrder<T>>();
  77. Direction = direction;
  78. }
  79. public SortOrder(string property, SortDirection direction = SortDirection.Ascending)
  80. {
  81. Thens = new List<SortOrder<T>>();
  82. Direction = direction;
  83. var iprop = DatabaseSchema.Property(typeof(T), property);
  84. Expression = iprop.Expression();
  85. }
  86. public SortOrder(SerializationInfo info, StreamingContext context)
  87. {
  88. Deserialize(info, context);
  89. }
  90. public static explicit operator SortOrder<T>(SortOrder<Entity> v)
  91. {
  92. if (v == null)
  93. return null;
  94. var json = Serialization.Serialize(v);
  95. json = json.Replace(typeof(Entity).EntityName(), typeof(T).EntityName());
  96. var result = Serialization.Deserialize<SortOrder<T>>(json);
  97. return result;
  98. }
  99. #endregion
  100. #region Display Functions
  101. public string AsOData()
  102. {
  103. var orderby = new Dictionary<SortDirection, string>
  104. {
  105. { SortDirection.Ascending, "asc" },
  106. { SortDirection.Descending, "desc" }
  107. };
  108. var prop = "";
  109. if (CoreUtils.TryFindMemberExpression(Expression, out var mexp))
  110. prop = CoreUtils.GetFullPropertyName(mexp, "/");
  111. else
  112. prop = Expression.ToString();
  113. var result = string.Format("{0} {1}", prop, orderby[Direction]);
  114. if (Thens != null && Thens.Count > 0)
  115. foreach (var then in Thens)
  116. {
  117. var ThenResult = then.AsOData();
  118. if (!string.IsNullOrEmpty(ThenResult))
  119. result = string.Format("{0}, {1}", result, ThenResult);
  120. }
  121. return result;
  122. }
  123. public override string ToString()
  124. {
  125. return AsOData();
  126. }
  127. public IEnumerable<string> ColumnNames()
  128. {
  129. yield return CoreUtils.ExpressionToString(typeof(T), Expression);
  130. foreach (var then in Thens)
  131. foreach (var x in then.ColumnNames())
  132. yield return x;
  133. }
  134. #endregion
  135. //public Expression<Func<T,Object>> AsExpression()
  136. //{
  137. // var param = Expression.Parameter(typeof(T), "x");
  138. // var result = Expression.Lambda<Func<T,Object>>(Expression,param);
  139. // return result;
  140. //}
  141. #region Serialization
  142. public override void Serialize(SerializationInfo info, StreamingContext context)
  143. {
  144. info.AddValue("Direction", Direction.ToString());
  145. if (Thens.Count > 0)
  146. info.AddValue("Thens", Thens, typeof(List<SortOrder<T>>));
  147. }
  148. public override void Deserialize(SerializationInfo info, StreamingContext context)
  149. {
  150. Direction = (SortDirection)Enum.Parse(typeof(SortDirection), (string)info.GetValue("Direction", typeof(string)));
  151. try
  152. {
  153. Thens = (List<SortOrder<T>>)info.GetValue("Thens", typeof(List<SortOrder<T>>));
  154. }
  155. catch
  156. {
  157. Thens = new List<SortOrder<T>>();
  158. }
  159. }
  160. #endregion
  161. #region Binary Serialization
  162. public void SerializeBinary(CoreBinaryWriter writer)
  163. {
  164. writer.SerialiseExpression(typeof(T), Expression, false);
  165. writer.Write((byte)Direction);
  166. writer.Write(Thens.Count);
  167. foreach (var then in Thens)
  168. {
  169. then.SerializeBinary(writer);
  170. }
  171. }
  172. public void DeserializeBinary(CoreBinaryReader reader)
  173. {
  174. Expression = reader.DeserialiseExpression(typeof(T));
  175. Direction = (SortDirection)reader.ReadByte();
  176. Thens.Clear();
  177. var nThens = reader.ReadInt32();
  178. for(int i = 0; i < nThens; ++i)
  179. {
  180. var then = new SortOrder<T>();
  181. then.DeserializeBinary(reader);
  182. Thens.Add(then);
  183. }
  184. }
  185. #endregion
  186. }
  187. public static class SortOrderSerialization
  188. {
  189. /// <summary>
  190. /// Inverse of <see cref="Write{T}(CoreBinaryWriter, SortOrder{T}?)"/>.
  191. /// </summary>
  192. /// <param name="reader"></param>
  193. /// <returns></returns>
  194. public static SortOrder<T>? ReadSortOrder<T>(this CoreBinaryReader reader)
  195. {
  196. if (reader.ReadBoolean())
  197. {
  198. var sortOrder = new SortOrder<T>();
  199. sortOrder.DeserializeBinary(reader);
  200. return sortOrder;
  201. }
  202. return null;
  203. }
  204. /// <summary>
  205. /// Inverse of <see cref="ReadSortOrder{T}(CoreBinaryReader)"/>.
  206. /// </summary>
  207. /// <param name="filter"></param>
  208. /// <param name="writer"></param>
  209. public static void Write<T>(this CoreBinaryWriter writer, SortOrder<T>? sortOrder)
  210. {
  211. if (sortOrder is null)
  212. {
  213. writer.Write(false);
  214. }
  215. else
  216. {
  217. writer.Write(true);
  218. sortOrder.SerializeBinary(writer);
  219. }
  220. }
  221. }
  222. public class SortOrderJsonConverter : CustomJsonConverter<ISortOrder>
  223. {
  224. public override bool CanConvert(Type objectType)
  225. {
  226. if (objectType.IsConstructedGenericType)
  227. {
  228. var ot = objectType.GetGenericTypeDefinition();
  229. var tt = typeof(SortOrder<>);
  230. if (ot == tt)
  231. return true;
  232. }
  233. return false;
  234. }
  235. public override ISortOrder? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
  236. {
  237. if (reader.TokenType == JsonTokenType.Null)
  238. return null;
  239. var type = typeToConvert;
  240. var sExpr = "";
  241. SortDirection dir = default;
  242. List<ISortOrder> thens = new List<ISortOrder>();
  243. while (reader.Read() && reader.TokenType != JsonTokenType.EndObject)
  244. {
  245. var tag = reader.GetString();
  246. reader.Read();
  247. if (string.Equals(tag, "$type"))
  248. {
  249. var sType = reader.GetString();
  250. if (!string.IsNullOrWhiteSpace(sType))
  251. {
  252. type = Type.GetType(sType) ?? typeToConvert;
  253. }
  254. }
  255. else if (string.Equals(tag, "Expression"))
  256. sExpr = reader.GetString();
  257. else if (string.Equals(tag, "Direction"))
  258. dir = Enum.Parse<SortDirection>(reader.GetString());
  259. else if (string.Equals(tag, "Thens"))
  260. {
  261. while (reader.Read() && reader.TokenType != JsonTokenType.EndArray)
  262. {
  263. var then = Read(ref reader, typeof(ISortOrder), options);
  264. if (then != null)
  265. thens.Add(then);
  266. }
  267. }
  268. }
  269. var prop = (CoreUtils.StringToExpression(sExpr) as MemberExpression)!;
  270. var result = (Activator.CreateInstance(type, CoreUtils.GetFullPropertyName(prop, "."), dir) as ISortOrder)!;
  271. foreach(var then in thens)
  272. {
  273. result.ThenBy(then);
  274. }
  275. return result;
  276. }
  277. public override void Write(Utf8JsonWriter writer, ISortOrder value, JsonSerializerOptions options)
  278. {
  279. var type = value.GetType().GenericTypeArguments[0];
  280. var property = value.Expression;
  281. var prop = CoreUtils.ExpressionToString(type, property, true);
  282. var dir = value.Direction;
  283. writer.WriteStartObject();
  284. writer.WriteString("$type", value.GetType().FullName);
  285. writer.WriteString("Expression", prop);
  286. writer.WriteString("Direction", dir.ToString());
  287. if(value.Thens.Any())
  288. {
  289. writer.WritePropertyName("Thens");
  290. writer.WriteStartArray();
  291. foreach(var then in value.Thens)
  292. {
  293. Write(writer, then, options);
  294. }
  295. writer.WriteEndArray();
  296. }
  297. writer.WriteEndObject();
  298. }
  299. }
  300. }