예제 #1
0
        public new object Encode(object obj)
        {
            if (obj is AVObject av)
            {
                return(EncodeParseObject(av, false));
            }
            var list = Conversion.As <IList <object> >(obj);

            if (list != null)
            {
                var newArray = new List <object>();
                // We need to explicitly cast `list` to `List<object>` rather than
                // `IList<object>` because IL2CPP is stricter than the usual Unity AOT compiler pipeline.
                if (list.GetType().IsArray)
                {
                    list = new List <object>(list);
                }
                foreach (var item in list)
                {
                    if (!IsValidType(item))
                    {
                        throw new ArgumentException("Invalid type for value in an array");
                    }
                    newArray.Add(Encode(item));
                }
                return(newArray);
            }
            if (AVEncoder.IsValidType(obj))
            {
                return(base.Encode(obj));
            }
            return(obj);
        }
예제 #2
0
        /// <summary>
        /// Converts a normalized binary expression into the appropriate AVQuery clause.
        /// </summary>
        private static AVQuery <T> WhereBinaryExpression <T>(
            this AVQuery <T> source, Expression <Func <T, bool> > expression, BinaryExpression node)
            where T : AVObject
        {
            var leftTransformed = new ObjectNormalizer().Visit(node.Left) as MethodCallExpression;

            if (!(IsParseObjectGet(leftTransformed) &&
                  leftTransformed.Object == expression.Parameters[0]))
            {
                throw new InvalidOperationException(
                          "Where expressions must have one side be a field operation on a AVObject.");
            }

            var fieldPath   = GetValue(leftTransformed.Arguments[0]) as string;
            var filterValue = GetValue(node.Right);

            if (filterValue != null && !AVEncoder.IsValidType(filterValue))
            {
                throw new InvalidOperationException(
                          "Where clauses must use types compatible with AVObjects.");
            }

            switch (node.NodeType)
            {
            case ExpressionType.GreaterThan:
                return(source.WhereGreaterThan(fieldPath, filterValue));

            case ExpressionType.GreaterThanOrEqual:
                return(source.WhereGreaterThanOrEqualTo(fieldPath, filterValue));

            case ExpressionType.LessThan:
                return(source.WhereLessThan(fieldPath, filterValue));

            case ExpressionType.LessThanOrEqual:
                return(source.WhereLessThanOrEqualTo(fieldPath, filterValue));

            case ExpressionType.Equal:
                return(source.WhereEqualTo(fieldPath, filterValue));

            case ExpressionType.NotEqual:
                return(source.WhereNotEqualTo(fieldPath, filterValue));

            default:
                throw new InvalidOperationException(
                          "Where expressions do not support this operator.");
            }
        }
예제 #3
0
        public void TestIsValidType()
        {
            var corgi         = new AVObject("Corgi");
            var corgiRelation = corgi.GetRelation <AVObject>("corgi");

            Assert.IsTrue(AVEncoder.IsValidType(322));
            Assert.IsTrue(AVEncoder.IsValidType(0.3f));
            Assert.IsTrue(AVEncoder.IsValidType(new byte[] { 1, 2, 3, 4 }));
            Assert.IsTrue(AVEncoder.IsValidType("corgi"));
            Assert.IsTrue(AVEncoder.IsValidType(corgi));
            Assert.IsTrue(AVEncoder.IsValidType(new AVACL()));
            Assert.IsTrue(AVEncoder.IsValidType(new AVFile("Corgi", new byte[0])));
            Assert.IsTrue(AVEncoder.IsValidType(new AVGeoPoint(1, 2)));
            Assert.IsTrue(AVEncoder.IsValidType(corgiRelation));
            Assert.IsTrue(AVEncoder.IsValidType(new DateTime()));
            Assert.IsTrue(AVEncoder.IsValidType(new List <object>()));
            Assert.IsTrue(AVEncoder.IsValidType(new Dictionary <string, string>()));
            Assert.IsTrue(AVEncoder.IsValidType(new Dictionary <string, object>()));

            Assert.IsFalse(AVEncoder.IsValidType(new AVAddOperation(new List <object>())));
            Assert.IsFalse(AVEncoder.IsValidType(Task <AVObject> .FromResult(new AVObject("Corgi"))));
            Assert.Throws <MissingMethodException>(() => AVEncoder.IsValidType(new Dictionary <object, object>()));
            Assert.Throws <MissingMethodException>(() => AVEncoder.IsValidType(new Dictionary <object, string>()));
        }