Skip to content

Commit 90714c2

Browse files
committed
Refactorïng FieldSelection to IsADictionary
1 parent b1217a8 commit 90714c2

File tree

17 files changed

+147
-181
lines changed

17 files changed

+147
-181
lines changed

src/Nest/CommonAbstractions/Fields/FieldSelection.cs

Lines changed: 0 additions & 82 deletions
This file was deleted.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using Newtonsoft.Json;
2+
using Newtonsoft.Json.Linq;
3+
using System;
4+
using System.Collections;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Linq.Expressions;
8+
using System.Text;
9+
using System.Threading.Tasks;
10+
11+
namespace Nest
12+
{
13+
[JsonConverter(typeof(FieldValuesJsonConverter))]
14+
public class FieldValues : IsADictionary<string, object>
15+
{
16+
private ElasticInferrer _inferrer;
17+
18+
public FieldValues() : base() { }
19+
20+
public FieldValues(ElasticInferrer inferrer, IDictionary<string, object> container)
21+
: base(container)
22+
{
23+
_inferrer = inferrer;
24+
}
25+
26+
public K Value<K>(Field field) => Values<K>(field).FirstOrDefault();
27+
28+
public K Value<T, K>(Expression<Func<T, object>> objectPath)
29+
where T : class => Values<T, K>(objectPath).FirstOrDefault();
30+
31+
public K[] Values<K>(Field field)
32+
{
33+
var path = this._inferrer.Field(field);
34+
return this.FieldArray<K[]>(path);
35+
}
36+
37+
public K[] Values<T, K>(Expression<Func<T, object>> objectPath)
38+
where T : class
39+
{
40+
var field = this._inferrer.Field(objectPath);
41+
return this.FieldArray<K[]>(field);
42+
}
43+
44+
private K FieldArray<K>(string field)
45+
{
46+
object o;
47+
if (this.BackingDictionary != null && this.BackingDictionary.TryGetValue(field, out o))
48+
{
49+
var t = typeof(K);
50+
if (o is JArray && t.GetInterfaces().Contains(typeof(IEnumerable)))
51+
{
52+
var array = (JArray)o;
53+
return array.ToObject<K>();
54+
}
55+
return (K)Convert.ChangeType(o, typeof(K));
56+
}
57+
return default(K);
58+
}
59+
}
60+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using Newtonsoft.Json;
2+
using Newtonsoft.Json.Linq;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace Nest
10+
{
11+
internal class FieldValuesJsonConverter : JsonConverter
12+
{
13+
public override bool CanWrite => false;
14+
15+
public override bool CanConvert(Type objectType)
16+
{
17+
throw new NotImplementedException();
18+
}
19+
20+
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
21+
{
22+
var o = JObject.Load(reader);
23+
var fields = o.Properties().ToDictionary(p => p.Name, p => p.Value.ToObject<object>());
24+
var inferrer = serializer.GetConnectionSettings().Inferrer;
25+
var fieldValues = new FieldValues(inferrer, fields);
26+
return fieldValues;
27+
}
28+
29+
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
30+
{
31+
throw new NotSupportedException();
32+
}
33+
}
34+
}

src/Nest/CommonAbstractions/SerializationBehavior/StatefulDeserialization/ConcreteTypeConverter.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ internal class FieldsSetter
4747
//This method is used through reflection (cached though)
4848
// do not remove
4949
private static void SetFields<TFieldsType>(
50-
Hit<TFieldsType> hit, FieldSelection<TFieldsType> fieldSelection)
50+
Hit<TFieldsType> hit, FieldValues fieldSelection)
5151
where TFieldsType : class
5252
{
5353
hit.Fields = fieldSelection;
@@ -80,7 +80,7 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
8080

8181
var instance = (Hit<T>)(typeof(Hit<T>).CreateInstance());
8282
serializer.Populate(reader, instance);
83-
instance.Fields = new FieldSelection<T>(serializer.GetConnectionSettings().Inferrer, instance._fields);
83+
instance.Fields = new FieldValues(serializer.GetConnectionSettings().Inferrer, instance.Fields);
8484
return instance;
8585
}
8686

