| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. See License.txt in the project root for |
| | 3 | | // license information. |
| | 4 | |
|
| | 5 | | using System; |
| | 6 | | using System.Collections.Generic; |
| | 7 | | using System.Linq; |
| | 8 | | using System.Reflection; |
| | 9 | | using Microsoft.Azure.Search.Models; |
| | 10 | | using Microsoft.Spatial; |
| | 11 | | using Newtonsoft.Json.Serialization; |
| | 12 | | using Newtonsoft.Json; |
| | 13 | | using System.Collections.ObjectModel; |
| | 14 | |
|
| | 15 | | namespace Microsoft.Azure.Search |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Builds field definitions for a search index by reflecting over a user-defined model type. |
| | 19 | | /// </summary> |
| | 20 | | public static class FieldBuilder |
| | 21 | | { |
| 2 | 22 | | private static readonly IReadOnlyDictionary<Type, DataType> PrimitiveTypeMap = |
| 2 | 23 | | new ReadOnlyDictionary<Type, DataType>( |
| 2 | 24 | | new Dictionary<Type, DataType>() |
| 2 | 25 | | { |
| 2 | 26 | | [typeof(string)] = DataType.String, |
| 2 | 27 | | [typeof(int)] = DataType.Int32, |
| 2 | 28 | | [typeof(long)] = DataType.Int64, |
| 2 | 29 | | [typeof(double)] = DataType.Double, |
| 2 | 30 | | [typeof(bool)] = DataType.Boolean, |
| 2 | 31 | | [typeof(DateTime)] = DataType.DateTimeOffset, |
| 2 | 32 | | [typeof(DateTimeOffset)] = DataType.DateTimeOffset, |
| 2 | 33 | | [typeof(GeographyPoint)] = DataType.GeographyPoint |
| 2 | 34 | | }); |
| | 35 | |
|
| 8 | 36 | | private static IContractResolver CamelCaseResolver { get; } = new CamelCasePropertyNamesContractResolver(); |
| | 37 | |
|
| 432 | 38 | | private static IContractResolver DefaultResolver { get; } = new DefaultContractResolver(); |
| | 39 | |
|
| | 40 | | /// <summary> |
| | 41 | | /// Creates a collection of <see cref="Field"/> objects corresponding to |
| | 42 | | /// the properties of the type supplied. |
| | 43 | | /// </summary> |
| | 44 | | /// <typeparam name="T"> |
| | 45 | | /// The type for which fields will be created, based on its properties. |
| | 46 | | /// </typeparam> |
| | 47 | | /// <returns>A collection of fields.</returns> |
| 18 | 48 | | public static IList<Field> BuildForType<T>() => BuildForType(typeof(T)); |
| | 49 | |
|
| | 50 | | /// <summary> |
| | 51 | | /// Creates a collection of <see cref="Field"/> objects corresponding to |
| | 52 | | /// the properties of the type supplied. |
| | 53 | | /// </summary> |
| | 54 | | /// <param name="modelType"> |
| | 55 | | /// The type for which fields will be created, based on its properties. |
| | 56 | | /// </param> |
| | 57 | | /// <returns>A collection of fields.</returns> |
| | 58 | | public static IList<Field> BuildForType(Type modelType) |
| | 59 | | { |
| 436 | 60 | | bool useCamelCase = SerializePropertyNamesAsCamelCaseAttribute.IsDefinedOnType(modelType); |
| 436 | 61 | | IContractResolver resolver = useCamelCase |
| 436 | 62 | | ? CamelCaseResolver |
| 436 | 63 | | : DefaultResolver; |
| 436 | 64 | | return BuildForType(modelType, resolver); |
| | 65 | | } |
| | 66 | |
|
| | 67 | | /// <summary> |
| | 68 | | /// Creates a collection of <see cref="Field"/> objects corresponding to |
| | 69 | | /// the properties of the type supplied. |
| | 70 | | /// </summary> |
| | 71 | | /// <typeparam name="T"> |
| | 72 | | /// The type for which fields will be created, based on its properties. |
| | 73 | | /// </typeparam> |
| | 74 | | /// <param name="contractResolver"> |
| | 75 | | /// Contract resolver that the SearchIndexClient will use. |
| | 76 | | /// This ensures that the field names are generated in a way that is |
| | 77 | | /// consistent with the way the model will be serialized. |
| | 78 | | /// </param> |
| | 79 | | /// <returns>A collection of fields.</returns> |
| 0 | 80 | | public static IList<Field> BuildForType<T>(IContractResolver contractResolver) => BuildForType(typeof(T), contra |
| | 81 | |
|
| | 82 | | /// <summary> |
| | 83 | | /// Creates a collection of <see cref="Field"/> objects corresponding to |
| | 84 | | /// the properties of the type supplied. |
| | 85 | | /// </summary> |
| | 86 | | /// <param name="modelType"> |
| | 87 | | /// The type for which fields will be created, based on its properties. |
| | 88 | | /// </param> |
| | 89 | | /// <param name="contractResolver"> |
| | 90 | | /// Contract resolver that the SearchIndexClient will use. |
| | 91 | | /// This ensures that the field names are generated in a way that is |
| | 92 | | /// consistent with the way the model will be serialized. |
| | 93 | | /// </param> |
| | 94 | | /// <returns>A collection of fields.</returns> |
| | 95 | | public static IList<Field> BuildForType(Type modelType, IContractResolver contractResolver) |
| | 96 | | { |
| | 97 | | ArgumentException FailOnNonObjectDataType() |
| | 98 | | { |
| 20 | 99 | | string errorMessage = |
| 20 | 100 | | $"Type '{modelType}' does not have properties which map to fields of an Azure Search index. Please u |
| 20 | 101 | | "class or struct with public properties."; |
| | 102 | |
|
| 20 | 103 | | throw new ArgumentException(errorMessage, nameof(modelType)); |
| | 104 | | } |
| | 105 | |
|
| 822 | 106 | | if (contractResolver.ResolveContract(modelType) is JsonObjectContract contract) |
| | 107 | | { |
| 804 | 108 | | if (contract.Properties.Count == 0) |
| | 109 | | { |
| 2 | 110 | | throw FailOnNonObjectDataType(); |
| | 111 | | } |
| | 112 | |
|
| | 113 | | // Use Stack to avoid a dependency on ImmutableStack for now. |
| 802 | 114 | | return BuildForTypeRecursive(modelType, contract, contractResolver, new Stack<Type>(new[] { modelType }) |
| | 115 | | } |
| | 116 | |
|
| 18 | 117 | | throw FailOnNonObjectDataType(); |
| | 118 | | } |
| | 119 | |
|
| | 120 | | private static IList<Field> BuildForTypeRecursive( |
| | 121 | | Type modelType, |
| | 122 | | JsonObjectContract contract, |
| | 123 | | IContractResolver contractResolver, |
| | 124 | | Stack<Type> processedTypes) |
| | 125 | | { |
| | 126 | | Field BuildField(JsonProperty prop) |
| | 127 | | { |
| | 128 | | bool ShouldIgnore(Attribute attribute) => |
| 78554 | 129 | | attribute is JsonIgnoreAttribute || attribute is FieldBuilderIgnoreAttribute; |
| | 130 | |
|
| 73236 | 131 | | IList<Attribute> attributes = prop.AttributeProvider.GetAttributes(true); |
| 73236 | 132 | | if (attributes.Any(ShouldIgnore)) |
| | 133 | | { |
| 776 | 134 | | return null; |
| | 135 | | } |
| | 136 | |
|
| | 137 | | Field CreateComplexField(DataType dataType, Type underlyingClrType, JsonObjectContract jsonObjectContrac |
| | 138 | | { |
| | 139 | | try |
| | 140 | | { |
| 9164 | 141 | | if (processedTypes.Contains(underlyingClrType)) |
| | 142 | | { |
| | 143 | | // Skip recursive types. |
| 4 | 144 | | return null; |
| | 145 | | } |
| | 146 | |
|
| 9160 | 147 | | processedTypes.Push(underlyingClrType); |
| 9160 | 148 | | IList<Field> subFields = |
| 9160 | 149 | | BuildForTypeRecursive(underlyingClrType, jsonObjectContract, contractResolver, processedType |
| 9160 | 150 | | return new Field(prop.PropertyName, dataType, subFields); |
| | 151 | | } |
| | 152 | | finally |
| | 153 | | { |
| 9164 | 154 | | processedTypes.Pop(); |
| 9164 | 155 | | } |
| 9164 | 156 | | } |
| | 157 | |
|
| | 158 | | Field CreateSimpleField(DataType dataType) |
| | 159 | | { |
| 63288 | 160 | | var field = new Field(prop.PropertyName, dataType); |
| | 161 | |
|
| 200024 | 162 | | foreach (Attribute attribute in attributes) |
| | 163 | | { |
| | 164 | | switch (attribute) |
| | 165 | | { |
| | 166 | | case IsSearchableAttribute _: |
| 10678 | 167 | | field.IsSearchable = true; |
| 10678 | 168 | | break; |
| | 169 | |
|
| | 170 | | case IsFilterableAttribute _: |
| 9974 | 171 | | field.IsFilterable = true; |
| 9974 | 172 | | break; |
| | 173 | |
|
| | 174 | | case IsSortableAttribute _: |
| 784 | 175 | | field.IsSortable = true; |
| 784 | 176 | | break; |
| | 177 | |
|
| | 178 | | case IsFacetableAttribute _: |
| 5358 | 179 | | field.IsFacetable = true; |
| 5358 | 180 | | break; |
| | 181 | |
|
| | 182 | | case IsRetrievableAttribute isRetrievableAttribute: |
| 1520 | 183 | | field.IsRetrievable = isRetrievableAttribute.IsRetrievable; |
| 1520 | 184 | | break; |
| | 185 | |
|
| | 186 | | case AnalyzerAttribute analyzerAttribute: |
| 5328 | 187 | | field.Analyzer = analyzerAttribute.Name; |
| 5328 | 188 | | break; |
| | 189 | |
|
| | 190 | | case SearchAnalyzerAttribute searchAnalyzerAttribute: |
| 760 | 191 | | field.SearchAnalyzer = searchAnalyzerAttribute.Name; |
| 760 | 192 | | break; |
| | 193 | |
|
| | 194 | | case IndexAnalyzerAttribute indexAnalyzerAttribute: |
| 760 | 195 | | field.IndexAnalyzer = indexAnalyzerAttribute.Name; |
| 760 | 196 | | break; |
| | 197 | |
|
| | 198 | | case SynonymMapsAttribute synonymMapsAttribute: |
| 760 | 199 | | field.SynonymMaps = synonymMapsAttribute.SynonymMaps; |
| 760 | 200 | | break; |
| | 201 | |
|
| | 202 | | default: |
| 802 | 203 | | Type attributeType = attribute.GetType(); |
| | 204 | |
|
| | 205 | | // Match on name to avoid dependency - don't want to force people not using |
| | 206 | | // this feature to bring in the annotations component. |
| | 207 | | // |
| | 208 | | // Also, ignore key attributes on sub-fields. |
| 802 | 209 | | if (attributeType.FullName == "System.ComponentModel.DataAnnotations.KeyAttribute" && |
| 802 | 210 | | processedTypes.Count <= 1) |
| | 211 | | { |
| 798 | 212 | | field.IsKey = true; |
| | 213 | | } |
| | 214 | | break; |
| | 215 | | } |
| | 216 | | } |
| | 217 | |
|
| 63288 | 218 | | return field; |
| | 219 | | } |
| | 220 | |
|
| | 221 | | ArgumentException FailOnUnknownDataType() |
| | 222 | | { |
| 8 | 223 | | string errorMessage = |
| 8 | 224 | | $"Property '{prop.PropertyName}' is of type '{prop.PropertyType}', which does not map to an " + |
| 8 | 225 | | "Azure Search data type. Please use a supported data type or mark the property with [JsonIgnore] |
| 8 | 226 | | "[FieldBuilderIgnore] and define the field by creating a Field object."; |
| | 227 | |
|
| 8 | 228 | | return new ArgumentException(errorMessage, nameof(modelType)); |
| | 229 | | } |
| | 230 | |
|
| 72460 | 231 | | IDataTypeInfo dataTypeInfo = GetDataTypeInfo(prop.PropertyType, contractResolver); |
| | 232 | |
|
| 72460 | 233 | | return dataTypeInfo.Match( |
| 72468 | 234 | | onUnknownDataType: () => throw FailOnUnknownDataType(), |
| 72460 | 235 | | onSimpleDataType: CreateSimpleField, |
| 72460 | 236 | | onComplexDataType: CreateComplexField); |
| | 237 | | } |
| | 238 | |
|
| 83190 | 239 | | return contract.Properties.Select(BuildField).Where(field => field != null).ToArray(); |
| | 240 | | } |
| | 241 | |
|
| | 242 | | private static IDataTypeInfo GetDataTypeInfo(Type propertyType, IContractResolver contractResolver) |
| | 243 | | { |
| | 244 | | bool IsNullableType(Type type) => |
| 44584 | 245 | | type.IsConstructedGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>); |
| | 246 | |
|
| 107872 | 247 | | if (PrimitiveTypeMap.TryGetValue(propertyType, out DataType dataType)) |
| | 248 | | { |
| 63288 | 249 | | return DataTypeInfo.Simple(dataType); |
| | 250 | | } |
| 44584 | 251 | | else if (IsNullableType(propertyType)) |
| | 252 | | { |
| 1162 | 253 | | return GetDataTypeInfo(propertyType.GenericTypeArguments[0], contractResolver); |
| | 254 | | } |
| 43422 | 255 | | else if (TryGetEnumerableElementType(propertyType, out Type elementType)) |
| | 256 | | { |
| 34250 | 257 | | IDataTypeInfo elementTypeInfo = GetDataTypeInfo(elementType, contractResolver); |
| 34250 | 258 | | return DataTypeInfo.AsCollection(elementTypeInfo); |
| | 259 | | } |
| 9172 | 260 | | else if (contractResolver.ResolveContract(propertyType) is JsonObjectContract jsonContract) |
| | 261 | | { |
| 9164 | 262 | | return DataTypeInfo.Complex(DataType.Complex, propertyType, jsonContract); |
| | 263 | | } |
| | 264 | | else |
| | 265 | | { |
| 8 | 266 | | return DataTypeInfo.Unknown; |
| | 267 | | } |
| | 268 | | } |
| | 269 | |
|
| | 270 | | private static bool TryGetEnumerableElementType(Type candidateType, out Type elementType) |
| | 271 | | { |
| | 272 | | Type GetElementTypeIfIEnumerable(Type t) => |
| 208132 | 273 | | t.IsConstructedGenericType && t.GetGenericTypeDefinition() == typeof(IEnumerable<>) |
| 208132 | 274 | | ? t.GenericTypeArguments[0] |
| 208132 | 275 | | : null; |
| | 276 | |
|
| 43422 | 277 | | elementType = GetElementTypeIfIEnumerable(candidateType); |
| 43422 | 278 | | if (elementType != null) |
| | 279 | | { |
| 6842 | 280 | | return true; |
| | 281 | | } |
| | 282 | | else |
| | 283 | | { |
| 36580 | 284 | | TypeInfo ti = candidateType.GetTypeInfo(); |
| 36580 | 285 | | var listElementTypes = ti |
| 36580 | 286 | | .ImplementedInterfaces |
| 36580 | 287 | | .Select(GetElementTypeIfIEnumerable) |
| 201290 | 288 | | .Where(p => p != null) |
| 36580 | 289 | | .ToList(); |
| | 290 | |
|
| 36580 | 291 | | if (listElementTypes.Count == 1) |
| | 292 | | { |
| 27408 | 293 | | elementType = listElementTypes[0]; |
| 27408 | 294 | | return true; |
| | 295 | | } |
| | 296 | | else |
| | 297 | | { |
| 9172 | 298 | | return false; |
| | 299 | | } |
| | 300 | | } |
| | 301 | | } |
| | 302 | |
|
| | 303 | | private interface IDataTypeInfo |
| | 304 | | { |
| | 305 | | T Match<T>( |
| | 306 | | Func<T> onUnknownDataType, |
| | 307 | | Func<DataType, T> onSimpleDataType, |
| | 308 | | Func<DataType, Type, JsonObjectContract, T> onComplexDataType); |
| | 309 | | } |
| | 310 | |
|
| | 311 | | private static class DataTypeInfo |
| | 312 | | { |
| 14 | 313 | | public static IDataTypeInfo Unknown { get; } = new UnknownDataTypeInfo(); |
| | 314 | |
|
| 93720 | 315 | | public static IDataTypeInfo Simple(DataType dataType) => new SimpleDataTypeInfo(dataType); |
| | 316 | |
|
| | 317 | | public static IDataTypeInfo Complex(DataType dataType, Type underlyingClrType, JsonObjectContract jsonContra |
| 12978 | 318 | | new ComplexDataTypeInfo(dataType, underlyingClrType, jsonContract); |
| | 319 | |
|
| | 320 | | public static IDataTypeInfo AsCollection(IDataTypeInfo dataTypeInfo) => |
| 34250 | 321 | | dataTypeInfo.Match( |
| 34254 | 322 | | onUnknownDataType: () => Unknown, |
| 64682 | 323 | | onSimpleDataType: dataType => Simple(DataType.Collection(dataType)), |
| 34250 | 324 | | onComplexDataType: (dataType, underlyingClrType, jsonContract) => |
| 38064 | 325 | | Complex(DataType.Collection(dataType), underlyingClrType, jsonContract)); |
| | 326 | |
|
| | 327 | | private sealed class UnknownDataTypeInfo : IDataTypeInfo |
| | 328 | | { |
| 2 | 329 | | public UnknownDataTypeInfo() |
| | 330 | | { |
| 2 | 331 | | } |
| | 332 | |
|
| | 333 | | public T Match<T>( |
| | 334 | | Func<T> onUnknownDataType, |
| | 335 | | Func<DataType, T> onSimpleDataType, |
| | 336 | | Func<DataType, Type, JsonObjectContract, T> onComplexDataType) |
| 12 | 337 | | => onUnknownDataType(); |
| | 338 | | } |
| | 339 | |
|
| | 340 | | private sealed class SimpleDataTypeInfo : IDataTypeInfo |
| | 341 | | { |
| | 342 | | private readonly DataType _dataType; |
| | 343 | |
|
| 93720 | 344 | | public SimpleDataTypeInfo(DataType dataType) |
| | 345 | | { |
| 93720 | 346 | | _dataType = dataType; |
| 93720 | 347 | | } |
| | 348 | |
|
| | 349 | | public T Match<T>( |
| | 350 | | Func<T> onUnknownDataType, |
| | 351 | | Func<DataType, T> onSimpleDataType, |
| | 352 | | Func<DataType, Type, JsonObjectContract, T> onComplexDataType) |
| 93720 | 353 | | => onSimpleDataType(_dataType); |
| | 354 | | } |
| | 355 | |
|
| | 356 | | private sealed class ComplexDataTypeInfo : IDataTypeInfo |
| | 357 | | { |
| | 358 | | private readonly DataType _dataType; |
| | 359 | | private readonly Type _underlyingClrType; |
| | 360 | | private readonly JsonObjectContract _jsonContract; |
| | 361 | |
|
| 12978 | 362 | | public ComplexDataTypeInfo(DataType dataType, Type underlyingClrType, JsonObjectContract jsonContract) |
| | 363 | | { |
| 12978 | 364 | | _dataType = dataType; |
| 12978 | 365 | | _underlyingClrType = underlyingClrType; |
| 12978 | 366 | | _jsonContract = jsonContract; |
| 12978 | 367 | | } |
| | 368 | |
|
| | 369 | | public T Match<T>( |
| | 370 | | Func<T> onUnknownDataType, |
| | 371 | | Func<DataType, T> onSimpleDataType, |
| | 372 | | Func<DataType, Type, JsonObjectContract, T> onComplexDataType) |
| 12978 | 373 | | => onComplexDataType(_dataType, _underlyingClrType, _jsonContract); |
| | 374 | | } |
| | 375 | | } |
| | 376 | | } |
| | 377 | | } |