public static IndexDefinitionCollection GetIndexDefinitions(this TypeInfo type) { var definitions = new IndexDefinitionCollection(); GetIndexDefinitionsWorker(type, definitions, string.Empty); definitions .SetDocumentType(type.UnderlyingSystemType) .SetCollectionName(type.GetCollectionName()); return(definitions); }
private static void GetIndexDefinitionsWorker(TypeInfo type, IndexDefinitionCollection indexes, string fieldPrefix) { if (fieldPrefix.Split('.').Count() > 20) { throw new InvalidOperationException($"Tree is too deep - could be a recursion. Current 'path' is {fieldPrefix}"); } var definitions = new Dictionary <string, IndexDefinition>(); type .GetProperties() .Where(prop => IsIndexCandidate(prop)) .ToList() .ForEach(prop => { if (prop.HasAttribute(typeof(IndexAttribute))) { prop .GetSpecificAttribute(typeof(IndexAttribute)) .ForEach(a => { var attribute = (IndexAttribute)a; IndexDefinition def; if (!string.IsNullOrEmpty(attribute.Name) && definitions.ContainsKey(attribute.Name)) { def = definitions[attribute.Name]; } else { def = new IndexDefinition(); def.Name = attribute.Name; var lookupName = def.Name ?? prop.Name; definitions.Add(lookupName, def); } def.Update(attribute, $"{fieldPrefix}{prop.Name}"); }); } // if the type of the property could have its own // properties with [Index], we should look at those too if (IsSubDocument(prop.PropertyType)) { GetIndexDefinitionsWorker(prop.PropertyType.GetTypeInfo(), indexes, $"{fieldPrefix}{prop.Name}."); } }); indexes.AddRange(definitions.Values.ToList()); }