5
5
using System ;
6
6
using System . Collections . Generic ;
7
7
using System . Diagnostics ;
8
+ using System . Diagnostics . CodeAnalysis ;
8
9
using System . Linq ;
9
10
using System . Text . Json ;
10
11
using System . Text . Json . Serialization ;
@@ -16,31 +17,40 @@ namespace Elastic.Clients.Elasticsearch;
16
17
17
18
[ DebuggerDisplay ( "{DebugDisplay,nq}" ) ]
18
19
[ JsonConverter ( typeof ( NamesConverter ) ) ]
19
- public sealed class Names : IEquatable < Names > , IUrlParameter
20
+ public sealed class Names :
21
+ IEquatable < Names > ,
22
+ IUrlParameter
23
+ #if NET7_0_OR_GREATER
24
+ , IParsable < Names >
25
+ #endif
20
26
{
21
- public Names ( IEnumerable < string > names ) : this ( names ? . Select ( n => ( Name ) n ) . ToList ( ) )
27
+ public Names ( )
22
28
{
29
+ Values = [ ] ;
23
30
}
24
31
25
32
public Names ( IEnumerable < Name > names )
26
33
{
27
- Value = names ? . ToList ( ) ;
28
- if ( ! Value . HasAny ( ) )
29
- throw new ArgumentException ( $ "can not create { nameof ( Names ) } on an empty enumerable of ", nameof ( names ) ) ;
34
+ Values = names ? . ToList ( ) ;
30
35
}
31
36
32
- internal IList < Name > Value { get ; }
37
+ public Names ( IEnumerable < string > names ) :
38
+ this ( names ? . Select ( n => ( Name ) n ) . ToList ( ) )
39
+ {
40
+ }
41
+
42
+ internal IList < Name > Values { get ; init ; }
33
43
34
44
private string DebugDisplay => ( ( IUrlParameter ) this ) . GetString ( null ) ;
35
45
36
46
public override string ToString ( ) => DebugDisplay ;
37
47
38
- public bool Equals ( Names other ) => EqualsAllIds ( Value , other . Value ) ;
48
+ public bool Equals ( Names other ) => EqualsAllIds ( Values , other . Values ) ;
39
49
40
50
string IUrlParameter . GetString ( ITransportConfiguration ? settings ) =>
41
- string . Join ( "," , Value . Cast < IUrlParameter > ( ) . Select ( n => n . GetString ( settings ) ) ) ;
51
+ string . Join ( "," , Values . Cast < IUrlParameter > ( ) . Select ( n => n . GetString ( settings ) ) ) ;
42
52
43
- public static Names Parse ( string names ) => names . IsNullOrEmptyCommaSeparatedList ( out var list ) ? null : new Names ( list ) ;
53
+ public static Names Parse ( string names ) => names . IsNullOrEmptyCommaSeparatedList ( out var list ) ? new Names ( ) : new Names ( list ) ;
44
54
45
55
public static implicit operator Names ( Name name ) => name == null ? null : new Names ( new [ ] { name } ) ;
46
56
@@ -66,7 +76,49 @@ private static bool EqualsAllIds(ICollection<Name> thisIds, ICollection<Name> ot
66
76
67
77
public override bool Equals ( object obj ) => obj is string s ? Equals ( Parse ( s ) ) : obj is Names i && Equals ( i ) ;
68
78
69
- public override int GetHashCode ( ) => Value . GetHashCode ( ) ;
79
+ public override int GetHashCode ( ) => Values . GetHashCode ( ) ;
80
+
81
+ #region IParsable
82
+
83
+ #if NET7_0_OR_GREATER
84
+
85
+ public static Names Parse ( string s , IFormatProvider ? provider ) =>
86
+ TryParse ( s , provider , out var result ) ? result : throw new FormatException ( ) ;
87
+
88
+ public static bool TryParse ( [ NotNullWhen ( true ) ] string ? s , IFormatProvider ? provider ,
89
+ [ NotNullWhen ( true ) ] out Names ? result )
90
+ {
91
+ if ( s is null )
92
+ {
93
+ result = null ;
94
+ return false ;
95
+ }
96
+
97
+ if ( s . IsNullOrEmptyCommaSeparatedList ( out var list ) )
98
+ {
99
+ result = new Names ( ) ;
100
+ return true ;
101
+ }
102
+
103
+ var names = new List < Name > ( ) ;
104
+ foreach ( var item in list )
105
+ {
106
+ if ( ! Name . TryParse ( item , provider , out var name ) )
107
+ {
108
+ result = null ;
109
+ return false ;
110
+ }
111
+
112
+ names . Add ( name ) ;
113
+ }
114
+
115
+ result = new Names { Values = names } ;
116
+ return true ;
117
+ }
118
+
119
+ #endif
120
+
121
+ #endregion IParsable
70
122
}
71
123
72
124
internal sealed class NamesConverter :
@@ -84,12 +136,12 @@ public override Names Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSe
84
136
85
137
public override void Write ( Utf8JsonWriter writer , Names value , JsonSerializerOptions options )
86
138
{
87
- if ( value . Value is [ { } single ] )
139
+ if ( value . Values is [ { } single ] )
88
140
{
89
141
writer . WriteValue ( options , single ) ;
90
142
return ;
91
143
}
92
144
93
- writer . WriteValue ( options , value . Value ) ;
145
+ writer . WriteValue ( options , value . Values ) ;
94
146
}
95
147
}
0 commit comments