Skip to content

Commit 0c784f4

Browse files
committed
Merge branch 'feature/non-generic-poco-infer-mapping'
2 parents 4224de3 + ce11271 commit 0c784f4

File tree

5 files changed

+220
-130
lines changed

5 files changed

+220
-130
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System;
2+
using System.Linq.Expressions;
3+
4+
namespace Nest
5+
{
6+
public abstract class ClrPropertyMappingBase<TDocument> : IClrPropertyMapping<TDocument>
7+
where TDocument : class
8+
{
9+
Expression<Func<TDocument, object>> IClrPropertyMapping<TDocument>.Property { get; set; }
10+
bool IClrPropertyMapping<TDocument>.Ignore { get; set; }
11+
string IClrPropertyMapping<TDocument>.NewName { get; set; }
12+
13+
protected IClrPropertyMapping<TDocument> Self => this;
14+
15+
protected ClrPropertyMappingBase(Expression<Func<TDocument, object>> property) => Self.Property = property;
16+
17+
IPropertyMapping IClrPropertyMapping<TDocument>.ToPropertyMapping() => Self.Ignore
18+
? PropertyMapping.Ignored
19+
: new PropertyMapping {Name = Self.NewName};
20+
}
21+
22+
public interface IClrPropertyMapping<TDocument> where TDocument : class
23+
{
24+
Expression<Func<TDocument, object>> Property { get; set; }
25+
bool Ignore { get; set; }
26+
string NewName { get; set; }
27+
IPropertyMapping ToPropertyMapping();
28+
}
29+
30+
public class IgnoreClrPropertyMapping<TDocument> : ClrPropertyMappingBase<TDocument> where TDocument : class
31+
{
32+
public IgnoreClrPropertyMapping(Expression<Func<TDocument, object>> property) : base(property) => Self.Ignore = true;
33+
}
34+
35+
public class RenameClrPropertyMapping<TDocument> : ClrPropertyMappingBase<TDocument> where TDocument : class
36+
{
37+
public RenameClrPropertyMapping(Expression<Func<TDocument, object>> property, string newName) : base(property)
38+
{
39+
newName.ThrowIfNull(nameof(newName));
40+
Self.NewName = newName;
41+
}
42+
}
43+
}
Lines changed: 84 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,155 +1,149 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq.Expressions;
4-
using Elasticsearch.Net;
54

