Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 39 additions & 84 deletions src/Microsoft.OData.ModelBuilder/Commons/EdmLibHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using Microsoft.OData.Edm;
using Microsoft.OData.Edm.Vocabularies;
using Microsoft.OData.Edm.Vocabularies.V1;
using Microsoft.OData.ModelBuilder.Annotations;
using Microsoft.OData.ModelBuilder.Config;
using Microsoft.Spatial;
using Microsoft.OData.ModelBuilder.Providers;

namespace Microsoft.OData.ModelBuilder
{
Expand All @@ -24,77 +22,34 @@ internal static class EdmLibHelpers
{
private static readonly EdmCoreModel _coreModel = EdmCoreModel.Instance;

private static readonly Dictionary<Type, IEdmPrimitiveType> _builtInTypesMapping =
new[]
{
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(string), GetPrimitiveType(EdmPrimitiveTypeKind.String)),
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(bool), GetPrimitiveType(EdmPrimitiveTypeKind.Boolean)),
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(bool?), GetPrimitiveType(EdmPrimitiveTypeKind.Boolean)),
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(byte), GetPrimitiveType(EdmPrimitiveTypeKind.Byte)),
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(byte?), GetPrimitiveType(EdmPrimitiveTypeKind.Byte)),
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(decimal), GetPrimitiveType(EdmPrimitiveTypeKind.Decimal)),
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(decimal?), GetPrimitiveType(EdmPrimitiveTypeKind.Decimal)),
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(double), GetPrimitiveType(EdmPrimitiveTypeKind.Double)),
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(double?), GetPrimitiveType(EdmPrimitiveTypeKind.Double)),
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(Guid), GetPrimitiveType(EdmPrimitiveTypeKind.Guid)),
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(Guid?), GetPrimitiveType(EdmPrimitiveTypeKind.Guid)),
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(short), GetPrimitiveType(EdmPrimitiveTypeKind.Int16)),
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(short?), GetPrimitiveType(EdmPrimitiveTypeKind.Int16)),
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(int), GetPrimitiveType(EdmPrimitiveTypeKind.Int32)),
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(int?), GetPrimitiveType(EdmPrimitiveTypeKind.Int32)),
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(long), GetPrimitiveType(EdmPrimitiveTypeKind.Int64)),
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(long?), GetPrimitiveType(EdmPrimitiveTypeKind.Int64)),
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(sbyte), GetPrimitiveType(EdmPrimitiveTypeKind.SByte)),
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(sbyte?), GetPrimitiveType(EdmPrimitiveTypeKind.SByte)),
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(float), GetPrimitiveType(EdmPrimitiveTypeKind.Single)),
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(float?), GetPrimitiveType(EdmPrimitiveTypeKind.Single)),
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(byte[]), GetPrimitiveType(EdmPrimitiveTypeKind.Binary)),
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(Stream), GetPrimitiveType(EdmPrimitiveTypeKind.Stream)),
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(Geography), GetPrimitiveType(EdmPrimitiveTypeKind.Geography)),
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(GeographyPoint), GetPrimitiveType(EdmPrimitiveTypeKind.GeographyPoint)),
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(GeographyLineString), GetPrimitiveType(EdmPrimitiveTypeKind.GeographyLineString)),
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(GeographyPolygon), GetPrimitiveType(EdmPrimitiveTypeKind.GeographyPolygon)),
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(GeographyCollection), GetPrimitiveType(EdmPrimitiveTypeKind.GeographyCollection)),
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(GeographyMultiLineString), GetPrimitiveType(EdmPrimitiveTypeKind.GeographyMultiLineString)),
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(GeographyMultiPoint), GetPrimitiveType(EdmPrimitiveTypeKind.GeographyMultiPoint)),
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(GeographyMultiPolygon), GetPrimitiveType(EdmPrimitiveTypeKind.GeographyMultiPolygon)),
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(Geometry), GetPrimitiveType(EdmPrimitiveTypeKind.Geometry)),
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(GeometryPoint), GetPrimitiveType(EdmPrimitiveTypeKind.GeometryPoint)),
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(GeometryLineString), GetPrimitiveType(EdmPrimitiveTypeKind.GeometryLineString)),
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(GeometryPolygon), GetPrimitiveType(EdmPrimitiveTypeKind.GeometryPolygon)),
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(GeometryCollection), GetPrimitiveType(EdmPrimitiveTypeKind.GeometryCollection)),
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(GeometryMultiLineString), GetPrimitiveType(EdmPrimitiveTypeKind.GeometryMultiLineString)),
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(GeometryMultiPoint), GetPrimitiveType(EdmPrimitiveTypeKind.GeometryMultiPoint)),
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(GeometryMultiPolygon), GetPrimitiveType(EdmPrimitiveTypeKind.GeometryMultiPolygon)),
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(DateTimeOffset), GetPrimitiveType(EdmPrimitiveTypeKind.DateTimeOffset)),
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(DateTimeOffset?), GetPrimitiveType(EdmPrimitiveTypeKind.DateTimeOffset)),
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(TimeSpan), GetPrimitiveType(EdmPrimitiveTypeKind.Duration)),
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(TimeSpan?), GetPrimitiveType(EdmPrimitiveTypeKind.Duration)),
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(Date), GetPrimitiveType(EdmPrimitiveTypeKind.Date)),
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(Date?), GetPrimitiveType(EdmPrimitiveTypeKind.Date)),
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(TimeOfDay), GetPrimitiveType(EdmPrimitiveTypeKind.TimeOfDay)),
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(TimeOfDay?), GetPrimitiveType(EdmPrimitiveTypeKind.TimeOfDay)),
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(DateOnly), GetPrimitiveType(EdmPrimitiveTypeKind.Date)),
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(DateOnly?), GetPrimitiveType(EdmPrimitiveTypeKind.Date)),
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(TimeOnly), GetPrimitiveType(EdmPrimitiveTypeKind.TimeOfDay)),
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(TimeOnly?), GetPrimitiveType(EdmPrimitiveTypeKind.TimeOfDay)),

