44
55using System ;
66using System . Diagnostics ;
7- using System . Diagnostics . CodeAnalysis ;
87using System . Globalization ;
98using System . Linq . Expressions ;
109using System . Reflection ;
1514#if ELASTICSEARCH_SERVERLESS
1615namespace Elastic . Clients . Elasticsearch . Serverless ;
1716#else
18-
1917namespace Elastic . Clients . Elasticsearch ;
2018#endif
2119
@@ -25,6 +23,10 @@ public sealed class Field :
2523 IEquatable < Field > ,
2624 IUrlParameter
2725{
26+ #if ! NET && ! NETSTANDARD2_1_OR_GREATER
27+ private static readonly char [ ] BoostSplit = [ '^' ] ;
28+ #endif
29+
2830 private readonly object _comparisonValue ;
2931 private readonly Type ? _type ;
3032
@@ -67,22 +69,10 @@ public sealed class Field :
6769
6870 #region Constructors
6971
70- public Field ( string name ) : this ( name , null , null )
71- {
72- }
73-
74- public Field ( string name , double boost ) : this ( name , boost , null )
75- {
76- }
77-
78- public Field ( string name , string format ) : this ( name , null , format )
79- {
80- }
81-
82- public Field ( string name , double ? boost , string ? format )
72+ public Field ( string name , double ? boost = null , string ? format = null )
8373 {
8474 if ( string . IsNullOrEmpty ( name ) )
85- throw new ArgumentException ( $ " { name } can not be null or empty.", nameof ( name ) ) ;
75+ throw new ArgumentException ( "Field name can not be null or empty.", nameof ( name ) ) ;
8676
8777 Name = ParseFieldName ( name , out var b ) ;
8878 Boost = b ?? boost ;
@@ -91,6 +81,16 @@ public Field(string name, double? boost, string? format)
9181 _comparisonValue = Name ;
9282 }
9383
84+ public Field ( string name , double boost ) :
85+ this ( name , boost , null )
86+ {
87+ }
88+
89+ public Field ( string name , string format ) :
90+ this ( name , null , format )
91+ {
92+ }
93+
9494 public Field ( Expression expression , double ? boost = null , string ? format = null )
9595 {
9696 Expression = expression ?? throw new ArgumentNullException ( nameof ( expression ) ) ;
@@ -117,21 +117,21 @@ public Field(PropertyInfo property, double? boost = null, string? format = null)
117117
118118 #region Factory Methods
119119
120- public static Field ? FromString ( string ? name ) => string . IsNullOrEmpty ( name ) ? null : new Field ( name ) ;
120+ public static Field FromString ( string name ) => new ( name ) ;
121121
122- public static Field ? FromExpression ( Expression ? expression ) => expression is null ? null : new Field ( expression ) ;
122+ public static Field FromExpression ( Expression expression ) => new ( expression ) ;
123123
124- public static Field ? FromProperty ( PropertyInfo ? property ) => property is null ? null : new Field ( property ) ;
124+ public static Field FromProperty ( PropertyInfo property ) => new ( property ) ;
125125
126126 #endregion Factory Methods
127127
128128 #region Conversion Operators
129129
130- public static implicit operator Field ? ( string ? name ) => FromString ( name ) ;
130+ public static implicit operator Field ( string name ) => FromString ( name ) ;
131131
132- public static implicit operator Field ? ( Expression ? expression ) => FromExpression ( expression ) ;
132+ public static implicit operator Field ( Expression expression ) => FromExpression ( expression ) ;
133133
134- public static implicit operator Field ? ( PropertyInfo ? property ) => FromProperty ( property ) ;
134+ public static implicit operator Field ( PropertyInfo property ) => FromProperty ( property ) ;
135135
136136 #endregion Conversion Operators
137137
@@ -203,15 +203,7 @@ public override bool Equals(object? obj) =>
203203 _ => false
204204 } ;
205205
206- public override int GetHashCode ( )
207- {
208- unchecked
209- {
210- var hashCode = _comparisonValue ? . GetHashCode ( ) ?? 0 ;
211- hashCode = ( hashCode * 397 ) ^ ( _type ? . GetHashCode ( ) ?? 0 ) ;
212- return hashCode ;
213- }
214- }
206+ public override int GetHashCode ( ) => HashCode . Combine ( _comparisonValue , _type ) ;
215207
216208 #endregion Equality
217209
@@ -243,20 +235,29 @@ public override string ToString() =>
243235
244236 #endregion Debugging
245237
246- [ return : NotNullIfNotNull ( nameof ( name ) ) ]
247- private static string ? ParseFieldName ( string ? name , out double ? boost )
238+ private static string ParseFieldName ( string name , out double ? boost )
248239 {
249240 boost = null ;
250- if ( name is null )
251- return null ;
252241
253- var caretIndex = name . IndexOf ( '^' ) ;
254- if ( caretIndex == - 1 )
242+ #if NET || NETSTANDARD2_1_OR_GREATER
243+ if ( ! name . Contains ( '^' , StringComparison . Ordinal ) )
255244 return name ;
245+ #else
246+ if ( name . IndexOf ( '^' ) == - 1 )
247+ return name ;
248+ #endif
256249
257- var parts = name . Split ( new [ ] { '^' } , 2 , StringSplitOptions . RemoveEmptyEntries ) ;
250+ #if NET || NETSTANDARD2_1_OR_GREATER
251+ var parts = name . Split ( '^' , 2 , StringSplitOptions . RemoveEmptyEntries ) ;
252+ #else
253+ var parts = name . Split ( BoostSplit , 2 , StringSplitOptions . RemoveEmptyEntries ) ;
254+ #endif
258255 name = parts [ 0 ] ;
259256 boost = double . Parse ( parts [ 1 ] , CultureInfo . InvariantCulture ) ;
257+
258+ if ( string . IsNullOrWhiteSpace ( name ) )
259+ throw new ArgumentException ( "Field name can not be null or empty." , nameof ( name ) ) ;
260+
260261 return name ;
261262 }
262263}
0 commit comments