예제 #1
0
        private static IEdmValue AssertType(IEdmTypeReference targetType, IEdmValue operand)
        {
            IEdmTypeReference type      = operand.Type;
            EdmValueKind      valueKind = operand.ValueKind;

            if ((type == null || valueKind == EdmValueKind.Null || !type.Definition.IsOrInheritsFrom(targetType.Definition)) && targetType.TypeKind() != EdmTypeKind.None)
            {
                bool         flag         = true;
                EdmValueKind edmValueKind = valueKind;
                if (edmValueKind == EdmValueKind.Collection)
                {
                    if (!targetType.IsCollection())
                    {
                        flag = false;
                    }
                    else
                    {
                        return(new EdmExpressionEvaluator.AssertTypeCollectionValue(targetType.AsCollection(), (IEdmCollectionValue)operand));
                    }
                }
                else
                {
                    if (edmValueKind == EdmValueKind.Structured)
                    {
                        if (!targetType.IsStructured())
                        {
                            flag = false;
                        }
                        else
                        {
                            IEdmStructuredTypeReference edmStructuredTypeReference = targetType.AsStructured();
                            List <IEdmPropertyValue>    edmPropertyValues          = new List <IEdmPropertyValue>();
                            flag = EdmExpressionEvaluator.AssertOrMatchStructuredType(edmStructuredTypeReference, (IEdmStructuredValue)operand, true, edmPropertyValues);
                            if (flag)
                            {
                                return(new EdmStructuredValue(edmStructuredTypeReference, edmPropertyValues));
                            }
                        }
                    }
                    else
                    {
                        flag = EdmExpressionEvaluator.MatchesType(targetType, operand);
                    }
                }
                if (flag)
                {
                    return(operand);
                }
                else
                {
                    throw new InvalidOperationException(Strings.Edm_Evaluator_FailedTypeAssertion(targetType.ToTraceString()));
                }
            }
            else
            {
                return(operand);
            }
        }
