123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Linq.Expressions;
- using System.Text.RegularExpressions;
- using Luticate2.Auth.Utils.Business.Utils;
- using Luticate2.Auth.Utils.Dbo.Fields;
-
- namespace Luticate2.Auth.Utils.Business.Fields
- {
- public static class LuFieldsExtensions
- {
- public static bool IsIncluded(this IEnumerable<LuFieldDbo> fields, IEnumerable<string> parts, bool ignoreCase = true)
- {
- return fields.Any(x => x.IsIncluded(parts, ignoreCase));
- }
-
- public static bool IsIncluded(this IEnumerable<LuFieldDbo> fields, string path, bool ignoreCase = true)
- {
- return fields.Any(x => x.IsIncluded(path, ignoreCase));
- }
-
- public static bool IsIncluded(this IEnumerable<LuFieldDbo> fields, LuFieldDbo path, bool ignoreCase = true)
- {
- return fields.Any(x => x.IsIncluded(path, ignoreCase));
- }
-
- public static bool IsIncluded<TModel>(this IEnumerable<LuFieldDbo> fields, Expression<Func<TModel, object>> property, bool ignoreCase = true)
- {
- return fields.Any(x => x.IsIncluded(property, ignoreCase));
- }
-
-
- public static bool IsIncluded(this LuFieldDbo field, IEnumerable<string> parts, bool ignoreCase = true)
- {
- var list = parts.ToList();
- var stringComparison = ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal;
-
- for (var index = 0; index < list.Count && index < field.Parts.Count; index++)
- {
- if (!string.Equals(list[index], field.Parts[index], stringComparison) && field.Parts[index] != "*")
- {
- return false;
- }
-
- // If the last part of the given value is reached and the field has more parts than the given value,
- // there is no match, eg. the field value is foo/bar and the given value is foo. If the field parts and
- // the given value parts have equal lengths, there is a match, eg. the field value is foo/bar and the
- // given value is foo/bar.
- if (index == list.Count - 1)
- {
- return list.Count <= field.Parts.Count;
- }
- }
-
- return true;
- }
-
- public static bool IsIncluded(this LuFieldDbo field, string path, bool ignoreCase = true)
- {
- return field.IsIncluded(LuFieldDbo.Make(path), ignoreCase);
- }
-
- public static bool IsIncluded(this LuFieldDbo field, LuFieldDbo path, bool ignoreCase = true)
- {
- return field.IsIncluded(path.Parts, ignoreCase);
- }
-
- public static bool IsIncluded<TModel>(this LuFieldDbo field, Expression<Func<TModel, object>> property, bool ignoreCase = true)
- {
- var memberInfo = LuExpressionUtils.GetMembersFromExpression(property);
- var parts = memberInfo.Select(x => x.Name).ToList();
- return field.IsIncluded(parts, ignoreCase);
- }
-
-
- public static bool StartsWith(this LuFieldDbo field, IEnumerable<string> subParts, bool ignoreCase = true)
- {
- var comparer = ignoreCase ? StringComparer.CurrentCultureIgnoreCase : StringComparer.CurrentCulture;
- using (IEnumerator<string> enumerator = field.Parts.GetEnumerator())
- {
- foreach (string source in subParts)
- {
- if (!enumerator.MoveNext() || comparer.Compare(enumerator.Current, source) != 0)
- return false;
- }
- }
- return true;
- }
-
- public static bool StartsWith(this LuFieldDbo field, string subparts, bool ignoreCase = true)
- {
- return field.StartsWith(LuFieldDbo.Make(subparts), ignoreCase);
- }
-
- public static bool StartsWith(this LuFieldDbo field, LuFieldDbo path, bool ignoreCase = true)
- {
- return field.StartsWith(path.Parts, ignoreCase);
- }
-
- public static bool StartsWith<TDbo>(this LuFieldDbo field, Expression<Func<TDbo, object>> property, bool ignoreCase = true)
- {
- var memberInfo = LuExpressionUtils.GetMembersFromExpression(property);
- var memberInfoString = memberInfo.Select(x => x.Name).ToList();
- return field.StartsWith(memberInfoString, ignoreCase);
- }
-
-
- public static bool Is(this LuFieldDbo field, IEnumerable<string> parts, bool ignoreCase = true)
- {
- var comparer = ignoreCase ? StringComparer.CurrentCultureIgnoreCase : StringComparer.CurrentCulture;
- return field.Parts.SequenceEqual(parts, comparer);
- }
-
- public static bool Is(this LuFieldDbo field, string parts, bool ignoreCase = true)
- {
- return field.Is(LuFieldDbo.Make(parts), ignoreCase);
- }
-
- public static bool Is(this LuFieldDbo field, LuFieldDbo path, bool ignoreCase = true)
- {
- return field.Is(path.Parts, ignoreCase);
- }
-
- public static bool Is<TDbo>(this LuFieldDbo field, Expression<Func<TDbo, object>> property, bool ignoreCase = true)
- {
- var memberInfo = LuExpressionUtils.GetMembersFromExpression(property);
- var memberInfoString = memberInfo.Select(x => x.Name).ToList();
- return field.Is(memberInfoString, ignoreCase);
- }
-
-
- public static bool IsRoot(this LuFieldDbo field)
- {
- return !field.Parts.Any();
- }
-
- public static LuFieldDbo Pop(this LuFieldDbo field)
- {
- field.Parts = field.Parts.Where((s, i) => i != 0).ToList();
- return field;
- }
-
- public static LuFieldDbo Popped(this LuFieldDbo field)
- {
- var newField = LuFieldDbo.Make(field).Pop();
- return newField;
- }
-
-
- public static LuFieldDbo Add(this LuFieldDbo field, IEnumerable<string> parts)
- {
- foreach (var property in parts)
- {
- if (!string.IsNullOrEmpty(property))
- {
- field.Parts.Add(property);
- }
- }
-
- return field;
- }
-
- public static LuFieldDbo Add(this LuFieldDbo field, LuFieldDbo path)
- {
- return field.Add(path.Parts);
- }
-
- public static LuFieldDbo Add(this LuFieldDbo field, string path)
- {
- return field.Add(Regex.Split(path, "[\\./]"));
- }
-
- public static LuFieldDbo Add<TTypeTo>(this LuFieldDbo field, Expression<Func<TTypeTo, object>> property)
- {
- var memberInfo = LuExpressionUtils.GetMembersFromExpression(property);
-
- if (memberInfo == null)
- {
- throw new ArgumentException("Invalid property", nameof(property));
- }
-
- return field.Add(memberInfo.Select(x => x.Name));
- }
- }
- }
|