/// <summary> /// Parses a <paramref name="value"/> into an <see cref="IEdmExpression"/> value of the correct EDM type. /// </summary> /// <param name="edmType">The type of value.</param> /// <param name="value">The value to parse.</param> /// <returns> /// An IEdmExpression of type <paramref name="edmType" /> or null if the type is not supported/implemented. /// </returns> internal static IEdmExpression BuildEdmExpression(IEdmType edmType, string value) { EdmUtil.CheckArgumentNull(edmType, "edmType"); EdmUtil.CheckArgumentNull(value, "value"); EdmTypeKind termTypeKind = edmType.TypeKind; // Create expressions/constants for the corresponding types switch (termTypeKind) { case EdmTypeKind.Primitive: IEdmPrimitiveType primitiveTypeReference = (IEdmPrimitiveType)edmType; return(BuildEdmPrimitiveValueExp(primitiveTypeReference, value)); case EdmTypeKind.TypeDefinition: IEdmTypeDefinition typeDefinitionReference = (IEdmTypeDefinition)edmType; return(BuildEdmPrimitiveValueExp(typeDefinitionReference.UnderlyingType(), value)); case EdmTypeKind.Path: return(BuildEdmPathExp((IEdmPathType)edmType, value)); case EdmTypeKind.Enum: case EdmTypeKind.Complex: case EdmTypeKind.Entity: case EdmTypeKind.Collection: case EdmTypeKind.EntityReference: case EdmTypeKind.Untyped: default: throw new NotSupportedException(Strings.EdmVocabularyAnnotations_TermTypeNotSupported(edmType.FullTypeName())); } }
/// <summary> /// Returns an IEdmExpression for an EDM primitive value. /// </summary> /// <param name="typeReference">Reference to the type of term.</param> /// <param name="value">The value to parse.</param> /// <returns> /// Primitive expression for the value of the according type or null if the type is not supported/implemented. /// </returns> private static IEdmExpression BuildEdmPrimitiveValueExp(IEdmPrimitiveType typeReference, string value) { // From OData spec, the following primitive types are supported as Constant Expression switch (typeReference.PrimitiveKind) { case EdmPrimitiveTypeKind.Binary: if (EdmValueParser.TryParseBinary(value, out byte[] binary))
internal static object CoerceTemporalType(object primitiveValue, IEdmPrimitiveType targetEdmType) { // This is implemented to match TypePromotionUtils and MetadataUtilsCommon.CanConvertPrimitiveTypeTo() ExceptionUtils.CheckArgumentNotNull(primitiveValue, "primitiveValue"); ExceptionUtils.CheckArgumentNotNull(targetEdmType, "targetEdmType"); EdmPrimitiveTypeKind targetPrimitiveKind = targetEdmType.PrimitiveKind; switch (targetPrimitiveKind) { case EdmPrimitiveTypeKind.DateTimeOffset: if (primitiveValue is Date) { var dateValue = (Date)primitiveValue; return(new DateTimeOffset(dateValue.Year, dateValue.Month, dateValue.Day, 0, 0, 0, new TimeSpan(0))); } break; case EdmPrimitiveTypeKind.Date: var stringValue = primitiveValue as string; if (stringValue != null) { // Coerce to Date Type from String. return(PlatformHelper.ConvertStringToDate(stringValue)); } break; } return(null); }
/// <summary> /// Try getting the constant DateTimeOffset node value /// </summary> /// <param name="sourceNodeOrNull">The Node</param> /// <param name="primitiveValue">The out parameter if succeeds</param> /// <returns>true if the constant node is for date type</returns> internal static bool TryGetConstantNodePrimitiveDate(SingleValueNode sourceNodeOrNull, out object primitiveValue) { primitiveValue = null; ConstantNode constantNode = sourceNodeOrNull as ConstantNode; if (constantNode != null) { IEdmPrimitiveType primitiveType = constantNode.TypeReference.AsPrimitiveOrNull().Definition as IEdmPrimitiveType; if (primitiveType != null) { switch (primitiveType.PrimitiveKind) { case EdmPrimitiveTypeKind.DateTimeOffset: Date result; if (UriParser.UriUtils.TryUriStringToDate(constantNode.LiteralText, out result)) { primitiveValue = constantNode.LiteralText; return(true); } break; default: return(false); } } } return(false); }
public static IEdmTypeReference GetEdmTypeReference(this IEdmModel edmModel, Type clrType) { IEdmTypeReference edmTypeRef; bool nullable; Type underlyingType = Nullable.GetUnderlyingType(clrType); if (underlyingType == null) { nullable = clrType.IsClass; } else { clrType = underlyingType; nullable = true; } IEdmPrimitiveType primitiveEdmType = PrimitiveTypeHelper.GetPrimitiveType(clrType); if (primitiveEdmType == null) { var edmEnumType = (IEdmEnumType)edmModel.FindType(clrType.FullName); edmTypeRef = new EdmEnumTypeReference(edmEnumType, nullable); } else { edmTypeRef = EdmCoreModel.Instance.GetPrimitive(primitiveEdmType.PrimitiveKind, nullable); } return(edmTypeRef); }
public CsdlSemanticsPrimitiveTypeReference(CsdlSemanticsSchema schema, CsdlPrimitiveTypeReference reference) : base(reference) { this.schema = schema; this.Reference = reference; this.definition = EdmCoreModel.Instance.GetPrimitiveType(this.Reference.Kind); }
internal static IEdmTypeReference GetTypeReference(this IEdmType type, bool isNullable) { IEdmPrimitiveType primitiveType = type as IEdmPrimitiveType; if (primitiveType != null) { return(primitiveType.GetPrimitiveTypeReference(isNullable)); } IEdmComplexType complexType = type as IEdmComplexType; if (complexType != null) { return(new EdmComplexTypeReference(complexType, isNullable)); } IEdmEntityType entityType = type as IEdmEntityType; if (entityType != null) { return(new EdmEntityTypeReference(entityType, isNullable)); } IEdmEnumType enumType = type as IEdmEnumType; if (enumType != null) { return(new EdmEnumTypeReference(enumType, isNullable)); } throw new InvalidOperationException(Edm.Strings.EdmType_UnexpectedEdmType); }
private static Type ConvertPrimitiveEdmTypeToType(IEdmPrimitiveType edmType, bool isNullable) { switch (edmType.PrimitiveKind) { case EdmPrimitiveTypeKind.DateTimeOffset: return(isNullable ? typeof(DateTimeOffset?) : typeof(DateTimeOffset)); case EdmPrimitiveTypeKind.Decimal: return(isNullable ? typeof(decimal?) : typeof(decimal)); case EdmPrimitiveTypeKind.Double: return(isNullable ? typeof(double?) : typeof(double)); case EdmPrimitiveTypeKind.Int16: return(isNullable ? typeof(Int16?) : typeof(Int16)); case EdmPrimitiveTypeKind.Int32: return(isNullable ? typeof(Int32?) : typeof(Int32)); case EdmPrimitiveTypeKind.Int64: return(isNullable ? typeof(Int64?) : typeof(Int64)); case EdmPrimitiveTypeKind.String: return(typeof(string)); default: return(null); } }
public void GetPrimitiveClrTypeForBuiltInTypesShouldBeExpect(EdmPrimitiveTypeKind kind, bool nullable, Type expect) { IEdmPrimitiveType primitiveType = EdmCoreModel.Instance.GetPrimitiveType(kind); Type actual = EdmLibraryExtensions.GetPrimitiveClrType(primitiveType, nullable); actual.Should().Be(expect); }
/// <summary> /// Tries getting the constant node's primitive L M D F value (which can be converted to other primitive type while primitive AccessPropertyNode can't). /// </summary> /// <param name="sourceNodeOrNull">The Node</param> /// <param name="primitiveValue">THe out parameter if succeeds</param> /// <returns>true if the constant node is for long, float, double or decimal type</returns> internal static bool TryGetConstantNodePrimitiveLDMF(SingleValueNode sourceNodeOrNull, out object primitiveValue) { primitiveValue = null; if ((sourceNodeOrNull != null) && (sourceNodeOrNull is ConstantNode)) { ConstantNode tmp = (ConstantNode)sourceNodeOrNull; IEdmPrimitiveType primitiveType = tmp.TypeReference.AsPrimitiveOrNull().Definition as IEdmPrimitiveType; if (primitiveType != null) { switch (primitiveType.PrimitiveKind) { case EdmPrimitiveTypeKind.Int32: case EdmPrimitiveTypeKind.Int64: case EdmPrimitiveTypeKind.Single: case EdmPrimitiveTypeKind.Double: case EdmPrimitiveTypeKind.Decimal: primitiveValue = tmp.Value; return(true); default: return(false); } } } return(false); }
public CsdlSemanticsPrimitiveTypeReference(CsdlSemanticsSchema schema, CsdlPrimitiveTypeReference reference) : base(reference) { this.schema = schema; this.Reference = reference; this.definition = EdmCoreModel.Instance.GetPrimitiveType(this.Reference.Kind); }
/// <summary> /// Created a SingleValueCastNode with the given source node and the given type to cast to. /// </summary> /// <param name="source"> Source <see cref="SingleValueNode"/> that is being cast.</param> /// <param name="primitiveType">Type to cast to.</param> /// <exception cref="System.ArgumentNullException">Throws if the input primitiveType is null.</exception> public SingleValueCastNode(SingleValueNode source, IEdmPrimitiveType primitiveType) { ExceptionUtils.CheckArgumentNotNull(source, "source"); ExceptionUtils.CheckArgumentNotNull(primitiveType, "primitiveType"); this.source = source; this.primitiveTypeReference = new EdmPrimitiveTypeReference(primitiveType, true); }
/// <summary> /// Validates that the expected primitive type or type definition matches the actual primitive type. /// </summary> /// <param name="expectedTypeReference">The expected type.</param> /// <param name="typeReferenceFromValue">The actual type.</param> internal static void ValidateMetadataPrimitiveType(IEdmTypeReference expectedTypeReference, IEdmTypeReference typeReferenceFromValue) { Debug.Assert(expectedTypeReference != null && (expectedTypeReference.IsODataPrimitiveTypeKind() || expectedTypeReference.IsODataTypeDefinitionTypeKind()), "expectedTypeReference must be a primitive type or type definition."); Debug.Assert(typeReferenceFromValue != null && typeReferenceFromValue.IsODataPrimitiveTypeKind(), "typeReferenceFromValue must be a primitive type."); IEdmType expectedType = expectedTypeReference.Definition; IEdmPrimitiveType typeFromValue = (IEdmPrimitiveType)typeReferenceFromValue.Definition; // The two primitive types match if they have the same definition and either both or only the // expected type is nullable // NOTE: for strings and binary values we must not check nullability here because the type reference // from the value is always nullable since C# has no way to express non-nullable strings. // However, this codepath is only hit if the value is not 'null' so we can assign a non-null // value to both nullable and non-nullable string/binary types. bool nullableCompatible = expectedTypeReference.IsNullable == typeReferenceFromValue.IsNullable || expectedTypeReference.IsNullable && !typeReferenceFromValue.IsNullable || !MetadataUtilsCommon.IsODataValueType(typeReferenceFromValue); bool typeCompatible = expectedType.IsAssignableFrom(typeFromValue); if (!nullableCompatible || !typeCompatible) { // incompatible type name for value! throw new ODataException(Strings.ValidationUtils_IncompatiblePrimitiveItemType( typeReferenceFromValue.FullName(), typeReferenceFromValue.IsNullable, expectedTypeReference.FullName(), expectedTypeReference.IsNullable)); } }
/// <summary> /// Attempts to find a pre-configured structural type or a primitive type or an enum type that matches the T. /// If no matches are found NULL is returned. /// </summary> public IEdmTypeConfiguration GetTypeConfigurationOrNull(Type type) { if (_primitiveTypes.ContainsKey(type)) { return(_primitiveTypes[type]); } else { IEdmPrimitiveType edmType = EdmLibHelpers.GetEdmPrimitiveTypeOrNull(type); PrimitiveTypeConfiguration primitiveType = null; if (edmType != null) { primitiveType = new PrimitiveTypeConfiguration(this, edmType, type); _primitiveTypes[type] = primitiveType; return(primitiveType); } else if (_structuralTypes.ContainsKey(type)) { return(_structuralTypes[type]); } else if (_enumTypes.ContainsKey(type)) { return(_enumTypes[type]); } } return(null); }
private IEdmPrimitiveTypeReference GetFacetlessEdmTypeReference(PrimitiveDataType dataType) { IEdmPrimitiveType typeDefinition = this.GetEdmTypeDefinition(dataType); var typeReference = new EdmPrimitiveTypeReference(typeDefinition, dataType.IsNullable); return(typeReference); }
internal static object CoerceTemporalType(object primitiveValue, IEdmPrimitiveType targetEdmType) { // This is implemented to match TypePromotionUtils and MetadataUtilsCommon.CanConvertPrimitiveTypeTo() ExceptionUtils.CheckArgumentNotNull(primitiveValue, "primitiveValue"); ExceptionUtils.CheckArgumentNotNull(targetEdmType, "targetEdmType"); EdmPrimitiveTypeKind targetPrimitiveKind = targetEdmType.PrimitiveKind; if (targetPrimitiveKind == EdmPrimitiveTypeKind.Date) { if (primitiveValue is DateTimeOffset) { // Coerce to Date Type from DateTimeOffset. var dtoValue = (DateTimeOffset)primitiveValue; return(new Date(dtoValue.Year, dtoValue.Month, dtoValue.Day)); } var stringValue = primitiveValue as String; if (stringValue != null) { // Coerce to Date Type from String. return(PlatformHelper.ConvertStringToDate(stringValue)); } } return(null); }
public static IEdmType GetEdmType(this IEdmModel edmModel, Type clrType) { if (edmModel == null) { throw Error.ArgumentNull("edmModel"); } if (clrType == null) { throw Error.ArgumentNull("clrType"); } IEdmPrimitiveType primitiveType = GetEdmPrimitiveTypeOrNull(clrType); if (primitiveType != null) { return(primitiveType); } else { Type enumerableOfT = ExtractGenericInterface(clrType, typeof(IEnumerable <>)); if (enumerableOfT != null) { Type elementClrType = enumerableOfT.GetGenericArguments()[0]; // IEnumerable<SelectExpandWrapper<T>> is a collection of T. if (elementClrType.IsGenericType && elementClrType.GetGenericTypeDefinition() == typeof(SelectExpandWrapper <>)) { elementClrType = elementClrType.GetGenericArguments()[0]; } IEdmTypeReference elementType = GetEdmTypeReference(edmModel, elementClrType); if (elementType != null) { return(new EdmCollectionType(elementType)); } } // search for the ClrTypeAnnotation and return it if present IEdmType returnType = edmModel .SchemaElements .OfType <IEdmType>() .Select(edmType => new { EdmType = edmType, Annotation = edmModel.GetAnnotationValue <ClrTypeAnnotation>(edmType) }) .Where(tuple => tuple.Annotation != null && tuple.Annotation.ClrType == clrType) .Select(tuple => tuple.EdmType) .SingleOrDefault(); // default to the EdmType with the same name as the ClrType name returnType = returnType ?? edmModel.FindType(clrType.EdmFullName()); if (clrType.BaseType != null) { // go up the inheritance tree to see if we have a mapping defined for the base type. returnType = returnType ?? edmModel.GetEdmType(clrType.BaseType); } return(returnType); } }
private static string GetPrimitiveTypeAndFormat(IEdmPrimitiveType primitiveType, out string format) { Contract.Requires(primitiveType != null); format = null; switch (primitiveType.PrimitiveKind) { case EdmPrimitiveTypeKind.String: case EdmPrimitiveTypeKind.None: return("string"); case EdmPrimitiveTypeKind.Int16: case EdmPrimitiveTypeKind.Int32: format = "int32"; return("integer"); case EdmPrimitiveTypeKind.Int64: format = "int64"; return("integer"); case EdmPrimitiveTypeKind.Boolean: return("boolean"); case EdmPrimitiveTypeKind.Byte: format = "byte"; return("string"); case EdmPrimitiveTypeKind.Date: format = "date"; return("string"); case EdmPrimitiveTypeKind.DateTimeOffset: format = "date-time"; return("string"); case EdmPrimitiveTypeKind.Double: format = "double"; return("number"); case EdmPrimitiveTypeKind.Decimal: format = "decimal"; return("number"); case EdmPrimitiveTypeKind.Single: format = "float"; return("number"); case EdmPrimitiveTypeKind.Guid: format = "guid"; return("string"); case EdmPrimitiveTypeKind.Binary: format = "binary"; return("string"); default: return("string"); } }
private IEdmPrimitiveType GetEdmTypeDefinition(PrimitiveDataType dataType) { string edmTypeName = dataType.GetFacet <EdmTypeNameFacet>().Value; EdmPrimitiveTypeKind primitiveTypeKind = (EdmPrimitiveTypeKind)Enum.Parse(typeof(EdmPrimitiveTypeKind), edmTypeName, false); IEdmPrimitiveType edmIntTypeDefinition = EdmCoreModel.Instance.GetPrimitiveType(primitiveTypeKind); return(edmIntTypeDefinition); }
public static OePropertyAccessor CreatePropertyAccessor(IEdmProperty edmProperty, Expression expression, ParameterExpression parameter) { UnaryExpression instance = Expression.Convert(expression, typeof(Object)); var func = (Func <Object, Object>)Expression.Lambda(instance, parameter).Compile(); IEdmPrimitiveType primitiveType = PrimitiveTypeHelper.GetPrimitiveType(expression.Type); return(new OePropertyAccessor(edmProperty, func)); }
public CustomEnumType(string namespaceName, string name, EdmTypeKind typeKind) { this.typeKind = typeKind; this.name = name; this.namespaceName = namespaceName; this.isFlags = false; this.underlyingType = EdmCoreModel.Instance.GetPrimitiveType(EdmPrimitiveTypeKind.Int32); }
public static IEdmPrimitiveTypeReference GetEdmPrimitiveTypeReferenceOrNull(Type clrType) { IEdmPrimitiveType primitiveType = GetEdmPrimitiveTypeOrNull(clrType); return(primitiveType != null ? _coreModel.GetPrimitive(primitiveType.PrimitiveKind, IsNullable(clrType)) : null); }
public CustomEnumType(string namespaceName, string name, IEdmPrimitiveType underlyingType) { this.typeKind = EdmTypeKind.Enum; this.name = name; this.namespaceName = namespaceName; this.isFlags = false; this.underlyingType = underlyingType; }
private static bool IsEquivalentTo(this IEdmPrimitiveType thisType, IEdmPrimitiveType otherType) { // ODataLib creates one-off instances of primitive type definitions that match by name and kind, but have different object refs. // So we can't use object ref comparison here like for other IEdmSchemaType objects. // See "src\Web\Client\System\Data\Services\Client\Serialization\PrimitiveType.cs:CreateEdmPrimitiveType()" for more info. return(thisType.PrimitiveKind == otherType.PrimitiveKind && thisType.FullName() == otherType.FullName()); }
private string EdmToClr(IEdmPrimitiveType type) { var kind = type.PrimitiveKind; return(ClrDictionary.ContainsKey(kind) ? ClrDictionary[kind] : kind.ToString()); }
/// <summary> /// Returns the instance type for primitive Edm types or null if none exists. /// </summary> /// <param name="type">The type to get the instance type for.</param> /// <returns>The instance type for the <paramref name="typeReference"/> or null if no instance type exists.</returns> private static Type GetPrimitiveInstanceType(IEdmPrimitiveType type, bool isNullable) { if (type == null) { throw new ArgumentNullException("type"); } return(GetPrimitiveClrType(type.PrimitiveKind, isNullable)); }
/// <summary> /// Get Open Api Data type kind. /// </summary> /// <param name="primitiveType">The primitive type.</param> /// <returns>The Open Api Data type kind.</returns> public static OpenApiDataTypeKind GetOpenApiDataType(this IEdmPrimitiveType typeReference) { if (typeReference == null) { return(OpenApiDataTypeKind.None); } return(GetOpenApiDataType(typeReference.PrimitiveKind)); }
/// <summary> /// Validate that the expected and actual type names and type kinds are compatible. /// </summary> /// <param name="collectionItemTypeName">The actual type name.</param> /// <param name="collectionItemTypeKind">The actual type kind.</param> private void ValidateCollectionItemTypeNameAndKind(string collectionItemTypeName, EdmTypeKind collectionItemTypeKind) { // Compare the item type kinds. if (this.itemTypeKind != collectionItemTypeKind) { throw new ODataException(Strings.CollectionWithoutExpectedTypeValidator_IncompatibleItemTypeKind(collectionItemTypeKind, this.itemTypeKind)); } if (this.itemTypeKind == EdmTypeKind.Primitive) { Debug.Assert(this.primitiveItemType != null, "this.primitiveItemType != null"); Debug.Assert(collectionItemTypeName != null, "collectionItemTypeName != null"); // NOTE: we do support type inheritance for spatial primitive types; otherwise the type names have to match. if (!string.IsNullOrEmpty(this.itemTypeName) && this.itemTypeName.Equals(collectionItemTypeName, System.StringComparison.OrdinalIgnoreCase)) { return; } if (this.primitiveItemType.IsSpatial()) { EdmPrimitiveTypeKind collectionItemPrimitiveKind = EdmCoreModel.Instance.GetPrimitiveTypeKind(collectionItemTypeName); IEdmPrimitiveType collectionItemPrimitiveType = EdmCoreModel.Instance.GetPrimitiveType(collectionItemPrimitiveKind); if (this.itemTypeDerivedFromCollectionValue) { // If the collection defines an item type, the collection item type has to be assignable to it. if (this.primitiveItemType.IsAssignableFrom(collectionItemPrimitiveType)) { return; } } else { // If the collection does not define an item type, the collection items must have a common base type. IEdmPrimitiveType commonBaseType = EdmLibraryExtensions.GetCommonBaseType(this.primitiveItemType, collectionItemPrimitiveType); if (commonBaseType != null) { this.primitiveItemType = commonBaseType; this.itemTypeName = commonBaseType.FullTypeName(); return; } } } throw new ODataException(Strings.CollectionWithoutExpectedTypeValidator_IncompatibleItemTypeName(collectionItemTypeName, this.itemTypeName)); } else { // Since we do not support type inheritance for complex types, comparison of the type names is sufficient if (string.CompareOrdinal(this.itemTypeName, collectionItemTypeName) != 0) { throw new ODataException(Strings.CollectionWithoutExpectedTypeValidator_IncompatibleItemTypeName(collectionItemTypeName, this.itemTypeName)); } } }
private static IEdmType GetEdmType(IEdmModel edmModel, Type clrType, bool testCollections) { Contract.Assert(edmModel != null); Contract.Assert(clrType != null); IEdmPrimitiveType primitiveType = GetEdmPrimitiveTypeOrNull(clrType); if (primitiveType != null) { return(primitiveType); } else { if (testCollections) { Type enumerableOfT = ExtractGenericInterface(clrType, typeof(IEnumerable <>)); if (enumerableOfT != null) { Type elementClrType = enumerableOfT.GetGenericArguments()[0]; // IEnumerable<SelectExpandWrapper<T>> is a collection of T. Type entityType; if (IsSelectExpandWrapper(elementClrType, out entityType)) { elementClrType = entityType; } IEdmType elementType = GetEdmType(edmModel, elementClrType, testCollections: false); if (elementType != null) { return(new EdmCollectionType(elementType.ToEdmTypeReference(IsNullable(elementClrType)))); } } } // search for the ClrTypeAnnotation and return it if present IEdmType returnType = edmModel .SchemaElements .OfType <IEdmType>() .Select(edmType => new { EdmType = edmType, Annotation = edmModel.GetAnnotationValue <ClrTypeAnnotation>(edmType) }) .Where(tuple => tuple.Annotation != null && tuple.Annotation.ClrType == clrType) .Select(tuple => tuple.EdmType) .SingleOrDefault(); // default to the EdmType with the same name as the ClrType name returnType = returnType ?? edmModel.FindType(clrType.EdmFullName()); if (clrType.BaseType != null) { // go up the inheritance tree to see if we have a mapping defined for the base type. returnType = returnType ?? GetEdmType(edmModel, clrType.BaseType, testCollections); } return(returnType); } }
/// <summary> /// Initializes a new instance of the <see cref="EdmTypeDefinition"/> class. /// </summary> /// <param name="namespaceName">Namespace this type definition belongs to.</param> /// <param name="name">Name of this type definition.</param> /// <param name="underlyingType">The underlying type of this type definition.</param> public EdmTypeDefinition(string namespaceName, string name, IEdmPrimitiveType underlyingType) { EdmUtil.CheckArgumentNull(underlyingType, "underlyingType"); EdmUtil.CheckArgumentNull(namespaceName, "namespaceName"); EdmUtil.CheckArgumentNull(name, "name"); this.underlyingType = underlyingType; this.name = name; this.namespaceName = namespaceName; }
IEdmPrimitiveTypeReference IPrimitiveDataTypeVisitor <IEdmPrimitiveTypeReference> .Visit(StreamDataType dataType) { IEdmPrimitiveType typeDefinition = this.GetEdmTypeDefinition(dataType); var typeReference = new EdmPrimitiveTypeReference( typeDefinition, dataType.IsNullable); return(typeReference); }
/// <summary> /// Initializes a new instance of the <see cref="EdmTypeDefinition"/> class. /// </summary> /// <param name="namespaceName">Namespace this type definition belongs to.</param> /// <param name="name">Name of this type definition.</param> /// <param name="underlyingType">The underlying type of this type definition.</param> public EdmTypeDefinition(string namespaceName, string name, IEdmPrimitiveType underlyingType) { EdmUtil.CheckArgumentNull(underlyingType, "underlyingType"); EdmUtil.CheckArgumentNull(namespaceName, "namespaceName"); EdmUtil.CheckArgumentNull(name, "name"); this.underlyingType = underlyingType; this.name = name; this.namespaceName = namespaceName; }
/// <summary> /// Initializes a new instance of the <see cref="EdmBinaryTypeReference"/> class. /// </summary> /// <param name="definition">The type this reference refers to.</param> /// <param name="isNullable">Denotes whether the type can be nullable.</param> /// <param name="isUnbounded">Denotes whether the max length is the maximum allowed value.</param> /// <param name="maxLength">Maximum length of a value of this type.</param> public EdmBinaryTypeReference(IEdmPrimitiveType definition, bool isNullable, bool isUnbounded, int? maxLength) : base(definition, isNullable) { if (isUnbounded && maxLength != null) { throw new InvalidOperationException(Edm.Strings.EdmModel_Validator_Semantic_IsUnboundedCannotBeTrueWhileMaxLengthIsNotNull); } this.isUnbounded = isUnbounded; this.maxLength = maxLength; }
/// <summary> /// Initializes a new instance of the EdmEnumTypeWithDelayLoadedMembers class. /// </summary> /// <param name="namespaceName">Namespace the enumeration belongs to.</param> /// <param name="name">Name of the enumeration Type.</param> /// <param name="underlyingType">The enum's underlying type, one of Edm.Byte, Edm.SByte, Edm.Int16, Edm.Int32, or Edm.Int64.</param> /// <param name="isFlags">The isFlags or not</param> /// <param name="memberLoadAction">An action that is used to create the members for this enum type.</param> internal EdmEnumTypeWithDelayLoadedMembers( string namespaceName, string name, IEdmPrimitiveType underlyingType, bool isFlags, Action<EdmEnumTypeWithDelayLoadedMembers> memberLoadAction) : base(namespaceName, name, underlyingType, isFlags) { Debug.Assert(memberLoadAction != null, "memberLoadAction != null"); this.memberLoadAction = memberLoadAction; }
/// <summary> /// Initializes a new instance of the <see cref="EdmEnumType"/> class. /// </summary> /// <param name="namespaceName">Namespace this enumeration type belongs to.</param> /// <param name="name">Name of this enumeration type.</param> /// <param name="underlyingType">The underlying type of this enumeration type.</param> /// <param name="isFlags">A value indicating whether the enumeration type can be treated as a bit field.</param> public EdmEnumType(string namespaceName, string name, IEdmPrimitiveType underlyingType, bool isFlags) { EdmUtil.CheckArgumentNull(underlyingType, "underlyingType"); EdmUtil.CheckArgumentNull(namespaceName, "namespaceName"); EdmUtil.CheckArgumentNull(name, "name"); this.underlyingType = underlyingType; this.name = name; this.namespaceName = namespaceName; this.isFlags = isFlags; }
private static EdmTypeKind ComputeExpectedTypeKind(string typeName, out IEdmPrimitiveType primitiveItemType) { IEdmSchemaType type = EdmCoreModel.Instance.FindDeclaredType(typeName); if (type != null) { primitiveItemType = (IEdmPrimitiveType) type; return EdmTypeKind.Primitive; } primitiveItemType = null; return EdmTypeKind.Complex; }
public EdmBinaryTypeReference(IEdmPrimitiveType definition, bool isNullable, bool isUnbounded, int? maxLength, bool? isFixedLength) : base(definition, isNullable) { if (!isUnbounded || !maxLength.HasValue) { this.isUnbounded = isUnbounded; this.maxLength = maxLength; this.isFixedLength = isFixedLength; return; } else { throw new InvalidOperationException(Strings.EdmModel_Validator_Semantic_IsUnboundedCannotBeTrueWhileMaxLengthIsNotNull); } }
/// <summary> /// This constructor is public only for unit testing purposes. /// To get a PrimitiveTypeConfiguration use ODataModelBuilder.GetTypeConfigurationOrNull(Type) /// </summary> public PrimitiveTypeConfiguration(ODataModelBuilder builder, IEdmPrimitiveType edmType, Type clrType) { if (builder == null) { throw Error.ArgumentNull("builder"); } if (edmType == null) { throw Error.ArgumentNull("edmType"); } if (clrType == null) { throw Error.ArgumentNull("clrType"); } _builder = builder; _clrType = clrType; _edmType = edmType; }
public ODataCollectionSerializerTests() { _model = SerializationTestsHelpers.SimpleCustomerOrderModel(); _customerSet = _model.FindDeclaredEntityContainer("Default.Container").FindEntitySet("Customers"); _edmIntType = _model.FindType("Edm.Int32") as IEdmPrimitiveType; _customer = new Customer() { FirstName = "Foo", LastName = "Bar", ID = 10, }; ODataSerializerProvider serializerProvider = new DefaultODataSerializerProvider(); _serializer = new ODataCollectionSerializer( new EdmCollectionTypeReference( new EdmCollectionType( new EdmPrimitiveTypeReference(_edmIntType, isNullable: false)), isNullable: false), serializerProvider); }
internal void ValidateCollectionItem(string collectionItemTypeName, EdmTypeKind collectionItemTypeKind) { if ((collectionItemTypeKind != EdmTypeKind.Primitive) && (collectionItemTypeKind != EdmTypeKind.Complex)) { throw new ODataException(Microsoft.Data.OData.Strings.CollectionWithoutExpectedTypeValidator_InvalidItemTypeKind(collectionItemTypeKind)); } if (this.itemTypeDerivedFromCollectionValue) { collectionItemTypeName = collectionItemTypeName ?? this.itemTypeName; this.ValidateCollectionItemTypeNameAndKind(collectionItemTypeName, collectionItemTypeKind); } else { if (this.itemTypeKind == EdmTypeKind.None) { this.itemTypeKind = (collectionItemTypeName == null) ? collectionItemTypeKind : ComputeExpectedTypeKind(collectionItemTypeName, out this.primitiveItemType); if (collectionItemTypeName == null) { this.itemTypeKind = collectionItemTypeKind; if (this.itemTypeKind == EdmTypeKind.Primitive) { this.itemTypeName = "Edm.String"; this.primitiveItemType = EdmCoreModel.Instance.GetString(false).PrimitiveDefinition(); } else { this.itemTypeName = null; this.primitiveItemType = null; } } else { this.itemTypeKind = ComputeExpectedTypeKind(collectionItemTypeName, out this.primitiveItemType); this.itemTypeName = collectionItemTypeName; } } if ((collectionItemTypeName == null) && (collectionItemTypeKind == EdmTypeKind.Primitive)) { collectionItemTypeName = "Edm.String"; } this.ValidateCollectionItemTypeNameAndKind(collectionItemTypeName, collectionItemTypeKind); } }
public EdmSpatialTypeReference(IEdmPrimitiveType definition, bool isNullable) : this(definition, isNullable, null) { EdmUtil.CheckArgumentNull<IEdmPrimitiveType>(definition, "definition"); EdmPrimitiveTypeKind primitiveKind = definition.PrimitiveKind; switch (primitiveKind) { case EdmPrimitiveTypeKind.Geography: case EdmPrimitiveTypeKind.GeographyPoint: case EdmPrimitiveTypeKind.GeographyLineString: case EdmPrimitiveTypeKind.GeographyPolygon: case EdmPrimitiveTypeKind.GeographyCollection: case EdmPrimitiveTypeKind.GeographyMultiPolygon: case EdmPrimitiveTypeKind.GeographyMultiLineString: case EdmPrimitiveTypeKind.GeographyMultiPoint: { this.spatialReferenceIdentifier = new int?(0x10e6); return; } } this.spatialReferenceIdentifier = new int?(0); }
/// <summary> /// Initializes a new instance of the <see cref="EdmSpatialTypeReference"/> class. /// </summary> /// <param name="definition">The type this reference refers to.</param> /// <param name="isNullable">Denotes whether the type can be nullable.</param> public EdmSpatialTypeReference(IEdmPrimitiveType definition, bool isNullable) : this(definition, isNullable, null) { EdmUtil.CheckArgumentNull(definition, "definition"); switch (definition.PrimitiveKind) { case EdmPrimitiveTypeKind.Geography: case EdmPrimitiveTypeKind.GeographyPoint: case EdmPrimitiveTypeKind.GeographyLineString: case EdmPrimitiveTypeKind.GeographyPolygon: case EdmPrimitiveTypeKind.GeographyCollection: case EdmPrimitiveTypeKind.GeographyMultiPolygon: case EdmPrimitiveTypeKind.GeographyMultiLineString: case EdmPrimitiveTypeKind.GeographyMultiPoint: this.spatialReferenceIdentifier = CsdlConstants.Default_SpatialGeographySrid; break; // In the case of a BadSpatialTypeReference, the PrimitiveTypeKind is none, and we will treat that the same as Geometry. default: this.spatialReferenceIdentifier = CsdlConstants.Default_SpatialGeometrySrid; break; } }
/// <summary> /// Gets a reference to a primitive kind definition of the appropriate kind. /// </summary> /// <param name="primitiveType">Primitive type to create a reference for.</param> /// <param name="nullable">Flag specifying if the referenced type should be nullable per default.</param> /// <returns>A new primitive type reference.</returns> private static EdmPrimitiveTypeReference ToTypeReference(IEdmPrimitiveType primitiveType, bool nullable) { EdmPrimitiveTypeKind kind = primitiveType.PrimitiveKind; switch (kind) { case EdmPrimitiveTypeKind.Boolean: case EdmPrimitiveTypeKind.Byte: case EdmPrimitiveTypeKind.Double: case EdmPrimitiveTypeKind.Guid: case EdmPrimitiveTypeKind.Int16: case EdmPrimitiveTypeKind.Int32: case EdmPrimitiveTypeKind.Int64: case EdmPrimitiveTypeKind.SByte: case EdmPrimitiveTypeKind.Single: case EdmPrimitiveTypeKind.Stream: return new EdmPrimitiveTypeReference(primitiveType, nullable); case EdmPrimitiveTypeKind.Binary: return new EdmBinaryTypeReference(primitiveType, nullable); case EdmPrimitiveTypeKind.String: return new EdmStringTypeReference(primitiveType, nullable); case EdmPrimitiveTypeKind.Decimal: return new EdmDecimalTypeReference(primitiveType, nullable); case EdmPrimitiveTypeKind.DateTimeOffset: case EdmPrimitiveTypeKind.Duration: return new EdmTemporalTypeReference(primitiveType, nullable); case EdmPrimitiveTypeKind.Geography: case EdmPrimitiveTypeKind.GeographyPoint: case EdmPrimitiveTypeKind.GeographyLineString: case EdmPrimitiveTypeKind.GeographyPolygon: case EdmPrimitiveTypeKind.GeographyCollection: case EdmPrimitiveTypeKind.GeographyMultiPolygon: case EdmPrimitiveTypeKind.GeographyMultiLineString: case EdmPrimitiveTypeKind.GeographyMultiPoint: case EdmPrimitiveTypeKind.Geometry: case EdmPrimitiveTypeKind.GeometryPoint: case EdmPrimitiveTypeKind.GeometryLineString: case EdmPrimitiveTypeKind.GeometryPolygon: case EdmPrimitiveTypeKind.GeometryMultiPolygon: case EdmPrimitiveTypeKind.GeometryMultiLineString: case EdmPrimitiveTypeKind.GeometryMultiPoint: case EdmPrimitiveTypeKind.GeometryCollection: return new EdmSpatialTypeReference(primitiveType, nullable); default: throw new TaupoNotSupportedException("Invalid primitive type kind: " + kind.ToString()); } }
/// <summary> /// Gets a reference to a primitive kind definition of the appropriate kind. /// </summary> /// <param name="primitiveType">Primitive type to create a reference for.</param> /// <param name="nullable">Flag specifying if the referenced type should be nullable per default.</param> /// <returns>A new primitive type reference.</returns> private static EdmPrimitiveTypeReference ToTypeReference(IEdmPrimitiveType primitiveType, bool nullable) { EdmPrimitiveTypeKind kind = primitiveType.PrimitiveKind; switch (kind) { case EdmPrimitiveTypeKind.Boolean: case EdmPrimitiveTypeKind.Byte: case EdmPrimitiveTypeKind.Double: case EdmPrimitiveTypeKind.Guid: case EdmPrimitiveTypeKind.Int16: case EdmPrimitiveTypeKind.Int32: case EdmPrimitiveTypeKind.Int64: case EdmPrimitiveTypeKind.SByte: case EdmPrimitiveTypeKind.Single: case EdmPrimitiveTypeKind.Stream: return new EdmPrimitiveTypeReference(primitiveType, nullable); case EdmPrimitiveTypeKind.Binary: return new EdmBinaryTypeReference(primitiveType, nullable); case EdmPrimitiveTypeKind.String: return new EdmStringTypeReference(primitiveType, nullable); case EdmPrimitiveTypeKind.Decimal: return new EdmDecimalTypeReference(primitiveType, nullable); case EdmPrimitiveTypeKind.DateTime: case EdmPrimitiveTypeKind.DateTimeOffset: case EdmPrimitiveTypeKind.Time: return new EdmTemporalTypeReference(primitiveType, nullable); case EdmPrimitiveTypeKind.Geography: case EdmPrimitiveTypeKind.GeographyPoint: case EdmPrimitiveTypeKind.GeographyLineString: case EdmPrimitiveTypeKind.GeographyPolygon: case EdmPrimitiveTypeKind.GeographyCollection: case EdmPrimitiveTypeKind.GeographyMultiPolygon: case EdmPrimitiveTypeKind.GeographyMultiLineString: case EdmPrimitiveTypeKind.GeographyMultiPoint: case EdmPrimitiveTypeKind.Geometry: case EdmPrimitiveTypeKind.GeometryPoint: case EdmPrimitiveTypeKind.GeometryLineString: case EdmPrimitiveTypeKind.GeometryPolygon: case EdmPrimitiveTypeKind.GeometryMultiPolygon: case EdmPrimitiveTypeKind.GeometryMultiLineString: case EdmPrimitiveTypeKind.GeometryMultiPoint: case EdmPrimitiveTypeKind.GeometryCollection: return new EdmSpatialTypeReference(primitiveType, nullable); default: throw new ODataException(Strings.General_InternalError(InternalErrorCodesCommon.EdmLibraryExtensions_PrimitiveTypeReference)); } }
/// <summary> /// Initializes a new instance of the <see cref="EdmSpatialTypeReference"/> class. /// </summary> /// <param name="definition">The type this reference refers to.</param> /// <param name="isNullable">Denotes whether the type can be nullable.</param> /// <param name="spatialReferenceIdentifier">Spatial Reference Identifier for the spatial type being created.</param> public EdmSpatialTypeReference(IEdmPrimitiveType definition, bool isNullable, int? spatialReferenceIdentifier) : base(definition, isNullable) { this.spatialReferenceIdentifier = spatialReferenceIdentifier; }
public EdmTemporalTypeReference(IEdmPrimitiveType definition, bool isNullable) : this(definition, isNullable, null) { }
private static object GetPrimitiveValue(IValuedNode valuedNode, IEdmPrimitiveType targetType, bool asNullable) { // TODO: Some sort of cast to nullable when necessary switch (targetType.PrimitiveKind) { case EdmPrimitiveTypeKind.Boolean: return valuedNode.AsBoolean(); case EdmPrimitiveTypeKind.Byte: return (byte) valuedNode.AsInteger(); case EdmPrimitiveTypeKind.DateTime: return valuedNode.AsDateTime(); case EdmPrimitiveTypeKind.Decimal: return valuedNode.AsDecimal(); case EdmPrimitiveTypeKind.Double: return valuedNode.AsDouble(); case EdmPrimitiveTypeKind.Int16: return (Int16) valuedNode.AsInteger(); case EdmPrimitiveTypeKind.Int32: return (Int32) valuedNode.AsInteger(); case EdmPrimitiveTypeKind.Int64: return valuedNode.AsInteger(); case EdmPrimitiveTypeKind.String: return valuedNode.AsString(); case EdmPrimitiveTypeKind.DateTimeOffset: return valuedNode.AsDateTime(); default: throw new NotSupportedException( String.Format("Support for primitive type {0} has not been implemented yet", targetType.PrimitiveKind)); } }
/// <summary> /// Computes the expected type kind of an item from the type name read from the payload. /// </summary> /// <param name="typeName">The type name to compute the type kind from.</param> /// <param name="primitiveItemType">The primitive type for the specified type name or null if the type name is not a valid primitve type.</param> /// <returns>The <see cref="EdmTypeKind"/> of the type with the specified <paramref name="typeName"/>.</returns> private static EdmTypeKind ComputeExpectedTypeKind(string typeName, out IEdmPrimitiveType primitiveItemType) { IEdmSchemaType knownType = EdmCoreModel.Instance.FindDeclaredType(typeName); if (knownType != null) { Debug.Assert(knownType.TypeKind == EdmTypeKind.Primitive, "Only primitive types should be resolved by the core model."); primitiveItemType = (IEdmPrimitiveType)knownType; return EdmTypeKind.Primitive; } primitiveItemType = null; return EdmTypeKind.Complex; }
private void ValidateCollectionItemTypeNameAndKind(string collectionItemTypeName, EdmTypeKind collectionItemTypeKind) { if (this.itemTypeKind != collectionItemTypeKind) { throw new ODataException(Microsoft.Data.OData.Strings.CollectionWithoutExpectedTypeValidator_IncompatibleItemTypeKind(collectionItemTypeKind, this.itemTypeKind)); } if (this.itemTypeKind == EdmTypeKind.Primitive) { if (string.CompareOrdinal(this.itemTypeName, collectionItemTypeName) == 0) { return; } if (this.primitiveItemType.IsSpatial()) { EdmPrimitiveTypeKind primitiveTypeKind = EdmCoreModel.Instance.GetPrimitiveTypeKind(collectionItemTypeName); IEdmPrimitiveType primitiveType = EdmCoreModel.Instance.GetPrimitiveType(primitiveTypeKind); if (this.itemTypeDerivedFromCollectionValue) { if (this.primitiveItemType.IsAssignableFrom(primitiveType)) { return; } } else { IEdmPrimitiveType commonBaseType = this.primitiveItemType.GetCommonBaseType(primitiveType); if (commonBaseType != null) { this.primitiveItemType = commonBaseType; this.itemTypeName = commonBaseType.ODataFullName(); return; } } } throw new ODataException(Microsoft.Data.OData.Strings.CollectionWithoutExpectedTypeValidator_IncompatibleItemTypeName(collectionItemTypeName, this.itemTypeName)); } if (string.CompareOrdinal(this.itemTypeName, collectionItemTypeName) != 0) { throw new ODataException(Microsoft.Data.OData.Strings.CollectionWithoutExpectedTypeValidator_IncompatibleItemTypeName(collectionItemTypeName, this.itemTypeName)); } }
/// <summary> /// Checks if the <paramref name="firstType"/> primitive type and the <paramref name="secondType"/> primitive type /// have a common base type. /// In other words, if <paramref name="secondType"/> is a subtype of <paramref name="firstType"/> or not. /// </summary> /// <param name="firstType">Type of the base type.</param> /// <param name="secondType">Type of the sub type.</param> /// <returns>The common base type or null if no common base type exists.</returns> internal static IEdmPrimitiveType GetCommonBaseType(this IEdmPrimitiveType firstType, IEdmPrimitiveType secondType) { DebugUtils.CheckNoExternalCallers(); Debug.Assert(firstType != null, "firstType != null"); Debug.Assert(secondType != null, "secondType != null"); if (firstType.IsEquivalentTo(secondType)) { return firstType; } IEdmPrimitiveType commonBaseType = firstType; while (commonBaseType != null) { if (commonBaseType.IsAssignableFrom(secondType)) { return commonBaseType; } commonBaseType = commonBaseType.BaseType(); } commonBaseType = secondType; while (commonBaseType != null) { if (commonBaseType.IsAssignableFrom(firstType)) { return commonBaseType; } commonBaseType = commonBaseType.BaseType(); } return null; }
internal static Type GetPrimitiveClrType(IEdmPrimitiveType primitiveType, bool isNullable) { DebugUtils.CheckNoExternalCallers(); Debug.Assert(primitiveType != null, "primitiveType != null"); switch (primitiveType.PrimitiveKind) { case EdmPrimitiveTypeKind.Binary: return typeof(byte[]); case EdmPrimitiveTypeKind.Boolean: return isNullable ? typeof(Boolean?) : typeof(Boolean); case EdmPrimitiveTypeKind.Byte: return isNullable ? typeof(Byte?) : typeof(Byte); case EdmPrimitiveTypeKind.DateTime: return isNullable ? typeof(DateTime?) : typeof(DateTime); case EdmPrimitiveTypeKind.DateTimeOffset: return isNullable ? typeof(DateTimeOffset?) : typeof(DateTimeOffset); case EdmPrimitiveTypeKind.Decimal: return isNullable ? typeof(Decimal?) : typeof(Decimal); case EdmPrimitiveTypeKind.Double: return isNullable ? typeof(Double?) : typeof(Double); case EdmPrimitiveTypeKind.Geography: return typeof(Geography); case EdmPrimitiveTypeKind.GeographyCollection: return typeof(GeographyCollection); case EdmPrimitiveTypeKind.GeographyLineString: return typeof(GeographyLineString); case EdmPrimitiveTypeKind.GeographyMultiLineString: return typeof(GeographyMultiLineString); case EdmPrimitiveTypeKind.GeographyMultiPoint: return typeof(GeographyMultiPoint); case EdmPrimitiveTypeKind.GeographyMultiPolygon: return typeof(GeographyMultiPolygon); case EdmPrimitiveTypeKind.GeographyPoint: return typeof(GeographyPoint); case EdmPrimitiveTypeKind.GeographyPolygon: return typeof(GeographyPolygon); case EdmPrimitiveTypeKind.Geometry: return typeof(Geometry); case EdmPrimitiveTypeKind.GeometryCollection: return typeof(GeometryCollection); case EdmPrimitiveTypeKind.GeometryLineString: return typeof(GeometryLineString); case EdmPrimitiveTypeKind.GeometryMultiLineString: return typeof(GeometryMultiLineString); case EdmPrimitiveTypeKind.GeometryMultiPoint: return typeof(GeometryMultiPoint); case EdmPrimitiveTypeKind.GeometryMultiPolygon: return typeof(GeometryMultiPolygon); case EdmPrimitiveTypeKind.GeometryPoint: return typeof(GeometryPoint); case EdmPrimitiveTypeKind.GeometryPolygon: return typeof(GeometryPolygon); case EdmPrimitiveTypeKind.Guid: return isNullable ? typeof(Guid?) : typeof(Guid); case EdmPrimitiveTypeKind.Int16: return isNullable ? typeof(Int16?) : typeof(Int16); case EdmPrimitiveTypeKind.Int32: return isNullable ? typeof(Int32?) : typeof(Int32); case EdmPrimitiveTypeKind.Int64: return isNullable ? typeof(Int64?) : typeof(Int64); case EdmPrimitiveTypeKind.SByte: return isNullable ? typeof(SByte?) : typeof(SByte); case EdmPrimitiveTypeKind.Single: return isNullable ? typeof(Single?) : typeof(Single); case EdmPrimitiveTypeKind.Stream: return typeof(Stream); case EdmPrimitiveTypeKind.String: return typeof(String); case EdmPrimitiveTypeKind.Time: return isNullable ? typeof(TimeSpan?) : typeof(TimeSpan); default: return null; } }
/// <summary> /// Initializes a new instance of the <see cref="EdmStringTypeReference"/> class. /// </summary> /// <param name="definition">The type this reference refers to.</param> /// <param name="isNullable">Denotes whether the type can be nullable.</param> public EdmStringTypeReference(IEdmPrimitiveType definition, bool isNullable) : this(definition, isNullable, false, null, null, null, null) { }
public EdmTemporalTypeReference(IEdmPrimitiveType definition, bool isNullable, int? precision) : base(definition, isNullable) { this.precision = precision; }
private static object NullOrDefault(IEdmPrimitiveType targetType, bool nullable) { switch (targetType.PrimitiveKind) { case EdmPrimitiveTypeKind.Boolean: return nullable ? new bool?() : false; case EdmPrimitiveTypeKind.Byte: return nullable ? new byte?() : 0; case EdmPrimitiveTypeKind.DateTime: case EdmPrimitiveTypeKind.DateTimeOffset: return nullable ? new DateTime?() : DateTime.MinValue; case EdmPrimitiveTypeKind.Decimal: return nullable ? new decimal?() : 0.0m; case EdmPrimitiveTypeKind.Double: return nullable ? new double?() : double.NaN; case EdmPrimitiveTypeKind.Int16: return nullable ? new short?() : 0; case EdmPrimitiveTypeKind.Int32: return nullable ? new int?() : 0; case EdmPrimitiveTypeKind.Int64: return nullable ? new long?() : 0; case EdmPrimitiveTypeKind.String: return null; default: throw new NotSupportedException( String.Format("Support for primitive type {0} has not been implemented yet", targetType.PrimitiveKind)); } }
private static object GetPrimitiveValue(string value, IEdmPrimitiveType targetType, bool asNullable) { // TODO: Some sort of cast to nullable when necessary if (value == null) return NullOrDefault(targetType, asNullable); try { switch (targetType.PrimitiveKind) { case EdmPrimitiveTypeKind.Boolean: return Convert.ToBoolean(value); case EdmPrimitiveTypeKind.Byte: return Convert.ToByte(value); case EdmPrimitiveTypeKind.DateTime: case EdmPrimitiveTypeKind.DateTimeOffset: return Convert.ToDateTime(value); case EdmPrimitiveTypeKind.Decimal: return Convert.ToDecimal(value); case EdmPrimitiveTypeKind.Double: return Convert.ToDouble(value); case EdmPrimitiveTypeKind.Int16: return Convert.ToInt16(value); case EdmPrimitiveTypeKind.Int32: return Convert.ToInt32(value); case EdmPrimitiveTypeKind.Int64: return Convert.ToInt64(value); case EdmPrimitiveTypeKind.String: return value; default: throw new NotSupportedException( String.Format("Support for primitive type {0} has not been implemented yet", targetType.PrimitiveKind)); } } catch (FormatException) { return NullOrDefault(targetType, asNullable); } }
/// <summary> /// Validate that the expected and actual type names and type kinds are compatible. /// </summary> /// <param name="collectionItemTypeName">The actual type name.</param> /// <param name="collectionItemTypeKind">The actual type kind.</param> private void ValidateCollectionItemTypeNameAndKind(string collectionItemTypeName, EdmTypeKind collectionItemTypeKind) { // Compare the item type kinds. if (this.itemTypeKind != collectionItemTypeKind) { throw new ODataException(Strings.CollectionWithoutExpectedTypeValidator_IncompatibleItemTypeKind(collectionItemTypeKind, this.itemTypeKind)); } if (this.itemTypeKind == EdmTypeKind.Primitive) { Debug.Assert(this.primitiveItemType != null, "this.primitiveItemType != null"); Debug.Assert(collectionItemTypeName != null, "collectionItemTypeName != null"); // NOTE: we do support type inheritance for spatial primitive types; otherwise the type names have to match. if (!string.IsNullOrEmpty(this.itemTypeName) && this.itemTypeName.Equals(collectionItemTypeName, System.StringComparison.OrdinalIgnoreCase)) { return; } if (this.primitiveItemType.IsSpatial()) { EdmPrimitiveTypeKind collectionItemPrimitiveKind = EdmCoreModel.Instance.GetPrimitiveTypeKind(collectionItemTypeName); IEdmPrimitiveType collectionItemPrimitiveType = EdmCoreModel.Instance.GetPrimitiveType(collectionItemPrimitiveKind); if (this.itemTypeDerivedFromCollectionValue) { // If the collection defines an item type, the collection item type has to be assignable to it. if (this.primitiveItemType.IsAssignableFrom(collectionItemPrimitiveType)) { return; } } else { // If the collection does not define an item type, the collection items must have a common base type. IEdmPrimitiveType commonBaseType = EdmLibraryExtensions.GetCommonBaseType(this.primitiveItemType, collectionItemPrimitiveType); if (commonBaseType != null) { this.primitiveItemType = commonBaseType; this.itemTypeName = commonBaseType.FullTypeName(); return; } } } throw new ODataException(Strings.CollectionWithoutExpectedTypeValidator_IncompatibleItemTypeName(collectionItemTypeName, this.itemTypeName)); } else { // Since we do not support type inheritance for complex types, comparison of the type names is sufficient if (string.CompareOrdinal(this.itemTypeName, collectionItemTypeName) != 0) { throw new ODataException(Strings.CollectionWithoutExpectedTypeValidator_IncompatibleItemTypeName(collectionItemTypeName, this.itemTypeName)); } } }
/// <summary> /// Validates a collection item that was read to make sure it is valid (i.e., has the correct /// type name and type kind) with respect to the other items in the collection. /// </summary> /// <param name="collectionItemTypeName">The type name of the item from the payload.</param> /// <param name="collectionItemTypeKind">The type kind of the item from the payload.</param> internal void ValidateCollectionItem(string collectionItemTypeName, EdmTypeKind collectionItemTypeKind) { // Only primitive and complex values are allowed in collections if (collectionItemTypeKind != EdmTypeKind.Primitive && collectionItemTypeKind != EdmTypeKind.Enum && collectionItemTypeKind != EdmTypeKind.Complex) { throw new ODataException(Strings.CollectionWithoutExpectedTypeValidator_InvalidItemTypeKind(collectionItemTypeKind)); } if (this.itemTypeDerivedFromCollectionValue) { Debug.Assert(this.itemTypeName != null, "this.itemType != null"); // If the collection has a type name assign missing item type names from it. collectionItemTypeName = collectionItemTypeName ?? this.itemTypeName; // If we have a type name from the collection, make sure the type names of all items match this.ValidateCollectionItemTypeNameAndKind(collectionItemTypeName, collectionItemTypeKind); } else { // If we don't have a type name from the collection, store the type name and type kind of the first non-null item. if (this.itemTypeKind == EdmTypeKind.None) { // Compute the kind from the specified type name if available. this.itemTypeKind = collectionItemTypeName == null ? collectionItemTypeKind : ComputeExpectedTypeKind(collectionItemTypeName, out this.primitiveItemType); // If no payload type name is specified either default to Edm.String (for primitive type kinds) or leave the type name // null (for complex items without type name) if (collectionItemTypeName == null) { this.itemTypeKind = collectionItemTypeKind; if (this.itemTypeKind == EdmTypeKind.Primitive) { this.itemTypeName = Metadata.EdmConstants.EdmStringTypeName; this.primitiveItemType = EdmCoreModel.Instance.GetString(/*isNullable*/ false).PrimitiveDefinition(); } else { this.itemTypeName = null; this.primitiveItemType = null; } } else { this.itemTypeKind = ComputeExpectedTypeKind(collectionItemTypeName, out this.primitiveItemType); this.itemTypeName = collectionItemTypeName; } } if (collectionItemTypeName == null && collectionItemTypeKind == EdmTypeKind.Primitive) { // Default to Edm.String if no payload type is specified and the type kind is 'Primitive' collectionItemTypeName = Metadata.EdmConstants.EdmStringTypeName; } // Validate the expected and actual type names and type kinds. // Note that we compute the expected type kind from the expected type name and thus the payload // type kind (passed to this method) might be different from the computed expected type kind. this.ValidateCollectionItemTypeNameAndKind(collectionItemTypeName, collectionItemTypeKind); } }
public EdmEnumType(string namespaceName, string name, IEdmPrimitiveType underlyingType, bool isFlags) { this.members = new List<IEdmEnumMember>(); EdmUtil.CheckArgumentNull<IEdmPrimitiveType>(underlyingType, "underlyingType"); EdmUtil.CheckArgumentNull<string>(namespaceName, "namespaceName"); EdmUtil.CheckArgumentNull<string>(name, "name"); this.underlyingType = underlyingType; this.name = name; this.namespaceName = namespaceName; this.isFlags = isFlags; }
internal static bool IsAssignableFrom(this IEdmPrimitiveType baseType, IEdmPrimitiveType subtype) { DebugUtils.CheckNoExternalCallers(); Debug.Assert(baseType != null, "baseType != null"); Debug.Assert(subtype != null, "subtype != null"); if (baseType.IsEquivalentTo(subtype)) { return true; } // Only spatial types are assignable if (!baseType.IsSpatialType() || !subtype.IsSpatialType()) { return false; } // For two spatial types, test for assignability EdmPrimitiveTypeKind baseTypeKind = baseType.PrimitiveKind; EdmPrimitiveTypeKind subTypeKind = subtype.PrimitiveKind; switch (baseTypeKind) { case EdmPrimitiveTypeKind.Geography: return subTypeKind == EdmPrimitiveTypeKind.Geography || subTypeKind == EdmPrimitiveTypeKind.GeographyCollection || subTypeKind == EdmPrimitiveTypeKind.GeographyLineString || subTypeKind == EdmPrimitiveTypeKind.GeographyMultiLineString || subTypeKind == EdmPrimitiveTypeKind.GeographyMultiPoint || subTypeKind == EdmPrimitiveTypeKind.GeographyMultiPolygon || subTypeKind == EdmPrimitiveTypeKind.GeographyPoint || subTypeKind == EdmPrimitiveTypeKind.GeographyPolygon; case EdmPrimitiveTypeKind.GeographyPoint: return subTypeKind == EdmPrimitiveTypeKind.GeographyPoint; case EdmPrimitiveTypeKind.GeographyLineString: return subTypeKind == EdmPrimitiveTypeKind.GeographyLineString; case EdmPrimitiveTypeKind.GeographyPolygon: return subTypeKind == EdmPrimitiveTypeKind.GeographyPolygon; case EdmPrimitiveTypeKind.GeographyCollection: return subTypeKind == EdmPrimitiveTypeKind.GeographyCollection || subTypeKind == EdmPrimitiveTypeKind.GeographyMultiLineString || subTypeKind == EdmPrimitiveTypeKind.GeographyMultiPoint || subTypeKind == EdmPrimitiveTypeKind.GeographyMultiPolygon; case EdmPrimitiveTypeKind.GeographyMultiPolygon: return subTypeKind == EdmPrimitiveTypeKind.GeographyMultiPolygon; case EdmPrimitiveTypeKind.GeographyMultiLineString: return subTypeKind == EdmPrimitiveTypeKind.GeographyMultiLineString; case EdmPrimitiveTypeKind.GeographyMultiPoint: return subTypeKind == EdmPrimitiveTypeKind.GeographyMultiPoint; case EdmPrimitiveTypeKind.Geometry: return subTypeKind == EdmPrimitiveTypeKind.Geometry || subTypeKind == EdmPrimitiveTypeKind.GeometryCollection || subTypeKind == EdmPrimitiveTypeKind.GeometryLineString || subTypeKind == EdmPrimitiveTypeKind.GeometryMultiLineString || subTypeKind == EdmPrimitiveTypeKind.GeometryMultiPoint || subTypeKind == EdmPrimitiveTypeKind.GeometryMultiPolygon || subTypeKind == EdmPrimitiveTypeKind.GeometryPoint || subTypeKind == EdmPrimitiveTypeKind.GeometryPolygon; case EdmPrimitiveTypeKind.GeometryPoint: return subTypeKind == EdmPrimitiveTypeKind.GeometryPoint; case EdmPrimitiveTypeKind.GeometryLineString: return subTypeKind == EdmPrimitiveTypeKind.GeometryLineString; case EdmPrimitiveTypeKind.GeometryPolygon: return subTypeKind == EdmPrimitiveTypeKind.GeometryPolygon; case EdmPrimitiveTypeKind.GeometryCollection: return subTypeKind == EdmPrimitiveTypeKind.GeometryCollection || subTypeKind == EdmPrimitiveTypeKind.GeometryMultiLineString || subTypeKind == EdmPrimitiveTypeKind.GeometryMultiPoint || subTypeKind == EdmPrimitiveTypeKind.GeometryMultiPolygon; case EdmPrimitiveTypeKind.GeometryMultiPolygon: return subTypeKind == EdmPrimitiveTypeKind.GeometryMultiPolygon; case EdmPrimitiveTypeKind.GeometryMultiLineString: return subTypeKind == EdmPrimitiveTypeKind.GeometryMultiLineString; case EdmPrimitiveTypeKind.GeometryMultiPoint: return subTypeKind == EdmPrimitiveTypeKind.GeometryMultiPoint; default: throw new ODataException(Strings.General_InternalError(InternalErrorCodesCommon.EdmLibraryExtensions_IsAssignableFrom)); } }
internal static bool CanConvertPrimitiveTypeTo(IEdmPrimitiveType sourcePrimitiveType, IEdmPrimitiveType targetPrimitiveType) { EdmPrimitiveTypeKind primitiveKind = sourcePrimitiveType.PrimitiveKind; EdmPrimitiveTypeKind kind2 = targetPrimitiveType.PrimitiveKind; switch (primitiveKind) { case EdmPrimitiveTypeKind.Byte: switch (kind2) { case EdmPrimitiveTypeKind.Byte: case EdmPrimitiveTypeKind.Decimal: case EdmPrimitiveTypeKind.Double: case EdmPrimitiveTypeKind.Int16: case EdmPrimitiveTypeKind.Int32: case EdmPrimitiveTypeKind.Int64: case EdmPrimitiveTypeKind.Single: return true; } break; case EdmPrimitiveTypeKind.Int16: switch (kind2) { case EdmPrimitiveTypeKind.Decimal: case EdmPrimitiveTypeKind.Double: case EdmPrimitiveTypeKind.Int16: case EdmPrimitiveTypeKind.Int32: case EdmPrimitiveTypeKind.Int64: case EdmPrimitiveTypeKind.Single: return true; } break; case EdmPrimitiveTypeKind.Int32: switch (kind2) { case EdmPrimitiveTypeKind.Decimal: case EdmPrimitiveTypeKind.Double: case EdmPrimitiveTypeKind.Int32: case EdmPrimitiveTypeKind.Int64: case EdmPrimitiveTypeKind.Single: return true; } break; case EdmPrimitiveTypeKind.Int64: switch (kind2) { case EdmPrimitiveTypeKind.Decimal: case EdmPrimitiveTypeKind.Double: case EdmPrimitiveTypeKind.Int64: case EdmPrimitiveTypeKind.Single: return true; } break; case EdmPrimitiveTypeKind.SByte: switch (kind2) { case EdmPrimitiveTypeKind.Decimal: case EdmPrimitiveTypeKind.Double: case EdmPrimitiveTypeKind.Int16: case EdmPrimitiveTypeKind.Int32: case EdmPrimitiveTypeKind.Int64: case EdmPrimitiveTypeKind.SByte: case EdmPrimitiveTypeKind.Single: return true; } break; case EdmPrimitiveTypeKind.Single: switch (kind2) { case EdmPrimitiveTypeKind.Double: case EdmPrimitiveTypeKind.Single: break; } return true; default: if (primitiveKind == kind2) { return true; } break; } return false; }