예제 #2
0
        private static bool MatchesType(IEdmTypeReference targetType, IEdmValue operand, bool testPropertyTypes)
        {
            bool flag;
            IEdmTypeReference type      = operand.Type;
            EdmValueKind      valueKind = operand.ValueKind;

            if (type == null || valueKind == EdmValueKind.Null || !type.Definition.IsOrInheritsFrom(targetType.Definition))
            {
                EdmValueKind edmValueKind = valueKind;
                switch (edmValueKind)
                {
                case EdmValueKind.Binary:
                {
                    if (!targetType.IsBinary())
                    {
                        break;
                    }
                    IEdmBinaryTypeReference edmBinaryTypeReference = targetType.AsBinary();
                    if (!edmBinaryTypeReference.IsUnbounded)
                    {
                        int?maxLength = edmBinaryTypeReference.MaxLength;
                        if (maxLength.HasValue)
                        {
                            int?nullable = edmBinaryTypeReference.MaxLength;
                            return(nullable.Value >= (int)((IEdmBinaryValue)operand).Value.Length);
                        }
                    }
                    return(true);
                }

                case EdmValueKind.Boolean:
                {
                    return(targetType.IsBoolean());
                }

                case EdmValueKind.Collection:
                {
                    if (!targetType.IsCollection())
                    {
                        break;
                    }
                    IEdmTypeReference edmTypeReference        = targetType.AsCollection().ElementType();
                    IEnumerator <IEdmDelayedValue> enumerator = ((IEdmCollectionValue)operand).Elements.GetEnumerator();
                    using (enumerator)
                    {
                        while (enumerator.MoveNext())
                        {
                            IEdmDelayedValue current = enumerator.Current;
                            if (EdmExpressionEvaluator.MatchesType(edmTypeReference, current.Value))
                            {
                                continue;
                            }
                            flag = false;
                            return(flag);
                        }
                        return(true);
                    }
                    return(flag);
                }

                case EdmValueKind.DateTimeOffset:
                {
                    return(targetType.IsDateTimeOffset());
                }

                case EdmValueKind.DateTime:
                {
                    return(targetType.IsDateTime());
                }

                case EdmValueKind.Decimal:
                {
                    return(targetType.IsDecimal());
                }

                case EdmValueKind.Enum:
                {
                    return(((IEdmEnumValue)operand).Type.Definition.IsEquivalentTo(targetType.Definition));
                }

                case EdmValueKind.Floating:
                {
                    if (targetType.IsDouble())
                    {
                        return(true);
                    }
                    else
                    {
                        if (!targetType.IsSingle())
                        {
                            return(false);
                        }
                        else
                        {
                            return(EdmExpressionEvaluator.FitsInSingle(((IEdmFloatingValue)operand).Value));
                        }
                    }
                }

                case EdmValueKind.Guid:
                {
                    return(targetType.IsGuid());
                }

                case EdmValueKind.Integer:
                {
                    if (targetType.TypeKind() != EdmTypeKind.Primitive)
                    {
                        break;
                    }
                    EdmPrimitiveTypeKind edmPrimitiveTypeKind = targetType.AsPrimitive().PrimitiveKind();
                    if (edmPrimitiveTypeKind == EdmPrimitiveTypeKind.Byte)
                    {
                        return(EdmExpressionEvaluator.InRange(((IEdmIntegerValue)operand).Value, (long)0, (long)0xff));
                    }
                    else if (edmPrimitiveTypeKind == EdmPrimitiveTypeKind.DateTime || edmPrimitiveTypeKind == EdmPrimitiveTypeKind.DateTimeOffset || edmPrimitiveTypeKind == EdmPrimitiveTypeKind.Decimal || edmPrimitiveTypeKind == EdmPrimitiveTypeKind.Guid)
                    {
                        break;
                    }
                    else if (edmPrimitiveTypeKind == EdmPrimitiveTypeKind.Double || edmPrimitiveTypeKind == EdmPrimitiveTypeKind.Int64 || edmPrimitiveTypeKind == EdmPrimitiveTypeKind.Single)
                    {
                        return(true);
                    }
                    else if (edmPrimitiveTypeKind == EdmPrimitiveTypeKind.Int16)
                    {
                        return(EdmExpressionEvaluator.InRange(((IEdmIntegerValue)operand).Value, (long)-32768, (long)0x7fff));
                    }
                    else if (edmPrimitiveTypeKind == EdmPrimitiveTypeKind.Int32)
                    {
                        return(EdmExpressionEvaluator.InRange(((IEdmIntegerValue)operand).Value, (long)-2147483648, (long)0x7fffffff));
                    }
                    else if (edmPrimitiveTypeKind == EdmPrimitiveTypeKind.SByte)
                    {
                        return(EdmExpressionEvaluator.InRange(((IEdmIntegerValue)operand).Value, (long)-128, (long)127));
                    }
                    break;
                }

                case EdmValueKind.Null:
                {
                    return(targetType.IsNullable);
                }

                case EdmValueKind.String:
                {
                    if (!targetType.IsString())
                    {
                        break;
                    }
                    IEdmStringTypeReference edmStringTypeReference = targetType.AsString();
                    if (!edmStringTypeReference.IsUnbounded)
                    {
                        int?maxLength1 = edmStringTypeReference.MaxLength;
                        if (maxLength1.HasValue)
                        {
                            int?nullable1 = edmStringTypeReference.MaxLength;
                            return(nullable1.Value >= ((IEdmStringValue)operand).Value.Length);
                        }
                    }
                    return(true);
                }

                case EdmValueKind.Structured:
                {
                    if (!targetType.IsStructured())
                    {
                        break;
                    }
                    return(EdmExpressionEvaluator.AssertOrMatchStructuredType(targetType.AsStructured(), (IEdmStructuredValue)operand, testPropertyTypes, null));
                }

                case EdmValueKind.Time:
                {
                    return(targetType.IsTime());
                }
                }
                return(false);
            }
            else
            {
                return(true);
            }
            return(true);
        }
예제 #3
0
 private static bool MatchesType(IEdmTypeReference targetType, IEdmValue operand)
 {
     return(EdmExpressionEvaluator.MatchesType(targetType, operand, true));
 }
