Skip to content

Commit aa15593

Browse files
committed
No longer FluentDict but dedicated descriptor with .Rename and .Ignore
1 parent f798ff2 commit aa15593

File tree

4 files changed

+61
-27
lines changed

4 files changed

+61
-27
lines changed

src/Nest/Domain/Connection/ConnectionSettings.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,12 +205,12 @@ public T MapDefaultTypeNames(Action<FluentDictionary<Type, string>> mappingSelec
205205
return (T)this;
206206
}
207207

208-
public T MapPropertiesFor<TDocument>(Action<FluentDictionary<Expression<Func<TDocument, object>>, PropertyMapping>> propertiesSelector)
208+
public T MapPropertiesFor<TDocument>(Action<PropertyMappingDescriptor<TDocument>> propertiesSelector)
209209
{
210210
propertiesSelector.ThrowIfNull("propertiesSelector");
211-
var properties = new FluentDictionary<Expression<Func<TDocument, object>>, PropertyMapping>();
212-
propertiesSelector(properties);
213-
foreach (var p in properties)
211+
var mapper = new PropertyMappingDescriptor<TDocument>();
212+
propertiesSelector(mapper);
213+
foreach (var p in mapper.Mappings)
214214
{
215215
var e = p.Key;
216216
var memberInfoResolver = new MemberInfoResolver(this, e);

src/Nest/Domain/Mapping/PropertyMapping.cs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,39 @@
1-
namespace Nest
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq.Expressions;
4+
using System.Reflection;
5+
using System.Reflection.Emit;
6+
using Nest.Resolvers;
7+
8+
namespace Nest
29
{
10+
11+
public class PropertyMappingDescriptor<TDocument>
12+
{
13+
14+
private readonly IList<KeyValuePair<Expression<Func<TDocument, object>>, PropertyMapping>> _mappings = new List<KeyValuePair<Expression<Func<TDocument, object>>, PropertyMapping>>();
15+
16+
internal IList<KeyValuePair<Expression<Func<TDocument, object>>, PropertyMapping>> Mappings { get { return _mappings; } }
17+
18+
public PropertyMappingDescriptor<TDocument> Rename(Expression<Func<TDocument, object>> property, string propertyName)
19+
{
20+
property.ThrowIfNull("property");
21+
propertyName.ThrowIfNullOrEmpty("propertyName");
22+
this._mappings.Add(new KeyValuePair<Expression<Func<TDocument, object>>, PropertyMapping>(property, propertyName));
23+
return this;
24+
}
25+
26+
public PropertyMappingDescriptor<TDocument> Ignore(Expression<Func<TDocument, object>> property)
27+
{
28+
property.ThrowIfNull("property");
29+
this._mappings.Add(new KeyValuePair<Expression<Func<TDocument, object>>, PropertyMapping>(property, PropertyMapping.Ignored));
30+
return this;
31+
}
32+
}
33+
34+
35+
36+
337
/// <summary>
438
/// This class allows you to map aspects of a Type's property
539
/// that influences how NEST treats it.

src/Tests/Nest.Tests.Unit/Internals/Inferno/MapPropertyIgnoreTests.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public ElasticClient ConfigureClient(Action<ConnectionSettings> settingsSelector
8787
return client;
8888
}
8989

90-
public IElasticClient ClientWithPropertiesFor<T>(Action<FluentDictionary<Expression<Func<T, object>>, PropertyMapping>> propertiesSelector)
90+
public IElasticClient ClientWithPropertiesFor<T>(Action<PropertyMappingDescriptor<T>> propertiesSelector)
9191
{
9292
return this.ConfigureClient(c=>c.MapPropertiesFor<T>(propertiesSelector));
9393
}
@@ -96,7 +96,7 @@ public IElasticClient ClientWithPropertiesFor<T>(Action<FluentDictionary<Express
9696
public void Global_Ignore_Should_Be_Adhered()
9797
{
9898
var client = this.ClientWithPropertiesFor<MyCustomClass>(props => props
99-
.Add(p=>p.MyProperty, PropertyMapping.Ignored)
99+
.Ignore(p=>p.MyProperty)
100100
);
101101
var json = client.Serializer.Serialize(new MyCustomClass
102102
{
@@ -111,8 +111,8 @@ public void CanIgnoreTwice_ExceptionMessageMakesSense()
111111
var e = Assert.Throws<ArgumentException>(() =>
112112
{
113113
this.ClientWithPropertiesFor<MyCustomClass>(props => props
114-
.Add(p => p.MyProperty, PropertyMapping.Ignored)
115-
.Add(p => p.MyProperty, PropertyMapping.Ignored)
114+
.Ignore(p => p.MyProperty)
115+
.Ignore(p => p.MyProperty)
116116
);
117117
});
118118
e.Message.Should()
@@ -126,8 +126,8 @@ public void CanNotMapTwiceDifferently_ExceptionMessageMakesSense()
126126
var e = Assert.Throws<ArgumentException>(() =>
127127
{
128128
this.ClientWithPropertiesFor<MyCustomClass>(props => props
129-
.Add(p => p.MyProperty, PropertyMapping.Ignored)
130-
.Add(p => p.MyProperty, "mahProperty4")
129+
.Ignore(p => p.MyProperty)
130+
.Rename(p => p.MyProperty, "mahProperty4")
131131
);
132132
});
133133
e.Message.Should()
@@ -141,8 +141,8 @@ public void CanNotMapTwiceDifferently_ExceptionMessageMakesSense_AlternativeOrde
141141
var e = Assert.Throws<ArgumentException>(() =>
142142
{
143143
this.ClientWithPropertiesFor<MyCustomClass>(props => props
144-
.Add(p => p.MyProperty, "mahProperty4")
145-
.Add(p => p.MyProperty, PropertyMapping.Ignored)
144+
.Rename(p => p.MyProperty, "mahProperty4")
145+
.Ignore(p => p.MyProperty)
146146
);
147147
});
148148
e.Message.Should()

src/Tests/Nest.Tests.Unit/Internals/Inferno/MapPropertyNamesForTests.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public ElasticClient ConfigureClient(Action<ConnectionSettings> settingsSelector
8787
return client;
8888
}
8989

90-
public IElasticClient ClientWithPropertiesFor<T>(Action<FluentDictionary<Expression<Func<T, object>>, PropertyMapping>> propertiesSelector)
90+
public IElasticClient ClientWithPropertiesFor<T>(Action<PropertyMappingDescriptor<T>> propertiesSelector)
9191
{
9292
return this.ConfigureClient(c=>c.MapPropertiesFor<T>(propertiesSelector));
9393
}
@@ -96,7 +96,7 @@ public IElasticClient ClientWithPropertiesFor<T>(Action<FluentDictionary<Express
9696
public void SettingsTakePrecedenceOverAttributes()
9797
{
9898
var client = this.ClientWithPropertiesFor<MyCustomClass>(props => props
99-
.Add(p=>p.MyProperty, "mahPropertah")
99+
.Rename(p=>p.MyProperty, "mahPropertah")
100100
);
101101
Expression<Func<SomeClass, object>> exp = (m) => m.MyCustomClass.MyProperty;
102102
client.Infer.PropertyPath(exp).Should().Be("myCustomClass.mahPropertah");
@@ -107,7 +107,7 @@ public void CanNotMapAnyDepth()
107107
{
108108
var e = Assert.Throws<ArgumentException>(()=>
109109
this.ClientWithPropertiesFor<SomeClass>(props => props
110-
.Add(p=>p.MyCustomClass.MyProperty, "mahPropertah")
110+
.Rename(p=>p.MyCustomClass.MyProperty, "mahPropertah")
111111
)
112112
);
113113
e.Message.Should().Contain("can only map direct properties");
@@ -118,8 +118,8 @@ public void CanNotMapSamePropertyTwice()
118118
{
119119
var e = Assert.Throws<ArgumentException>(()=>
120120
this.ClientWithPropertiesFor<MyCustomClass>(props => props
121-
.Add(p=>p.MyProperty, "mahPropertah")
122-
.Add(p=>p.MyProperty, "mahPropertah2")
121+
.Rename(p=>p.MyProperty, "mahPropertah")
122+
.Rename(p=>p.MyProperty, "mahPropertah2")
123123
)
124124
);
125125
e.Message.Should()
@@ -134,10 +134,10 @@ public void CanNotMapSamePropertyTwice_SubClasses()
134134
var e = Assert.Throws<ArgumentException>(()=>
135135
this.ConfigureClient(c=>c
136136
.MapPropertiesFor<B>(props => props
137-
.Add(p=>p.X, "bX")
137+
.Rename(p=>p.X, "bX")
138138
)
139139
.MapPropertiesFor<C>(props => props
140-
.Add(p=>p.X, "cX")
140+
.Rename(p=>p.X, "cX")
141141
)
142142
)
143143
);
@@ -152,10 +152,10 @@ public void ResolverShouldResolveAllNestedMembers()
152152
{
153153
var client = this.ConfigureClient(c=>c
154154
.MapPropertiesFor<SomeClass>(props => props
155-
.Add(p=>p.MyCustomClass, "customClazz")
155+
.Rename(p=>p.MyCustomClass, "customClazz")
156156
)
157157
.MapPropertiesFor<MyCustomClass>(props => props
158-
.Add(p=>p.MyProperty, "mahPropertah")
158+
.Rename(p=>p.MyProperty, "mahPropertah")
159159
)
160160
);
161161
Expression<Func<SomeClass, object>> exp = (m) => m.MyCustomClass.MyProperty;
@@ -167,10 +167,10 @@ public void ResolverShouldResolveAllNestedMembers_Dictionary()
167167
{
168168
var client = this.ConfigureClient(c=>c
169169
.MapPropertiesFor<SomeClass>(props => props
170-
.Add(p=>p.StringDict, "map")
170+
.Rename(p=>p.StringDict, "map")
171171
)
172172
.MapPropertiesFor<SomeOtherClass>(props => props
173-
.Add(p=>p.CreateDate, "d0b")
173+
.Rename(p=>p.CreateDate, "d0b")
174174
)
175175
);
176176
Expression<Func<SomeClass, object>> exp = (m) => m.StringDict["path"].CreateDate;
@@ -182,7 +182,7 @@ public void PropertiesOn_CInstanceTakeNamesFrom_B()
182182
{
183183
var client = this.ConfigureClient(c=>c
184184
.MapPropertiesFor<B>(props => props
185-
.Add(p=>p.X, "Xavier")
185+
.Rename(p=>p.X, "Xavier")
186186
)
187187
);
188188
Expression<Func<SomeClass, object>> exp = (m) => m.CInstance.X;
@@ -194,7 +194,7 @@ public void PropertiesOn_BInstanceTakeNamesFrom_C()
194194
{
195195
var client = this.ConfigureClient(c=>c
196196
.MapPropertiesFor<C>(props => props
197-
.Add(p=>p.X, "Xavier")
197+
.Rename(p=>p.X, "Xavier")
198198
)
199199
);
200200
Expression<Func<SomeClass, object>> exp = (m) => m.BInstance.X;
@@ -206,7 +206,7 @@ public void PropertiesOnCollectionExpressionsResolve()
206206
{
207207
var client = this.ConfigureClient(c=>c
208208
.MapPropertiesFor<MyCustomClass>(props => props
209-
.Add(p=>p.MyProperty, "myProp")
209+
.Rename(p=>p.MyProperty, "myProp")
210210
)
211211
);
212212
Expression<Func<SomeClass, object>> exp = (m) => m.ListOfCustomClasses.First().MyProperty;

0 commit comments

Comments
 (0)