65
namespace Nest
76
{
8-
public interface IClrTypeMapping<T> where T : class
7+
public interface IClrTypeMapping
98
{
10-
Type Type { get; }
9+
/// <summary>
10+
/// The CLR type the mapping relates to
11+
/// </summary>
12+
Type ClrType { get; }
1113

1214
/// <summary>
13-
/// When specified dictates the default Elasticsearch index name for <typeparamref name="T"/>
15+
/// The default Elasticsearch index name for <see cref="ClrType"/>
1416
/// </summary>
1517
string IndexName { get; set; }
1618

1719
/// <summary>
18-
/// When specified dictates the default Elasticsearch type name for <typeparamref name="T" />
20+
/// The default Elasticsearch type name for <see cref="ClrType"/>
1921
/// </summary>
2022
string TypeName { get; set; }
2123

2224
/// <summary>
23-
/// When specified dictates the relation name for <typeparamref name="T" /> to resolve to.
25+
/// The relation name for <see cref="ClrType"/> to resolve to.
2426
/// </summary>
2527
string RelationName { get; set; }
28+
}
2629

30+
public interface IClrTypeMapping<TDocument> : IClrTypeMapping where TDocument : class
31+
{
2732
/// <summary>
28-
/// Allows you to set a default Id property on <typeparamref name="T" /> that NEST will evaluate
33+
/// Set a default Id property on CLR type <typeparamref name="TDocument" /> that NEST will evaluate
2934
/// </summary>
30-
Expression<Func<T, object>> IdProperty { get; set; }
35+
Expression<Func<TDocument, object>> IdProperty { get; set; }
3136

3237
/// <summary>
33-
/// When specified allows you to ignore or rename certain properties of clr type <typeparamref name="T" />
38+
/// Ignore or rename certain properties of CLR type <typeparamref name="TDocument" />
3439
/// </summary>
35-
IList<IClrTypePropertyMapping<T>> Properties { get; set; }
40+
IList<IClrPropertyMapping<TDocument>> Properties { get; set; }
3641
}
3742

38-
public class ClrTypeMapping<T> : IClrTypeMapping<T> where T : class
43+
public class ClrTypeMapping : IClrTypeMapping
3944
{
40-
public Type Type { get; } = typeof (T);
41-
4245
/// <summary>
43-
/// When specified dictates the default Elasticsearch index name for <typeparamref name="T"/>
46+
/// Initializes a new instance of <see cref="ClrTypeMapping"/>
4447
/// </summary>
48+
public ClrTypeMapping(Type type) => ClrType = type;
49+
50+
/// <inheritdoc />
51+
public Type ClrType { get; }
52+
53+
/// <inheritdoc />
4554
public string IndexName { get; set; }
4655

47-
/// <summary>
48-
/// When specified dictates the default Elasticsearch type name for <typeparamref name="T" />
49-
/// </summary>
56+
/// <inheritdoc />
5057
public string TypeName { get; set; }
5158

52-
/// <summary>
53-
/// When specified dictates the relation name for <typeparamref name="T" /> to resolve to.
54-
/// </summary>
59+
/// <inheritdoc />
5560
public string RelationName { get; set; }
5661

57-
/// <summary>
58-
/// Allows you to set a default Id property on <typeparamref name="T" /> that NEST will evaluate
59-
/// </summary>
60-
public Expression<Func<T, object>> IdProperty { get; set; }
62+
}
63+
public class ClrTypeMapping<TDocument> : ClrTypeMapping, IClrTypeMapping<TDocument> where TDocument : class
64+
{
65+
public ClrTypeMapping() : base(typeof(TDocument)) { }
66+
67+
/// <inheritdoc />
68+
public Expression<Func<TDocument, object>> IdProperty { get; set; }
6169

62-
public IList<IClrTypePropertyMapping<T>> Properties { get; set; }
70+
/// <inheritdoc />
71+
public IList<IClrPropertyMapping<TDocument>> Properties { get; set; }
6372
}
6473

65-
public class ClrTypeMappingDescriptor<T> : DescriptorBase<ClrTypeMappingDescriptor<T>,IClrTypeMapping<T>> , IClrTypeMapping<T>
66-
where T : class
74+
public class ClrTypeMappingDescriptor : DescriptorBase<ClrTypeMappingDescriptor,IClrTypeMapping> , IClrTypeMapping
6775
{
68-
Type IClrTypeMapping<T>.Type { get; } = typeof (T);
69-
string IClrTypeMapping<T>.IndexName { get; set; }
70-
string IClrTypeMapping<T>.TypeName { get; set; }
71-
string IClrTypeMapping<T>.RelationName { get; set; }
72-
Expression<Func<T, object>> IClrTypeMapping<T>.IdProperty { get; set; }
73-
IList<IClrTypePropertyMapping<T>> IClrTypeMapping<T>.Properties { get; set; } = new List<IClrTypePropertyMapping<T>>();
76+
private readonly Type _type;
7477

7578
/// <summary>
76-
/// When specified dictates the default Elasticsearch index name for <typeparamref name="T"/>
79+
/// Instantiates a new instance of <see cref="ClrTypeMappingDescriptor"/>
7780
/// </summary>
78-
public ClrTypeMappingDescriptor<T> IndexName(string indexName) => Assign(a => a.IndexName = indexName);
81+
/// <param name="type">The CLR type to map</param>
82+
public ClrTypeMappingDescriptor(Type type) => _type = type;
7983

80-
/// <summary>
81-
/// When specified dictates the default Elasticsearch type name for <typeparamref name="T" />
82-
/// </summary>
83-
public ClrTypeMappingDescriptor<T> TypeName(string typeName) => Assign(a => a.TypeName = typeName);
84+
Type IClrTypeMapping.ClrType => _type;
85+
string IClrTypeMapping.IndexName { get; set; }
86+
string IClrTypeMapping.TypeName { get; set; }
87+
string IClrTypeMapping.RelationName { get; set; }
8488

8589
/// <summary>
86-
/// When specified dictates the relation name for <typeparamref name="T" /> to resolve to.
90+
/// The default Elasticsearch index name for the CLR type
8791
/// </summary>
88-
public ClrTypeMappingDescriptor<T> RelationName(string relationName) => Assign(a => a.RelationName = relationName);
92+
public ClrTypeMappingDescriptor IndexName(string indexName) => Assign(a => a.IndexName = indexName);
8993

9094
/// <summary>
91-
/// Allows you to set a default Id property on <typeparamref name="T" /> that NEST will evaluate
95+
/// The default Elasticsearch type name for the CLR type
9296
/// </summary>
93-
public ClrTypeMappingDescriptor<T> IdProperty(Expression<Func<T, object>> property) => Assign(a => a.IdProperty = property);
97+
public ClrTypeMappingDescriptor TypeName(string typeName) => Assign(a => a.TypeName = typeName);
9498

9599
/// <summary>
96-
/// When specified allows you to ignore <param name="property"></param> on clr type <typeparamref name="T" />
100+
/// The relation name for the CLR type to resolve to.
97101
/// </summary>
98-
public ClrTypeMappingDescriptor<T> Ignore(Expression<Func<T, object>> property) =>
99-
Assign(a => a.Properties.Add(new IgnorePropertyMapping<T>(property)));
100-
101-
/// <summary>
102-
/// When specified allows you to rename <param name="property"></param> on clr type <typeparamref name="T" />
103-
/// </summary>
104-
public ClrTypeMappingDescriptor<T> Rename(Expression<Func<T, object>> property, string newName) =>
105-
Assign(a => a.Properties.Add(new RenamePropertyMapping<T>(property, newName)));
106-
102+
public ClrTypeMappingDescriptor RelationName(string relationName) => Assign(a => a.RelationName = relationName);
107103
}
108104

109-
public interface IClrTypePropertyMapping<T> where T : class
105+
public class ClrTypeMappingDescriptor<TDocument>
106+
: DescriptorBase<ClrTypeMappingDescriptor<TDocument>,IClrTypeMapping<TDocument>>, IClrTypeMapping<TDocument>
107+
where TDocument : class
110108
{
111-
Expression<Func<T, object>> Property { get; set; }
112-
bool Ignore { get; set; }
113-
string NewName { get; set; }
114-
IPropertyMapping ToPropertyMapping();
115-
}
116-
117-
public abstract class ClrPropertyMappingBase<T> : IClrTypePropertyMapping<T>
118-
where T : class
119-
{
120-
protected IClrTypePropertyMapping<T> Self => this;
109+
Type IClrTypeMapping.ClrType { get; } = typeof (TDocument);
110+
string IClrTypeMapping.IndexName { get; set; }
111+
string IClrTypeMapping.TypeName { get; set; }
112+
string IClrTypeMapping.RelationName { get; set; }
113+
Expression<Func<TDocument, object>> IClrTypeMapping<TDocument>.IdProperty { get; set; }
114+
IList<IClrPropertyMapping<TDocument>> IClrTypeMapping<TDocument>.Properties { get; set; } = new List<IClrPropertyMapping<TDocument>>();
121115

122-
Expression<Func<T, object>> IClrTypePropertyMapping<T>.Property { get; set; }
116+
/// <summary>
117+
/// The default Elasticsearch index name for <typeparamref name="TDocument"/>
118+
/// </summary>
119+
public ClrTypeMappingDescriptor<TDocument> IndexName(string indexName) => Assign(a => a.IndexName = indexName);
123120

124-
bool IClrTypePropertyMapping<T>.Ignore { get; set; }
121+
/// <summary>
122+
/// The default Elasticsearch type name for <typeparamref name="TDocument" />
123+
/// </summary>
124+
public ClrTypeMappingDescriptor<TDocument> TypeName(string typeName) => Assign(a => a.TypeName = typeName);
125125

126-
string IClrTypePropertyMapping<T>.NewName { get; set; }
126+
/// <summary>
127+
/// The relation name for <typeparamref name="TDocument" /> to resolve to.
128+
/// </summary>
129+
public ClrTypeMappingDescriptor<TDocument> RelationName(string relationName) => Assign(a => a.RelationName = relationName);
127130

128-
protected ClrPropertyMappingBase(Expression<Func<T, object>> property)
129-
{
130-
Self.Property = property;
131-
}
131+
/// <summary>
132+
/// Set a default Id property on CLR type <typeparamref name="TDocument" /> that NEST will evaluate
133+
/// </summary>
134+
public ClrTypeMappingDescriptor<TDocument> IdProperty(Expression<Func<TDocument, object>> property) => Assign(a => a.IdProperty = property);
132135

133-
IPropertyMapping IClrTypePropertyMapping<T>.ToPropertyMapping() => Self.Ignore ? PropertyMapping.Ignored : new PropertyMapping {Name = Self.NewName};
134-
}
136+
/// <summary>
137+
/// Ignore <paramref name="property" /> on CLR type <typeparamref name="TDocument" />
138+
/// </summary>
139+
public ClrTypeMappingDescriptor<TDocument> Ignore(Expression<Func<TDocument, object>> property) =>
140+
Assign(a => a.Properties.Add(new IgnoreClrPropertyMapping<TDocument>(property)));
135141

136-
public class IgnorePropertyMapping<T> : ClrPropertyMappingBase<T> where T : class
137-
{
138-
public IgnorePropertyMapping(Expression<Func<T, object>> property) : base(property)
139-
{
140-
Self.Ignore = true;
141-
}
142-
}
142+
/// <summary>
143+
/// Rename <paramref name="property" /> on CLR type <typeparamref name="TDocument" />
144+
/// </summary>
145+
public ClrTypeMappingDescriptor<TDocument> Rename(Expression<Func<TDocument, object>> property, string newName) =>
146+
Assign(a => a.Properties.Add(new RenameClrPropertyMapping<TDocument>(property, newName)));
143147

144-
public class RenamePropertyMapping<T> : ClrPropertyMappingBase<T> where T : class
145-
{
146-
public RenamePropertyMapping(Expression<Func<T, object>> property, string newName) : base(property)
147-
{
148-
newName.ThrowIfNull(nameof(newName));
149-
Self.NewName = newName;
150-
}
151148
}
152-
153-
154-
155149
}

0 commit comments

Comments
 (0)