/// <summary> /// Converts <paramref name="edmValue"/> to a <see cref="Microsoft.OData.Edm.TimeOfDay"/> value. /// </summary> /// <param name="edmValue">The EDM value to be converted.</param> /// <returns>Converted TimeOfDay.</returns> /// <exception cref="InvalidCastException">Exception is thrown if <paramref name="edmValue"/> is not <see cref="IEdmTimeOfDayValue"/>.</exception> internal static TimeOfDay AsClrTimeOfDay(IEdmValue edmValue) { EdmUtil.CheckArgumentNull(edmValue, "edmValue"); return(((IEdmTimeOfDayValue)edmValue).Value); }
/// <summary> /// Initializes a new instance of the <see cref="EdmLabeledExpressionReferenceExpression"/> class. /// This constructor will not allow changing <see cref="ReferencedLabeledExpression"/> property after the EdmLabeledExpressionReferenceExpression instance has been constructed. /// </summary> /// <param name="referencedLabeledExpression">Referenced labeled element.</param> public EdmLabeledExpressionReferenceExpression(IEdmLabeledExpression referencedLabeledExpression) { EdmUtil.CheckArgumentNull(referencedLabeledExpression, "referencedLabeledExpression"); this.referencedLabeledExpression = referencedLabeledExpression; }
/// <summary> /// Sets the schema an annotation should appear in. /// </summary> /// <param name="annotation">The annotation the schema should be set for.</param> /// <param name="model">Model containing the annotation.</param> /// <param name="schemaNamespace">The schema the annotation belongs in.</param> public static void SetSchemaNamespace(this IEdmVocabularyAnnotation annotation, IEdmModel model, string schemaNamespace) { EdmUtil.CheckArgumentNull(annotation, "annotation"); EdmUtil.CheckArgumentNull(model, "model"); model.SetAnnotationValue(annotation, EdmConstants.InternalUri, CsdlConstants.SchemaNamespaceAnnotation, schemaNamespace); }
/// <summary> /// Sets an annotation on the IEdmModel to notify the serializer of preferred prefix mappings for xml namespaces. /// </summary> /// <param name="model">Reference to the calling object.</param> /// <param name="mappings">XmlNamespaceManage containing mappings between namespace prefixes and xml namespaces.</param> public static void SetNamespacePrefixMappings(this IEdmModel model, IEnumerable <KeyValuePair <string, string> > mappings) { EdmUtil.CheckArgumentNull(model, "model"); model.SetAnnotationValue(model, EdmConstants.InternalUri, CsdlConstants.NamespacePrefixAnnotation, mappings); }
private static string PathAsXml(IEnumerable <string> path) { return(EdmUtil.JoinInternal("/", path)); }
// This internal method exists so we can get a consistent view of the mappings through the entire serialization process. // Otherwise, changes to the dictionary durring serialization would result in an invalid or inconsistent output. internal static VersioningDictionary <string, string> GetNamespaceAliases(this IEdmModel model) { EdmUtil.CheckArgumentNull(model, "model"); return(model.GetAnnotationValue <VersioningDictionary <string, string> >(model, EdmConstants.InternalUri, CsdlConstants.NamespaceAliasAnnotation)); }
/// <summary> /// Gets the value for the EDMX version of the <paramref name="model"/>. /// </summary> /// <param name="model">Model the version has been set for.</param> /// <returns>The version.</returns> public static Version GetEdmxVersion(this IEdmModel model) { EdmUtil.CheckArgumentNull(model, "model"); return(model.GetAnnotationValue <Version>(model, EdmConstants.InternalUri, CsdlConstants.EdmxVersionAnnotation)); }
/// <summary> /// Initializes a new instance of the <see cref="EdmToClrConverter"/> class. /// </summary> /// <param name="tryCreateObjectInstanceDelegate">The delegate customizing conversion of structured values.</param> public EdmToClrConverter(TryCreateObjectInstance tryCreateObjectInstanceDelegate) { EdmUtil.CheckArgumentNull(tryCreateObjectInstanceDelegate, "tryCreateObjectInstanceDelegate"); this.tryCreateObjectInstanceDelegate = tryCreateObjectInstanceDelegate; }
private object AsClrObject(IEdmValue edmValue, Type clrObjectType) { EdmUtil.CheckArgumentNull(edmValue, "edmValue"); EdmUtil.CheckArgumentNull(clrObjectType, "clrObjectType"); if (edmValue is IEdmNullValue) { return(null); } IEdmStructuredValue edmStructuredValue = edmValue as IEdmStructuredValue; if (edmStructuredValue == null) { if (edmValue is IEdmCollectionValue) { throw new InvalidCastException(Strings.EdmToClr_CannotConvertEdmCollectionValueToClrType(clrObjectType.FullName)); } else { throw new InvalidCastException(Strings.EdmToClr_CannotConvertEdmValueToClrType(GetEdmValueInterfaceName(edmValue), clrObjectType.FullName)); } } object clrObject; if (this.convertedObjects.TryGetValue(edmStructuredValue, out clrObject)) { return(clrObject); } // By convention we only support mapping structured values to a CLR class. if (!clrObjectType.IsClass()) { throw new InvalidCastException(Strings.EdmToClr_StructuredValueMappedToNonClass); } // Try user-defined logic before the default logic. bool clrObjectInitialized; if (this.tryCreateObjectInstanceDelegate != null && this.tryCreateObjectInstanceDelegate(edmStructuredValue, clrObjectType, this, out clrObject, out clrObjectInitialized)) { // The user-defined logic might have produced null, which is Ok, but we need to null the type in to keep them in sync. if (clrObject != null) { Type newClrObjectType = clrObject.GetType(); if (!clrObjectType.IsAssignableFrom(newClrObjectType)) { throw new InvalidCastException(Strings.EdmToClr_TryCreateObjectInstanceReturnedWrongObject(newClrObjectType.FullName, clrObjectType.FullName)); } clrObjectType = newClrObjectType; } } else { // Default instance creation logic: use Activator to create the new CLR object from the type. clrObject = Activator.CreateInstance(clrObjectType); clrObjectInitialized = false; } // Cache the object before populating its properties as their values might refer to the object. this.convertedObjects[edmStructuredValue] = clrObject; if (!clrObjectInitialized && clrObject != null) { this.PopulateObjectProperties(edmStructuredValue, clrObject, clrObjectType); } return(clrObject); }
/// <summary> /// Converts <paramref name="edmValue"/> to a <see cref="System.Guid"/> value. /// </summary> /// <param name="edmValue">The EDM value to be converted.</param> /// <returns>Converted Guid.</returns> /// <exception cref="InvalidCastException">Exception is thrown if <paramref name="edmValue"/> is not <see cref="IEdmGuidValue"/>.</exception> internal static Guid AsClrGuid(IEdmValue edmValue) { EdmUtil.CheckArgumentNull(edmValue, "edmValue"); return(((IEdmGuidValue)edmValue).Value); }
/// <summary> /// Converts <paramref name="edmValue"/> to a <see cref="System.DateTimeOffset"/> value. /// </summary> /// <param name="edmValue">The EDM value to be converted.</param> /// <returns>Converted DateTimeOffset.</returns> /// <exception cref="InvalidCastException">Exception is thrown if <paramref name="edmValue"/> is not <see cref="IEdmDateTimeOffsetValue"/>.</exception> internal static DateTimeOffset AsClrDateTimeOffset(IEdmValue edmValue) { EdmUtil.CheckArgumentNull(edmValue, "edmValue"); return(((IEdmDateTimeOffsetValue)edmValue).Value); }
/// <summary> /// Converts <paramref name="edmValue"/> to a <see cref="System.TimeSpan"/> value. /// </summary> /// <param name="edmValue">The EDM value to be converted.</param> /// <returns>Converted Duration.</returns> /// <exception cref="InvalidCastException">Exception is thrown if <paramref name="edmValue"/> is not <see cref="IEdmDurationValue"/>.</exception> internal static TimeSpan AsClrDuration(IEdmValue edmValue) { EdmUtil.CheckArgumentNull(edmValue, "edmValue"); return(((IEdmDurationValue)edmValue).Value); }
/// <summary> /// Converts <paramref name="edmValue"/> to a <see cref="System.Decimal"/> value. /// </summary> /// <param name="edmValue">The EDM value to be converted.</param> /// <returns>Converted decimal.</returns> /// <exception cref="InvalidCastException">Exception is thrown if <paramref name="edmValue"/> is not <see cref="IEdmDecimalValue"/>.</exception> internal static decimal AsClrDecimal(IEdmValue edmValue) { EdmUtil.CheckArgumentNull(edmValue, "edmValue"); return(((IEdmDecimalValue)edmValue).Value); }
/// <summary> /// Converts <paramref name="edmValue"/> to a <see cref="Microsoft.OData.Edm.Date"/> value. /// </summary> /// <param name="edmValue">The EDM value to be converted.</param> /// <returns>Converted date.</returns> /// <exception cref="InvalidCastException">Exception is thrown if <paramref name="edmValue"/> is not <see cref="IEdmDateValue"/>.</exception> internal static Date AsClrDate(IEdmValue edmValue) { EdmUtil.CheckArgumentNull(edmValue, "edmValue"); return(((IEdmDateValue)edmValue).Value); }
/// <summary> /// Gets an annotation indicating whether the value of an enum member should be explicitly serialized. /// </summary> /// <param name="member">The member the annotation is on.</param> /// <param name="model">Model containing the member.</param> /// <returns>Whether the member should have its value serialized.</returns> public static bool?IsValueExplicit(this IEdmEnumMember member, IEdmModel model) { EdmUtil.CheckArgumentNull(member, "member"); EdmUtil.CheckArgumentNull(model, "model"); return(model.GetAnnotationValue(member, EdmConstants.InternalUri, CsdlConstants.IsEnumMemberValueExplicitAnnotation) as bool?); }
/// <summary> /// Returns true if this element contains errors returned by the <see cref="Errors"/> method. /// </summary> /// <param name="element">Reference to the calling object.</param> /// <returns>This element is an invalid element.</returns> public static bool IsBad(this IEdmElement element) { EdmUtil.CheckArgumentNull(element, "element"); return(element.Errors().FirstOrDefault() != null); }
/// <summary> /// Gets an annotation indicating if the value should be serialized as an element. /// </summary> /// <param name="value">Value the annotation is on.</param> /// <param name="model">Model containing the value.</param> /// <returns>Value indicating if the string should be serialized as an element.</returns> public static bool IsSerializedAsElement(this IEdmValue value, IEdmModel model) { EdmUtil.CheckArgumentNull(value, "value"); EdmUtil.CheckArgumentNull(model, "model"); return((model.GetAnnotationValue(value, EdmConstants.InternalUri, CsdlConstants.IsSerializedAsElementAnnotation) as bool?) ?? false); }
/// <summary> /// Gets the errors, if any, that belong to this element or elements that this element contains. For example errors for a structural type include the errors of the type itself and errors of its declared properties. /// The method does not analyze elements referenced by this element. For example errors of a property do not include errors from its type. /// </summary> /// <param name="element">Reference to the calling object.</param> /// <returns>Any errors that belong to this element or elements that element contains.</returns> public static IEnumerable <EdmError> Errors(this IEdmElement element) { EdmUtil.CheckArgumentNull(element, "element"); return(InterfaceValidator.GetStructuralErrors(element)); }
internal static string TargetString(this IEdmVocabularyAnnotation annotation) { return(EdmUtil.FullyQualifiedName(annotation.Target)); }
/// <summary> /// Gets the errors, if any, that belong to this type reference or its definition. /// </summary> /// <param name="type">The type reference.</param> /// <returns>Any errors that belong to this type reference or its definition.</returns> public static IEnumerable <EdmError> TypeErrors(this IEdmTypeReference type) { EdmUtil.CheckArgumentNull(type, "type"); return(InterfaceValidator.GetStructuralErrors(type).Concat(InterfaceValidator.GetStructuralErrors(type.Definition))); }
/// <summary> /// Sets a value of EDMX version attribute of the <paramref name="model"/>. /// </summary> /// <param name="model">The model the version should be set for.</param> /// <param name="version">The version.</param> public static void SetEdmxVersion(this IEdmModel model, Version version) { EdmUtil.CheckArgumentNull(model, "model"); model.SetAnnotationValue(model, EdmConstants.InternalUri, CsdlConstants.EdmxVersionAnnotation, version); }
/// <summary> /// Gets the preferred prefix mappings for xml namespaces from an IEdmModel /// </summary> /// <param name="model">Reference to the calling object.</param> /// <returns>Namespace prefixes that exist on the model.</returns> public static IEnumerable <KeyValuePair <string, string> > GetNamespacePrefixMappings(this IEdmModel model) { EdmUtil.CheckArgumentNull(model, "model"); return(model.GetAnnotationValue <IEnumerable <KeyValuePair <string, string> > >(model, EdmConstants.InternalUri, CsdlConstants.NamespacePrefixAnnotation)); }
private void AddSchema(CsdlSchema schema, bool addAnnotations) { CsdlSemanticsSchema schemaWrapper = new CsdlSemanticsSchema(this, schema); this.schemata.Add(schemaWrapper); foreach (IEdmSchemaType type in schemaWrapper.Types) { CsdlSemanticsStructuredTypeDefinition structuredType = type as CsdlSemanticsStructuredTypeDefinition; if (structuredType != null) { string baseTypeNamespace; string baseTypeName; string baseTypeFullName = ((CsdlNamedStructuredType)structuredType.Element).BaseTypeName; if (baseTypeFullName != null) { EdmUtil.TryGetNamespaceNameFromQualifiedName(baseTypeFullName, out baseTypeNamespace, out baseTypeName); if (baseTypeName != null) { List <IEdmStructuredType> derivedTypes; if (!this.derivedTypeMappings.TryGetValue(baseTypeName, out derivedTypes)) { derivedTypes = new List <IEdmStructuredType>(); this.derivedTypeMappings[baseTypeName] = derivedTypes; } // TODO: REF referenced derived types derivedTypes.Add(structuredType); } } } RegisterElement(type); } foreach (IEdmOperation function in schemaWrapper.Operations) { RegisterElement(function); } foreach (IEdmTerm valueTerm in schemaWrapper.Terms) { RegisterElement(valueTerm); } foreach (IEdmEntityContainer container in schemaWrapper.EntityContainers) { RegisterElement(container); } if (!string.IsNullOrEmpty(schema.Alias)) { this.SetNamespaceAlias(schema.Namespace, schema.Alias); } if (addAnnotations) { foreach (CsdlAnnotations schemaOutOfLineAnnotations in schema.OutOfLineAnnotations) { string target = schemaOutOfLineAnnotations.Target; string replaced = this.ReplaceAlias(target); if (replaced != null) { target = replaced; } List <CsdlSemanticsAnnotations> annotations; if (!this.outOfLineAnnotations.TryGetValue(target, out annotations)) { annotations = new List <CsdlSemanticsAnnotations>(); this.outOfLineAnnotations[target] = annotations; } annotations.Add(new CsdlSemanticsAnnotations(schemaWrapper, schemaOutOfLineAnnotations)); } } var edmVersion = this.GetEdmVersion(); if (edmVersion == null || edmVersion < schema.Version) { this.SetEdmVersion(schema.Version); } }
/// <summary> /// Sets a value for the MaxDataServiceVersion attribute in an EDMX artifact. /// </summary> /// <param name="model">The model the attribute should be set for.</param> /// <param name="version">The value of the attribute.</param> public static void SetMaxDataServiceVersion(this IEdmModel model, Version version) { EdmUtil.CheckArgumentNull(model, "model"); model.SetAnnotationValue(model, EdmConstants.InternalUri, EdmConstants.MaxDataServiceVersion, version); }
/// <summary> /// Initializes a new instance of the ValidationRuleSet class. /// </summary> /// <param name="baseSet">Ruleset whose rules should be contained in this set.</param> /// <param name="newRules">Additional rules to add to the set.</param> public ValidationRuleSet(IEnumerable <ValidationRule> baseSet, IEnumerable <ValidationRule> newRules) : this(EdmUtil.CheckArgumentNull(baseSet, "baseSet").Concat(EdmUtil.CheckArgumentNull(newRules, "newRules"))) { }
/// <summary> /// Gets the value for the MaxDataServiceVersion attribute used during EDMX serialization. /// </summary> /// <param name="model">Model the attribute has been set for</param> /// <returns>Value of the attribute.</returns> public static Version GetMaxDataServiceVersion(this IEdmModel model) { EdmUtil.CheckArgumentNull(model, "model"); return(model.GetAnnotationValue <Version>(model, EdmConstants.InternalUri, EdmConstants.MaxDataServiceVersion)); }
/// <summary> /// Determines if the type of an expression is compatible with the provided type /// </summary> /// <param name="expression">The expression to assert the type of.</param> /// <param name="type">The type to assert the expression as.</param> /// <param name="context">The context paths are to be evaluated in.</param> /// <param name="matchExactly">A value indicating whether the expression must match the asserted type exactly, or simply be compatible.</param> /// <param name="discoveredErrors">Errors produced if the expression does not match the specified type.</param> /// <returns>A value indicating whether the expression is valid for the given type or not.</returns> /// <remarks>If the expression has an associated type, this function will check that it matches the expected type and stop looking further. /// If an expression claims a type, it must be validated that the type is valid for the expression. If the expression does not claim a type /// this method will attempt to check the validity of the expression itself with the asserted type.</remarks> public static bool TryAssertType(this IEdmExpression expression, IEdmTypeReference type, IEdmType context, bool matchExactly, out IEnumerable <EdmError> discoveredErrors) { EdmUtil.CheckArgumentNull(expression, "expression"); // If we don't have a type to assert this passes vacuously. if (type == null || type.TypeKind() == EdmTypeKind.None) { discoveredErrors = Enumerable.Empty <EdmError>(); return(true); } switch (expression.ExpressionKind) { case EdmExpressionKind.IntegerConstant: case EdmExpressionKind.StringConstant: case EdmExpressionKind.BinaryConstant: case EdmExpressionKind.BooleanConstant: case EdmExpressionKind.DateTimeConstant: case EdmExpressionKind.DateTimeOffsetConstant: case EdmExpressionKind.DecimalConstant: case EdmExpressionKind.FloatingConstant: case EdmExpressionKind.GuidConstant: case EdmExpressionKind.TimeConstant: IEdmPrimitiveValue primitiveValue = (IEdmPrimitiveValue)expression; if (primitiveValue.Type != null) { return(TestTypeReferenceMatch(primitiveValue.Type, type, expression.Location(), matchExactly, out discoveredErrors)); } return(TryAssertPrimitiveAsType(primitiveValue, type, out discoveredErrors)); case EdmExpressionKind.Null: return(TryAssertNullAsType((IEdmNullExpression)expression, type, out discoveredErrors)); case EdmExpressionKind.Path: return(TryAssertPathAsType((IEdmPathExpression)expression, type, context, matchExactly, out discoveredErrors)); case EdmExpressionKind.FunctionApplication: IEdmApplyExpression applyExpression = (IEdmApplyExpression)expression; if (applyExpression.AppliedFunction != null) { IEdmFunctionBase function = applyExpression.AppliedFunction as IEdmFunctionBase; if (function != null) { return(TestTypeReferenceMatch(function.ReturnType, type, expression.Location(), matchExactly, out discoveredErrors)); } } // If we don't have the applied function we just assume that it will work. discoveredErrors = Enumerable.Empty <EdmError>(); return(true); case EdmExpressionKind.If: return(TryAssertIfAsType((IEdmIfExpression)expression, type, context, matchExactly, out discoveredErrors)); case EdmExpressionKind.IsType: return(TestTypeReferenceMatch(EdmCoreModel.Instance.GetBoolean(false), type, expression.Location(), matchExactly, out discoveredErrors)); case EdmExpressionKind.Record: IEdmRecordExpression recordExpression = (IEdmRecordExpression)expression; if (recordExpression.DeclaredType != null) { return(TestTypeReferenceMatch(recordExpression.DeclaredType, type, expression.Location(), matchExactly, out discoveredErrors)); } return(TryAssertRecordAsType(recordExpression, type, context, matchExactly, out discoveredErrors)); case EdmExpressionKind.Collection: IEdmCollectionExpression collectionExpression = (IEdmCollectionExpression)expression; if (collectionExpression.DeclaredType != null) { return(TestTypeReferenceMatch(collectionExpression.DeclaredType, type, expression.Location(), matchExactly, out discoveredErrors)); } return(TryAssertCollectionAsType(collectionExpression, type, context, matchExactly, out discoveredErrors)); case EdmExpressionKind.Labeled: return(TryAssertType(((IEdmLabeledExpression)expression).Expression, type, context, matchExactly, out discoveredErrors)); case EdmExpressionKind.AssertType: return(TestTypeReferenceMatch(((IEdmAssertTypeExpression)expression).Type, type, expression.Location(), matchExactly, out discoveredErrors)); case EdmExpressionKind.LabeledExpressionReference: return(TryAssertType(((IEdmLabeledExpressionReferenceExpression)expression).ReferencedLabeledExpression, type, out discoveredErrors)); default: discoveredErrors = new EdmError[] { new EdmError(expression.Location(), EdmErrorCode.ExpressionNotValidForTheAssertedType, Edm.Strings.EdmModel_Validator_Semantic_ExpressionNotValidForTheAssertedType) }; return(false); } }
/// <summary> /// Sets an annotation indicating whether the value of an enum member should be explicitly serialized. /// </summary> /// <param name="member">Member to set the annotation on.</param> /// <param name="model">Model containing the member.</param> /// <param name="isExplicit">If the value of the enum member should be explicitly serialized</param> public static void SetIsValueExplicit(this IEdmEnumMember member, IEdmModel model, bool?isExplicit) { EdmUtil.CheckArgumentNull(member, "member"); EdmUtil.CheckArgumentNull(model, "model"); model.SetAnnotationValue(member, EdmConstants.InternalUri, CsdlConstants.IsEnumMemberValueExplicitAnnotation, (object)isExplicit); }
/// <summary> /// Gets the schema an annotation should be serialized in. /// </summary> /// <param name="annotation">Reference to the calling annotation.</param> /// <param name="model">Model containing the annotation.</param> /// <returns>Name of the schema the annotation belongs to.</returns> public static string GetSchemaNamespace(this IEdmVocabularyAnnotation annotation, IEdmModel model) { EdmUtil.CheckArgumentNull(annotation, "annotation"); EdmUtil.CheckArgumentNull(model, "model"); return(model.GetAnnotationValue <string>(annotation, EdmConstants.InternalUri, CsdlConstants.SchemaNamespaceAnnotation)); }
/// <summary> /// Converts <paramref name="edmValue"/> to a <see cref="System.Double"/> value. /// </summary> /// <param name="edmValue">The EDM value to be converted.</param> /// <returns>Converted double.</returns> /// <exception cref="InvalidCastException">Exception is thrown if <paramref name="edmValue"/> is not <see cref="IEdmFloatingValue"/>.</exception> internal static Double AsClrDouble(IEdmValue edmValue) { EdmUtil.CheckArgumentNull(edmValue, "edmValue"); return(((IEdmFloatingValue)edmValue).Value); }