예제 #4
0
        private static bool AssertOrMatchStructuredType(IEdmStructuredTypeReference structuredTargetType, IEdmStructuredValue structuredValue, bool testPropertyTypes, List <IEdmPropertyValue> newProperties)
        {
            bool flag;
            IEdmTypeReference type = structuredValue.Type;

            if (type == null || type.TypeKind() == EdmTypeKind.Row || structuredTargetType.StructuredDefinition().InheritsFrom(type.AsStructured().StructuredDefinition()))
            {
                HashSetInternal <IEdmPropertyValue>  edmPropertyValues = new HashSetInternal <IEdmPropertyValue>();
                IEnumerator <IEdmStructuralProperty> enumerator        = structuredTargetType.StructuralProperties().GetEnumerator();
                using (enumerator)
                {
                    while (enumerator.MoveNext())
                    {
                        IEdmProperty      current          = enumerator.Current;
                        IEdmPropertyValue edmPropertyValue = structuredValue.FindPropertyValue(current.Name);
                        if (edmPropertyValue != null)
                        {
                            edmPropertyValues.Add(edmPropertyValue);
                            if (!testPropertyTypes)
                            {
                                continue;
                            }
                            if (newProperties == null)
                            {
                                if (EdmExpressionEvaluator.MatchesType(current.Type, edmPropertyValue.Value))
                                {
                                    continue;
                                }
                                flag = false;
                                return(flag);
                            }
                            else
                            {
                                newProperties.Add(new EdmPropertyValue(edmPropertyValue.Name, EdmExpressionEvaluator.AssertType(current.Type, edmPropertyValue.Value)));
                            }
                        }
                        else
                        {
                            flag = false;
                            return(flag);
                        }
                    }
                    if (structuredTargetType.IsEntity())
                    {
                        IEnumerator <IEdmNavigationProperty> enumerator1 = structuredTargetType.AsEntity().NavigationProperties().GetEnumerator();
                        using (enumerator1)
                        {
                            while (enumerator1.MoveNext())
                            {
                                IEdmNavigationProperty edmNavigationProperty = enumerator1.Current;
                                IEdmPropertyValue      edmPropertyValue1     = structuredValue.FindPropertyValue(edmNavigationProperty.Name);
                                if (edmPropertyValue1 != null)
                                {
                                    if (!testPropertyTypes || EdmExpressionEvaluator.MatchesType(edmNavigationProperty.Type, edmPropertyValue1.Value, false))
                                    {
                                        edmPropertyValues.Add(edmPropertyValue1);
                                        if (newProperties == null)
                                        {
                                            continue;
                                        }
                                        newProperties.Add(edmPropertyValue1);
                                    }
                                    else
                                    {
                                        flag = false;
                                        return(flag);
                                    }
                                }
                                else
                                {
                                    flag = false;
                                    return(flag);
                                }
                            }
                        }
                    }
                    if (newProperties != null)
                    {
                        IEnumerator <IEdmPropertyValue> enumerator2 = structuredValue.PropertyValues.GetEnumerator();
                        using (enumerator2)
                        {
                            while (enumerator2.MoveNext())
                            {
                                IEdmPropertyValue current1 = enumerator2.Current;
                                if (edmPropertyValues.Contains(current1))
                                {
                                    continue;
                                }
                                newProperties.Add(current1);
                            }
                        }
                    }
                    return(true);
                }
                return(flag);
            }
            else
            {
                return(false);
            }
        }
