2
2
// The .NET Foundation licenses this file to you under the MIT license.
3
3
4
4
using System . Collections ;
5
+ using System . Diagnostics ;
5
6
using System . Diagnostics . CodeAnalysis ;
6
7
7
8
namespace Aspire . Hosting ;
@@ -108,7 +109,7 @@ public sealed class InteractionInput
108
109
/// Gets or sets the name for the input. Used for accessing inputs by name from a keyed collection.
109
110
/// If not specified, a name will be generated automatically.
110
111
/// </summary>
111
- public string ? Name { get ; init ; }
112
+ public string ? Name { get ; internal set ; }
112
113
113
114
/// <summary>
114
115
/// Gets or sets the label for the input.
@@ -175,6 +176,7 @@ public int? MaxLength
175
176
/// A collection of interaction inputs that supports both indexed and name-based access.
176
177
/// </summary>
177
178
[ Experimental ( InteractionService . DiagnosticId , UrlFormat = "https://aka.ms/aspire/diagnostics/{0}" ) ]
179
+ [ DebuggerDisplay ( "Count = {Count}" ) ]
178
180
public sealed class InteractionInputCollection : IReadOnlyList < InteractionInput >
179
181
{
180
182
private readonly IReadOnlyList < InteractionInput > _inputs ;
@@ -190,7 +192,7 @@ public InteractionInputCollection(IReadOnlyList<InteractionInput> inputs)
190
192
var processedInputs = new List < InteractionInput > ( ) ;
191
193
var inputsByName = new Dictionary < string , InteractionInput > ( StringComparer . OrdinalIgnoreCase ) ;
192
194
var usedNames = new HashSet < string > ( StringComparer . OrdinalIgnoreCase ) ;
193
-
195
+
194
196
// First pass: collect explicit names and check for duplicates
195
197
foreach ( var input in inputs )
196
198
{
@@ -203,53 +205,39 @@ public InteractionInputCollection(IReadOnlyList<InteractionInput> inputs)
203
205
usedNames . Add ( input . Name ) ;
204
206
}
205
207
}
206
-
208
+
207
209
// Second pass: create new inputs with generated names where needed
208
- for ( int i = 0 ; i < inputs . Count ; i ++ )
210
+ for ( var i = 0 ; i < inputs . Count ; i ++ )
209
211
{
210
212
var input = inputs [ i ] ;
211
213
string finalName ;
212
-
214
+
213
215
if ( ! string . IsNullOrWhiteSpace ( input . Name ) )
214
216
{
215
217
finalName = input . Name ;
216
218
}
217
219
else
218
220
{
219
221
// Generate a unique name based on the label or index
220
- string baseName = GenerateBaseName ( input . Label ) ;
222
+ var baseName = GenerateBaseName ( input . Label ) ;
221
223
finalName = baseName ;
222
- int suffix = 1 ;
223
-
224
+ var suffix = 1 ;
225
+
224
226
while ( usedNames . Contains ( finalName ) )
225
227
{
226
228
finalName = $ "{ baseName } _{ suffix } ";
227
229
suffix ++ ;
228
230
}
229
-
231
+
230
232
usedNames . Add ( finalName ) ;
233
+
234
+ input . Name = finalName ;
231
235
}
232
-
233
- // Create a new input with the final name if it was generated
234
- var finalInput = string . IsNullOrWhiteSpace ( input . Name ) ?
235
- new InteractionInput
236
- {
237
- Name = finalName ,
238
- Label = input . Label ,
239
- Description = input . Description ,
240
- EnableDescriptionMarkdown = input . EnableDescriptionMarkdown ,
241
- InputType = input . InputType ,
242
- Required = input . Required ,
243
- Options = input . Options ,
244
- Value = input . Value ,
245
- Placeholder = input . Placeholder ,
246
- MaxLength = input . MaxLength
247
- } : input ;
248
-
249
- processedInputs . Add ( finalInput ) ;
250
- inputsByName [ finalName ] = finalInput ;
236
+
237
+ processedInputs . Add ( input ) ;
238
+ inputsByName [ finalName ] = input ;
251
239
}
252
-
240
+
253
241
_inputs = processedInputs ;
254
242
_inputsByName = inputsByName ;
255
243
}
@@ -260,14 +248,14 @@ private static string GenerateBaseName(string label)
260
248
{
261
249
return "Input" ;
262
250
}
263
-
251
+
264
252
// Convert to a valid identifier-like name
265
253
var chars = label . ToCharArray ( ) ;
266
254
var result = new System . Text . StringBuilder ( ) ;
267
-
268
- for ( int i = 0 ; i < chars . Length ; i ++ )
255
+
256
+ for ( var i = 0 ; i < chars . Length ; i ++ )
269
257
{
270
- char c = chars [ i ] ;
258
+ var c = chars [ i ] ;
271
259
if ( char . IsLetterOrDigit ( c ) )
272
260
{
273
261
result . Append ( c ) ;
@@ -277,9 +265,9 @@ private static string GenerateBaseName(string label)
277
265
result . Append ( '_' ) ;
278
266
}
279
267
}
280
-
268
+
281
269
// Ensure we have a valid name
282
- string name = result . ToString ( ) . Trim ( '_' ) ;
270
+ var name = result . ToString ( ) . Trim ( '_' ) ;
283
271
return string . IsNullOrEmpty ( name ) ? "Input" : name ;
284
272
}
285
273
0 commit comments