@@ -159,12 +159,11 @@ internal static Type GetConcreteTypeUsingSelector<T>(
159159
//Hit<dynamic> hitDynamic = new Hit<dynamic>();
160160
dynamic d = jObject;
161161
var fields = jObject["fields"];
162-
var fieldSelectionData = fields?.ToObject<IDictionary<string, object>>();
163-
var sel = new FieldSelection<T>(settings.Inferrer, fieldSelectionData);
162+
var fieldsDictionary = fields?.ToObject<IDictionary<string, object>>();
163+
var fieldValues = new FieldValues(settings.Inferrer, fieldsDictionary);
164164
var hitDynamic = new Hit<dynamic>();
165165
//favor manual mapping over doing Populate twice.
166-
hitDynamic._fields = fieldSelectionData;
167-
hitDynamic.Fields = sel;
166+
hitDynamic.Fields = fieldValues;
168167
hitDynamic.Source = d._source;
169168
hitDynamic.Index = d._index;
170169
hitDynamic.Score = d._score;
@@ -174,16 +173,16 @@ internal static Type GetConcreteTypeUsingSelector<T>(
174173
hitDynamic.Sorts = d.sort;
175174
hitDynamic._Highlight = d.highlight is Dictionary<string, List<string>> ? d.highlight : null;
176175
hitDynamic.Explanation = d._explanation is Explanation ? d._explanation : null;
177-
object o = d._source ?? DynamicResponse.Create(fieldSelectionData) ?? new object {};
176+
object o = d._source ?? DynamicResponse.Create(fieldsDictionary) ?? new object {};
178177
var concreteType = selector(o, hitDynamic);
179178

180179
Type fieldSelectionType;
181180
if (!ConcreteTypeConverter.TypeToFieldTypes.TryGetValue(concreteType, out fieldSelectionType))
182181
{
183-
fieldSelectionType = typeof(FieldSelection<>).MakeGenericType(concreteType);
182+
fieldSelectionType = typeof(FieldValues).MakeGenericType(concreteType);
184183
ConcreteTypeConverter.TypeToFieldTypes.TryAdd(concreteType, fieldSelectionType);
185184
}
186-
selection = fieldSelectionType.CreateInstance(settings, fieldSelectionData);
185+
selection = fieldSelectionType.CreateInstance(settings, fieldsDictionary);
187186
return concreteType;
188187
}
189188
}

src/Nest/Document/Multiple/MultiGet/Response/MultiGetHit.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public interface IMultiGetHit<out T> where T : class
2121
public class MultiGetHit<T> : IMultiGetHit<T>
2222
where T : class
2323
{
24-
public IFieldSelection<T> FieldSelection { get; internal set; }
24+
public FieldValues Fields { get; internal set; }
2525

2626
[JsonProperty(PropertyName = "_source")]
2727
public T Source { get; internal set; }

src/Nest/Document/Multiple/MultiGet/Response/MultiGetHitJsonConverter.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,11 @@ private static void CreateMultiHit<T>(MultiHitTuple tuple, JsonSerializer serial
4141
serializer.Populate(reader, hit);
4242

4343
var settings = serializer.GetConnectionSettings();
44-
var f = new FieldSelection<T>(settings.Inferrer);
4544
var source = tuple.Hit["fields"];
4645
if (source != null)
4746
{
48-
((IFieldSelection<T>)f).FieldValuesDictionary = serializer.Deserialize<Dictionary<string, object>>(source.CreateReader());
49-
hit.FieldSelection = f;
47+
var fieldsDictionary = serializer.Deserialize<Dictionary<string, object>>(source.CreateReader());
48+
hit.Fields = new FieldValues(settings.Inferrer, fieldsDictionary);
5049
}
5150

5251
collection.Add(hit);

src/Nest/Document/Multiple/MultiGet/Response/MultiGetResponse.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ public interface IMultiGetResponse : IResponse
1212
MultiGetHit<T> Get<T>(long id) where T : class;
1313
T Source<T>(string id) where T : class;
1414
T Source<T>(long id) where T : class;
15-
IFieldSelection<T> GetFieldSelection<T>(string id) where T : class;
16-
IFieldSelection<T> GetFieldSelection<T>(long id) where T : class;
15+
FieldValues GetFieldValues<T>(string id) where T : class;
16+
FieldValues GetFieldSelection<T>(long id) where T : class;
1717
IEnumerable<T> SourceMany<T>(IEnumerable<string> ids) where T : class;
1818
IEnumerable<T> SourceMany<T>(IEnumerable<long> ids) where T : class;
1919
IEnumerable<IMultiGetHit<T>> GetMany<T>(IEnumerable<string> ids) where T : class;
@@ -78,16 +78,16 @@ public IEnumerable<IMultiGetHit<T>> GetMany<T>(IEnumerable<long> ids) where T :
7878
{
7979
return this.GetMany<T>(ids.Select(i=>i.ToString(CultureInfo.InvariantCulture)));
8080
}
81-
public IFieldSelection<T> GetFieldSelection<T>(string id) where T : class
81+
public FieldValues GetFieldValues<T>(string id) where T : class
8282
{
8383
var multiHit = this.Get<T>(id);
8484
if (multiHit == null)
8585
return null;
86-
return multiHit.FieldSelection;
86+
return multiHit.Fields;
8787
}
88-
public IFieldSelection<T> GetFieldSelection<T>(long id) where T : class
88+
public FieldValues GetFieldSelection<T>(long id) where T : class
8989
{
90-
return this.GetFieldSelection<T>(id.ToString(CultureInfo.InvariantCulture));
90+
return this.GetFieldValues<T>(id.ToString(CultureInfo.InvariantCulture));
9191
}
9292
}
9393
}