예제 #5
0
        private IEdmValue Eval(IEdmExpression expression, IEdmStructuredValue context)
        {
            Func <IEdmValue[], IEdmValue> func = null;
            IEdmStructuredTypeReference   edmStructuredTypeReference;
            IEdmCollectionTypeReference   edmCollectionTypeReference;
            object            traceString;
            EdmExpressionKind expressionKind = expression.ExpressionKind;

            switch (expressionKind)
            {
            case EdmExpressionKind.BinaryConstant:
            {
                return((IEdmBinaryConstantExpression)expression);
            }

            case EdmExpressionKind.BooleanConstant:
            {
                return((IEdmBooleanConstantExpression)expression);
            }

            case EdmExpressionKind.DateTimeConstant:
            {
                return((IEdmDateTimeConstantExpression)expression);
            }

            case EdmExpressionKind.DateTimeOffsetConstant:
            {
                return((IEdmDateTimeOffsetConstantExpression)expression);
            }

            case EdmExpressionKind.DecimalConstant:
            {
                return((IEdmDecimalConstantExpression)expression);
            }

            case EdmExpressionKind.FloatingConstant:
            {
                return((IEdmFloatingConstantExpression)expression);
            }

            case EdmExpressionKind.GuidConstant:
            {
                return((IEdmGuidConstantExpression)expression);
            }

            case EdmExpressionKind.IntegerConstant:
            {
                return((IEdmIntegerConstantExpression)expression);
            }

            case EdmExpressionKind.StringConstant:
            {
                return((IEdmStringConstantExpression)expression);
            }

            case EdmExpressionKind.TimeConstant:
            {
                return((IEdmTimeConstantExpression)expression);
            }

            case EdmExpressionKind.Null:
            {
                return((IEdmNullExpression)expression);
            }

            case EdmExpressionKind.Record:
            {
                IEdmRecordExpression edmRecordExpression = (IEdmRecordExpression)expression;
                EdmExpressionEvaluator.DelayedExpressionContext delayedExpressionContext = new EdmExpressionEvaluator.DelayedExpressionContext(this, context);
                List <IEdmPropertyValue> edmPropertyValues = new List <IEdmPropertyValue>();
                foreach (IEdmPropertyConstructor property in edmRecordExpression.Properties)
                {
                    edmPropertyValues.Add(new EdmExpressionEvaluator.DelayedRecordProperty(delayedExpressionContext, property));
                }
                if (edmRecordExpression.DeclaredType != null)
                {
                    edmStructuredTypeReference = edmRecordExpression.DeclaredType.AsStructured();
                }
                else
                {
                    edmStructuredTypeReference = null;
                }
                EdmStructuredValue edmStructuredValue = new EdmStructuredValue(edmStructuredTypeReference, edmPropertyValues);
                return(edmStructuredValue);
            }

            case EdmExpressionKind.Collection:
            {
                IEdmCollectionExpression edmCollectionExpression = (IEdmCollectionExpression)expression;
                EdmExpressionEvaluator.DelayedExpressionContext delayedExpressionContext1 = new EdmExpressionEvaluator.DelayedExpressionContext(this, context);
                List <IEdmDelayedValue> edmDelayedValues = new List <IEdmDelayedValue>();
                foreach (IEdmExpression element in edmCollectionExpression.Elements)
                {
                    edmDelayedValues.Add(this.MapLabeledExpressionToDelayedValue(element, delayedExpressionContext1, context));
                }
                if (edmCollectionExpression.DeclaredType != null)
                {
                    edmCollectionTypeReference = edmCollectionExpression.DeclaredType.AsCollection();
                }
                else
                {
                    edmCollectionTypeReference = null;
                }
                EdmCollectionValue edmCollectionValue = new EdmCollectionValue(edmCollectionTypeReference, edmDelayedValues);
                return(edmCollectionValue);
            }

            case EdmExpressionKind.Path:
            {
                EdmUtil.CheckArgumentNull <IEdmStructuredValue>(context, "context");
                IEdmPathExpression edmPathExpression = (IEdmPathExpression)expression;
                IEdmValue          edmValue          = context;
                foreach (string path in edmPathExpression.Path)
                {
                    edmValue = this.FindProperty(path, edmValue);
                    if (edmValue != null)
                    {
                        continue;
                    }
                    throw new InvalidOperationException(Strings.Edm_Evaluator_UnboundPath(path));
                }
                return(edmValue);
            }

            case EdmExpressionKind.ParameterReference:
            case EdmExpressionKind.FunctionReference:
            case EdmExpressionKind.PropertyReference:
            case EdmExpressionKind.ValueTermReference:
            case EdmExpressionKind.EntitySetReference:
            case EdmExpressionKind.EnumMemberReference:
            {
                throw new InvalidOperationException(string.Concat("Not yet implemented: evaluation of ", expression.ExpressionKind.ToString(), " expressions."));
            }

            case EdmExpressionKind.If:
            {
                IEdmIfExpression edmIfExpression = (IEdmIfExpression)expression;
                if (!((IEdmBooleanValue)this.Eval(edmIfExpression.TestExpression, context)).Value)
                {
                    return(this.Eval(edmIfExpression.FalseExpression, context));
                }
                else
                {
                    return(this.Eval(edmIfExpression.TrueExpression, context));
                }
            }

            case EdmExpressionKind.AssertType:
            {
                IEdmAssertTypeExpression edmAssertTypeExpression = (IEdmAssertTypeExpression)expression;
                IEdmValue         edmValue1 = this.Eval(edmAssertTypeExpression.Operand, context);
                IEdmTypeReference type      = edmAssertTypeExpression.Type;
                return(EdmExpressionEvaluator.AssertType(type, edmValue1));
            }

            case EdmExpressionKind.IsType:
            {
                IEdmIsTypeExpression edmIsTypeExpression = (IEdmIsTypeExpression)expression;
                IEdmValue            edmValue2           = this.Eval(edmIsTypeExpression.Operand, context);
                IEdmTypeReference    edmTypeReference    = edmIsTypeExpression.Type;
                return(new EdmBooleanConstant(EdmExpressionEvaluator.MatchesType(edmTypeReference, edmValue2)));
            }

            case EdmExpressionKind.FunctionApplication:
            {
                IEdmApplyExpression             edmApplyExpression             = (IEdmApplyExpression)expression;
                IEdmExpression                  appliedFunction                = edmApplyExpression.AppliedFunction;
                IEdmFunctionReferenceExpression edmFunctionReferenceExpression = appliedFunction as IEdmFunctionReferenceExpression;
                if (edmFunctionReferenceExpression != null)
                {
                    IList <IEdmExpression> list          = edmApplyExpression.Arguments.ToList <IEdmExpression>();
                    IEdmValue[]            edmValueArray = new IEdmValue[list.Count <IEdmExpression>()];
                    int num = 0;
                    foreach (IEdmExpression edmExpression in list)
                    {
                        int num1 = num;
                        num = num1 + 1;
                        edmValueArray[num1] = this.Eval(edmExpression, context);
                    }
                    IEdmFunction referencedFunction = edmFunctionReferenceExpression.ReferencedFunction;
                    if (referencedFunction.IsBad() || !this.builtInFunctions.TryGetValue(referencedFunction, out func))
                    {
                        if (this.lastChanceFunctionApplier != null)
                        {
                            return(this.lastChanceFunctionApplier(referencedFunction.FullName(), edmValueArray));
                        }
                    }
                    else
                    {
                        return(func(edmValueArray));
                    }
                }
                if (edmFunctionReferenceExpression != null)
                {
                    traceString = edmFunctionReferenceExpression.ReferencedFunction.ToTraceString();
                }
                else
                {
                    traceString = string.Empty;
                }
                throw new InvalidOperationException(Strings.Edm_Evaluator_UnboundFunction(traceString));
            }

            case EdmExpressionKind.LabeledExpressionReference:
            {
                return(this.MapLabeledExpressionToDelayedValue(((IEdmLabeledExpressionReferenceExpression)expression).ReferencedLabeledExpression, null, context).Value);
            }

            case EdmExpressionKind.Labeled:
            {
                return(this.MapLabeledExpressionToDelayedValue(expression, new EdmExpressionEvaluator.DelayedExpressionContext(this, context), context).Value);
            }
            }
            int expressionKind1 = (int)expression.ExpressionKind;

            throw new InvalidOperationException(Strings.Edm_Evaluator_UnrecognizedExpressionKind(expressionKind1.ToString(CultureInfo.InvariantCulture)));
        }