private void WriteParameters(ODataMessageWriterTestWrapper messageWriter, ODataParameters parameters, IEdmOperationImport functionImport) { ODataParameterWriter parameterWriter = messageWriter.CreateODataParameterWriter(functionImport); parameterWriter.WriteStart(); foreach (var parameter in parameters) { ODataCollectionStart collectionStart = parameter.Value as ODataCollectionStart; ODataResourceSet feed; ODataResource entry; if (collectionStart != null) { ODataCollectionWriter collectionWriter = parameterWriter.CreateCollectionWriter(parameter.Key); this.WriteCollection(collectionWriter, collectionStart); collectionWriter.Flush(); } else if ((feed = parameter.Value as ODataResourceSet) != null) { this.WriteFeed(parameterWriter.CreateResourceSetWriter(parameter.Key), feed); } else if ((entry = parameter.Value as ODataResource) != null) { this.WriteEntry(parameterWriter.CreateResourceWriter(parameter.Key), entry); } else { parameterWriter.WriteValue(parameter.Key, parameter.Value); } } parameterWriter.WriteEnd(); parameterWriter.Flush(); }
/// <summary> /// Visits parameters. /// </summary> /// <param name="collection">The parameters to visit.</param> private void VisitParameters(ODataParameters parameters) { foreach (var parameter in parameters) { this.Visit(parameter.Value); } }
/// <summary> /// Visits a collection of parameters. /// </summary> /// <param name="parameters">The parameters to visit.</param> protected override ODataPayloadElement VisitParameters(ODataParameters parameters) { ExceptionUtilities.CheckArgumentNotNull(parameters, "parameters"); ComplexInstance result = new ComplexInstance(); result.IsNull = parameters.Count == 0; foreach (var parameter in parameters) { if (parameter.Value == null) { result.Add(new PrimitiveProperty(parameter.Key, null, null)); continue; } ODataCollectionStart odataCollectionStart = parameter.Value as ODataCollectionStart; if (odataCollectionStart != null) { ODataCollectionItemsObjectModelAnnotation annotation = odataCollectionStart.GetAnnotation <ODataCollectionItemsObjectModelAnnotation>(); PrimitiveMultiValue primitiveCollection = PayloadBuilder.PrimitiveMultiValue(); foreach (var value in annotation) { primitiveCollection.Item(value); } PrimitiveMultiValueProperty primitiveCollectionProperty = new PrimitiveMultiValueProperty(parameter.Key, primitiveCollection); result.Add(primitiveCollectionProperty); } else { result.Add(new PrimitiveProperty(parameter.Key, null, PayloadBuilder.PrimitiveValue(parameter.Value).ClrValue)); } } return(result); }
/// <summary> /// Visits parameters. /// </summary> /// <param name="parameters">The parameters to visit.</param> protected abstract T VisitParameters(ODataParameters parameters);
/// <summary> /// Visits an item in the object model. /// </summary> /// <param name="objectModelItem">The item to visit.</param> public virtual T Visit(object objectModelItem) { ODataResourceSet feed = objectModelItem as ODataResourceSet; if (feed != null) { return(this.VisitFeed(feed)); } ODataResource entry = objectModelItem as ODataResource; if (entry != null) { return(this.VisitEntry(entry)); } ODataProperty property = objectModelItem as ODataProperty; if (property != null) { return(this.VisitProperty(property)); } ODataNestedResourceInfo navigationLink = objectModelItem as ODataNestedResourceInfo; if (navigationLink != null) { return(this.VisitNavigationLink(navigationLink)); } ODataResourceValue resourceValue = objectModelItem as ODataResourceValue; if (resourceValue != null) { return(this.VisitResourceValue(resourceValue)); } ODataCollectionValue collection = objectModelItem as ODataCollectionValue; if (collection != null) { return(this.VisitCollectionValue(collection)); } ODataStreamReferenceValue streamReferenceValue = objectModelItem as ODataStreamReferenceValue; if (streamReferenceValue != null) { return(this.VisitStreamReferenceValue(streamReferenceValue)); } ODataCollectionStart collectionStart = objectModelItem as ODataCollectionStart; if (collectionStart != null) { return(this.VisitCollectionStart(collectionStart)); } ODataServiceDocument serviceDocument = objectModelItem as ODataServiceDocument; if (serviceDocument != null) { return(this.VisitWorkspace(serviceDocument)); } ODataEntitySetInfo entitySetInfo = objectModelItem as ODataEntitySetInfo; if (entitySetInfo != null) { return(this.VisitResourceCollection(entitySetInfo)); } ODataError error = objectModelItem as ODataError; if (error != null) { return(this.VisitError(error)); } ODataInnerError innerError = objectModelItem as ODataInnerError; if (innerError != null) { return(this.VisitInnerError(innerError)); } ODataEntityReferenceLinks entityReferenceLinks = objectModelItem as ODataEntityReferenceLinks; if (entityReferenceLinks != null) { return(this.VisitEntityReferenceLinks(entityReferenceLinks)); } ODataEntityReferenceLink entityReferenceLink = objectModelItem as ODataEntityReferenceLink; if (entityReferenceLink != null) { return(this.VisitEntityReferenceLink(entityReferenceLink)); } ODataAction action = objectModelItem as ODataAction; if (action != null) { return(this.VisitODataOperation(action)); } ODataFunction function = objectModelItem as ODataFunction; if (function != null) { return(this.VisitODataOperation(function)); } ODataParameters parameters = objectModelItem as ODataParameters; if (parameters != null) { return(this.VisitParameters(parameters)); } ODataBatch batch = objectModelItem as ODataBatch; if (batch != null) { return(this.VisitBatch(batch)); } if (objectModelItem == null || objectModelItem is ODataPrimitiveValue || objectModelItem.GetType().IsValueType || objectModelItem is string || objectModelItem is byte[] || objectModelItem is ISpatial) { return(this.VisitPrimitiveValue(objectModelItem)); } if (objectModelItem is ODataUntypedValue) { object val = ODataObjectModelVisitor.ParseJsonToPrimitiveValue( (objectModelItem as ODataUntypedValue).RawValue); return(this.VisitPrimitiveValue(val)); } return(this.VisitUnsupportedValue(objectModelItem)); }
/// <summary> /// Read parameters as the message content. /// </summary> /// <param name="messageReader">The message reader to use for reading.</param> /// <param name="functionImport">The function import to pass to the parameter reader.</param> /// <returns>A ComplexInstance representing parameters.</returns> public object ReadParameters(ODataMessageReaderTestWrapper messageReader, IEdmOperationImport functionImport) { // TODO: ODataLib test item: Add new ODataPayloadElement for parameters payload ODataParameterReader parameterReader = messageReader.CreateODataParameterReader(functionImport); ODataParameters odataParameters = new ODataParameters(); List<ODataProperty> parameters = new List<ODataProperty>(); try { // read the start of the parameters this.assert.AreEqual(ODataParameterReaderState.Start, parameterReader.State, "Reader states don't match."); while (parameterReader.Read()) { switch (parameterReader.State) { case ODataParameterReaderState.Value: odataParameters.Add(new KeyValuePair<string,object>(parameterReader.Name, parameterReader.Value)); break; case ODataParameterReaderState.Entry: ODataReader entryReader = parameterReader.CreateEntryReader(); entryReader.Read(); this.assert.AreEqual(ODataReaderState.EntryStart, entryReader.State, "Reader states don't match."); odataParameters.Add(new KeyValuePair<string,object>(parameterReader.Name, this.ReadEntry(entryReader))); this.assert.AreEqual(ODataReaderState.EntryEnd, entryReader.State, "Reader states don't match."); this.assert.IsFalse(entryReader.Read(), "Read() should return false after EntryEnd."); this.assert.AreEqual(ODataReaderState.Completed, entryReader.State, "Reader states don't match."); break; case ODataParameterReaderState.Feed: ODataReader feedReader = parameterReader.CreateFeedReader(); feedReader.Read(); this.assert.AreEqual(ODataReaderState.FeedStart, feedReader.State, "Reader states don't match."); odataParameters.Add(new KeyValuePair<string,object>(parameterReader.Name, this.ReadFeed(feedReader))); this.assert.AreEqual(ODataReaderState.FeedEnd, feedReader.State, "Reader states don't match."); this.assert.IsFalse(feedReader.Read(), "Read() should return false after EntryEnd."); this.assert.AreEqual(ODataReaderState.Completed, feedReader.State, "Reader states don't match."); break; case ODataParameterReaderState.Collection: ODataCollectionReader collectionReader = parameterReader.CreateCollectionReader(); odataParameters.Add(new KeyValuePair<string,object>(parameterReader.Name, this.ReadCollection(collectionReader))); break; default: this.assert.Fail("Unexpected state: " + parameterReader.State); break; } } this.assert.AreEqual(ODataParameterReaderState.Completed, parameterReader.State, "Reader states don't match."); return odataParameters; } catch (Exception e) { if (ExceptionUtilities.IsCatchable(e)) { this.assert.AreEqual(ODataParameterReaderState.Exception, parameterReader.State, "Expected the reader to be in 'Exception' state."); } throw; } }
/// <summary> /// Visits a collection of parameters. /// </summary> /// <param name="parameters">The parameters to visit.</param> protected override ODataPayloadElement VisitParameters(ODataParameters parameters) { ExceptionUtilities.CheckArgumentNotNull(parameters, "parameters"); ComplexInstance result = new ComplexInstance(); result.IsNull = parameters.Count == 0; foreach (var parameter in parameters) { if (parameter.Value == null) { result.Add(new PrimitiveProperty(parameter.Key, null, null)); continue; } ODataComplexValue odataComplexValue = parameter.Value as ODataComplexValue; ODataCollectionStart odataCollectionStart = parameter.Value as ODataCollectionStart; if (odataCollectionStart != null) { ODataCollectionItemsObjectModelAnnotation annotation = odataCollectionStart.GetAnnotation <ODataCollectionItemsObjectModelAnnotation>(); if (annotation.OfType <ODataComplexValue>().FirstOrDefault() != null) { ComplexMultiValue complexCollection = PayloadBuilder.ComplexMultiValue(); foreach (var value in annotation) { complexCollection.Item(this.VisitComplexValue(value as ODataComplexValue) as ComplexInstance); } ComplexMultiValueProperty complexCollectionProperty = new ComplexMultiValueProperty(parameter.Key, complexCollection); result.Add(complexCollectionProperty); } else { PrimitiveMultiValue primitiveCollection = PayloadBuilder.PrimitiveMultiValue(); foreach (var value in annotation) { primitiveCollection.Item(value); } PrimitiveMultiValueProperty primitiveCollectionProperty = new PrimitiveMultiValueProperty(parameter.Key, primitiveCollection); result.Add(primitiveCollectionProperty); } } else if (odataComplexValue != null) { ComplexInstance complexInstance = PayloadBuilder.ComplexValue(odataComplexValue.TypeName); complexInstance.IsNull = false; foreach (ODataProperty odataProperty in odataComplexValue.Properties) { complexInstance.Property(odataProperty.Name, this.Visit(odataProperty.Value)); } result.Add(new ComplexProperty(parameter.Key, complexInstance)); } else { result.Add(new PrimitiveProperty(parameter.Key, null, PayloadBuilder.PrimitiveValue(parameter.Value).ClrValue)); } } return(result); }
private void WriteParameters(ODataMessageWriterTestWrapper messageWriter, ODataParameters parameters, IEdmOperationImport functionImport) { ODataParameterWriter parameterWriter = messageWriter.CreateODataParameterWriter(functionImport); parameterWriter.WriteStart(); foreach (var parameter in parameters) { ODataCollectionStart collectionStart = parameter.Value as ODataCollectionStart; ODataFeed feed; ODataEntry entry; if (collectionStart != null) { ODataCollectionWriter collectionWriter = parameterWriter.CreateCollectionWriter(parameter.Key); this.WriteCollection(collectionWriter, collectionStart); collectionWriter.Flush(); } else if ((feed = parameter.Value as ODataFeed) != null) { this.WriteFeed(parameterWriter.CreateFeedWriter(parameter.Key), feed); } else if ((entry = parameter.Value as ODataEntry) != null) { this.WriteEntry(parameterWriter.CreateEntryWriter(parameter.Key), entry); } else { parameterWriter.WriteValue(parameter.Key, parameter.Value); } } parameterWriter.WriteEnd(); parameterWriter.Flush(); }
/// <summary> /// Visits an item in the object model. /// </summary> /// <param name="objectModelItem">The item to visit.</param> public virtual void Visit(object objectModelItem) { ODataFeed feed = objectModelItem as ODataFeed; if (feed != null) { this.VisitFeed(feed); return; } ODataEntry entry = objectModelItem as ODataEntry; if (entry != null) { this.VisitEntry(entry); return; } ODataProperty property = objectModelItem as ODataProperty; if (property != null) { this.VisitProperty(property); return; } ODataNavigationLink navigationLink = objectModelItem as ODataNavigationLink; if (navigationLink != null) { this.VisitNavigationLink(navigationLink); return; } ODataComplexValue complexValue = objectModelItem as ODataComplexValue; if (complexValue != null) { this.VisitComplexValue(complexValue); return; } ODataCollectionValue collectionValue = objectModelItem as ODataCollectionValue; if (collectionValue != null) { this.VisitCollectionValue(collectionValue); return; } ODataStreamReferenceValue streamReferenceValue = objectModelItem as ODataStreamReferenceValue; if (streamReferenceValue != null) { this.VisitStreamReferenceValue(streamReferenceValue); return; } ODataCollectionStart collectionStart = objectModelItem as ODataCollectionStart; if (collectionStart != null) { this.VisitCollectionStart(collectionStart); return; } ODataServiceDocument serviceDocument = objectModelItem as ODataServiceDocument; if (serviceDocument != null) { this.VisitServiceDocument(serviceDocument); return; } ODataEntitySetInfo entitySetInfo = objectModelItem as ODataEntitySetInfo; if (entitySetInfo != null) { this.VisitEntitySet(entitySetInfo); return; } ODataError error = objectModelItem as ODataError; if (error != null) { this.VisitError(error); return; } ODataInnerError innerError = objectModelItem as ODataInnerError; if (innerError != null) { this.VisitInnerError(innerError); return; } ODataEntityReferenceLinks entityReferenceLinks = objectModelItem as ODataEntityReferenceLinks; if (entityReferenceLinks != null) { this.VisitEntityReferenceLinks(entityReferenceLinks); return; } ODataEntityReferenceLink entityReferenceLink = objectModelItem as ODataEntityReferenceLink; if (entityReferenceLink != null) { this.VisitEntityReferenceLink(entityReferenceLink); return; } ODataAction action = objectModelItem as ODataAction; if (action != null) { this.VisitODataOperation(action); return; } ODataFunction function = objectModelItem as ODataFunction; if (function != null) { this.VisitODataOperation(function); return; } ODataParameters parameters = objectModelItem as ODataParameters; if (parameters != null) { this.VisitParameters(parameters); return; } ODataBatch batch = objectModelItem as ODataBatch; if (batch != null) { this.VisitBatch(batch); return; } ODataBatchChangeset batchChangeset = objectModelItem as ODataBatchChangeset; if (batchChangeset != null) { this.VisitBatchChangeset(batchChangeset); return; } ODataBatchRequestOperation batchRequestOperation = objectModelItem as ODataBatchRequestOperation; if (batchRequestOperation != null) { this.VisitBatchRequestOperation(batchRequestOperation); return; } ODataBatchResponseOperation batchResponseOperation = objectModelItem as ODataBatchResponseOperation; if (batchResponseOperation != null) { this.VisitBatchResponseOperation(batchResponseOperation); return; } if (objectModelItem == null || objectModelItem.GetType().IsValueType || objectModelItem is string || objectModelItem is byte[] || objectModelItem is ISpatial) { this.VisitPrimitiveValue(objectModelItem); return; } this.VisitUnsupportedValue(objectModelItem); }