public override SyntaxNode VisitInvocationExpression(InvocationExpressionSyntax node)
            {
                var expression = node.Expression.ToString();

                if (expression.StartsWith("results.") == false || expression.EndsWith(".GroupBy") == false)
                {
                    return(base.VisitInvocationExpression(node));
                }

                var groupByLambda = node.ArgumentList.DescendantNodes(x => true)
                                    .FirstOrDefault(x => x.IsKind(SyntaxKind.SimpleLambdaExpression)) as SimpleLambdaExpressionSyntax;

                if (groupByLambda == null)
                {
                    throw new InvalidOperationException("Could not extract arguments from group by expression");
                }

                var argument = groupByLambda.Parent as ArgumentSyntax;

                if (argument == null)
                {
                    throw new InvalidOperationException("Could not extract arguments from group by expression");
                }

                var arguments = argument.Parent as ArgumentListSyntax;

                if (arguments == null)
                {
                    throw new InvalidOperationException("Could not extract arguments from group by expression");
                }

                if (arguments.Arguments.Count != 1)
                {
                    throw new InvalidOperationException("Incorrect number of arguments in group by expression");
                }

                var singleGroupByField    = groupByLambda.Body as MemberAccessExpressionSyntax;
                var multipleGroupByFields = groupByLambda.Body as AnonymousObjectCreationExpressionSyntax;
                var literalGroupByField   = groupByLambda.Body as LiteralExpressionSyntax;

                if (singleGroupByField != null)
                {
                    GroupByFields = new[] { RewritersHelper.ExtractField(singleGroupByField) };
                }
                else if (multipleGroupByFields != null)
                {
                    GroupByFields = RewritersHelper.ExtractFields(multipleGroupByFields, retrieveOriginal: true, nestFields: true).ToArray();
                }
                else if (literalGroupByField != null)
                {
                    GroupByFields = new CompiledIndexField[0];
                }
                else
                {
                    throw new InvalidOperationException("Could not extract group by fields");
                }

                return(node);
            }
Esempio n. 2
0
        public IEnumerable <(string Key, object Value, CompiledIndexField GroupByField, bool IsGroupByField)> GetPropertiesInOrder(object target)
        {
            if (!(target is ObjectInstance oi))
            {
                throw new ArgumentException($"JintPropertyAccessor.GetPropertiesInOrder is expecting a target of type ObjectInstance but got one of type {target.GetType().Name}.");
            }
            foreach (var property in oi.GetOwnProperties())
            {
                CompiledIndexField field = null;
                var isGroupByField       = _groupByFields?.TryGetValue(new SimpleField(property.Key), out field) ?? false;

                yield return(property.Key, GetValue(property.Value.Value), field, isGroupByField);
            }
        }