// Keep the Binary and XElement in the end, since there are not the default mappings for Edm.Binary and Edm.String.
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(XElement), GetPrimitiveType(EdmPrimitiveTypeKind.String)),
// new KeyValuePair<Type, IEdmPrimitiveType>(typeof(Binary), GetPrimitiveType(EdmPrimitiveTypeKind.Binary)),
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(ushort), GetPrimitiveType(EdmPrimitiveTypeKind.Int32)),
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(ushort?), GetPrimitiveType(EdmPrimitiveTypeKind.Int32)),
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(uint), GetPrimitiveType(EdmPrimitiveTypeKind.Int64)),
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(uint?), GetPrimitiveType(EdmPrimitiveTypeKind.Int64)),
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(ulong), GetPrimitiveType(EdmPrimitiveTypeKind.Int64)),
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(ulong?), GetPrimitiveType(EdmPrimitiveTypeKind.Int64)),
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(char[]), GetPrimitiveType(EdmPrimitiveTypeKind.String)),
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(char), GetPrimitiveType(EdmPrimitiveTypeKind.String)),
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(char?), GetPrimitiveType(EdmPrimitiveTypeKind.String)),
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(DateTime), GetPrimitiveType(EdmPrimitiveTypeKind.DateTimeOffset)),
new KeyValuePair<Type, IEdmPrimitiveType>(typeof(DateTime?), GetPrimitiveType(EdmPrimitiveTypeKind.DateTimeOffset)),
}
.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
private static Lazy<IEdmTypeMappingProvider> lazyPrimitiveTypeMappingProvider;

private static IEdmTypeMappingProvider PrimitiveTypeMappingProvider
{
get
{
if (lazyPrimitiveTypeMappingProvider == null)
{
lazyPrimitiveTypeMappingProvider = new Lazy<IEdmTypeMappingProvider>(
() => new DefaultEdmTypeMappingProvider());
}

return lazyPrimitiveTypeMappingProvider.Value;
}
}

public static void ConfigurePrimitiveTypeMappingProvider(Func<IEdmTypeMappingProvider> factory)
{
if (lazyPrimitiveTypeMappingProvider?.IsValueCreated == true)
{
return;
}

if (lazyPrimitiveTypeMappingProvider == null)
{
lazyPrimitiveTypeMappingProvider = new Lazy<IEdmTypeMappingProvider>(factory);
}
}

