|
3 | 3 | // See the LICENSE file in the project root for more information |
4 | 4 |
|
5 | 5 | using System; |
| 6 | +using System.Collections; |
6 | 7 | using System.Collections.Generic; |
| 8 | +using System.Diagnostics; |
| 9 | +using System.Linq; |
| 10 | +using System.Linq.Expressions; |
| 11 | +using System.Reflection; |
7 | 12 | using Elastic.Transport; |
8 | 13 |
|
9 | | -namespace Elastic.Clients.Elasticsearch |
| 14 | +namespace Elastic.Clients.Elasticsearch; |
| 15 | + |
| 16 | +[DebuggerDisplay("{DebugDisplay,nq}")] |
| 17 | +public partial class Fields : IUrlParameter, IEnumerable<Field>, IEquatable<Fields> |
10 | 18 | { |
11 | | - public partial class Fields |
| 19 | + internal readonly List<Field> ListOfFields; |
| 20 | + |
| 21 | + internal Fields() => ListOfFields = new List<Field>(); |
| 22 | + |
| 23 | + internal Fields(IEnumerable<Field> fieldNames) => ListOfFields = fieldNames.ToList(); |
| 24 | + |
| 25 | + private string DebugDisplay => |
| 26 | + $"Count: {ListOfFields.Count} [" + |
| 27 | + string.Join(",", ListOfFields.Select((t, i) => $"({i + 1}: {t?.DebugDisplay ?? "NULL"})")) + "]"; |
| 28 | + |
| 29 | + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); |
| 30 | + |
| 31 | + public IEnumerator<Field> GetEnumerator() => ListOfFields.GetEnumerator(); |
| 32 | + |
| 33 | + public bool Equals(Fields other) => EqualsAllFields(ListOfFields, other?.ListOfFields); |
| 34 | + |
| 35 | + string IUrlParameter.GetString(ITransportConfiguration settings) |
12 | 36 | { |
13 | | - // This is temporary |
14 | | - public Fields(IEnumerable<Field> fields) => _fieldList.AddRange(fields); |
| 37 | + if (!(settings is IElasticsearchClientSettings elasticsearchClientSettings)) |
| 38 | + { |
| 39 | + throw new ArgumentNullException(nameof(settings), |
| 40 | + $"Can not resolve {nameof(Fields)} if no {nameof(IElasticsearchClientSettings)} is provided"); |
| 41 | + } |
| 42 | + |
| 43 | + return string.Join(",", |
| 44 | + ListOfFields.Where(f => f != null).Select(f => ((IUrlParameter)f).GetString(elasticsearchClientSettings))); |
| 45 | + } |
| 46 | + |
| 47 | + public override string ToString() => DebugDisplay; |
| 48 | + |
| 49 | + public static implicit operator Fields(string[] fields) => |
| 50 | + fields.IsEmpty() ? null : new Fields(fields.Select(f => new Field(f))); |
15 | 51 |
|
16 | | - public string GetString(ITransportConfiguration settings) => throw new NotImplementedException(); |
| 52 | + public static implicit operator Fields(string field) => field.IsNullOrEmptyCommaSeparatedList(out var split) |
| 53 | + ? null |
| 54 | + : new Fields(split.Select(f => new Field(f))); |
| 55 | + |
| 56 | + public static implicit operator Fields(Expression[] fields) => |
| 57 | + fields.IsEmpty() ? null : new Fields(fields.Select(f => new Field(f))); |
| 58 | + |
| 59 | + public static implicit operator Fields(Expression field) => |
| 60 | + field == null ? null : new Fields(new[] { new Field(field) }); |
| 61 | + |
| 62 | + public static implicit operator Fields(Field field) => field == null ? null : new Fields(new[] { field }); |
| 63 | + |
| 64 | + public static implicit operator Fields(PropertyInfo field) => |
| 65 | + field == null ? null : new Fields(new Field[] { field }); |
| 66 | + |
| 67 | + public static implicit operator Fields(PropertyInfo[] fields) => |
| 68 | + fields.IsEmpty() ? null : new Fields(fields.Select(f => new Field(f))); |
| 69 | + |
| 70 | + public static implicit operator Fields(Field[] fields) => fields.IsEmpty() ? null : new Fields(fields); |
| 71 | + |
| 72 | + public Fields And<T, TValue>(Expression<Func<T, TValue>> field, double? boost = null, string format = null) |
| 73 | + where T : class |
| 74 | + { |
| 75 | + ListOfFields.Add(new Field(field, boost, format)); |
| 76 | + return this; |
17 | 77 | } |
18 | 78 |
|
19 | | - //[DebuggerDisplay("{DebugDisplay,nq}")] |
20 | | - //public partial class Fields : IUrlParameter, IEnumerable<Field>, IEquatable<Fields> |
21 | | - //{ |
22 | | - // internal readonly List<Field> ListOfFields; |
| 79 | + public Fields And(string field, double? boost = null, string format = null) |
| 80 | + { |
| 81 | + ListOfFields.Add(new Field(field, boost, format)); |
| 82 | + return this; |
| 83 | + } |
23 | 84 |
|
24 | | - // internal Fields() => ListOfFields = new List<Field>(); |
| 85 | + public Fields And(PropertyInfo property, double? boost = null) |
| 86 | + { |
| 87 | + ListOfFields.Add(new Field(property, boost)); |
| 88 | + return this; |
| 89 | + } |
| 90 | + |
| 91 | + public Fields And<T>(params Expression<Func<T, object>>[] fields) where T : class |
| 92 | + { |
| 93 | + ListOfFields.AddRange(fields.Select(f => new Field(f))); |
| 94 | + return this; |
| 95 | + } |
| 96 | + |
| 97 | + public Fields And(params string[] fields) |
| 98 | + { |
| 99 | + ListOfFields.AddRange(fields.Select(f => new Field(f))); |
| 100 | + return this; |
| 101 | + } |
| 102 | + |
| 103 | + public Fields And(params PropertyInfo[] properties) |
| 104 | + { |
| 105 | + ListOfFields.AddRange(properties.Select(f => new Field(f))); |
| 106 | + return this; |
| 107 | + } |
| 108 | + |
| 109 | + public Fields And(params Field[] fields) |
| 110 | + { |
| 111 | + ListOfFields.AddRange(fields); |
| 112 | + return this; |
| 113 | + } |
| 114 | + |
| 115 | + public static bool operator ==(Fields left, Fields right) => Equals(left, right); |
25 | 116 |
|
26 | | - // internal Fields(IEnumerable<Field> fieldNames) => ListOfFields = fieldNames.ToList(); |
| 117 | + public static bool operator !=(Fields left, Fields right) => !Equals(left, right); |
27 | 118 |
|
28 | | - // private string DebugDisplay => |
29 | | - // $"Count: {ListOfFields.Count} [" + |
30 | | - // string.Join(",", ListOfFields.Select((t, i) => $"({i + 1}: {t?.DebugDisplay ?? "NULL"})")) + "]"; |
| 119 | + public override bool Equals(object obj) |
| 120 | + { |
| 121 | + switch (obj) |
| 122 | + { |
| 123 | + case Fields f: |
| 124 | + return Equals(f); |
| 125 | + case string s: |
| 126 | + return Equals(s); |
| 127 | + case Field fn: |
| 128 | + return Equals(fn); |
| 129 | + case Field[] fns: |
| 130 | + return Equals(fns); |
| 131 | + case Expression e: |
| 132 | + return Equals(e); |
| 133 | + case Expression[] es: |
| 134 | + return Equals(es); |
| 135 | + default: |
| 136 | + return false; |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + private static bool EqualsAllFields(IReadOnlyList<Field> thisTypes, IReadOnlyList<Field> otherTypes) |
| 141 | + { |
| 142 | + if (thisTypes == null && otherTypes == null) |
| 143 | + return true; |
| 144 | + if (thisTypes == null || otherTypes == null) |
| 145 | + return false; |
| 146 | + if (thisTypes.Count != otherTypes.Count) |
| 147 | + return false; |
| 148 | + |
| 149 | + return thisTypes.Count == otherTypes.Count && !thisTypes.Except(otherTypes).Any(); |
| 150 | + } |
31 | 151 |
|
32 | | - // IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); |
33 | | - |
34 | | - // public IEnumerator<Field> GetEnumerator() => ListOfFields.GetEnumerator(); |
35 | | - |
36 | | - // public bool Equals(Fields other) => EqualsAllFields(ListOfFields, other?.ListOfFields); |
37 | | - |
38 | | - // string IUrlParameter.GetString(ITransportConfiguration settings) |
39 | | - // { |
40 | | - // if (!(settings is IElasticsearchClientSettings Elastic.Clients.ElasticsearchSettings)) |
41 | | - // { |
42 | | - // throw new ArgumentNullException(nameof(settings), |
43 | | - // $"Can not resolve {nameof(Fields)} if no {nameof(IElasticsearchClientSettings)} is provided"); |
44 | | - // } |
45 | | - |
46 | | - // return string.Join(",", |
47 | | - // ListOfFields.Where(f => f != null).Select(f => ((IUrlParameter)f).GetString(Elastic.Clients.ElasticsearchSettings))); |
48 | | - // } |
49 | | - |
50 | | - // public override string ToString() => DebugDisplay; |
51 | | - |
52 | | - // public static implicit operator Fields(string[] fields) => |
53 | | - // fields.IsEmpty() ? null : new Fields(fields.Select(f => new Field(f))); |
54 | | - |
55 | | - // public static implicit operator Fields(string field) => field.IsNullOrEmptyCommaSeparatedList(out var split) |
56 | | - // ? null |
57 | | - // : new Fields(split.Select(f => new Field(f))); |
58 | | - |
59 | | - // public static implicit operator Fields(Expression[] fields) => |
60 | | - // fields.IsEmpty() ? null : new Fields(fields.Select(f => new Field(f))); |
61 | | - |
62 | | - // public static implicit operator Fields(Expression field) => |
63 | | - // field == null ? null : new Fields(new[] {new Field(field)}); |
64 | | - |
65 | | - // public static implicit operator Fields(Field field) => field == null ? null : new Fields(new[] {field}); |
66 | | - |
67 | | - // public static implicit operator Fields(PropertyInfo field) => |
68 | | - // field == null ? null : new Fields(new Field[] {field}); |
69 | | - |
70 | | - // public static implicit operator Fields(PropertyInfo[] fields) => |
71 | | - // fields.IsEmpty() ? null : new Fields(fields.Select(f => new Field(f))); |
72 | | - |
73 | | - // public static implicit operator Fields(Field[] fields) => fields.IsEmpty() ? null : new Fields(fields); |
74 | | - |
75 | | - // public Fields And<T, TValue>(Expression<Func<T, TValue>> field, double? boost = null, string format = null) |
76 | | - // where T : class |
77 | | - // { |
78 | | - // ListOfFields.Add(new Field(field, boost, format)); |
79 | | - // return this; |
80 | | - // } |
81 | | - |
82 | | - // public Fields And(string field, double? boost = null, string format = null) |
83 | | - // { |
84 | | - // ListOfFields.Add(new Field(field, boost, format)); |
85 | | - // return this; |
86 | | - // } |
87 | | - |
88 | | - // public Fields And(PropertyInfo property, double? boost = null) |
89 | | - // { |
90 | | - // ListOfFields.Add(new Field(property, boost)); |
91 | | - // return this; |
92 | | - // } |
93 | | - |
94 | | - // public Fields And<T>(params Expression<Func<T, object>>[] fields) where T : class |
95 | | - // { |
96 | | - // ListOfFields.AddRange(fields.Select(f => new Field(f))); |
97 | | - // return this; |
98 | | - // } |
99 | | - |
100 | | - // public Fields And(params string[] fields) |
101 | | - // { |
102 | | - // ListOfFields.AddRange(fields.Select(f => new Field(f))); |
103 | | - // return this; |
104 | | - // } |
105 | | - |
106 | | - // public Fields And(params PropertyInfo[] properties) |
107 | | - // { |
108 | | - // ListOfFields.AddRange(properties.Select(f => new Field(f))); |
109 | | - // return this; |
110 | | - // } |
111 | | - |
112 | | - // public Fields And(params Field[] fields) |
113 | | - // { |
114 | | - // ListOfFields.AddRange(fields); |
115 | | - // return this; |
116 | | - // } |
117 | | - |
118 | | - // public static bool operator ==(Fields left, Fields right) => Equals(left, right); |
119 | | - |
120 | | - // public static bool operator !=(Fields left, Fields right) => !Equals(left, right); |
121 | | - |
122 | | - // public override bool Equals(object obj) |
123 | | - // { |
124 | | - // switch (obj) |
125 | | - // { |
126 | | - // case Fields f: |
127 | | - // return Equals(f); |
128 | | - // case string s: |
129 | | - // return Equals(s); |
130 | | - // case Field fn: |
131 | | - // return Equals(fn); |
132 | | - // case Field[] fns: |
133 | | - // return Equals(fns); |
134 | | - // case Expression e: |
135 | | - // return Equals(e); |
136 | | - // case Expression[] es: |
137 | | - // return Equals(es); |
138 | | - // default: |
139 | | - // return false; |
140 | | - // } |
141 | | - // } |
142 | | - |
143 | | - // private static bool EqualsAllFields(IReadOnlyList<Field> thisTypes, IReadOnlyList<Field> otherTypes) |
144 | | - // { |
145 | | - // if (thisTypes == null && otherTypes == null) |
146 | | - // return true; |
147 | | - // if (thisTypes == null || otherTypes == null) |
148 | | - // return false; |
149 | | - // if (thisTypes.Count != otherTypes.Count) |
150 | | - // return false; |
151 | | - |
152 | | - // return thisTypes.Count == otherTypes.Count && !thisTypes.Except(otherTypes).Any(); |
153 | | - // } |
154 | | - |
155 | | - // public override int GetHashCode() => ListOfFields.GetHashCode(); |
156 | | - //} |
| 152 | + public override int GetHashCode() => ListOfFields.GetHashCode(); |
157 | 153 | } |
0 commit comments