public IEdmPropertyValue FindPropertyValue(string propertyName) { IEdmPropertyValue edmPropertyValue = null; IEdmPropertyValue edmPropertyValue1; Dictionary <string, IEdmPropertyValue> propertiesDictionary = this.PropertiesDictionary; if (propertiesDictionary == null) { IEnumerator <IEdmPropertyValue> enumerator = this.propertyValues.GetEnumerator(); using (enumerator) { while (enumerator.MoveNext()) { IEdmPropertyValue current = enumerator.Current; if (current.Name != propertyName) { continue; } edmPropertyValue1 = current; return(edmPropertyValue1); } return(null); } return(edmPropertyValue1); } else { propertiesDictionary.TryGetValue(propertyName, out edmPropertyValue); return(edmPropertyValue); } }
/// <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()); }
public void Term_Path_OnProperty() { this.SetupModelsAndValues(); const string applicationCsdl = @"<Schema Namespace=""Annotations"" xmlns=""http://docs.oasis-open.org/odata/ns/edm""> <Annotations Target=""foo.Person/ContactInfo""> <Annotation Term=""bar.Int16Value"" Qualifier=""WorkPhoneLocal"" Path=""WorkPhone/Local"" /> </Annotations> </Schema>"; IEdmModel applicationModel = this.Parse(applicationCsdl, this.baseModel, this.vocabularyDefinitionModel); EdmExpressionEvaluator expressionEvaluator = new EdmExpressionEvaluator(this.operationsLookup); IEdmEntityType person = this.baseModel.FindEntityType("foo.Person"); IEdmProperty property = person.FindProperty("ContactInfo"); IEdmPropertyValue contextPropertyValue = ((IEdmStructuredValue)this.personValue).FindPropertyValue("ContactInfo"); IEdmTerm termInt16Value = this.vocabularyDefinitionModel.FindTerm("bar.Int16Value"); // ?? Assumes Entity always?? // IEdmValue annotationHotIndex = applicationModel.GetTermValue(contextPropertyValue.Value, termInt16Value, evaluator); IEdmVocabularyAnnotation annotation = property.VocabularyAnnotations(applicationModel).SingleOrDefault(a => a.Term == termInt16Value); IEdmExpression expression = annotation.Value; IEdmValue annotationWorkphoneLocal = expressionEvaluator.Evaluate(expression, (IEdmStructuredValue)contextPropertyValue.Value); Assert.AreEqual(9991234, ((IEdmIntegerValue)annotationWorkphoneLocal).Value, "Term annotation value"); }
public void Term_FunctionApplication_OnNavigationProperty() { this.SetupModelsAndValues(); const string applicationCsdl = @"<Schema Namespace=""Annotations"" xmlns=""http://docs.oasis-open.org/odata/ns/edm""> <Annotations Target=""foo.Person/Address""> <Annotation Term=""bar.StringValue""> <Apply Function=""Functions.StringConcat""> <Path>Zip/Main</Path> <Apply Function=""Functions.StringConcat""> <String>-</String> <Path>Zip/Extension</Path> </Apply> </Apply> </Annotation> </Annotations> </Schema>"; IEdmModel applicationModel = this.Parse(applicationCsdl, this.baseModel, this.vocabularyDefinitionModel, this.operationsModel); EdmExpressionEvaluator expressionEvaluator = new EdmExpressionEvaluator(this.operationsLookup); IEdmEntityType person = this.baseModel.FindEntityType("foo.Person"); IEdmProperty property = person.FindProperty("Address"); IEdmPropertyValue contextPropertyValue = ((IEdmStructuredValue)this.personValue).FindPropertyValue("Address"); IEdmTerm termStringValue = this.vocabularyDefinitionModel.FindTerm("bar.StringValue"); IEdmVocabularyAnnotation annotationString = property.VocabularyAnnotations(applicationModel).SingleOrDefault(a => a.Term == termStringValue); IEdmValue annotationStringValue = expressionEvaluator.Evaluate(annotationString.Value, (IEdmStructuredValue)contextPropertyValue.Value); Assert.AreEqual("98052-0000", ((IEdmStringValue)annotationStringValue).Value, "Term annotation value"); }
public void Term_Constant_OnProperty() { this.SetupModelsAndValues(); const string applicationCsdl = @"<Schema Namespace=""Annotations"" xmlns=""http://docs.oasis-open.org/odata/ns/edm""> <Annotations Target=""foo.Person/CoolnessIndex""> <Annotation Term=""bar.Int32Value"" Qualifier=""HotIndex"" Int=""-1"" /> <Annotation Term=""bar.StringValue"" String=""Goofy"" /> </Annotations> </Schema>"; IEdmModel applicationModel = this.Parse(applicationCsdl, this.baseModel, this.vocabularyDefinitionModel); EdmExpressionEvaluator expressionEvaluator = new EdmExpressionEvaluator(this.operationsLookup); EdmToClrEvaluator clrEvaluator = new EdmToClrEvaluator(this.operationsLookup); IEdmEntityType person = this.baseModel.FindEntityType("foo.Person"); IEdmProperty property = person.FindProperty("CoolnessIndex"); IEdmPropertyValue contextPropertyValue = ((IEdmStructuredValue)this.personValue).FindPropertyValue("CoolnessIndex"); IEdmTerm termInt32Value = this.vocabularyDefinitionModel.FindTerm("bar.Int32Value"); IEdmTerm termStringValue = this.vocabularyDefinitionModel.FindTerm("bar.StringValue"); IEdmVocabularyAnnotation annotation = property.VocabularyAnnotations(applicationModel).SingleOrDefault(a => a.Term == termInt32Value); IEdmExpression expression = annotation.Value; IEdmValue annotationHotIndex = expressionEvaluator.Evaluate(expression); Assert.AreEqual(-1, ((IEdmIntegerValue)annotationHotIndex).Value, "Term annotation value"); annotationHotIndex = applicationModel.GetTermValue(property, termInt32Value, "HotIndex", expressionEvaluator); Assert.AreEqual(-1, ((IEdmIntegerValue)annotationHotIndex).Value, "Term annotation value"); annotationHotIndex = applicationModel.GetTermValue(property, "bar.Int32Value", "HotIndex", expressionEvaluator); Assert.AreEqual(-1, ((IEdmIntegerValue)annotationHotIndex).Value, "Term annotation value"); int hotIndex = applicationModel.GetTermValue <int>(property, termInt32Value, "HotIndex", clrEvaluator); Assert.AreEqual(-1, hotIndex, "Term annotation value"); hotIndex = applicationModel.GetTermValue <int>(property, "bar.Int32Value", "HotIndex", clrEvaluator); Assert.AreEqual(-1, hotIndex, "Term annotation value"); IEdmValue annotationString = applicationModel.GetTermValue(property, termStringValue, expressionEvaluator); Assert.AreEqual("Goofy", ((IEdmStringValue)annotationString).Value, "Term annotation value"); annotationString = applicationModel.GetTermValue(property, "bar.StringValue", expressionEvaluator); Assert.AreEqual("Goofy", ((IEdmStringValue)annotationString).Value, "Term annotation value"); string stringValue = applicationModel.GetTermValue <string>(property, termStringValue, clrEvaluator); Assert.AreEqual("Goofy", stringValue, "Term annotation value"); stringValue = applicationModel.GetTermValue <string>(property, "bar.StringValue", clrEvaluator); Assert.AreEqual("Goofy", stringValue, "Term annotation value"); }
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); }
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."); } }
/// <summary> /// Gets the raw CLR value for the given <see cref="IEdmPropertyValue"/>. /// </summary> /// <param name="property">The property to get the value for.</param> /// <param name="type">The type which declared the property.</param> /// <returns>The raw CLR value of the property.</returns> private static object GetPropertyValue(IEdmPropertyValue property, IEdmTypeReference type) { Debug.Assert(property != null, "property != null"); IEdmValue propertyValue = property.Value; // DEVNOTE: though this check is not strictly necessary, and would be caught by later checks, // it seems worthwhile to fail fast if we can. if (propertyValue.ValueKind == EdmValueKind.Null) { throw Error.InvalidOperation(ErrorStrings.Context_NullKeysAreNotSupported(property.Name)); } var primitiveValue = propertyValue as IEdmPrimitiveValue; if (primitiveValue == null) { throw Error.InvalidOperation(ErrorStrings.ClientType_KeysMustBeSimpleTypes(property.Name, type.FullName(), propertyValue.Type.FullName())); } // DEVNOTE: This can return null, and will be handled later. The reason for this is that the client // and server have different ways of getting property values, but both will eventually hit the same // codepath and that is where the null is handled. return(primitiveValue.ToClrValue()); }
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); }
private bool TrySetCollectionProperty(PropertyInfo clrProperty, object clrObject, IEdmPropertyValue propertyValue) { Type clrPropertyType = clrProperty.PropertyType; // Process the following cases: // class C1 // { // IEnumerable<ElementType> EnumerableProperty { get; set; } // // ICollection<ElementType> CollectionProperty { get; set; } // // IList<ElementType> ListProperty { get; set; } // // ICollection<ElementType> CollectionProperty { get { return this.nonNullCollection; } } // // IList<ElementType> ListProperty { get { return this.nonNullList; } } // } if (clrPropertyType.IsGenericType() && clrPropertyType.IsInterface()) { Type genericTypeDefinition = clrPropertyType.GetGenericTypeDefinition(); bool genericTypeDefinitionIsIEnumerableOfT = genericTypeDefinition == TypeIEnumerableOfT; if (genericTypeDefinitionIsIEnumerableOfT || genericTypeDefinition == TypeICollectionOfT || genericTypeDefinition == TypeIListOfT) { object clrPropertyValue = clrProperty.GetValue(clrObject, null); // If property already has a value, we are trying to reuse it and add elements into it (except the case of IEnumerable<T>), // otherwise we create List<elementType>, add elements to it and then assign it to the property. Type elementType = clrPropertyType.GetGenericArguments().Single(); Type clrPropertyValueType; if (clrPropertyValue == null) { // If collection property has no value, create an instance of List<elementType> and // assign it to the property. clrPropertyValueType = TypeListOfT.MakeGenericType(elementType); clrPropertyValue = Activator.CreateInstance(clrPropertyValueType); clrProperty.SetValue(clrObject, clrPropertyValue, null); } else { if (genericTypeDefinitionIsIEnumerableOfT) { // Cannot add elements to an existing value of type IEnumerable<T>. throw new InvalidCastException(Strings.EdmToClr_IEnumerableOfTPropertyAlreadyHasValue(clrProperty.Name, clrProperty.DeclaringType.FullName)); } clrPropertyValueType = clrPropertyValue.GetType(); } // Find the ICollection<elementType>.Add(elementType) method. Note that IList<T> implements MethodInfo clrPropertyValueTypeAddMethod = FindICollectionOfElementTypeAddMethod(clrPropertyValueType, elementType); // Convert the collection elements and add them to the CLR collection. foreach (object convertedElementValue in this.AsIEnumerable(propertyValue.Value, elementType)) { try { clrPropertyValueTypeAddMethod.Invoke(clrPropertyValue, new object[] { convertedElementValue }); } catch (TargetInvocationException targetInvokationException) { // Unwrap the target invokation exception that masks an interesting invalid cast exception. if (targetInvokationException.InnerException != null && targetInvokationException.InnerException is InvalidCastException) { throw targetInvokationException.InnerException; } else { throw; } } } return(true); } } return(false); }
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); } }
private static void CompareProperty(IEdmPropertyValue edmProperty, ODataProperty odataProperty, AssertionHandler assert) { assert.AreEqual(edmProperty.Name, odataProperty.Name, "Property names don't match."); CompareValue(edmProperty.Value, odataProperty.Value, assert); }
private bool TrySetCollectionProperty(PropertyInfo clrProperty, object clrObject, IEdmPropertyValue propertyValue) { Type type; Type propertyType = clrProperty.PropertyType; if (propertyType.IsGenericType() && propertyType.IsInterface()) { Type genericTypeDefinition = propertyType.GetGenericTypeDefinition(); bool typeIEnumerableOfT = genericTypeDefinition == EdmToClrConverter.TypeIEnumerableOfT; if (typeIEnumerableOfT || genericTypeDefinition == EdmToClrConverter.TypeICollectionOfT || genericTypeDefinition == EdmToClrConverter.TypeIListOfT) { object value = clrProperty.GetValue(clrObject, null); Type type1 = propertyType.GetGenericArguments().Single <Type>(); if (value != null) { if (!typeIEnumerableOfT) { type = value.GetType(); } else { throw new InvalidCastException(Strings.EdmToClr_IEnumerableOfTPropertyAlreadyHasValue(clrProperty.Name, clrProperty.DeclaringType.FullName)); } } else { Type[] typeArray = new Type[1]; typeArray[0] = type1; type = EdmToClrConverter.TypeListOfT.MakeGenericType(typeArray); value = Activator.CreateInstance(type); clrProperty.SetValue(clrObject, value, null); } MethodInfo methodInfo = EdmToClrConverter.FindICollectionOfElementTypeAddMethod(type, type1); foreach (object obj in this.AsIEnumerable(propertyValue.Value, type1)) { try { object[] objArray = new object[1]; objArray[0] = obj; methodInfo.Invoke(value, objArray); } catch (TargetInvocationException targetInvocationException1) { TargetInvocationException targetInvocationException = targetInvocationException1; if (targetInvocationException.InnerException == null || targetInvocationException.InnerException as InvalidCastException == null) { throw; } else { throw targetInvocationException.InnerException; } } } return(true); } } return(false); }