Exemple #1
0
    /// <summary>
    /// if given mapper was null this function creates default generated mapper
    /// </summary>
    /// <param name="mapper">a <c>GridifyMapper<c /> that can be null</param>
    /// <param name="syntaxTree">optional syntaxTree to Lazy mapping generation</param>
    /// <typeparam name="T">type to set mappings</typeparam>
    /// <returns>return back mapper or new generated mapper if it was null</returns>
    private static IGridifyMapper <T> FixMapper <T>(this IGridifyMapper <T>?mapper, SyntaxTree syntaxTree)
    {
        if (mapper != null)
        {
            return(mapper);
        }

        mapper = new GridifyMapper <T>();
        foreach (var field in syntaxTree.Root.Descendants()
                 .Where(q => q.Kind == SyntaxKind.FieldExpression)
                 .Cast <FieldExpressionSyntax>())
        {
            try
            {
                mapper.AddMap(field.FieldToken.Text);
            }
            catch (Exception)
            {
                if (!mapper.Configuration.IgnoreNotMappedFields)
                {
                    throw new GridifyMapperException($"Property '{field.FieldToken.Text}' not found.");
                }
            }
        }

        return(mapper);
    }
Exemple #2
0
    internal static IQueryable <T> ProcessOrdering <T>(IQueryable <T> query, string orderings, bool startWithThenBy, IGridifyMapper <T>?mapper)
    {
        var isFirst = !startWithThenBy;

        var orders = ParseOrderings(orderings).ToList();

        // build the mapper if it is null
        if (mapper is null)
        {
            mapper = new GridifyMapper <T>();
            foreach (var order in orders)
            {
                try
                {
                    mapper.AddMap(order.MemberName);
                }
                catch (Exception)
                {
                    if (!mapper.Configuration.IgnoreNotMappedFields)
                    {
                        throw new GridifyMapperException($"Mapping '{order.MemberName}' not found");
                    }
                }
            }
        }

        foreach (var order in orders)
        {
            if (!mapper.HasMap(order.MemberName))
            {
                // skip if there is no mappings available
                if (mapper.Configuration.IgnoreNotMappedFields)
                {
                    continue;
                }

                throw new GridifyMapperException($"Mapping '{order.MemberName}' not found");
            }

            if (isFirst)
            {
                query   = query.OrderByMember(GetOrderExpression(order, mapper), order.IsAscending);
                isFirst = false;
            }
            else
            {
                query = query.ThenByMember(GetOrderExpression(order, mapper), order.IsAscending);
            }
        }

        return(query);
    }