Esempio n. 1
0
        /// <summary>
        /// Converts a normalized method call expression into the appropriate ParseQuery clause.
        /// </summary>
        private static ParseQuery <T> WhereMethodCall <T>(
            this ParseQuery <T> source, Expression <Func <T, bool> > expression, MethodCallExpression node)
            where T : ParseObject
        {
            if (IsParseObjectGet(node) && (node.Type == typeof(bool) || node.Type == typeof(bool?)))
            {
                // This is a raw boolean field access like 'where obj.Get<bool>("foo")'
                return(source.WhereEqualTo(GetValue(node.Arguments[0]) as string, true));
            }

            MethodInfo translatedMethod;

            if (functionMappings.TryGetValue(node.Method, out translatedMethod))
            {
                var objTransformed = new ObjectNormalizer().Visit(node.Object) as MethodCallExpression;
                if (!(IsParseObjectGet(objTransformed) &&
                      objTransformed.Object == expression.Parameters[0]))
                {
                    throw new InvalidOperationException(
                              "The left-hand side of a supported function call must be a ParseObject field access.");
                }
                var fieldPath   = GetValue(objTransformed.Arguments[0]);
                var containedIn = GetValue(node.Arguments[0]);
                var queryType   = translatedMethod.DeclaringType.GetGenericTypeDefinition()
                                  .MakeGenericType(typeof(T));
                translatedMethod = ReflectionHelpers.GetMethod(queryType,
                                                               translatedMethod.Name,
                                                               translatedMethod.GetParameters().Select(p => p.ParameterType).ToArray());
                return(translatedMethod.Invoke(source, new[] { fieldPath, containedIn }) as ParseQuery <T>);
            }

            if (node.Arguments[0] == expression.Parameters[0])
            {
                // obj.ContainsKey("foo") --> query.WhereExists("foo")
                if (node.Method == containsKeyMethod)
                {
                    return(source.WhereExists(GetValue(node.Arguments[1]) as string));
                }
                // !obj.ContainsKey("foo") --> query.WhereDoesNotExist("foo")
                if (node.Method == notContainsKeyMethod)
                {
                    return(source.WhereDoesNotExist(GetValue(node.Arguments[1]) as string));
                }
            }

            if (node.Method.IsGenericMethod)
            {
                if (node.Method.GetGenericMethodDefinition() == containsMethod)
                {
                    // obj.Get<IList<T>>("path").Contains(someValue)
                    if (IsParseObjectGet(node.Arguments[0] as MethodCallExpression))
                    {
                        return(source.WhereEqualTo(
                                   GetValue(((MethodCallExpression)node.Arguments[0]).Arguments[0]) as string,
                                   GetValue(node.Arguments[1])));
                    }
                    // someList.Contains(obj.Get<T>("path"))
                    if (IsParseObjectGet(node.Arguments[1] as MethodCallExpression))
                    {
                        var collection = GetValue(node.Arguments[0]) as System.Collections.IEnumerable;
                        return(source.WhereContainedIn(
                                   GetValue(((MethodCallExpression)node.Arguments[1]).Arguments[0]) as string,
                                   collection.Cast <object>()));
                    }
                }

                if (node.Method.GetGenericMethodDefinition() == notContainsMethod)
                {
                    // !obj.Get<IList<T>>("path").Contains(someValue)
                    if (IsParseObjectGet(node.Arguments[0] as MethodCallExpression))
                    {
                        return(source.WhereNotEqualTo(
                                   GetValue(((MethodCallExpression)node.Arguments[0]).Arguments[0]) as string,
                                   GetValue(node.Arguments[1])));
                    }
                    // !someList.Contains(obj.Get<T>("path"))
                    if (IsParseObjectGet(node.Arguments[1] as MethodCallExpression))
                    {
                        var collection = GetValue(node.Arguments[0]) as System.Collections.IEnumerable;
                        return(source.WhereNotContainedIn(
                                   GetValue(((MethodCallExpression)node.Arguments[1]).Arguments[0]) as string,
                                   collection.Cast <object>()));
                    }
                }
            }
            throw new InvalidOperationException(node.Method + " is not a supported method call in a where expression.");
        }