public static bool HasLength(EdmPrimitiveTypeKind primitiveTypeKind)
{
Expand Down Expand Up @@ -142,18 +97,14 @@ public static Type GetClrType(this IEdmModel edmModel,IEdmTypeReference edmTypeR
return typeof(object);
}

Type primitiveClrType = _builtInTypesMapping
.Where(kvp => edmTypeReference.Definition.IsEquivalentTo(kvp.Value) && (!edmTypeReference.IsNullable || IsNullable(kvp.Key)))
.Select(kvp => kvp.Key)
.FirstOrDefault();

if (primitiveClrType != null)
Type clrType;
if (PrimitiveTypeMappingProvider.TryGetClrType(edmTypeReference, out clrType))
{
return primitiveClrType;
return clrType;
}
else
{
Type clrType = edmModel.GetClrType(edmTypeReference.Definition, assembliesResolver);
clrType = edmModel.GetClrType(edmTypeReference.Definition, assembliesResolver);
if (clrType != null && TypeHelper.IsEnum(clrType) && edmTypeReference.IsNullable)
{
return TypeHelper.ToNullable(clrType);
Expand Down Expand Up @@ -196,8 +147,12 @@ public static Type GetClrType(this IEdmModel edmModel, IEdmType edmType, IAssemb

public static IEdmPrimitiveType GetEdmPrimitiveTypeOrNull(Type clrType)
{
IEdmPrimitiveType primitiveType;
return _builtInTypesMapping.TryGetValue(clrType, out primitiveType) ? primitiveType : null;
if (PrimitiveTypeMappingProvider.TryGetEdmType(clrType, out IEdmPrimitiveType primitiveType))
{
return primitiveType;
}

return null;
}

public static IEdmPrimitiveTypeReference GetEdmPrimitiveTypeReferenceOrNull(Type clrType)
Expand Down
1 change: 1 addition & 0 deletions src/Microsoft.OData.ModelBuilder/Commons/TypeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.OData.ModelBuilder.Providers;

namespace Microsoft.OData.ModelBuilder
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Microsoft.OData.ModelBuilder.Conventions.Attributes
/// Base class for all attribute based <see cref="IEdmPropertyConvention"/>'s.
/// </summary>
/// <typeparam name="TPropertyConfiguration">The type of the property this configuration applies to.</typeparam>
internal abstract class AttributeEdmPropertyConvention<TPropertyConfiguration> : AttributeConvention, IEdmPropertyConvention<TPropertyConfiguration>
public abstract class AttributeEdmPropertyConvention<TPropertyConfiguration> : AttributeConvention, IEdmPropertyConvention<TPropertyConfiguration>
where TPropertyConfiguration : PropertyConfiguration
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Microsoft.OData.ModelBuilder.Conventions.Attributes
/// Base class for all <see cref="IEdmTypeConvention"/>'s based on a attribute on the type.
/// </summary>
/// <typeparam name="TEdmTypeConfiguration">The kind of Edm type that this convention must be applied to.</typeparam>
internal abstract class AttributeEdmTypeConvention<TEdmTypeConfiguration> : AttributeConvention, IEdmTypeConvention
public abstract class AttributeEdmTypeConvention<TEdmTypeConfiguration> : AttributeConvention, IEdmTypeConvention
where TEdmTypeConfiguration : class, IEdmTypeConfiguration
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.OData.ModelBuilder.Providers;

namespace Microsoft.OData.ModelBuilder
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Microsoft.OData.ModelBuilder
/// </summary>
public class DefaultAssemblyResolver : IAssemblyResolver
{
private Assembly[] _assemblies = GetAssembliesInteral();
private Assembly[] _assemblies = GetAssembliesInternal();

/// <summary>
/// This static instance is used in the shared code in places where the request container context
Expand All @@ -25,7 +25,7 @@ public class DefaultAssemblyResolver : IAssemblyResolver
/// </summary>
public IEnumerable<Assembly> Assemblies => _assemblies;

private static Assembly[] GetAssembliesInteral()
private static Assembly[] GetAssembliesInternal()
{
return AppDomain.CurrentDomain.GetAssemblies();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Microsoft.OData.Edm.Vocabularies;
using Microsoft.OData.ModelBuilder.Annotations;
using Microsoft.OData.ModelBuilder.Config;
using Microsoft.OData.ModelBuilder.Providers;
using Microsoft.OData.ModelBuilder.Vocabularies;

namespace Microsoft.OData.ModelBuilder.Helpers
Expand Down
73 changes: 43 additions & 30 deletions src/Microsoft.OData.ModelBuilder/Helpers/EdmTypeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Contracts;
using System.Globalization;
using System.Linq;
Expand Down Expand Up @@ -354,7 +353,7 @@ private void CreateStructuralTypeBody(EdmStructuredType type, StructuralTypeConf
case PropertyKind.Primitive:
PrimitivePropertyConfiguration primitiveProperty = (PrimitivePropertyConfiguration)property;
EdmPrimitiveTypeKind typeKind = primitiveProperty.TargetEdmTypeKind ??
GetTypeKind(primitiveProperty.PropertyInfo.PropertyType);
GetTypeKind(primitiveProperty.PropertyInfo.PropertyType);
IEdmTypeReference primitiveTypeReference = EdmCoreModel.Instance.GetPrimitive(
typeKind,
primitiveProperty.NullableProperty);
Expand Down Expand Up @@ -443,46 +442,60 @@ private void CreateStructuralTypeBody(EdmStructuredType type, StructuralTypeConf

private IEdmProperty CreateStructuralTypeCollectionPropertyBody(EdmStructuredType type, CollectionPropertyConfiguration collectionProperty)
{
IEdmTypeReference elementTypeReference;
Type clrType = TypeHelper.GetUnderlyingTypeOrSelf(collectionProperty.ElementType);
IEdmTypeReference elementTypeReference= GetElementTypeReference(this, collectionProperty);

if (clrType == typeof(object))
{
elementTypeReference = EdmCoreModel.Instance.GetUntyped();
}
else if (TypeHelper.IsEnum(clrType))
return type.AddStructuralProperty(
collectionProperty.Name,
new EdmCollectionTypeReference(new EdmCollectionType(elementTypeReference)));

static IEdmTypeReference GetElementTypeReference(EdmTypeBuilder thisParam, CollectionPropertyConfiguration colProperty)
{
IEdmType edmType = GetEdmType(clrType);
Type clrType = TypeHelper.GetUnderlyingTypeOrSelf(colProperty.ElementType);

if (edmType == null)
// 1) Untyped
if (clrType == typeof(object))
{
throw Error.InvalidOperation(SRResources.EnumTypeDoesNotExist, clrType.Name);
return EdmCoreModel.Instance.GetUntyped();
}

IEdmEnumType enumElementType = (IEdmEnumType)edmType;
bool isNullable = collectionProperty.ElementType != clrType;
elementTypeReference = new EdmEnumTypeReference(enumElementType, isNullable);
}
else
{
IEdmType edmType = GetEdmType(collectionProperty.ElementType);
// 2) Enum
if (TypeHelper.IsEnum(clrType))
{
IEdmType enumEdmType = thisParam.GetEdmType(clrType);

if (enumEdmType == null)
{
throw Error.InvalidOperation(SRResources.EnumTypeDoesNotExist, clrType.Name);
}

IEdmEnumType enumElementType = (IEdmEnumType)enumEdmType;
bool isNullable = colProperty.ElementType != clrType;

return new EdmEnumTypeReference(enumElementType, isNullable);
}

// 3) Explicit primitive override
if (colProperty.ElementTargetEdmTypeKind != null)
{
return EdmCoreModel.Instance.GetPrimitive(colProperty.ElementTargetEdmTypeKind.Value, colProperty.NullableProperty);
}

// 4) Complex or primitive inferred from known types
IEdmType edmType = thisParam.GetEdmType(colProperty.ElementType);
if (edmType != null)
{
IEdmComplexType elementType = edmType as IEdmComplexType;
Contract.Assert(elementType != null);
elementTypeReference = new EdmComplexTypeReference(elementType, collectionProperty.NullableProperty);
}
else
{
elementTypeReference =
EdmLibHelpers.GetEdmPrimitiveTypeReferenceOrNull(collectionProperty.ElementType);
Contract.Assert(elementTypeReference != null);

return new EdmComplexTypeReference(elementType, colProperty.NullableProperty);
}
}

return type.AddStructuralProperty(
collectionProperty.Name,
new EdmCollectionTypeReference(new EdmCollectionType(elementTypeReference)));
// Fallback: primitive by CLR mapping
IEdmTypeReference primitiveTypeReference = EdmLibHelpers.GetEdmPrimitiveTypeReferenceOrNull(colProperty.ElementType);
Contract.Assert(primitiveTypeReference != null);

return primitiveTypeReference;
}
}

private IEdmProperty CreateStructuralTypeEnumPropertyBody(EdmStructuredType type, EnumPropertyConfiguration enumProperty)
Expand Down
Loading