You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LuFieldsExtensions.cs 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. using System.Text.RegularExpressions;
  6. using Luticate2.Auth.Utils.Business.Utils;
  7. using Luticate2.Auth.Utils.Dbo.Fields;
  8. namespace Luticate2.Auth.Utils.Business.Fields
  9. {
  10. public static class LuFieldsExtensions
  11. {
  12. public static bool IsIncluded(this IEnumerable<LuFieldDbo> fields, IEnumerable<string> parts, bool ignoreCase = true)
  13. {
  14. return fields.Any(x => x.IsIncluded(parts, ignoreCase));
  15. }
  16. public static bool IsIncluded(this IEnumerable<LuFieldDbo> fields, string path, bool ignoreCase = true)
  17. {
  18. return fields.Any(x => x.IsIncluded(path, ignoreCase));
  19. }
  20. public static bool IsIncluded(this IEnumerable<LuFieldDbo> fields, LuFieldDbo path, bool ignoreCase = true)
  21. {
  22. return fields.Any(x => x.IsIncluded(path, ignoreCase));
  23. }
  24. public static bool IsIncluded<TModel>(this IEnumerable<LuFieldDbo> fields, Expression<Func<TModel, object>> property, bool ignoreCase = true)
  25. {
  26. return fields.Any(x => x.IsIncluded(property, ignoreCase));
  27. }
  28. public static bool IsIncluded(this LuFieldDbo field, IEnumerable<string> parts, bool ignoreCase = true)
  29. {
  30. var list = parts.ToList();
  31. var stringComparison = ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal;
  32. for (var index = 0; index < list.Count && index < field.Parts.Count; index++)
  33. {
  34. if (!string.Equals(list[index], field.Parts[index], stringComparison) && field.Parts[index] != "*")
  35. {
  36. return false;
  37. }
  38. // If the last part of the given value is reached and the field has more parts than the given value,
  39. // there is no match, eg. the field value is foo/bar and the given value is foo. If the field parts and
  40. // the given value parts have equal lengths, there is a match, eg. the field value is foo/bar and the
  41. // given value is foo/bar.
  42. if (index == list.Count - 1)
  43. {
  44. return list.Count <= field.Parts.Count;
  45. }
  46. }
  47. return true;
  48. }
  49. public static bool IsIncluded(this LuFieldDbo field, string path, bool ignoreCase = true)
  50. {
  51. return field.IsIncluded(LuFieldDbo.Make(path), ignoreCase);
  52. }
  53. public static bool IsIncluded(this LuFieldDbo field, LuFieldDbo path, bool ignoreCase = true)
  54. {
  55. return field.IsIncluded(path.Parts, ignoreCase);
  56. }
  57. public static bool IsIncluded<TModel>(this LuFieldDbo field, Expression<Func<TModel, object>> property, bool ignoreCase = true)
  58. {
  59. var memberInfo = LuExpressionUtils.GetMembersFromExpression(property);
  60. var parts = memberInfo.Select(x => x.Name).ToList();
  61. return field.IsIncluded(parts, ignoreCase);
  62. }
  63. public static bool StartsWith(this LuFieldDbo field, IEnumerable<string> subParts, bool ignoreCase = true)
  64. {
  65. var comparer = ignoreCase ? StringComparer.CurrentCultureIgnoreCase : StringComparer.CurrentCulture;
  66. using (IEnumerator<string> enumerator = field.Parts.GetEnumerator())
  67. {
  68. foreach (string source in subParts)
  69. {
  70. if (!enumerator.MoveNext() || comparer.Compare(enumerator.Current, source) != 0)
  71. return false;
  72. }
  73. }
  74. return true;
  75. }
  76. public static bool StartsWith(this LuFieldDbo field, string subparts, bool ignoreCase = true)
  77. {
  78. return field.StartsWith(LuFieldDbo.Make(subparts), ignoreCase);
  79. }
  80. public static bool StartsWith(this LuFieldDbo field, LuFieldDbo path, bool ignoreCase = true)
  81. {
  82. return field.StartsWith(path.Parts, ignoreCase);
  83. }
  84. public static bool StartsWith<TDbo>(this LuFieldDbo field, Expression<Func<TDbo, object>> property, bool ignoreCase = true)
  85. {
  86. var memberInfo = LuExpressionUtils.GetMembersFromExpression(property);
  87. var memberInfoString = memberInfo.Select(x => x.Name).ToList();
  88. return field.StartsWith(memberInfoString, ignoreCase);
  89. }
  90. public static bool Is(this LuFieldDbo field, IEnumerable<string> parts, bool ignoreCase = true)
  91. {
  92. var comparer = ignoreCase ? StringComparer.CurrentCultureIgnoreCase : StringComparer.CurrentCulture;
  93. return field.Parts.SequenceEqual(parts, comparer);
  94. }
  95. public static bool Is(this LuFieldDbo field, string parts, bool ignoreCase = true)
  96. {
  97. return field.Is(LuFieldDbo.Make(parts), ignoreCase);
  98. }
  99. public static bool Is(this LuFieldDbo field, LuFieldDbo path, bool ignoreCase = true)
  100. {
  101. return field.Is(path.Parts, ignoreCase);
  102. }
  103. public static bool Is<TDbo>(this LuFieldDbo field, Expression<Func<TDbo, object>> property, bool ignoreCase = true)
  104. {
  105. var memberInfo = LuExpressionUtils.GetMembersFromExpression(property);
  106. var memberInfoString = memberInfo.Select(x => x.Name).ToList();
  107. return field.Is(memberInfoString, ignoreCase);
  108. }
  109. public static bool IsRoot(this LuFieldDbo field)
  110. {
  111. return !field.Parts.Any();
  112. }
  113. public static LuFieldDbo Pop(this LuFieldDbo field)
  114. {
  115. field.Parts = field.Parts.Where((s, i) => i != 0).ToList();
  116. return field;
  117. }
  118. public static LuFieldDbo Popped(this LuFieldDbo field)
  119. {
  120. var newField = LuFieldDbo.Make(field).Pop();
  121. return newField;
  122. }
  123. public static LuFieldDbo Add(this LuFieldDbo field, IEnumerable<string> parts)
  124. {
  125. foreach (var property in parts)
  126. {
  127. if (!string.IsNullOrEmpty(property))
  128. {
  129. field.Parts.Add(property);
  130. }
  131. }
  132. return field;
  133. }
  134. public static LuFieldDbo Add(this LuFieldDbo field, LuFieldDbo path)
  135. {
  136. return field.Add(path.Parts);
  137. }
  138. public static LuFieldDbo Add(this LuFieldDbo field, string path)
  139. {
  140. return field.Add(Regex.Split(path, "[\\./]"));
  141. }
  142. public static LuFieldDbo Add<TTypeTo>(this LuFieldDbo field, Expression<Func<TTypeTo, object>> property)
  143. {
  144. var memberInfo = LuExpressionUtils.GetMembersFromExpression(property);
  145. if (memberInfo == null)
  146. {
  147. throw new ArgumentException("Invalid property", nameof(property));
  148. }
  149. return field.Add(memberInfo.Select(x => x.Name));
  150. }
  151. }
  152. }