Skip to content

Commit f8990ce

Browse files
committed
Rename Poco* types back to Clr*
1 parent 3c7ca43 commit f8990ce

File tree

6 files changed

+214
-208
lines changed

6 files changed

+214
-208
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: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq.Expressions;
4+
5+
namespace Nest
6+
{
7+
public interface IClrTypeMapping
8+
{
9+
/// <summary>
10+
/// The CLR type the mapping relates to
11+
/// </summary>
12+
Type ClrType { get; }
13+
14+
/// <summary>
15+
/// The default Elasticsearch index name for <see cref="ClrType"/>
16+
/// </summary>
17+
string IndexName { get; set; }
18+
19+
/// <summary>
20+
/// The default Elasticsearch type name for <see cref="ClrType"/>
21+
/// </summary>
22+
string TypeName { get; set; }
23+
24+
/// <summary>
25+
/// The relation name for <see cref="ClrType"/> to resolve to.
26+
/// </summary>
27+
string RelationName { get; set; }
28+
}
29+
30+
public interface IClrTypeMapping<TDocument> : IClrTypeMapping where TDocument : class
31+
{
32+
/// <summary>
33+
/// Set a default Id property on CLR type <typeparamref name="TDocument" /> that NEST will evaluate
34+
/// </summary>
35+
Expression<Func<TDocument, object>> IdProperty { get; set; }
36+
37+
/// <summary>
38+
/// Ignore or rename certain properties of CLR type <typeparamref name="TDocument" />
39+
/// </summary>
40+
IList<IClrPropertyMapping<TDocument>> Properties { get; set; }
41+
}
42+
43+
public class ClrTypeMapping : IClrTypeMapping
44+
{
45+
/// <summary>
46+
/// Initializes a new instance of <see cref="ClrTypeMapping"/>
47+
/// </summary>
48+
public ClrTypeMapping(Type type) => ClrType = type;
49+
50+
/// <inheritdoc />
51+
public Type ClrType { get; }
52+
53+
/// <inheritdoc />
54+
public string IndexName { get; set; }
55+
56+
/// <inheritdoc />
57+
public string TypeName { get; set; }
58+
59+
/// <inheritdoc />
60+
public string RelationName { get; set; }
61+
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; }
69+
70+
/// <inheritdoc />
71+
public IList<IClrPropertyMapping<TDocument>> Properties { get; set; }
72+
}
73+
74+
public class ClrTypeMappingDescriptor : DescriptorBase<ClrTypeMappingDescriptor,IClrTypeMapping> , IClrTypeMapping
75+
{
76+
private readonly Type _type;
77+
78+
/// <summary>
79+
/// Instantiates a new instance of <see cref="ClrTypeMappingDescriptor"/>
80+
/// </summary>
81+
/// <param name="type">The CLR type to map</param>
82+
public ClrTypeMappingDescriptor(Type type) => _type = type;
83+
84+
Type IClrTypeMapping.ClrType => _type;
85+
string IClrTypeMapping.IndexName { get; set; }
86+
string IClrTypeMapping.TypeName { get; set; }
87+
string IClrTypeMapping.RelationName { get; set; }
88+
89+
/// <summary>
90+
/// The default Elasticsearch index name for the CLR type
91+
/// </summary>
92+
public ClrTypeMappingDescriptor IndexName(string indexName) => Assign(a => a.IndexName = indexName);
93+
94+
/// <summary>
95+
/// The default Elasticsearch type name for the CLR type
96+
/// </summary>
97+
public ClrTypeMappingDescriptor TypeName(string typeName) => Assign(a => a.TypeName = typeName);
98+
99+
/// <summary>
100+
/// The relation name for the CLR type to resolve to.
101+
/// </summary>
102+
public ClrTypeMappingDescriptor RelationName(string relationName) => Assign(a => a.RelationName = relationName);
103+
}
104+
105+
public class ClrTypeMappingDescriptor<TDocument>
106+
: DescriptorBase<ClrTypeMappingDescriptor<TDocument>,IClrTypeMapping<TDocument>>, IClrTypeMapping<TDocument>
107+
where TDocument : class
108+
{
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>>();
115+
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);
120+
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);
125+
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);
130+
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);
135+
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)));
141+
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)));
147+
148+
}
149+
}

src/Nest/CommonAbstractions/ConnectionSettings/ConnectionSettingsBase.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ private TConnectionSettings MapIdPropertyFor<TDocument>(Expression<Func<TDocumen
187187
return (TConnectionSettings) this;
188188
}
189189

190-
private void ApplyPropertyMappings<TDocument>(IList<IPocoPropertyMapping<TDocument>> mappings)
190+
private void ApplyPropertyMappings<TDocument>(IList<IClrPropertyMapping<TDocument>> mappings)
191191
where TDocument : class
192192
{
193193
foreach (var mapping in mappings)
@@ -226,10 +226,10 @@ private void ApplyPropertyMappings<TDocument>(IList<IPocoPropertyMapping<TDocume
226226
/// </summary>
227227
/// <typeparam name="TDocument">The type of the document.</typeparam>
228228
/// <param name="selector">The selector.</param>
229-
public TConnectionSettings InferMappingFor<TDocument>(Func<PocoMappingDescriptor<TDocument>, IPocoMapping<TDocument>> selector)
229+
public TConnectionSettings InferMappingFor<TDocument>(Func<ClrTypeMappingDescriptor<TDocument>, IClrTypeMapping<TDocument>> selector)
230230
where TDocument : class
231231
{
232-
var inferMapping = selector(new PocoMappingDescriptor<TDocument>());
232+
var inferMapping = selector(new ClrTypeMappingDescriptor<TDocument>());
233233
if (!inferMapping.IndexName.IsNullOrEmpty())
234234
this._defaultIndices.Add(inferMapping.ClrType, inferMapping.IndexName);
235235

@@ -253,9 +253,9 @@ public TConnectionSettings InferMappingFor<TDocument>(Func<PocoMappingDescriptor
253253
/// </summary>
254254
/// <param name="documentType">The type of the POCO you wish to configure</param>
255255
/// <param name="selector">describe the POCO configuration</param>
256-
public TConnectionSettings InferMappingFor(Type documentType, Func<PocoMappingDescriptor, IPocoMapping> selector)
256+
public TConnectionSettings InferMappingFor(Type documentType, Func<ClrTypeMappingDescriptor, IClrTypeMapping> selector)
257257
{
258-
var inferMapping = selector(new PocoMappingDescriptor(documentType));
258+
var inferMapping = selector(new ClrTypeMappingDescriptor(documentType));
259259
if (!inferMapping.IndexName.IsNullOrEmpty())
260260
this._defaultIndices.Add(inferMapping.ClrType, inferMapping.IndexName);
261261

@@ -273,7 +273,7 @@ public TConnectionSettings InferMappingFor(Type documentType, Func<PocoMappingDe
273273
/// </summary>
274274
/// <param name="documentType">The type of the POCO you wish to configure</param>
275275
/// <param name="selector">describe the POCO configuration</param>
276-
public TConnectionSettings InferMappings(IEnumerable<PocoMapping> typeMappings)
276+
public TConnectionSettings InferMappings(IEnumerable<ClrTypeMapping> typeMappings)
277277
{
278278
if (typeMappings == null) return (TConnectionSettings) this;
279279
foreach (var inferMapping in typeMappings)

0 commit comments

Comments
 (0)