src/Nest/Document/Single/Get/ElasticClient-Get.cs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ public IGetResponse<T> Get<T>(DocumentPath<T> document, Func<GetDescriptor<T>, I
3737
public IGetResponse<T> Get<T>(IGetRequest request) where T : class =>
3838
this.Dispatcher.Dispatch<IGetRequest, GetRequestParameters, GetResponse<T>>(
3939
request,
40-
(r, s) => DeserializeGetResponse<T>(s),
4140
(p, d) => this.LowLevelDispatch.GetDispatch<GetResponse<T>>(p)
4241
);
4342

@@ -49,16 +48,7 @@ public Task<IGetResponse<T>> GetAsync<T>(DocumentPath<T> document, Func<GetDescr
4948
public Task<IGetResponse<T>> GetAsync<T>(IGetRequest request) where T : class =>
5049
this.Dispatcher.DispatchAsync<IGetRequest, GetRequestParameters, GetResponse<T>, IGetResponse<T>>(
5150
request,
52-
(r, s) => DeserializeGetResponse<T>(s),
5351
(p, d) => this.LowLevelDispatch.GetDispatchAsync<GetResponse<T>>(p)
5452
);
55-
56-
private GetResponse<T> DeserializeGetResponse<T>(Stream stream)
57-
where T : class
58-
{
59-
var response = Serializer.Deserialize<GetResponse<T>>(stream);
60-
response.Inferrer = this.ConnectionSettings.Inferrer;
61-
return response;
62-
}
6353
}
6454
}

src/Nest/Document/Single/Get/GetResponse.cs

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,52 +8,37 @@ namespace Nest
88
{
99
public interface IGetResponse<T> : IResponse where T : class
1010
{
11-
bool Found { get; }
11+
[JsonProperty(PropertyName = "_index")]
1212
string Index { get; }
13+
14+
[JsonProperty(PropertyName = "_type")]
1315
string Type { get; }
16+
17+
[JsonProperty(PropertyName = "_id")]
1418
string Id { get; }
19+
20+
[JsonProperty(PropertyName = "_version")]
1521
long Version { get; }
22+
23+
[JsonProperty(PropertyName = "found")]
24+
bool Found { get; }
25+
26+
[JsonProperty(PropertyName = "_source")]
1627
T Source { get; }
17-
FieldSelection<T> Fields { get; }
28+
29+
[JsonProperty(PropertyName = "fields")]
30+
FieldValues Fields { get; }
1831
}
1932

2033
[JsonObject(MemberSerialization.OptIn)]
2134
public class GetResponse<T> : BaseResponse, IGetResponse<T> where T : class
2235
{
23-
internal ElasticInferrer Inferrer { get; set; }
24-
25-
[JsonProperty(PropertyName = "_index")]
2636
public string Index { get; private set; }
27-
28-
[JsonProperty(PropertyName = "_type")]
2937
public string Type { get; private set; }
30-
31-
[JsonProperty(PropertyName = "_id")]
3238
public string Id { get; private set; }
33-
34-
[JsonProperty(PropertyName = "_version")]
3539
public long Version { get; private set; }
36-
37-
[JsonProperty(PropertyName = "found")]
3840
public bool Found { get; private set; }
39-
40-
[JsonProperty(PropertyName = "_source")]
4141
public T Source { get; private set; }
42-
43-
[JsonProperty(PropertyName = "fields")]
44-
private IDictionary<string, object> _rawFields;
45-
46-
private FieldSelection<T> _fields = null;
47-
public FieldSelection<T> Fields
48-
{
49-
get
50-
{
51-
if (_fields != null)
52-
return _fields;
53-
54-
_fields = new FieldSelection<T>(Inferrer, _rawFields);
55-
return _fields;
56-
}
57-
}
42+
public FieldValues Fields { get; private set; }
5843
}
5944
}

0 commit comments

Comments
 (0)