| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Collections; |
| | 6 | | using System.Collections.Generic; |
| | 7 | | using System.Collections.ObjectModel; |
| | 8 | | using System.Diagnostics; |
| | 9 | | using System.Linq; |
| | 10 | | using System.Reflection; |
| | 11 | | using Azure.Core; |
| | 12 | | #if EXPERIMENTAL_SERIALIZER |
| | 13 | | using Azure.Core.Serialization; |
| | 14 | | #endif |
| | 15 | | using Azure.Search.Documents.Indexes.Models; |
| | 16 | | #if EXPERIMENTAL_SPATIAL |
| | 17 | | using Azure.Core.Spatial; |
| | 18 | | #endif |
| | 19 | |
|
| | 20 | | namespace Azure.Search.Documents.Indexes |
| | 21 | | { |
| | 22 | | /// <summary> |
| | 23 | | /// Builds field definitions for a search index by reflecting over a user-defined model type. |
| | 24 | | /// </summary> |
| | 25 | | public class FieldBuilder |
| | 26 | | { |
| | 27 | | private const string HelpLink = "https://aka.ms/azsdk/net/search/fieldbuilder"; |
| | 28 | |
|
| 1 | 29 | | private static readonly IReadOnlyDictionary<Type, SearchFieldDataType> s_primitiveTypeMap = |
| 1 | 30 | | new ReadOnlyDictionary<Type, SearchFieldDataType>( |
| 1 | 31 | | new Dictionary<Type, SearchFieldDataType>() |
| 1 | 32 | | { |
| 1 | 33 | | [typeof(string)] = SearchFieldDataType.String, |
| 1 | 34 | | [typeof(int)] = SearchFieldDataType.Int32, |
| 1 | 35 | | [typeof(long)] = SearchFieldDataType.Int64, |
| 1 | 36 | | [typeof(double)] = SearchFieldDataType.Double, |
| 1 | 37 | | [typeof(bool)] = SearchFieldDataType.Boolean, |
| 1 | 38 | | [typeof(DateTime)] = SearchFieldDataType.DateTimeOffset, |
| 1 | 39 | | [typeof(DateTimeOffset)] = SearchFieldDataType.DateTimeOffset, |
| 1 | 40 | | #if EXPERIMENTAL_SPATIAL |
| 1 | 41 | | [typeof(PointGeometry)] = SearchFieldDataType.GeographyPoint, |
| 1 | 42 | | #endif |
| 1 | 43 | | }); |
| | 44 | |
|
| 1 | 45 | | private static readonly ISet<Type> s_unsupportedTypes = |
| 1 | 46 | | new HashSet<Type> |
| 1 | 47 | | { |
| 1 | 48 | | typeof(decimal), |
| 1 | 49 | | }; |
| | 50 | |
|
| | 51 | | /// <summary> |
| | 52 | | /// Initializes a new instance of the <see cref="FieldBuilder"/> class. |
| | 53 | | /// </summary> |
| 153 | 54 | | public FieldBuilder() |
| | 55 | | { |
| 153 | 56 | | } |
| | 57 | |
|
| | 58 | | /// <summary> |
| | 59 | | /// Gets or sets the <see cref="ObjectSerializer"/> to use to generate field names that match JSON property name |
| | 60 | | /// You should use hte same value as <see cref="SearchClientOptions.Serializer"/>. |
| | 61 | | /// <see cref="JsonObjectSerializer"/> will be used if no value is provided. |
| | 62 | | /// </summary> |
| 459 | 63 | | public ObjectSerializer Serializer { get; set; } |
| | 64 | |
|
| | 65 | | /// <summary> |
| | 66 | | /// Creates a list of <see cref="SearchField"/> objects corresponding to |
| | 67 | | /// the properties of the type supplied. |
| | 68 | | /// </summary> |
| | 69 | | /// <param name="modelType"> |
| | 70 | | /// The type for which fields will be created, based on its properties. |
| | 71 | | /// </param> |
| | 72 | | /// <returns>A collection of fields.</returns> |
| | 73 | | /// <exception cref="ArgumentNullException"><paramref name="modelType"/>.</exception> |
| | 74 | | public IList<SearchField> Build(Type modelType) |
| | 75 | | { |
| 153 | 76 | | Argument.AssertNotNull(modelType, nameof(modelType)); |
| | 77 | |
|
| | 78 | | ArgumentException FailOnNonObjectDataType() |
| | 79 | | { |
| 12 | 80 | | string errorMessage = |
| 12 | 81 | | $"Type '{modelType}' does not have properties which map to fields of an Azure Search index. Please u |
| 12 | 82 | | "class or struct with public properties."; |
| | 83 | |
|
| 12 | 84 | | throw new ArgumentException(errorMessage, nameof(modelType)); |
| | 85 | | } |
| | 86 | |
|
| 153 | 87 | | Serializer ??= new JsonObjectSerializer(); |
| 153 | 88 | | IMemberNameConverter nameProvider = Serializer as IMemberNameConverter ?? DefaultSerializedNameProvider.Shar |
| | 89 | |
|
| 153 | 90 | | if (ObjectInfo.TryGet(modelType, nameProvider, out ObjectInfo info)) |
| | 91 | | { |
| 141 | 92 | | if (info.Properties.Length == 0) |
| | 93 | | { |
| 0 | 94 | | throw FailOnNonObjectDataType(); |
| | 95 | | } |
| | 96 | |
|
| | 97 | | // Use Stack to avoid a dependency on ImmutableStack for now. |
| 141 | 98 | | return Build(modelType, info, nameProvider, new Stack<Type>(new[] { modelType })); |
| | 99 | | } |
| | 100 | |
|
| 12 | 101 | | throw FailOnNonObjectDataType(); |
| | 102 | | } |
| | 103 | |
|
| | 104 | | private static IList<SearchField> Build( |
| | 105 | | Type modelType, |
| | 106 | | ObjectInfo info, |
| | 107 | | IMemberNameConverter nameProvider, |
| | 108 | | Stack<Type> processedTypes) |
| | 109 | | { |
| | 110 | | SearchField BuildField(ObjectPropertyInfo prop) |
| | 111 | | { |
| | 112 | | // The IMemberNameConverter will return null for implementation-specific ways of ignoring members. |
| | 113 | | static bool ShouldIgnore(Attribute attribute) => |
| 7709 | 114 | | attribute is FieldBuilderIgnoreAttribute; |
| | 115 | |
|
| 19409 | 116 | | IList<Attribute> attributes = prop.GetCustomAttributes(true).Cast<Attribute>().ToArray(); |
| 19409 | 117 | | if (attributes.Any(ShouldIgnore)) |
| | 118 | | { |
| 2 | 119 | | return null; |
| | 120 | | } |
| | 121 | |
|
| | 122 | | SearchField CreateComplexField(SearchFieldDataType dataType, Type underlyingClrType, ObjectInfo info) |
| | 123 | | { |
| 3128 | 124 | | if (processedTypes.Contains(underlyingClrType)) |
| | 125 | | { |
| | 126 | | // Skip recursive types. |
| 1 | 127 | | return null; |
| | 128 | | } |
| | 129 | |
|
| 3127 | 130 | | processedTypes.Push(underlyingClrType); |
| | 131 | | try |
| | 132 | | { |
| 3127 | 133 | | IList<SearchField> subFields = |
| 3127 | 134 | | Build(underlyingClrType, info, nameProvider, processedTypes); |
| | 135 | |
|
| 3127 | 136 | | if (prop.SerializedName is null) |
| | 137 | | { |
| | 138 | | // Member is unsupported or ignored. |
| 0 | 139 | | return null; |
| | 140 | | } |
| | 141 | |
|
| | 142 | | // Start with a ComplexField to make sure all properties default to language defaults. |
| 3127 | 143 | | SearchField field = new ComplexField(prop.SerializedName, dataType); |
| 28110 | 144 | | foreach (SearchField subField in subFields) |
| | 145 | | { |
| 10928 | 146 | | field.Fields.Add(subField); |
| | 147 | | } |
| | 148 | |
|
| 3127 | 149 | | return field; |
| | 150 | | } |
| | 151 | | finally |
| | 152 | | { |
| 3127 | 153 | | processedTypes.Pop(); |
| 3127 | 154 | | } |
| 3127 | 155 | | } |
| | 156 | |
|
| | 157 | | SearchField CreateSimpleField(SearchFieldDataType SearchFieldDataType) |
| | 158 | | { |
| 16275 | 159 | | if (prop.SerializedName is null) |
| | 160 | | { |
| | 161 | | // Member is unsupported or ignored. |
| 0 | 162 | | return null; |
| | 163 | | } |
| | 164 | |
|
| | 165 | | // Start with a SimpleField to make sure all properties default to language defaults. |
| 16275 | 166 | | SearchField field = new SimpleField(prop.SerializedName, SearchFieldDataType); |
| 46388 | 167 | | foreach (Attribute attribute in attributes) |
| | 168 | | { |
| | 169 | | switch (attribute) |
| | 170 | | { |
| | 171 | | case SearchableFieldAttribute searchableFieldAttribute: |
| 2212 | 172 | | ((ISearchFieldAttribute)searchableFieldAttribute).SetField(field); |
| 2212 | 173 | | break; |
| | 174 | |
|
| | 175 | | case SimpleFieldAttribute simpleFieldAttribute: |
| 2218 | 176 | | ((ISearchFieldAttribute)simpleFieldAttribute).SetField(field); |
| 2218 | 177 | | break; |
| | 178 | |
|
| | 179 | | default: |
| 2489 | 180 | | Type attributeType = attribute.GetType(); |
| | 181 | |
|
| | 182 | | // Match on name to avoid dependency - don't want to force people not using |
| | 183 | | // this feature to bring in the annotations component. |
| | 184 | | // |
| | 185 | | // Also, ignore key attributes on sub-fields. |
| 2489 | 186 | | if (attributeType.FullName == "System.ComponentModel.DataAnnotations.KeyAttribute" && |
| 2489 | 187 | | processedTypes.Count <= 1) |
| | 188 | | { |
| 138 | 189 | | field.IsKey = true; |
| | 190 | | } |
| | 191 | | break; |
| | 192 | | } |
| | 193 | | } |
| | 194 | |
|
| 16275 | 195 | | return field; |
| | 196 | | } |
| | 197 | |
|
| | 198 | | ArgumentException FailOnUnknownDataType() |
| | 199 | | { |
| 4 | 200 | | string errorMessage = |
| 4 | 201 | | $"Property '{prop.Name}' is of type '{prop.PropertyType}', which does not map to an " + |
| 4 | 202 | | "Azure Search data type. Please use a supported data type or mark the property with [FieldBuilde |
| 4 | 203 | | $"and define the field by creating a SearchField object. See {HelpLink} for more information."; |
| | 204 | |
|
| 4 | 205 | | return new ArgumentException(errorMessage, nameof(modelType)) |
| 4 | 206 | | { |
| 4 | 207 | | HelpLink = HelpLink, |
| 4 | 208 | | }; |
| | 209 | | } |
| | 210 | |
|
| 19407 | 211 | | IDataTypeInfo dataTypeInfo = GetDataTypeInfo(prop.PropertyType, nameProvider); |
| | 212 | |
|
| 19407 | 213 | | return dataTypeInfo.Match( |
| 19411 | 214 | | onUnknownDataType: () => throw FailOnUnknownDataType(), |
| 19407 | 215 | | onSimpleDataType: CreateSimpleField, |
| 19407 | 216 | | onComplexDataType: CreateComplexField); |
| | 217 | | } |
| | 218 | |
|
| 22673 | 219 | | return info.Properties.Select(BuildField).Where(field => field != null).ToList(); |
| | 220 | | } |
| | 221 | |
|
| | 222 | | private static IDataTypeInfo GetDataTypeInfo(Type propertyType, IMemberNameConverter nameProvider) |
| | 223 | | { |
| | 224 | | static bool IsNullableType(Type type) => |
| 11522 | 225 | | type.IsConstructedGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>); |
| | 226 | |
|
| 27797 | 227 | | if (s_primitiveTypeMap.TryGetValue(propertyType, out SearchFieldDataType SearchFieldDataType)) |
| | 228 | | { |
| 16275 | 229 | | return DataTypeInfo.Simple(SearchFieldDataType); |
| | 230 | | } |
| 11522 | 231 | | else if (IsNullableType(propertyType)) |
| | 232 | | { |
| 2535 | 233 | | return GetDataTypeInfo(propertyType.GenericTypeArguments[0], nameProvider); |
| | 234 | | } |
| 8987 | 235 | | else if (TryGetEnumerableElementType(propertyType, out Type elementType)) |
| | 236 | | { |
| 5855 | 237 | | IDataTypeInfo elementTypeInfo = GetDataTypeInfo(elementType, nameProvider); |
| 5855 | 238 | | return DataTypeInfo.AsCollection(elementTypeInfo); |
| | 239 | | } |
| 3132 | 240 | | else if (ObjectInfo.TryGet(propertyType, nameProvider, out ObjectInfo info)) |
| | 241 | | { |
| 3128 | 242 | | return DataTypeInfo.Complex(SearchFieldDataType.Complex, propertyType, info); |
| | 243 | | } |
| | 244 | | else |
| | 245 | | { |
| 4 | 246 | | return DataTypeInfo.Unknown; |
| | 247 | | } |
| | 248 | | } |
| | 249 | |
|
| | 250 | | private static bool TryGetEnumerableElementType(Type candidateType, out Type elementType) |
| | 251 | | { |
| | 252 | | static Type GetElementTypeIfIEnumerable(Type t) => |
| 37902 | 253 | | t.IsConstructedGenericType && t.GetGenericTypeDefinition() == typeof(IEnumerable<>) |
| 37902 | 254 | | ? t.GenericTypeArguments[0] |
| 37902 | 255 | | : null; |
| | 256 | |
|
| 8987 | 257 | | elementType = GetElementTypeIfIEnumerable(candidateType); |
| 8987 | 258 | | if (elementType != null) |
| | 259 | | { |
| 1171 | 260 | | return true; |
| | 261 | | } |
| | 262 | | else |
| | 263 | | { |
| 7816 | 264 | | TypeInfo ti = candidateType.GetTypeInfo(); |
| 7816 | 265 | | var listElementTypes = ti |
| 7816 | 266 | | .ImplementedInterfaces |
| 7816 | 267 | | .Select(GetElementTypeIfIEnumerable) |
| 36731 | 268 | | .Where(p => p != null) |
| 7816 | 269 | | .ToList(); |
| | 270 | |
|
| 7816 | 271 | | if (listElementTypes.Count == 1) |
| | 272 | | { |
| 4684 | 273 | | elementType = listElementTypes[0]; |
| 4684 | 274 | | return true; |
| | 275 | | } |
| | 276 | | else |
| | 277 | | { |
| 3132 | 278 | | return false; |
| | 279 | | } |
| | 280 | | } |
| | 281 | | } |
| | 282 | |
|
| | 283 | | private interface IDataTypeInfo |
| | 284 | | { |
| | 285 | | T Match<T>( |
| | 286 | | Func<T> onUnknownDataType, |
| | 287 | | Func<SearchFieldDataType, T> onSimpleDataType, |
| | 288 | | Func<SearchFieldDataType, Type, ObjectInfo, T> onComplexDataType); |
| | 289 | | } |
| | 290 | |
|
| | 291 | | private static class DataTypeInfo |
| | 292 | | { |
| 7 | 293 | | public static IDataTypeInfo Unknown { get; } = new UnknownDataTypeInfo(); |
| | 294 | |
|
| 20825 | 295 | | public static IDataTypeInfo Simple(SearchFieldDataType SearchFieldDataType) => new SimpleDataTypeInfo(Search |
| | 296 | |
|
| | 297 | | public static IDataTypeInfo Complex(SearchFieldDataType SearchFieldDataType, Type underlyingClrType, ObjectI |
| 4431 | 298 | | new ComplexDataTypeInfo(SearchFieldDataType, underlyingClrType, info); |
| | 299 | |
|
| | 300 | | public static IDataTypeInfo AsCollection(IDataTypeInfo dataTypeInfo) => |
| 5855 | 301 | | dataTypeInfo.Match( |
| 5857 | 302 | | onUnknownDataType: () => Unknown, |
| 10405 | 303 | | onSimpleDataType: SearchFieldDataType => Simple(SearchFieldDataType.Collection(SearchFieldDataType)) |
| 5855 | 304 | | onComplexDataType: (SearchFieldDataType, underlyingClrType, info) => |
| 7158 | 305 | | Complex(SearchFieldDataType.Collection(SearchFieldDataType), underlyingClrType, info)); |
| | 306 | |
|
| | 307 | | private sealed class UnknownDataTypeInfo : IDataTypeInfo |
| | 308 | | { |
| 1 | 309 | | public UnknownDataTypeInfo() |
| | 310 | | { |
| 1 | 311 | | } |
| | 312 | |
|
| | 313 | | public T Match<T>( |
| | 314 | | Func<T> onUnknownDataType, |
| | 315 | | Func<SearchFieldDataType, T> onSimpleDataType, |
| | 316 | | Func<SearchFieldDataType, Type, ObjectInfo, T> onComplexDataType) |
| 6 | 317 | | => onUnknownDataType(); |
| | 318 | | } |
| | 319 | |
|
| | 320 | | private sealed class SimpleDataTypeInfo : IDataTypeInfo |
| | 321 | | { |
| | 322 | | private readonly SearchFieldDataType _dataType; |
| | 323 | |
|
| 20825 | 324 | | public SimpleDataTypeInfo(SearchFieldDataType SearchFieldDataType) |
| | 325 | | { |
| 20825 | 326 | | _dataType = SearchFieldDataType; |
| 20825 | 327 | | } |
| | 328 | |
|
| | 329 | | public T Match<T>( |
| | 330 | | Func<T> onUnknownDataType, |
| | 331 | | Func<SearchFieldDataType, T> onSimpleDataType, |
| | 332 | | Func<SearchFieldDataType, Type, ObjectInfo, T> onComplexDataType) |
| 20825 | 333 | | => onSimpleDataType(_dataType); |
| | 334 | | } |
| | 335 | |
|
| | 336 | | private sealed class ComplexDataTypeInfo : IDataTypeInfo |
| | 337 | | { |
| | 338 | | private readonly SearchFieldDataType _dataType; |
| | 339 | | private readonly Type _underlyingClrType; |
| | 340 | | private readonly ObjectInfo _info; |
| | 341 | |
|
| 4431 | 342 | | public ComplexDataTypeInfo(SearchFieldDataType SearchFieldDataType, Type underlyingClrType, ObjectInfo i |
| | 343 | | { |
| 4431 | 344 | | _dataType = SearchFieldDataType; |
| 4431 | 345 | | _underlyingClrType = underlyingClrType; |
| 4431 | 346 | | _info = info; |
| 4431 | 347 | | } |
| | 348 | |
|
| | 349 | | public T Match<T>( |
| | 350 | | Func<T> onUnknownDataType, |
| | 351 | | Func<SearchFieldDataType, T> onSimpleDataType, |
| | 352 | | Func<SearchFieldDataType, Type, ObjectInfo, T> onComplexDataType) |
| 4431 | 353 | | => onComplexDataType(_dataType, _underlyingClrType, _info); |
| | 354 | | } |
| | 355 | | } |
| | 356 | |
|
| | 357 | | private class ObjectInfo |
| | 358 | | { |
| 3269 | 359 | | private ObjectInfo(ObjectPropertyInfo[] properties) |
| | 360 | | { |
| 3269 | 361 | | Properties = properties; |
| 3269 | 362 | | } |
| | 363 | |
|
| | 364 | | public static bool TryGet(Type type, IMemberNameConverter nameProvider, out ObjectInfo info) |
| | 365 | | { |
| | 366 | | // Close approximation to Newtonsoft.Json.Serialization.DefaultContractResolver that was used in Microso |
| 3285 | 367 | | if (!type.IsPrimitive && !type.IsEnum && !s_unsupportedTypes.Contains(type) && !s_primitiveTypeMap.Conta |
| | 368 | | { |
| | 369 | | const BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPubl |
| | 370 | |
|
| 3270 | 371 | | List<ObjectPropertyInfo> properties = new List<ObjectPropertyInfo>(); |
| 47188 | 372 | | foreach (PropertyInfo property in type.GetProperties(bindingFlags)) |
| | 373 | | { |
| 20324 | 374 | | string serializedName = nameProvider.ConvertMemberName(property); |
| 20324 | 375 | | if (serializedName != null) |
| | 376 | | { |
| 19411 | 377 | | properties.Add(new ObjectPropertyInfo(property, serializedName)); |
| | 378 | | } |
| | 379 | | } |
| | 380 | |
|
| 36268 | 381 | | foreach (FieldInfo field in type.GetFields(bindingFlags)) |
| | 382 | | { |
| 14864 | 383 | | string serializedName = nameProvider.ConvertMemberName(field); |
| 14864 | 384 | | if (serializedName != null) |
| | 385 | | { |
| 0 | 386 | | properties.Add(new ObjectPropertyInfo(field, serializedName)); |
| | 387 | | } |
| | 388 | | } |
| | 389 | |
|
| 3270 | 390 | | if (properties.Count != 0) |
| | 391 | | { |
| 3269 | 392 | | info = new ObjectInfo(properties.ToArray()); |
| 3269 | 393 | | return true; |
| | 394 | | } |
| | 395 | | } |
| | 396 | |
|
| 16 | 397 | | info = null; |
| 16 | 398 | | return false; |
| | 399 | | } |
| | 400 | |
|
| 3409 | 401 | | public ObjectPropertyInfo[] Properties { get; } |
| | 402 | | } |
| | 403 | |
|
| | 404 | | private struct ObjectPropertyInfo |
| | 405 | | { |
| | 406 | | private readonly MemberInfo _memberInfo; |
| | 407 | |
|
| | 408 | | public ObjectPropertyInfo(PropertyInfo property, string serializedName) |
| | 409 | | { |
| | 410 | | Debug.Assert(serializedName != null, $"{nameof(serializedName)} cannot be null"); |
| | 411 | |
|
| 19411 | 412 | | _memberInfo = property; |
| | 413 | |
|
| 19411 | 414 | | SerializedName = serializedName; |
| 19411 | 415 | | PropertyType = property.PropertyType; |
| 19411 | 416 | | } |
| | 417 | |
|
| | 418 | | public ObjectPropertyInfo(FieldInfo field, string serializedName) |
| | 419 | | { |
| | 420 | | Debug.Assert(serializedName != null, $"{nameof(serializedName)} cannot be null"); |
| | 421 | |
|
| 0 | 422 | | _memberInfo = field; |
| | 423 | |
|
| 0 | 424 | | SerializedName = serializedName; |
| 0 | 425 | | PropertyType = field.FieldType; |
| 0 | 426 | | } |
| | 427 | |
|
| 4 | 428 | | public string Name => _memberInfo.Name; |
| | 429 | |
|
| 38804 | 430 | | public string SerializedName { get; } |
| | 431 | |
|
| 19411 | 432 | | public Type PropertyType { get; } |
| | 433 | |
|
| | 434 | | public static implicit operator MemberInfo(ObjectPropertyInfo property) => |
| 0 | 435 | | property._memberInfo; |
| | 436 | |
|
| | 437 | | public object[] GetCustomAttributes(bool inherit) => |
| 19409 | 438 | | _memberInfo.GetCustomAttributes(inherit); |
| | 439 | | } |
| | 440 | |
|
| | 441 | | private class DefaultSerializedNameProvider : IMemberNameConverter |
| | 442 | | { |
| 0 | 443 | | public static IMemberNameConverter Shared { get; } = new DefaultSerializedNameProvider(); |
| | 444 | |
|
| 0 | 445 | | private DefaultSerializedNameProvider() |
| | 446 | | { |
| 0 | 447 | | } |
| | 448 | |
|
| 0 | 449 | | public string ConvertMemberName(MemberInfo member) => member?.Name; |
| | 450 | | } |
| | 451 | | } |
| | 452 | | } |