/// <summary> /// Converts a primitive data type to type. /// </summary> /// <param name="primitiveType">The primitive data type to convert.</param> /// <returns>The corresponding primitive type.</returns> private static IEdmPrimitiveTypeReference GetPrimitiveTypeReference(PrimitiveDataType primitiveType) { Debug.Assert(primitiveType != null, "primitiveType != null"); Type systemType = EntityModelUtils.GetPrimitiveClrType(primitiveType); // NOTE: if the primitiveType is not nullable but the type reference constructed from the CLR type is, // adjust the nullability if necessary. IEdmPrimitiveTypeReference primitiveTypeReference = MetadataUtils.GetPrimitiveTypeReference(systemType); if (primitiveType.IsNullable != primitiveTypeReference.IsNullable) { primitiveTypeReference = (IEdmPrimitiveTypeReference)primitiveTypeReference.Clone(primitiveType.IsNullable); } return(primitiveTypeReference); }
/// <summary> /// Builds a complex instance from the given payloadElements to represent a parameters payload. /// </summary> /// <param name="payloadElements">Each ODataPayloadElement represents the value for each parameter.</param> /// <param name="model">EdmModel instance.</param> /// <param name="functionImportName">Name of the function import to add to the model.</param> /// <returns></returns> private static ComplexInstance PayloadElementsToParameterPayload(ODataPayloadElement[] payloadElements, EdmModel model, string functionImportName) { EdmOperationImport operationImport = (EdmOperationImport)model.EntityContainer.FindOperationImports(functionImportName).FirstOrDefault(); EdmOperation operation = (EdmOperation)operationImport.Operation; var parameterPayload = new ComplexInstance(null, false); for (int idx = 0; idx < payloadElements.Length; idx++) { ODataPayloadElement p = payloadElements[idx]; string parameterName = "p" + idx; PropertyInstance parameter; IEdmTypeReference entityModelType = p.GetAnnotation <EntityModelTypeAnnotation>().EdmModelType; switch (p.ElementType) { case ODataPayloadElementType.PrimitiveValue: object clrValue = ((PrimitiveValue)p).ClrValue; PrimitiveValue primitiveValue = new PrimitiveValue(clrValue == null ? null : clrValue.GetType().FullName, clrValue); primitiveValue.CopyAnnotation <PrimitiveValue, EntityModelTypeAnnotation>(p); parameter = new PrimitiveProperty(parameterName, primitiveValue); operation.AddParameter(parameterName, MetadataUtils.GetPrimitiveTypeReference(primitiveValue.ClrValue.GetType())); break; case ODataPayloadElementType.ComplexInstance: parameter = new ComplexProperty(parameterName, (ComplexInstance)p); operation.AddParameter(parameterName, entityModelType); break; case ODataPayloadElementType.PrimitiveMultiValue: PrimitiveMultiValue primitiveMultiValue = (PrimitiveMultiValue)p; if (primitiveMultiValue.Annotations.OfType <JsonCollectionResultWrapperAnnotation>().SingleOrDefault() == null) { primitiveMultiValue.Annotations.Add(new JsonCollectionResultWrapperAnnotation(false)); } parameter = new PrimitiveMultiValueProperty(parameterName, primitiveMultiValue); operation.AddParameter(parameterName, entityModelType); break; case ODataPayloadElementType.ComplexMultiValue: ComplexMultiValue complexMultiValue = (ComplexMultiValue)p; if (complexMultiValue.Annotations.OfType <JsonCollectionResultWrapperAnnotation>().SingleOrDefault() == null) { complexMultiValue.Annotations.Add(new JsonCollectionResultWrapperAnnotation(false)); } parameter = new ComplexMultiValueProperty(parameterName, complexMultiValue); operation.AddParameter(parameterName, entityModelType); break; case ODataPayloadElementType.EntityInstance: parameter = new NavigationPropertyInstance(parameterName, (EntityInstance)p); operation.AddParameter(parameterName, entityModelType); break; case ODataPayloadElementType.EntitySetInstance: parameter = new NavigationPropertyInstance(parameterName, (EntitySetInstance)p); operation.AddParameter(parameterName, entityModelType); break; default: throw new NotSupportedException("PayloadElementsToParameterPayload() is called on unsupported ODataPayloadElement type: " + p.ElementType); } parameterPayload.Add(parameter); } parameterPayload.ExpectedFunctionImport(operationImport); return(parameterPayload); }
/// <summary> /// Resolves the specified entity model schema type and returns the Edm model type for it. /// </summary> /// <param name="model">The model to get the type from.</param> /// <param name="schemaType">The entity model schema type to resolve.</param> /// <returns>The resolved type for the specified <paramref name="schemaType"/>.</returns> public static IEdmTypeReference ResolveEntityModelSchemaType(IEdmModel model, DataType schemaType) { if (schemaType == null) { return(null); } PrimitiveDataType primitiveDataType = schemaType as PrimitiveDataType; if (primitiveDataType != null) { return(GetPrimitiveTypeReference(primitiveDataType)); } if (model == null) { return(null); } EntityDataType entityDataType = schemaType as EntityDataType; if (entityDataType != null) { IEdmNamedElement edmType = model.FindType(entityDataType.Definition.FullName); ExceptionUtilities.Assert( edmType != null, "The expected entity type '{0}' was not found in the entity model for this test.", entityDataType.Definition.FullName); IEdmEntityType entityType = edmType as IEdmEntityType; ExceptionUtilities.Assert( entityType != null, "The expected entity type '{0}' is not defined as entity type in the test's metadata.", entityDataType.Definition.FullName); return(entityType.ToTypeReference()); } ComplexDataType complexDataType = schemaType as ComplexDataType; if (complexDataType != null) { return(GetComplexType(model, complexDataType)); } CollectionDataType collectionDataType = schemaType as CollectionDataType; if (collectionDataType != null) { DataType collectionElementType = collectionDataType.ElementDataType; PrimitiveDataType primitiveElementType = collectionElementType as PrimitiveDataType; if (primitiveElementType != null) { IEdmPrimitiveTypeReference primitiveElementTypeReference = GetPrimitiveTypeReference(primitiveElementType); return(primitiveElementTypeReference.ToCollectionTypeReference()); } ComplexDataType complexElementType = collectionElementType as ComplexDataType; if (complexElementType != null) { IEdmComplexTypeReference complexElementTypeReference = GetComplexType(model, complexElementType); return(complexElementTypeReference.ToCollectionTypeReference()); } EntityDataType entityElementType = collectionElementType as EntityDataType; if (entityElementType != null) { IEdmEntityTypeReference entityElementTypeReference = GetEntityType(model, entityElementType); return(entityElementTypeReference.ToCollectionTypeReference()); } throw new NotSupportedException("Collection types only support primitive, complex, and entity element types."); } StreamDataType streamType = schemaType as StreamDataType; if (streamType != null) { Type systemType = streamType.GetFacet <PrimitiveClrTypeFacet>().Value; ExceptionUtilities.Assert(systemType == typeof(Stream), "Expected the system type 'System.IO.Stream' for a stream reference property."); return(MetadataUtils.GetPrimitiveTypeReference(systemType)); } throw new NotImplementedException("Unrecognized schema type " + schemaType.GetType().Name + "."); }