Skip to content

Commit 7097f6e

Browse files
committed
Add non-generic AutoMap overload
This commit adds a non-generic overload for automapping. Closes #2907 (cherry picked from commit e818092)
1 parent 83f2eaf commit 7097f6e

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

src/Nest/CrossPlatform/TypeExtensions.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,16 @@ internal static bool IsValue(this Type type)
5656
#if DOTNETCORE
5757
return type.GetTypeInfo().IsValueType;
5858
#else
59-
return type.IsValueType;
59+
return type.IsValueType;
60+
#endif
61+
}
62+
63+
internal static bool IsClass(this Type type)
64+
{
65+
#if DOTNETCORE
66+
return type.GetTypeInfo().IsClass;
67+
#else
68+
return type.IsClass;
6069
#endif
6170
}
6271

src/Nest/Mapping/TypeMapping.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,19 @@ public class TypeMappingDescriptor<T> : DescriptorBase<TypeMappingDescriptor<T>,
106106
public TypeMappingDescriptor<T> AutoMap(IPropertyVisitor visitor = null, int maxRecursion = 0) =>
107107
Assign(a => a.Properties = a.Properties.AutoMap<T>(visitor, maxRecursion));
108108

109+
/// <summary>
110+
/// Convenience method to map as much as it can based on <see cref="ElasticsearchTypeAttribute"/> attributes set on the type.
111+
/// This particular overload is useful for automapping any children
112+
/// <pre>This method also automatically sets up mappings for known values types (int, long, double, datetime, etc)</pre>
113+
/// <pre>Class types default to object and Enums to int</pre>
114+
/// <pre>Later calls can override whatever is set is by this call.</pre>
115+
/// </summary>
116+
public TypeMappingDescriptor<T> AutoMap(Type documentType, IPropertyVisitor visitor = null, int maxRecursion = 0)
117+
{
118+
if (!documentType.IsClass()) throw new ArgumentException("must be a reference type", nameof(documentType));
119+
return Assign(a => a.Properties = a.Properties.AutoMap(documentType, visitor, maxRecursion));
120+
}
121+
109122
/// <summary>
110123
/// Convenience method to map as much as it can based on <see cref="ElasticsearchTypeAttribute"/> attributes set on the type.
111124
/// This particular overload is useful for automapping any children

src/Nest/Mapping/Types/Properties.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Linq.Expressions;
5+
using System.Reflection;
56
using Elasticsearch.Net;
67
using Newtonsoft.Json;
78

@@ -152,11 +153,10 @@ private PropertiesDescriptor<T> SetProperty(IProperty type)
152153

153154
internal static class PropertiesExtensions
154155
{
155-
internal static IProperties AutoMap<T>(this IProperties existingProperties, IPropertyVisitor visitor = null, int maxRecursion = 0)
156-
where T : class
156+
internal static IProperties AutoMap(this IProperties existingProperties, Type documentType, IPropertyVisitor visitor = null, int maxRecursion = 0)
157157
{
158158
var properties = new Properties();
159-
var autoProperties = new PropertyWalker(typeof(T), visitor, maxRecursion).GetProperties();
159+
var autoProperties = new PropertyWalker(documentType, visitor, maxRecursion).GetProperties();
160160
foreach (var autoProperty in autoProperties)
161161
properties[autoProperty.Key] = autoProperty.Value;
162162

@@ -168,5 +168,8 @@ internal static IProperties AutoMap<T>(this IProperties existingProperties, IPro
168168

169169
return properties;
170170
}
171+
172+
internal static IProperties AutoMap<T>(this IProperties existingProperties, IPropertyVisitor visitor = null, int maxRecursion = 0)
173+
where T : class => existingProperties.AutoMap(typeof(T), visitor, maxRecursion);
171174
}
172175
}

0 commit comments

Comments
 (0)