/// <summary> /// Sets the Odcm restriction object with restriction values. /// </summary> /// <param name="instance">An {object} Vipr.Code.CodeModel.Vocabularies.Restrictions.* object without restrictions set on it.</param> /// <param name="value">Contains the vocabulary values that will be set on the </param> /// <returns>An {object} that represents a Vipr.Code.CodeModel.Vocabularies.Restrictions.* object with restrictions set on it.</returns> internal static object GetInstanceWithProperties(object instance, IEdmValue value) { if (instance == null) { return(null); } var instanceType = instance.GetType(); var structuredValue = (IEdmStructuredValue)value; foreach (var propertyValue in structuredValue.PropertyValues) { var fieldName = propertyValue.Name; var property = instanceType.GetProperty(fieldName); var v = propertyValue.Value; try { var res = MapToClr(v, property.PropertyType); property.SetValue(instance, res); } catch { return(null); } } return(instance); }
/// <summary> /// Converts <paramref name="edmValue"/> to a <see cref="System.Byte"/> value. /// </summary> /// <param name="edmValue">The EDM value to be converted.</param> /// <returns>Converted byte.</returns> /// <exception cref="InvalidCastException">Exception is thrown if <paramref name="edmValue"/> is not <see cref="IEdmIntegerValue"/>.</exception> /// <exception cref="OverflowException">Exception is thrown if <paramref name="edmValue"/> cannot be converted to <see cref="System.Byte"/>.</exception> internal static Byte AsClrByte(IEdmValue edmValue) { checked { return((Byte)AsClrInt64(edmValue)); } }
internal static bool ValidateValueCanBeWrittenAsXmlElementAnnotation(IEdmValue value, string annotationNamespace, string annotationName, out EdmError error) { IEdmStringValue edmStringValue = value as IEdmStringValue; if (edmStringValue == null) { error = new EdmError(value.Location(), EdmErrorCode.InvalidElementAnnotation, Edm.Strings.EdmModel_Validator_Semantic_InvalidElementAnnotationNotIEdmStringValue); return false; } string rawString = edmStringValue.Value; XmlReader reader = XmlReader.Create(new StringReader(rawString)); try { // Skip to root element. if (reader.NodeType != XmlNodeType.Element) { while (reader.Read() && reader.NodeType != XmlNodeType.Element) { } } // The annotation must be an element. if (reader.EOF) { error = new EdmError(value.Location(), EdmErrorCode.InvalidElementAnnotation, Edm.Strings.EdmModel_Validator_Semantic_InvalidElementAnnotationValueInvalidXml); return false; } // The root element must corespond to the term of the annotation string elementNamespace = reader.NamespaceURI; string elementName = reader.LocalName; if (EdmUtil.IsNullOrWhiteSpaceInternal(elementNamespace) || EdmUtil.IsNullOrWhiteSpaceInternal(elementName)) { error = new EdmError(value.Location(), EdmErrorCode.InvalidElementAnnotation, Edm.Strings.EdmModel_Validator_Semantic_InvalidElementAnnotationNullNamespaceOrName); return false; } if (!((annotationNamespace == null || elementNamespace == annotationNamespace) && (annotationName == null || elementName == annotationName))) { error = new EdmError(value.Location(), EdmErrorCode.InvalidElementAnnotation, Edm.Strings.EdmModel_Validator_Semantic_InvalidElementAnnotationMismatchedTerm); return false; } // Parse the entire fragment to determine if the XML is valid while (reader.Read()) { } error = null; return true; } catch (XmlException) { error = new EdmError(value.Location(), EdmErrorCode.InvalidElementAnnotation, Edm.Strings.EdmModel_Validator_Semantic_InvalidElementAnnotationValueInvalidXml); return false; } }
/// <summary> /// Converts <paramref name="edmValue"/> to a <see cref="System.Int16"/> value. /// </summary> /// <param name="edmValue">The EDM value to be converted.</param> /// <returns>Converted integer.</returns> /// <exception cref="InvalidCastException">Exception is thrown if <paramref name="edmValue"/> is not <see cref="IEdmIntegerValue"/>.</exception> /// <exception cref="OverflowException">Exception is thrown if <paramref name="edmValue"/> cannot be converted to <see cref="System.Int16"/>.</exception> internal static Int16 AsClrInt16(IEdmValue edmValue) { checked { return((Int16)AsClrInt64(edmValue)); } }
internal static void CompareValue(IEdmValue edmValue, object odataValue, AssertionHandler assert) { if (odataValue == null) { ValidateNullValue(edmValue, assert); return; } ODataEntry entry = odataValue as ODataEntry; if (entry != null) { CompareStructuralValue(edmValue, entry, assert); return; } ODataComplexValue complexValue = odataValue as ODataComplexValue; if (complexValue != null) { CompareStructuralValue(edmValue, complexValue, assert); return; } ODataCollectionValue collectionValue = odataValue as ODataCollectionValue; if (collectionValue != null) { CompareCollectionValue(edmValue, collectionValue, assert); return; } ComparePrimitiveValue(edmValue, odataValue, assert); }
private object AsListOfT(IEdmValue edmValue, Type clrType) { MethodInfo methodInfo = null; object obj; Type type = clrType.GetGenericArguments().Single <Type>(); if (!this.enumerableConverters.TryGetValue(type, out methodInfo)) { Type[] typeArray = new Type[1]; typeArray[0] = type; methodInfo = EdmToClrConverter.EnumerableToListOfTMethodInfo.MakeGenericMethod(typeArray); this.enumerableConverters.Add(type, methodInfo); } try { object[] objArray = new object[1]; objArray[0] = this.AsIEnumerable(edmValue, type); obj = methodInfo.Invoke(null, objArray); } catch (TargetInvocationException targetInvocationException1) { TargetInvocationException targetInvocationException = targetInvocationException1; if (targetInvocationException.InnerException == null || targetInvocationException.InnerException as InvalidCastException == null) { throw; } else { throw targetInvocationException.InnerException; } } return(obj); }
public T AsClrValue <T>(IEdmValue edmValue) { EdmUtil.CheckArgumentNull <IEdmValue>(edmValue, "edmValue"); bool flag = false; return((T)this.AsClrValue(edmValue, typeof(T), flag)); }
/// <summary> /// Converts <paramref name="edmValue"/> to a <see cref="System.Int32"/> value. /// </summary> /// <param name="edmValue">The EDM value to be converted.</param> /// <returns>Converted integer.</returns> /// <exception cref="InvalidCastException">Exception is thrown if <paramref name="edmValue"/> is not <see cref="IEdmIntegerValue"/>.</exception> /// <exception cref="OverflowException">Exception is thrown if <paramref name="edmValue"/> cannot be converted to <see cref="System.Int32"/>.</exception> internal static Int32 AsClrInt32(IEdmValue edmValue) { checked { return((Int32)AsClrInt64(edmValue)); } }
public EdmPropertyValue(string name, IEdmValue value) { EdmUtil.CheckArgumentNull<string>(name, "name"); EdmUtil.CheckArgumentNull<IEdmValue>(value, "value"); this.name = name; this.@value = value; }
/// <summary> /// Converts <paramref name="edmValue"/> to a <see cref="System.Char"/> value. /// </summary> /// <param name="edmValue">The EDM value to be converted.</param> /// <returns>Converted char.</returns> /// <exception cref="InvalidCastException">Exception is thrown if <paramref name="edmValue"/> is not <see cref="IEdmIntegerValue"/>.</exception> /// <exception cref="OverflowException">Exception is thrown if <paramref name="edmValue"/> cannot be converted to <see cref="System.Char"/>.</exception> internal static Char AsClrChar(IEdmValue edmValue) { checked { return((Char)AsClrInt64(edmValue)); } }
internal static void CompareValue(IEdmValue edmValue, object odataValue, AssertionHandler assert) { if (odataValue == null) { ValidateNullValue(edmValue, assert); return; } ODataEntry entry = odataValue as ODataEntry; if (entry != null) { CompareStructuralValue(edmValue, entry, assert); return; } ODataComplexValue complexValue = odataValue as ODataComplexValue; if (complexValue != null) { CompareStructuralValue(edmValue, complexValue, assert); return; } ODataCollectionValue collectionValue = odataValue as ODataCollectionValue; if (collectionValue != null) { CompareCollectionValue(edmValue, collectionValue, assert); return; } ComparePrimitiveValue(edmValue, odataValue, assert); }
private IEnumerable AsIEnumerable(IEdmValue edmValue, Type elementType) { foreach (IEdmDelayedValue edmDelayedValue in ((IEdmCollectionValue)edmValue).Elements) { yield return(this.AsClrValue(edmDelayedValue.Value, elementType)); } }
private object AsListOfT(IEdmValue edmValue, Type clrType) { Debug.Assert(clrType.IsGenericType(), "clrType.IsGenericType"); Type elementType = clrType.GetGenericArguments().Single(); MethodInfo enumerableConverter; if (!this.enumerableConverters.TryGetValue(elementType, out enumerableConverter)) { enumerableConverter = EnumerableToListOfTMethodInfo.MakeGenericMethod(elementType); this.enumerableConverters.Add(elementType, enumerableConverter); } try { return(enumerableConverter.Invoke(null, new object[] { this.AsIEnumerable(edmValue, elementType) })); } 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; } } }
public object AsClrValue(IEdmValue edmValue, Type clrType) { EdmUtil.CheckArgumentNull <IEdmValue>(edmValue, "edmValue"); EdmUtil.CheckArgumentNull <Type>(clrType, "clrType"); bool flag = true; return(this.AsClrValue(edmValue, clrType, flag)); }
private IEnumerable AsIEnumerable(IEdmValue edmValue, Type elementType) { // By convention we only support converting from a collection value. foreach (IEdmDelayedValue element in ((IEdmCollectionValue)edmValue).Elements) { yield return(this.AsClrValue(element.Value, elementType)); } }
/// <summary> /// Initializes a new instance of the <see cref="EdmPropertyValue"/> class. /// This constructor will not allow changing <see cref="Value"/> property after the EdmPropertyValue instance has been constructed. /// </summary> /// <param name="name">Name of the property for which this provides a value.</param> /// <param name="value">Value of the property.</param> public EdmPropertyValue(string name, IEdmValue value) { EdmUtil.CheckArgumentNull(name, "name"); EdmUtil.CheckArgumentNull(value, "value"); this.name = name; this.value = value; }
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); } }
private static IEdmValue Cast(IEdmTypeReference targetType, IEdmValue operand) { IEdmTypeReference operandType = operand.Type; EdmValueKind operandKind = operand.ValueKind; if ((operandType != null && operandKind != EdmValueKind.Null && operandType.Definition.IsOrInheritsFrom(targetType.Definition)) || targetType.TypeKind() == EdmTypeKind.None) { return(operand); } bool matches = true; switch (operandKind) { case EdmValueKind.Collection: if (targetType.IsCollection()) { // Avoid enumerating the collection at this point. return(new CastCollectionValue(targetType.AsCollection(), (IEdmCollectionValue)operand)); } else { matches = false; } break; case EdmValueKind.Structured: if (targetType.IsStructured()) { IEdmStructuredTypeReference structuredTargetType = targetType.AsStructured(); List <IEdmPropertyValue> newProperties = new List <IEdmPropertyValue>(); matches = AssertOrMatchStructuredType(structuredTargetType, (IEdmStructuredValue)operand, true, newProperties); if (matches) { return(new EdmStructuredValue(structuredTargetType, newProperties)); } } else { matches = false; } break; default: matches = MatchesType(targetType, operand); break; } if (!matches) { throw new InvalidOperationException(Strings.Edm_Evaluator_FailedTypeAssertion(targetType.ToTraceString())); } return(operand); }
/// <summary> /// Converts <paramref name="edmValue"/> to a CLR value of the specified type. /// Supported values for <typeparamref name="T"/> are: /// CLR primitive types such as <see cref="System.String"/> and <see cref="System.Int32"/>, /// CLR enum types, /// <see cref="IEnumerable<T>"/>, /// <see cref="ICollection<T>"/>, /// <see cref="IList<T>"/>, /// CLR classes with default constructors and public properties with setters and collection properties of the following shapes: /// <see cref="IEnumerable<T>"/> EnumerableProperty { get; set; }, /// <see cref="ICollection<T>"/> CollectionProperty { get; set; }, /// <see cref="IList<T>"/> ListProperty { get; set; }, /// <see cref="ICollection<T>"/> CollectionProperty { get { return this.nonNullCollection; } }, /// <see cref="IList<T>"/> ListProperty { get { return this.nonNullList; } }. /// </summary> /// <typeparam name="T">The CLR type.</typeparam> /// <param name="edmValue">The EDM value to be converted.</param> /// <returns>A CLR value converted from <paramref name="edmValue"/>.</returns> /// <remarks>This method performs boxing and unboxing for value types. Use value-type specific methods such as <see cref="AsClrString"/> to avoid boxing and unboxing.</remarks> public T AsClrValue <T>(IEdmValue edmValue) { EdmUtil.CheckArgumentNull(edmValue, "edmValue"); // convertEnumValues: false -- no need to produce an object of the enum type because // the produced underlying value will get converted to the enum type by (T)this.AsClrValue. bool convertEnumValues = false; return((T)this.AsClrValue(edmValue, typeof(T), convertEnumValues)); }
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"); }
/// <summary> /// Converts <paramref name="edmValue"/> to a CLR value of the specified type. /// Supported values for <paramref name="clrType"/> are: /// CLR primitive types such as <see cref="System.String"/> and <see cref="System.Int32"/>, /// CLR enum types, /// <see cref="IEnumerable<T>"/>, /// <see cref="ICollection<T>"/>, /// <see cref="IList<T>"/>, /// CLR classes with default constructors and public properties with setters and collection properties of the following shapes: /// <see cref="IEnumerable<T>"/> EnumerableProperty { get; set; }, /// <see cref="ICollection<T>"/> CollectionProperty { get; set; }, /// <see cref="IList<T>"/> ListProperty { get; set; }, /// <see cref="ICollection<T>"/> CollectionProperty { get { return this.nonNullCollection; } }, /// <see cref="IList<T>"/> ListProperty { get { return this.nonNullList; } }. /// </summary> /// <param name="edmValue">The EDM value to be converted.</param> /// <param name="clrType">The CLR type.</param> /// <returns>A CLR value converted from <paramref name="edmValue"/>.</returns> /// <remarks>This method performs boxing and unboxing for value types. Use value-type specific methods such as <see cref="AsClrString"/> to avoid boxing and unboxing.</remarks> public object AsClrValue(IEdmValue edmValue, Type clrType) { EdmUtil.CheckArgumentNull(edmValue, "edmValue"); EdmUtil.CheckArgumentNull(clrType, "clrType"); // convertEnumValues: true -- must produce an object of the requested enum type because there is nothing else // down the line that can convert an underlying value to an enum type. bool convertEnumValues = true; return(this.AsClrValue(edmValue, clrType, convertEnumValues)); }
/// <summary> /// Converts <paramref name="edmValue"/> to a CLR byte array value. /// </summary> /// <param name="edmValue">The EDM value to be converted.</param> /// <returns>Converted byte array.</returns> /// <exception cref="InvalidCastException">Exception is thrown if <paramref name="edmValue"/> is not <see cref="IEdmBinaryValue"/>.</exception> internal static byte[] AsClrByteArray(IEdmValue edmValue) { EdmUtil.CheckArgumentNull(edmValue, "edmValue"); if (edmValue is IEdmNullValue) { return(null); } return(((IEdmBinaryValue)edmValue).Value); }
/// <summary> /// Converts <paramref name="edmValue"/> to a <see cref="System.String"/> value. /// </summary> /// <param name="edmValue">The EDM value to be converted.</param> /// <returns>Converted string.</returns> /// <exception cref="InvalidCastException">Exception is thrown if <paramref name="edmValue"/> is not <see cref="IEdmStringValue"/>.</exception> internal static string AsClrString(IEdmValue edmValue) { EdmUtil.CheckArgumentNull(edmValue, "edmValue"); if (edmValue is IEdmNullValue) { return(null); } return(((IEdmStringValue)edmValue).Value); }
private IEnumerable AsIEnumerable(IEdmValue edmValue, Type elementType) { // By convention we only support converting from a collection value. foreach (IEdmDelayedValue element in ((IEdmCollectionValue)edmValue).Elements) { if (element.Value != null || elementType.IsGenericType() && elementType.GetGenericTypeDefinition() == TypeNullableOfT) { yield return(this.AsClrValue(element.Value, elementType)); } } }
internal static string AsClrString(IEdmValue edmValue) { EdmUtil.CheckArgumentNull <IEdmValue>(edmValue, "edmValue"); if (edmValue as IEdmNullValue == null) { return(((IEdmStringValue)edmValue).Value); } else { return(null); } }
public void JustDecompileGenerated_set_Value(IEdmValue value) { EdmUtil.CheckArgumentNull<IEdmValue>(value, "value"); if (this.@value == null) { this.@value = value; return; } else { throw new InvalidOperationException(Strings.ValueHasAlreadyBeenSet); } }
public void JustDecompileGenerated_set_Value(IEdmValue value) { EdmUtil.CheckArgumentNull <IEdmValue>(value, "value"); if (this.@value == null) { this.@value = value; return; } else { throw new InvalidOperationException(Strings.ValueHasAlreadyBeenSet); } }
private void VisitPrimitiveElementAnnotations(IEnumerable <IEdmDirectValueAnnotation> annotations) { foreach (IEdmDirectValueAnnotation annotation in annotations) { if (annotation.NamespaceUri != "http://schemas.microsoft.com/ado/2011/04/edm/internal") { IEdmValue value2 = annotation.Value as IEdmValue; if (((value2 != null) && value2.IsSerializedAsElement(base.Model)) && (value2.Type.TypeKind() == EdmTypeKind.Primitive)) { this.ProcessElementAnnotation(annotation); } } } }
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); }
public static bool IsSerializedAsElement(this IEdmValue value, IEdmModel model) { EdmUtil.CheckArgumentNull <IEdmValue>(value, "value"); EdmUtil.CheckArgumentNull <IEdmModel>(model, "model"); bool?annotationValue = (bool?)(model.GetAnnotationValue(value, "http://schemas.microsoft.com/ado/2011/04/edm/internal", "IsSerializedAsElement") as bool?); if (annotationValue.HasValue) { return(annotationValue.GetValueOrDefault()); } else { return(false); } }
private static string GetEdmValueInterfaceName(IEdmValue edmValue) { Type type = typeof(IEdmValue); Type[] interfaces = edmValue.GetType().GetInterfaces(); foreach (Type type1 in (IEnumerable <Type>)interfaces.OrderBy <Type, string>((Type i) => i.FullName)) { if (!type.IsAssignableFrom(type1) || !(type != type1)) { continue; } type = type1; } return(type.Name); }
/// <summary> /// Sets an annotation indicating if the value should be serialized as an element. /// </summary> /// <param name="value">Value to set the annotation on.</param> /// <param name="model">Model containing the value.</param> /// <param name="isSerializedAsElement">Value indicating if the value should be serialized as an element.</param> public static void SetIsSerializedAsElement(this IEdmValue value, IEdmModel model, bool isSerializedAsElement) { EdmUtil.CheckArgumentNull(value, "value"); EdmUtil.CheckArgumentNull(model, "model"); if (isSerializedAsElement) { EdmError error; if (!ValidationHelper.ValidateValueCanBeWrittenAsXmlElementAnnotation(value, null, null, out error)) { throw new InvalidOperationException(error.ToString()); } } model.SetAnnotationValue(value, EdmConstants.InternalUri, CsdlConstants.IsSerializedAsElementAnnotation, (object)isSerializedAsElement); }
public static void SetIsSerializedAsElement(this IEdmValue value, IEdmModel model, bool isSerializedAsElement) { EdmError edmError = null; EdmUtil.CheckArgumentNull <IEdmValue>(value, "value"); EdmUtil.CheckArgumentNull <IEdmModel>(model, "model"); if (!isSerializedAsElement || ValidationHelper.ValidateValueCanBeWrittenAsXmlElementAnnotation(value, null, null, out edmError)) { model.SetAnnotationValue(value, "http://schemas.microsoft.com/ado/2011/04/edm/internal", "IsSerializedAsElement", isSerializedAsElement); return; } else { throw new InvalidOperationException(edmError.ToString()); } }
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); }
/// <summary> /// Initializes a new instance of the <see cref="NullEdmDelayedValue"/> class. /// </summary> /// <param name="value">The value.</param> public NullEdmDelayedValue(IEdmValue value) { this.Value = value; }
private static void CompareStructuralValue(IEdmValue edmValue, ODataComplexValue complexValue, AssertionHandler assert) { assert.IsNotNull(edmValue, "EDM value instance must not be null."); if (complexValue == null) { ValidateNullValue(edmValue, assert); return; } assert.AreEqual(EdmValueKind.Structured, edmValue.ValueKind, "Value kinds differ."); if (edmValue.Type != null) { assert.AreEqual(EdmTypeKind.Complex, edmValue.Type.TypeKind(), "Type kinds differ."); } CompareStructuralValue(edmValue, complexValue.Properties, assert); }
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 void CompareCollectionValue(IEdmValue edmValue, ODataCollectionValue collectionValue, AssertionHandler assert) { assert.IsNotNull(edmValue, "EDM value instance must not be null."); if (collectionValue == null) { ValidateNullValue(edmValue, assert); return; } assert.AreEqual(EdmValueKind.Collection, edmValue.ValueKind, "Value kinds differ."); if (edmValue.Type != null) { assert.AreEqual(EdmTypeKind.Collection, edmValue.Type.TypeKind(), "Type kinds differ."); } IEdmCollectionValue edmCollectionValue = (IEdmCollectionValue)edmValue; IEnumerable items = collectionValue.Items; if (items != null) { CompareCollectionItems(edmCollectionValue.Elements, items, assert); } else { assert.IsTrue( edmCollectionValue.Elements == null || edmCollectionValue.Elements.Count() == 0, "Expected empty collection value."); } }
private static void ValidateNullValue(IEdmValue edmValue, AssertionHandler assert) { assert.AreEqual(EdmValueKind.Null, edmValue.ValueKind, "Expected null value kind."); assert.IsTrue(edmValue is IEdmNullValue, "Expected IEdmNullValue instance."); }
internal static bool ValidateValueCanBeWrittenAsXmlElementAnnotation(IEdmValue value, string annotationNamespace, string annotationName, out EdmError error) { bool flag; IEdmStringValue edmStringValue = value as IEdmStringValue; if (edmStringValue != null) { string str = edmStringValue.Value; XmlReader xmlReader = XmlReader.Create(new StringReader(str)); try { if (xmlReader.NodeType != XmlNodeType.Element) { while (xmlReader.Read() && xmlReader.NodeType != XmlNodeType.Element) { } } if (!xmlReader.EOF) { string namespaceURI = xmlReader.NamespaceURI; string localName = xmlReader.LocalName; if (EdmUtil.IsNullOrWhiteSpaceInternal(namespaceURI) || EdmUtil.IsNullOrWhiteSpaceInternal(localName)) { error = new EdmError(value.Location(), EdmErrorCode.InvalidElementAnnotation, Strings.EdmModel_Validator_Semantic_InvalidElementAnnotationNullNamespaceOrName); flag = false; } else { if ((annotationNamespace == null || namespaceURI == annotationNamespace) && (annotationName == null || localName == annotationName)) { while (xmlReader.Read()) { } error = null; flag = true; } else { error = new EdmError(value.Location(), EdmErrorCode.InvalidElementAnnotation, Strings.EdmModel_Validator_Semantic_InvalidElementAnnotationMismatchedTerm); flag = false; } } } else { error = new EdmError(value.Location(), EdmErrorCode.InvalidElementAnnotation, Strings.EdmModel_Validator_Semantic_InvalidElementAnnotationValueInvalidXml); flag = false; } } catch (Exception exception) { error = new EdmError(value.Location(), EdmErrorCode.InvalidElementAnnotation, Strings.EdmModel_Validator_Semantic_InvalidElementAnnotationValueInvalidXml); flag = false; } return flag; } else { error = new EdmError(value.Location(), EdmErrorCode.InvalidElementAnnotation, Strings.EdmModel_Validator_Semantic_InvalidElementAnnotationNotIEdmStringValue); return false; } }
private static IEdmValue MagicEvaluator(string functionName, IEdmValue[] arguments) { switch (functionName) { case "Functions.MagicConcat": { string result = ""; foreach (IEdmValue value in arguments) { IEdmStringValue stringValue = value as IEdmStringValue; if (stringValue != null) { result = result + stringValue.Value; continue; } IEdmIntegerValue intValue = value as IEdmIntegerValue; if (intValue != null) { result = result + intValue.Value.ToString(); continue; } } return new EdmStringConstant(result); } } return null; }
private static void ComparePrimitiveValue(IEdmValue edmValue, object odataValue, AssertionHandler assert) { TypeCode typeCode = Type.GetTypeCode(odataValue.GetType()); switch (typeCode) { case TypeCode.Boolean: assert.AreEqual(EdmValueKind.Boolean, edmValue.ValueKind, "Value kinds differ."); assert.AreEqual(odataValue, ((IEdmBooleanValue)edmValue).Value, "Values differ."); if (edmValue.Type != null) { assert.AreEqual(EdmTypeKind.Primitive, edmValue.Type.Definition.TypeKind, "EDM type kinds differ."); assert.AreEqual(EdmPrimitiveTypeKind.Boolean, edmValue.Type.PrimitiveKind(), "EDM primitive kinds differ."); } return; case TypeCode.Byte: assert.AreEqual(EdmValueKind.Integer, edmValue.ValueKind, "Value kinds differ."); assert.AreEqual(odataValue, (byte)((IEdmIntegerValue)edmValue).Value, "Values differ."); if (edmValue.Type != null) { assert.AreEqual(EdmTypeKind.Primitive, edmValue.Type.Definition.TypeKind, "EDM type kinds differ."); assert.AreEqual(EdmPrimitiveTypeKind.Byte, edmValue.Type.PrimitiveKind(), "EDM primitive kinds differ."); } return; case TypeCode.SByte: assert.AreEqual(EdmValueKind.Integer, edmValue.ValueKind, "Value kinds differ."); assert.AreEqual(odataValue, (sbyte)((IEdmIntegerValue)edmValue).Value, "Values differ."); if (edmValue.Type != null) { assert.AreEqual(EdmTypeKind.Primitive, edmValue.Type.Definition.TypeKind, "EDM type kinds differ."); assert.AreEqual(EdmPrimitiveTypeKind.SByte, edmValue.Type.PrimitiveKind(), "EDM primitive kinds differ."); } return; case TypeCode.Int16: assert.AreEqual(EdmValueKind.Integer, edmValue.ValueKind, "Value kinds differ."); assert.AreEqual(odataValue, (Int16)((IEdmIntegerValue)edmValue).Value, "Values differ."); if (edmValue.Type != null) { assert.AreEqual(EdmTypeKind.Primitive, edmValue.Type.Definition.TypeKind, "EDM type kinds differ."); assert.AreEqual(EdmPrimitiveTypeKind.Int16, edmValue.Type.PrimitiveKind(), "EDM primitive kinds differ."); } return; case TypeCode.Int32: assert.AreEqual(EdmValueKind.Integer, edmValue.ValueKind, "Value kinds differ."); assert.AreEqual(odataValue, (Int32)((IEdmIntegerValue)edmValue).Value, "Values differ."); if (edmValue.Type != null) { assert.AreEqual(EdmTypeKind.Primitive, edmValue.Type.Definition.TypeKind, "EDM type kinds differ."); assert.AreEqual(EdmPrimitiveTypeKind.Int32, edmValue.Type.PrimitiveKind(), "EDM primitive kinds differ."); } return; case TypeCode.Int64: assert.AreEqual(EdmValueKind.Integer, edmValue.ValueKind, "Value kinds differ."); assert.AreEqual(odataValue, (Int64)((IEdmIntegerValue)edmValue).Value, "Values differ."); if (edmValue.Type != null) { assert.AreEqual(EdmTypeKind.Primitive, edmValue.Type.Definition.TypeKind, "EDM type kinds differ."); assert.AreEqual(EdmPrimitiveTypeKind.Int64, edmValue.Type.PrimitiveKind(), "EDM primitive kinds differ."); } return; case TypeCode.Decimal: assert.AreEqual(EdmValueKind.Decimal, edmValue.ValueKind, "Value kinds differ."); assert.AreEqual(odataValue, ((IEdmDecimalValue)edmValue).Value, "Values differ."); if (edmValue.Type != null) { assert.AreEqual(EdmTypeKind.Primitive, edmValue.Type.Definition.TypeKind, "EDM type kinds differ."); assert.AreEqual(EdmPrimitiveTypeKind.Decimal, edmValue.Type.PrimitiveKind(), "EDM primitive kinds differ."); } return; case TypeCode.Single: assert.AreEqual(EdmValueKind.Floating, edmValue.ValueKind, "Value kinds differ."); assert.AreEqual(odataValue, (Single)((IEdmFloatingValue)edmValue).Value, "Values differ."); if (edmValue.Type != null) { assert.AreEqual(EdmTypeKind.Primitive, edmValue.Type.Definition.TypeKind, "EDM type kinds differ."); assert.AreEqual(EdmPrimitiveTypeKind.Single, edmValue.Type.PrimitiveKind(), "EDM primitive kinds differ."); } return; case TypeCode.Double: assert.AreEqual(EdmValueKind.Floating, edmValue.ValueKind, "Value kinds differ."); assert.AreEqual(odataValue, ((IEdmFloatingValue)edmValue).Value, "Values differ."); if (edmValue.Type != null) { assert.AreEqual(EdmTypeKind.Primitive, edmValue.Type.Definition.TypeKind, "EDM type kinds differ."); assert.AreEqual(EdmPrimitiveTypeKind.Double, edmValue.Type.PrimitiveKind(), "EDM primitive kinds differ."); } return; case TypeCode.String: assert.AreEqual(EdmValueKind.String, edmValue.ValueKind, "Value kinds differ."); assert.AreEqual(odataValue, ((IEdmStringValue)edmValue).Value, "Values differ."); if (edmValue.Type != null) { assert.AreEqual(EdmTypeKind.Primitive, edmValue.Type.Definition.TypeKind, "EDM type kinds differ."); assert.AreEqual(EdmPrimitiveTypeKind.String, edmValue.Type.PrimitiveKind(), "EDM primitive kinds differ."); } return; default: byte[] bytes = odataValue as byte[]; if (bytes != null) { assert.AreEqual(EdmValueKind.Binary, edmValue.ValueKind, "Value kinds differ."); assert.AreEqual(odataValue, ((IEdmBinaryValue)edmValue).Value, "Values differ."); if (edmValue.Type != null) { assert.AreEqual(EdmTypeKind.Primitive, edmValue.Type.Definition.TypeKind, "EDM type kinds differ."); assert.AreEqual(EdmPrimitiveTypeKind.Binary, edmValue.Type.PrimitiveKind(), "EDM primitive kinds differ."); } return; } if (odataValue is DateTimeOffset) { assert.AreEqual(EdmValueKind.DateTimeOffset, edmValue.ValueKind, "Value kinds differ."); assert.AreEqual(odataValue, ((IEdmDateTimeOffsetValue)edmValue).Value, "Values differ."); if (edmValue.Type != null) { assert.AreEqual(EdmTypeKind.Primitive, edmValue.Type.Definition.TypeKind, "EDM type kinds differ."); assert.AreEqual(EdmPrimitiveTypeKind.DateTimeOffset, edmValue.Type.PrimitiveKind(), "EDM primitive kinds differ."); } return; } if (odataValue is Guid) { assert.AreEqual(EdmValueKind.Guid, edmValue.ValueKind, "Value kinds differ."); assert.AreEqual(odataValue, ((IEdmGuidValue)edmValue).Value, "Values differ."); if (edmValue.Type != null) { assert.AreEqual(EdmTypeKind.Primitive, edmValue.Type.Definition.TypeKind, "EDM type kinds differ."); assert.AreEqual(EdmPrimitiveTypeKind.Guid, edmValue.Type.PrimitiveKind(), "EDM primitive kinds differ."); } return; } if (odataValue is TimeSpan) { assert.AreEqual(EdmValueKind.Duration, edmValue.ValueKind, "Value kinds differ."); assert.AreEqual(odataValue, ((IEdmDurationValue)edmValue).Value, "Values differ."); if (edmValue.Type != null) { assert.AreEqual(EdmTypeKind.Primitive, edmValue.Type.Definition.TypeKind, "EDM type kinds differ."); assert.AreEqual(EdmPrimitiveTypeKind.Duration, edmValue.Type.PrimitiveKind(), "EDM primitive kinds differ."); } return; } if (odataValue is ISpatial) { // TODO: [JsonLight] Add support for spatial values in ODataEdmStructuredValue throw new System.NotSupportedException(); } assert.Fail("Unsupported primitive type: " + odataValue.GetType().FullName); return; } }
private void ValidateEdmValueKindRoundTrip(EdmValueKind valueKind, IEdmTypeReference valueKindType, IEdmValue valueKindValue) { Assert.AreEqual(valueKind, valueKindValue.ValueKind, "Invalid value kind."); var model = this.BuildValueAnnotationModelWithEdmValueKind(valueKindType, valueKindValue as IEdmExpression); IEnumerable<EdmError> errors; model.Validate(EdmConstants.EdmVersionLatest, out errors); Assert.AreEqual(0, errors.Count(), "Invalid error count."); var csdl = this.GetSerializerResult(model); var csdlModel = this.GetParserResult(csdl); csdlModel.Validate(EdmConstants.EdmVersionLatest, out errors); Assert.AreEqual(0, errors.Count(), "Invalid error count."); Assert.AreEqual(1, csdlModel.VocabularyAnnotations.Count(), "Invalid count of vocabulary annotation."); var resultValueAnnotation = csdlModel.VocabularyAnnotations.ElementAt(0) as IEdmValueAnnotation; Assert.AreEqual(valueKind, (resultValueAnnotation.Value as IEdmValue).ValueKind, "Invalid value kind."); }