Пример #1
0
        /// <summary>
        /// Gets the the CLR value for a primitive property.
        /// </summary>
        /// <param name="structuredValue">The structured value.</param>
        /// <param name="propertyName">Name of the property.</param>
        /// <returns>The clr value of the property.</returns>
        internal static object GetPrimitivePropertyClrValue(this IEdmStructuredValue structuredValue, string propertyName)
        {
            Debug.Assert(structuredValue != null, "entityInstance != null");
            IEdmStructuredTypeReference valueType = structuredValue.Type.AsStructured();

            IEdmPropertyValue propertyValue = structuredValue.FindPropertyValue(propertyName);

            if (propertyValue == null)
            {
                throw new ODataException(ErrorStrings.EdmValueUtils_PropertyDoesntExist(valueType.FullName(), propertyName));
            }

            if (propertyValue.Value.ValueKind == EdmValueKind.Null)
            {
                return(null);
            }

            IEdmPrimitiveValue primitiveValue = propertyValue.Value as IEdmPrimitiveValue;

            if (primitiveValue == null)
            {
                throw new ODataException(ErrorStrings.EdmValueUtils_NonPrimitiveValue(propertyValue.Name, valueType.FullName()));
            }

            return(primitiveValue.ToClrValue());
        }
Пример #2
0
        private IEdmValue FindProperty(string name, IEdmValue context)
        {
            IEdmValue           value = null;
            IEdmStructuredValue edmStructuredValue = context as IEdmStructuredValue;

            if (edmStructuredValue != null)
            {
                IEdmPropertyValue edmPropertyValue = edmStructuredValue.FindPropertyValue(name);
                if (edmPropertyValue != null)
                {
                    value = edmPropertyValue.Value;
                }
            }
            return(value);
        }
        private static IEdmValue FindProperty(string name, IEdmValue context)
        {
            IEdmValue result = null;

            IEdmStructuredValue structuredContext = context as IEdmStructuredValue;

            if (structuredContext != null)
            {
                IEdmPropertyValue propertyValue = structuredContext.FindPropertyValue(name);
                if (propertyValue != null)
                {
                    result = propertyValue.Value;
                }
            }

            return(result);
        }
Пример #4
0
        private static void CompareStructuralValue(IEdmValue edmValue, IEnumerable <ODataProperty> properties, AssertionHandler assert)
        {
            IEdmStructuredValue structuredEdmValue = (IEdmStructuredValue)edmValue;

            if (properties != null)
            {
                // Use FindPropertyValue
                foreach (ODataProperty property in properties)
                {
                    IEdmPropertyValue edmPropertyValue = structuredEdmValue.FindPropertyValue(property.Name);
                    CompareProperty(edmPropertyValue, property, assert);
                }

                // Enumerate the properties
                CompareProperties(structuredEdmValue.PropertyValues, properties, assert);
            }
            else
            {
                assert.IsTrue(
                    structuredEdmValue.PropertyValues == null || structuredEdmValue.PropertyValues.Count() == 0,
                    "Expected empty structured value.");
            }
        }
        private static bool AssertOrMatchStructuredType(IEdmStructuredTypeReference structuredTargetType, IEdmStructuredValue structuredValue, bool testPropertyTypes, List <IEdmPropertyValue> newProperties)
        {
            // If the value has a nominal type, the target type must be derived from the nominal type for a type match to be possible.
            IEdmTypeReference operandType = structuredValue.Type;

            if (operandType != null && !structuredTargetType.StructuredDefinition().InheritsFrom(operandType.AsStructured().StructuredDefinition()))
            {
                return(false);
            }

            HashSetInternal <IEdmPropertyValue> visitedProperties = new HashSetInternal <IEdmPropertyValue>();

            foreach (IEdmProperty property in structuredTargetType.StructuralProperties())
            {
                IEdmPropertyValue propertyValue = structuredValue.FindPropertyValue(property.Name);
                if (propertyValue == null)
                {
                    return(false);
                }

                visitedProperties.Add(propertyValue);
                if (testPropertyTypes)
                {
                    if (newProperties != null)
                    {
                        newProperties.Add(new EdmPropertyValue(propertyValue.Name, Cast(property.Type, propertyValue.Value)));
                    }
                    else if (!MatchesType(property.Type, propertyValue.Value))
                    {
                        return(false);
                    }
                }
            }

            if (structuredTargetType.IsEntity())
            {
                foreach (IEdmNavigationProperty property in structuredTargetType.AsEntity().NavigationProperties())
                {
                    IEdmPropertyValue propertyValue = structuredValue.FindPropertyValue(property.Name);
                    if (propertyValue == null)
                    {
                        return(false);
                    }

                    // Make a superficial test of the navigation property value--check that it has a valid set of properties,
                    // but don't test their types.
                    if (testPropertyTypes && !MatchesType(property.Type, propertyValue.Value, false))
                    {
                        return(false);
                    }

                    visitedProperties.Add(propertyValue);
                    if (newProperties != null)
                    {
                        newProperties.Add(propertyValue);
                    }
                }
            }

            //// Allow property values not mentioned in the target type, whether or not the target type is open.

            if (newProperties != null)
            {
                foreach (IEdmPropertyValue propertyValue in structuredValue.PropertyValues)
                {
                    if (!visitedProperties.Contains(propertyValue))
                    {
                        newProperties.Add(propertyValue);
                    }
                }
            }

            return(true);
        }
Пример #6
0
        /// <summary>
        /// Appends the key expression for the given entity to the given <see cref="StringBuilder"/>
        /// </summary>
        /// <param name="entity">The entity to build the key expression from.</param>
        /// <param name="builder">The builder to append onto.</param>
        internal void AppendKeyExpression(IEdmStructuredValue entity, StringBuilder builder)
        {
            Debug.Assert(entity != null, "entity != null");
            Debug.Assert(builder != null, "builder != null");

            IEdmEntityTypeReference edmEntityTypeReference = entity.Type as IEdmEntityTypeReference;

            if (edmEntityTypeReference == null || !edmEntityTypeReference.Key().Any())
            {
                throw Error.Argument(ErrorStrings.Content_EntityWithoutKey, "entity");
            }

            this.AppendKeyExpression(edmEntityTypeReference.Key().ToList(), p => p.Name, p => GetPropertyValue(entity.FindPropertyValue(p.Name), entity.Type), builder);
        }
Пример #7
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);
            }
        }