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

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