internal static object GetNodeData(Node node, Type type) { try { if (type == typeof(string) && node.Value == MultiLineStringNode.Terminator && node.ChildLines.Count > 0) { return(BaseTypes.ParseSpecialStringCase(node)); } if (BaseTypes.IsBaseType(type)) { return(BaseTypes.ParseBaseType(node.Value, type)); } var collection = CollectionTypes.TryGetCollection(node, type); if (collection != null) { return(collection); } if (!String.IsNullOrEmpty(node.Value)) { return(ComplexTypeShortcuts.GetFromShortcut(node.Value, type)); } return(ComplexTypes.RetrieveComplexType(node, type)); } catch (Exception e) { throw new Exception($"Error getting data of type {type} from node: {e.Message}"); } }
internal static void SetNodeData(Node node, object data, Type type, FileStyle style) { if (data == null) { throw new Exception("you can't serialize null"); } string dataAsString = data as string; if (type == typeof(string) && (dataAsString.ContainsNewLine() || node.ChildNodes.Count > 0)) { BaseTypes.SerializeSpecialStringCase(dataAsString, node, style); } else if (BaseTypes.IsBaseType(type)) { node.Value = BaseTypes.SerializeBaseType(data, type, style); } else if (CollectionTypes.TrySetCollection(node, data, type, style)) { return; } else { ComplexTypes.SetComplexNode(node, data, type, style); } }
public void OmitCollections(List <AllSearchableObjects> ommisions) { foreach (var item in ommisions) { CollectionTypes.Remove(item); } RaisePropertyChanged("CollectionTypes"); ChosenType = CollectionTypes.First().Key; SearchCollectionExecute(); }
/// <summary> /// Registers a given SimplType into one of the relevant type registries. /// </summary> /// <param name="type">Type to register</param> /// <returns>True if it succeded, false if it was not registered, exception if an issue occurred or the type would not fit in a category.</returns> public static bool RegisterSimplType(SimplType type) { bool result = false; result |= ScalarTypes.TryAdd(type); result |= CollectionTypes.TryAdd(type); result |= CompositeTypes.TryAdd(type); result |= SimplTypes.TryAdd(type); return(result); }
//protected override Expression VisitParameter(ParameterExpression p) //{ // if (p.Type.IsGenericType && p.Type.GetGenericTypeDefinition() == typeof(IGrouping<,>)) // _isBlocked = true; // return base.VisitParameter(p); //} private static bool IsCollection(Type type) { //HACK: this is going to generally subvert custom objects that implement ICollection or ICollection<T>, //but are not collections if (type.IsGenericType) { type = type.GetGenericTypeDefinition(); } return(CollectionTypes.Any(x => x.IsAssignableFrom(type))); }
public void Deserialize_Collections() { string input = "{ " + "\"__typeAlias\" : \"CollectionTypes\", " + "\"ListOfInt\" : [" + "1, " + "3, " + "5" + "], " + "\"CollectionOfString\" : [" + "\"1\", " + "\"3\", " + "\"5\"" + "], " + "\"ImmutableListOfDouble\" : [" + "1.0, " + "3.0, " + "5.0" + "] " + "\"ListOfCustom\" : [" + "{ " + "\"__typeAlias\" : \"CustomType\", " + "\"String\" : \"Custom1\", " + "\"Double\" : 128.0 " + "}, " + "{ " + "\"__typeAlias\" : \"CustomType\", " + "\"String\" : \"Custom2\", " + "\"Double\" : 256.0 " + "}" + "] " + "}"; CollectionTypes result = Deserialize <CollectionTypes>(input); Assert.NotNull(result); Assert.Equal(new [] { 1, 3, 5 }, result.ListOfInt); Assert.Equal(new [] { "1", "3", "5" }, result.CollectionOfString); Assert.Equal(new [] { 1.0, 3.0, 5.0 }, result.ImmutableListOfDouble); Assert.Collection(result.ListOfCustom, type => { Assert.Equal("Custom1", type.String); Assert.Equal(128.0, type.Double); }, type => { Assert.Equal("Custom2", type.String); Assert.Equal(256.0, type.Double); }); }
string GetGenericTypeSuffix(CollectionTypes type) { switch (type) { case CollectionTypes.List: case CollectionTypes.LinkedList: case CollectionTypes.ObservableCollection: case CollectionTypes.Collection: case CollectionTypes.BindingList: return("`1"); } return(String.Empty); }
/// <summary> /// Gets the hash code /// </summary> /// <returns>Hash code</returns> public override int GetHashCode() { unchecked // Overflow is fine, just wrap { var hashCode = 41; // Suitable nullity checks etc, of course :) if (DavRoot != null) { hashCode = hashCode * 59 + DavRoot.GetHashCode(); } if (DavCreateAbsoluteUri != null) { hashCode = hashCode * 59 + DavCreateAbsoluteUri.GetHashCode(); } if (DavRealm != null) { hashCode = hashCode * 59 + DavRealm.GetHashCode(); } if (CollectionTypes != null) { hashCode = hashCode * 59 + CollectionTypes.GetHashCode(); } if (FilterPrefixes != null) { hashCode = hashCode * 59 + FilterPrefixes.GetHashCode(); } if (FilterTypes != null) { hashCode = hashCode * 59 + FilterTypes.GetHashCode(); } if (FilterUris != null) { hashCode = hashCode * 59 + FilterUris.GetHashCode(); } if (TypeCollections != null) { hashCode = hashCode * 59 + TypeCollections.GetHashCode(); } if (TypeNoncollections != null) { hashCode = hashCode * 59 + TypeNoncollections.GetHashCode(); } if (TypeContent != null) { hashCode = hashCode * 59 + TypeContent.GetHashCode(); } return(hashCode); } }
internal Task CreateIngestQueryDeleteAsync <T>( ConnectionModes connectionModes, CollectionTypes collectionTypes, IEnumerable <string> documents, Query <T> query, T testArgs, string partitionKey = "/id", Cosmos.IndexingPolicy indexingPolicy = null, CosmosClientFactory cosmosClientFactory = null) { return(this.CreateIngestQueryDeleteAsync( connectionModes, collectionTypes, documents, query, cosmosClientFactory ?? this.CreateDefaultCosmosClient, testArgs, partitionKey, indexingPolicy)); }
public void Serialize_Collections() { CollectionTypes collectionTypes = new CollectionTypes(); string result = Serialize(collectionTypes); Assert.NotNull(result); Assert.Equal( "{ " + "\"__typeAlias\" : \"CollectionTypes\", " + "\"ListOfInt\" : [" + "1, " + "3, " + "5" + "], " + "\"CollectionOfString\" : [" + "\"1\", " + "\"3\", " + "\"5\"" + "], " + "\"ImmutableListOfDouble\" : [" + "1.0, " + "3.0, " + "5.0" + "], " + "\"ListOfCustom\" : [" + "{ " + "\"__typeAlias\" : \"CustomType\", " + "\"String\" : \"Custom1\", " + "\"Double\" : 128.0 " + "}, " + "{ " + "\"__typeAlias\" : \"CustomType\", " + "\"String\" : \"Custom2\", " + "\"Double\" : 256.0 " + "}" + "] " + "}", result); }
private CloneDelegate <TEntity> AssembleExpression <TEntity>(IExpressionBuilder expressionBuilder) where TEntity : class { CloneExpressionContext context = new CloneExpressionContext { SourceEntity = expressionBuilder.AddParameter <TEntity>(nameof(CloneExpressionContext.SourceEntity)), CloneFactory = expressionBuilder.AddParameter <ICloneFactory>(nameof(CloneExpressionContext.CloneFactory)), }; if (CollectionTypes.Any(collectionType => IsCollectionType(collectionType, typeof(TEntity)))) { AddCollectionExpressions <TEntity>(expressionBuilder, context); } else { AddCloneExpressions <TEntity>(expressionBuilder, context); } expressionBuilder.ReturnObject(context.TargetEntity); return(expressionBuilder.Build <CloneDelegate <TEntity> >()); }
internal Task CreateIngestQueryDeleteAsync( ConnectionModes connectionModes, CollectionTypes collectionTypes, IEnumerable <string> documents, Query query, string partitionKey = "/id", Cosmos.IndexingPolicy indexingPolicy = null, CosmosClientFactory cosmosClientFactory = null) { Task queryWrapper(Container container, IReadOnlyList <CosmosObject> inputDocuments, object throwaway) { return(query(container, inputDocuments)); } return(this.CreateIngestQueryDeleteAsync <object>( connectionModes, collectionTypes, documents, queryWrapper, null, partitionKey, indexingPolicy, cosmosClientFactory)); }
/// <summary> /// Maps the properties of the values read from the VDF file, to the object created from the specified type /// </summary> /// <param name="targetObjectType">Type of the object to map to</param> /// <param name="source">Source from which to read the value</param> /// <param name="isDictionaryType">Is the targetObject a real dictionary?</param> /// <returns></returns> //We could have used a generic method, but because the type will be reflected when it is called recursively, this is the easier solution private object Map(Type targetObjectType, ExpandoObject source, CollectionTypes colType = CollectionTypes.None) { var src = source as IDictionary <string, dynamic>; object returnObj; // if it is a dictionary, we need to jump throuh some reflection hoops to instantiate the correct types if (colType == CollectionTypes.Dictionary) { // Instantiate the correct dictionary type Type[] dictionaryTypes = targetObjectType.GetGenericArguments(); Type genericDictionaryType = typeof(Dictionary <,>); Type constructedDictionaryType = genericDictionaryType.MakeGenericType(dictionaryTypes); var returnDictionary = Activator.CreateInstance(constructedDictionaryType); // Loop through the values in the source, and add them to the dictionary var keys = src.Keys; foreach (var key in keys) { var value = src[key]; // If the value type is string, then we don't need to instantiate the value and go through the reflection code again. if (value.GetType() == typeof(string)) { var arguments = new object[] { key, value }; constructedDictionaryType.InvokeMember("Add", BindingFlags.InvokeMethod, null, returnDictionary, arguments); } else // It is a complex object, so we need to instantiate and map to a new object before adding it to the dictionary { var arguments = new object[] { key, Map(dictionaryTypes[1], value, CollectionTypes.None) // dictionaryTypes[1] is the specified type it should be cast to. }; constructedDictionaryType.InvokeMember("Add", BindingFlags.InvokeMethod, null, returnDictionary, arguments); } } returnObj = (object)returnDictionary; } else if (colType == CollectionTypes.List) { Type[] listTypes = targetObjectType.GetGenericArguments(); Type genericListType = typeof(List <>); Type constructedListType = genericListType.MakeGenericType(listTypes); var returnList = Activator.CreateInstance(constructedListType); var keys = src.Keys; // Should only be indexes foreach (var key in keys) { var value = src[key]; // If the value type is string, then we don't need to instantiate the value and go through the reflection code again. if (value.GetType() == typeof(string)) { var arguments = new object[] { value }; constructedListType.InvokeMember("Add", BindingFlags.InvokeMethod, null, returnList, arguments); } else // It is a complex object, so we need to instantiate and map to a new object before adding it to the List { var arguments = new object[] { Map(listTypes[0], value, CollectionTypes.None) // dictionaryTypes[1] is the specified type it should be cast to. }; constructedListType.InvokeMember("Add", BindingFlags.InvokeMethod, null, returnList, arguments); } } returnObj = (object)returnList; } else // It is not a dictionary, so it is a simple property on an object that should be assigned a value. { returnObj = Activator.CreateInstance(targetObjectType); foreach (PropertyInfo destinationProperty in targetObjectType.GetProperties().Where(x => x.CanWrite)) { SetProperty(destinationProperty, src, returnObj); } } return(returnObj); }
private async Task <(Container, IReadOnlyList <CosmosObject>)> CreateContainerAndIngestDocumentsAsync( CollectionTypes collectionType, IEnumerable <string> documents, string partitionKey = "/id", Cosmos.IndexingPolicy indexingPolicy = null) { Container container = collectionType switch { CollectionTypes.NonPartitioned => await this.CreateNonPartitionedContainerAsync(indexingPolicy), CollectionTypes.SinglePartition => await this.CreateSinglePartitionContainer(partitionKey, indexingPolicy), CollectionTypes.MultiPartition => await this.CreateMultiPartitionContainer(partitionKey, indexingPolicy), _ => throw new ArgumentException($"Unknown {nameof(CollectionTypes)} : {collectionType}"), }; List <CosmosObject> insertedDocuments = new List <CosmosObject>(); foreach (string document in documents) { JObject documentObject = JsonConvert.DeserializeObject <JObject>(document); // Add an id if (documentObject["id"] == null) { documentObject["id"] = Guid.NewGuid().ToString(); } // Get partition key value. Cosmos.PartitionKey pkValue; if (partitionKey != null) { string jObjectPartitionKey = partitionKey.Remove(0, 1); JValue pkToken = (JValue)documentObject[jObjectPartitionKey]; if (pkToken == null) { pkValue = Cosmos.PartitionKey.None; } else { switch (pkToken.Type) { case JTokenType.Integer: case JTokenType.Float: pkValue = new Cosmos.PartitionKey(pkToken.Value <double>()); break; case JTokenType.String: pkValue = new Cosmos.PartitionKey(pkToken.Value <string>()); break; case JTokenType.Boolean: pkValue = new Cosmos.PartitionKey(pkToken.Value <bool>()); break; case JTokenType.Null: pkValue = Cosmos.PartitionKey.Null; break; default: throw new ArgumentException("Unknown partition key type"); } } } else { pkValue = Cosmos.PartitionKey.None; } JObject createdDocument = await container.CreateItemAsync(documentObject, pkValue); CosmosObject insertedDocument = CosmosObject.Parse <CosmosObject>(createdDocument.ToString()); insertedDocuments.Add(insertedDocument); } return(container, insertedDocuments); }
public static bool IsDictionary(this CollectionTypes collectionType) { return(collectionType == CollectionTypes.Dictionary || collectionType == CollectionTypes.HashSet); }
public CollectionField(CollectionTypes collectionType) { CollectionType = collectionType; }
private ObservableCollection <CollectionItemViewModel> PopulateCollectionData(CollectionTypes types) { var populatedData = new ObservableCollection <CollectionItemViewModel>(); var titles = new List <string>(); var coverUrls = new List <string>(); var descriptions = new List <string>(); switch (types) { case CollectionTypes.SingAlong: titles = new List <string> { "Songs to Sing in the Car", "So You Think You Can Sing", "Sing Along K-Pop", "Sing-along: 90's to Now" }; coverUrls = new List <string> { "sing_along1.jpeg", "sing_along2.jpeg", "sing_along3.jpeg", "sing_along4.jpeg" }; descriptions = new List <string> { "Shawn Mendes, Lady Gaga, Ariana Grande, Khalid, Queen", "Calum Scott, Beyoncé, Sia, Bruno Mars, David Guetta", "BTS, BLACKPINK, EXO, Red Velvet, SHINee, BIGBANG", "Kelly Clarkson, Beyoncé, Miley Cyrus, Rihanna, Ne-Yo" }; break; case CollectionTypes.MadeForUser: titles = new List <string> { "Daily Mix 1", "Daily Mix 2", "Daily Mix 3", "Daily Mix 4" }; coverUrls = new List <string> { "daily_mix1.jpeg", "daily_mix2.jpeg", "daily_mix3.jpeg", "daily_mix4.jpeg" }; descriptions = new List <string> { "JJ Lin, Leehom Wang, Kimberley Chen and more", "Glee Cast, Shawn Mendes, Katy Perry and more", "A.R. Rahman, Penn Masala, Pritam and more", "Andra & The Backbone, Gita Gutawa, Ungu and more" }; break; case CollectionTypes.RecentlyPlayed: titles = new List <string> { "This is Shawn Mendes", "This is JJ Lin", "All Out 00s", "Blast from the Past" }; coverUrls = new List <string> { "recently_played1.jpeg", "recently_played2.jpeg", "recently_played3.jpeg", "recently_played4.jpeg" }; descriptions = new List <string> { "", "", "", "" }; break; } for (int i = 0; i < titles.Count; i++) { populatedData.Add(new CollectionItemViewModel { Title = titles[i], CoverUrl = coverUrls[i], Description = descriptions[i], DescRowHeight = string.IsNullOrWhiteSpace(descriptions[i]) ? new GridLength(30) : new GridLength(50) }); } return(populatedData); }
/// <summary> /// Task that wraps boiler plate code for query tests (container create -> ingest documents -> query documents -> delete collections). /// Note that this function will take the cross product connectionModes /// </summary> /// <param name="connectionModes">The connection modes to use.</param> /// <param name="documents">The documents to ingest</param> /// <param name="query"> /// The callback for the queries. /// All the standard arguments will be passed in. /// Please make sure that this function is idempotent, since a container will be reused for each connection mode. /// </param> /// <param name="cosmosClientFactory"> /// The callback for the create CosmosClient. This is invoked for the different ConnectionModes that the query is targeting. /// If CosmosClient instantiated by this does not apply the expected ConnectionMode, an assert is thrown. /// </param> /// <param name="partitionKey">The partition key for the partition container.</param> /// <param name="testArgs">The optional args that you want passed in to the query.</param> /// <returns>A task to await on.</returns> internal async Task CreateIngestQueryDeleteAsync <T>( ConnectionModes connectionModes, CollectionTypes collectionTypes, IEnumerable <string> documents, Query <T> query, CosmosClientFactory cosmosClientFactory, T testArgs, string partitionKey = "/id", Cosmos.IndexingPolicy indexingPolicy = null) { try { IList <(Container, IReadOnlyList <CosmosObject>)> collectionsAndDocuments = new List <(Container, IReadOnlyList <CosmosObject>)>(); foreach (CollectionTypes collectionType in Enum.GetValues(collectionTypes.GetType()).Cast <Enum>().Where(collectionTypes.HasFlag)) { if (collectionType == CollectionTypes.None) { continue; } Task <(Container, IReadOnlyList <CosmosObject>)> createContainerTask = collectionType switch { CollectionTypes.NonPartitioned => this.CreateNonPartitionedContainerAndIngestDocumentsAsync( documents, indexingPolicy), CollectionTypes.SinglePartition => this.CreateSinglePartitionContainerAndIngestDocumentsAsync( documents, partitionKey, indexingPolicy), CollectionTypes.MultiPartition => this.CreateMultiPartitionContainerAndIngestDocumentsAsync( documents, partitionKey, indexingPolicy), _ => throw new ArgumentException($"Unknown {nameof(CollectionTypes)} : {collectionType}"), }; collectionsAndDocuments.Add(await createContainerTask); } List <CosmosClient> cosmosClients = new List <CosmosClient>(); foreach (ConnectionModes connectionMode in Enum.GetValues(connectionModes.GetType()).Cast <Enum>().Where(connectionModes.HasFlag)) { if (connectionMode == ConnectionModes.None) { continue; } ConnectionMode targetConnectionMode = GetTargetConnectionMode(connectionMode); CosmosClient cosmosClient = cosmosClientFactory(targetConnectionMode); Assert.AreEqual( targetConnectionMode, cosmosClient.ClientOptions.ConnectionMode, "Test setup: Invalid connection policy applied to CosmosClient"); cosmosClients.Add(cosmosClient); } List <Task> queryTasks = new List <Task>(); foreach (CosmosClient cosmosClient in cosmosClients) { foreach ((Container container, IReadOnlyList <CosmosObject> insertedDocuments) in collectionsAndDocuments) { Task queryTask = Task.Run(() => query(container, insertedDocuments, testArgs)); queryTasks.Add(queryTask); } } await Task.WhenAll(queryTasks); List <Task <ContainerResponse> > deleteContainerTasks = new List <Task <ContainerResponse> >(); foreach (Container container in collectionsAndDocuments.Select(tuple => tuple.Item1)) { deleteContainerTasks.Add(container.DeleteContainerAsync()); } await Task.WhenAll(deleteContainerTasks); } catch (Exception ex) when(ex.GetType() != typeof(AssertFailedException)) { while (ex.InnerException != null) { ex = ex.InnerException; } ExceptionDispatchInfo.Capture(ex).Throw(); } }
string GetGenericTypeSuffix(CollectionTypes type) { switch (type) { case CollectionTypes.List: case CollectionTypes.LinkedList: case CollectionTypes.ObservableCollection: case CollectionTypes.Collection: case CollectionTypes.BindingList: return "`1"; } return String.Empty; }
/// <summary> /// Returns true if OrgApacheSlingJcrWebdavImplServletsSimpleWebDavServletProperties instances are equal /// </summary> /// <param name="other">Instance of OrgApacheSlingJcrWebdavImplServletsSimpleWebDavServletProperties to be compared</param> /// <returns>Boolean</returns> public bool Equals(OrgApacheSlingJcrWebdavImplServletsSimpleWebDavServletProperties other) { if (other is null) { return(false); } if (ReferenceEquals(this, other)) { return(true); } return (( DavRoot == other.DavRoot || DavRoot != null && DavRoot.Equals(other.DavRoot) ) && ( DavCreateAbsoluteUri == other.DavCreateAbsoluteUri || DavCreateAbsoluteUri != null && DavCreateAbsoluteUri.Equals(other.DavCreateAbsoluteUri) ) && ( DavRealm == other.DavRealm || DavRealm != null && DavRealm.Equals(other.DavRealm) ) && ( CollectionTypes == other.CollectionTypes || CollectionTypes != null && CollectionTypes.Equals(other.CollectionTypes) ) && ( FilterPrefixes == other.FilterPrefixes || FilterPrefixes != null && FilterPrefixes.Equals(other.FilterPrefixes) ) && ( FilterTypes == other.FilterTypes || FilterTypes != null && FilterTypes.Equals(other.FilterTypes) ) && ( FilterUris == other.FilterUris || FilterUris != null && FilterUris.Equals(other.FilterUris) ) && ( TypeCollections == other.TypeCollections || TypeCollections != null && TypeCollections.Equals(other.TypeCollections) ) && ( TypeNoncollections == other.TypeNoncollections || TypeNoncollections != null && TypeNoncollections.Equals(other.TypeNoncollections) ) && ( TypeContent == other.TypeContent || TypeContent != null && TypeContent.Equals(other.TypeContent) )); }