20
20
21
21
namespace Microsoft . Diagnostics . Tools . Monitor
22
22
{
23
- internal static class CommonOptionsExtensions
23
+ internal class CommonOptionsMapper
24
24
{
25
25
private const string KeySegmentSeparator = "__" ;
26
26
@@ -30,7 +30,7 @@ internal static class CommonOptionsExtensions
30
30
/// <remarks>
31
31
/// Each key is the configuration path; each value is the configuration path value.
32
32
/// </remarks>
33
- public static IDictionary < string , string > ToConfigurationValues ( this RootOptions options )
33
+ public IDictionary < string , string > ToConfigurationValues ( RootOptions options )
34
34
{
35
35
Dictionary < string , string > variables = new ( StringComparer . OrdinalIgnoreCase ) ;
36
36
MapRootOptions ( options , string . Empty , ConfigurationPath . KeyDelimiter , variables ) ;
@@ -43,7 +43,7 @@ public static IDictionary<string, string> ToConfigurationValues(this RootOptions
43
43
/// <remarks>
44
44
/// Each key is the variable name; each value is the variable value.
45
45
/// </remarks>
46
- public static IDictionary < string , string > ToEnvironmentConfiguration ( this RootOptions options , bool useDotnetMonitorPrefix = false )
46
+ public IDictionary < string , string > ToEnvironmentConfiguration ( RootOptions options , bool useDotnetMonitorPrefix = false )
47
47
{
48
48
Dictionary < string , string > variables = new ( StringComparer . OrdinalIgnoreCase ) ;
49
49
MapRootOptions ( options , useDotnetMonitorPrefix ? ToolIdentifiers . StandardPrefix : string . Empty , KeySegmentSeparator , variables ) ;
@@ -56,7 +56,7 @@ public static IDictionary<string, string> ToEnvironmentConfiguration(this RootOp
56
56
/// <remarks>
57
57
/// Each key is the file name; each value is the file content.
58
58
/// </remarks>
59
- public static IDictionary < string , string > ToKeyPerFileConfiguration ( this RootOptions options )
59
+ public IDictionary < string , string > ToKeyPerFileConfiguration ( RootOptions options )
60
60
{
61
61
Dictionary < string , string > variables = new ( StringComparer . OrdinalIgnoreCase ) ;
62
62
MapRootOptions ( options , string . Empty , KeySegmentSeparator , variables ) ;
@@ -97,7 +97,7 @@ public static IDictionary<string, string> ToKeyPerFileConfiguration(this RootOpt
97
97
// }
98
98
// }
99
99
100
- private static void MapRootOptions ( RootOptions obj , string prefix , string separator , IDictionary < string , string > map )
100
+ private void MapRootOptions ( RootOptions obj , string prefix , string separator , IDictionary < string , string > map )
101
101
{
102
102
// TODO: in Tests, it has an additional property. Weird.
103
103
MapAuthenticationOptions ( obj . Authentication , FormattableString . Invariant ( $ "{ prefix } { nameof ( obj . Authentication ) } ") , separator , map ) ;
@@ -134,7 +134,7 @@ private static void MapRootOptions(RootOptions obj, string prefix, string separa
134
134
// }
135
135
// }
136
136
137
- private static void MapDictionary_String_CollectionRuleOptions ( IDictionary < string , CollectionRuleOptions > ? obj , string valueName , string separator , IDictionary < string , string > map )
137
+ private void MapDictionary_String_CollectionRuleOptions ( IDictionary < string , CollectionRuleOptions > ? obj , string valueName , string separator , IDictionary < string , string > map )
138
138
{
139
139
if ( null != obj )
140
140
{
@@ -147,7 +147,7 @@ private static void MapDictionary_String_CollectionRuleOptions(IDictionary<strin
147
147
}
148
148
}
149
149
150
- private static void MapCollectionRuleOptions ( CollectionRuleOptions obj , string valueName , string separator , IDictionary < string , string > map )
150
+ private void MapCollectionRuleOptions ( CollectionRuleOptions obj , string valueName , string separator , IDictionary < string , string > map )
151
151
{
152
152
string prefix = FormattableString . Invariant ( $ "{ valueName } { separator } ") ;
153
153
// MapFilters(obj.Filters, FormattableString.Invariant($"{prefix}{nameof(obj.Filters)}"), separator, map);
@@ -156,7 +156,7 @@ private static void MapCollectionRuleOptions(CollectionRuleOptions obj, string v
156
156
// MapLimits(obj.Limits, FormattableString.Invariant($"{prefix}{nameof(obj.Limits)}"), separator, map);
157
157
}
158
158
159
- private static void MapActions ( List < CollectionRuleActionOptions > obj , string valueName , string separator , IDictionary < string , string > map )
159
+ private void MapActions ( List < CollectionRuleActionOptions > obj , string valueName , string separator , IDictionary < string , string > map )
160
160
{
161
161
string prefix = FormattableString . Invariant ( $ "{ valueName } { separator } ") ;
162
162
for ( int index = 0 ; index < obj . Count ; index ++ )
@@ -166,7 +166,7 @@ private static void MapActions(List<CollectionRuleActionOptions> obj, string val
166
166
}
167
167
}
168
168
169
- private static void MapCollectionRuleActionOptions ( CollectionRuleActionOptions obj , string valueName , string separator , IDictionary < string , string > map )
169
+ private void MapCollectionRuleActionOptions ( CollectionRuleActionOptions obj , string valueName , string separator , IDictionary < string , string > map )
170
170
{
171
171
string prefix = FormattableString . Invariant ( $ "{ valueName } { separator } ") ;
172
172
MapString ( obj . Name , FormattableString . Invariant ( $ "{ prefix } { nameof ( obj . Name ) } ") , map ) ;
@@ -175,10 +175,21 @@ private static void MapCollectionRuleActionOptions(CollectionRuleActionOptions o
175
175
MapBool ( obj . WaitForCompletion , FormattableString . Invariant ( $ "{ prefix } { nameof ( obj . WaitForCompletion ) } ") , map ) ;
176
176
}
177
177
178
- private static void MapCollectionRuleActionOptions_Settings ( string type , object ? settings , string valueName , string separator , IDictionary < string , string > map )
178
+ private Dictionary < string , Action < object , string , string , IDictionary < string , string > > > ? _actionSettingsMap ;
179
+
180
+ public void AddActionSettings < TSettings > ( string type , Action < TSettings , string , string , IDictionary < string , string > > mapAction )
181
+ {
182
+ ( _actionSettingsMap ??= new ( ) ) . Add ( type , ( obj , valueName , separator , map ) =>
183
+ {
184
+ mapAction ( ( TSettings ) obj , valueName , separator , map ) ;
185
+ } ) ;
186
+ }
187
+
188
+ private void MapCollectionRuleActionOptions_Settings ( string type , object ? settings , string valueName , string separator , IDictionary < string , string > map )
179
189
{
180
190
if ( null != settings )
181
191
{
192
+ // TODO: inline the well-known ones to avoid a dictionary lookup.
182
193
switch ( type )
183
194
{
184
195
case KnownCollectionRuleActions . CollectDump :
@@ -206,6 +217,7 @@ private static void MapCollectionRuleActionOptions_Settings(string type, object?
206
217
// MapExecuteOptions(settings as ExecuteOptions, valueName, separator, map);
207
218
// break;
208
219
// case KnownCollectionRuleActions.LoadProfiler:
220
+
209
221
// MapLoadProfilerOptions(settings as LoadProfilerOptions, valueName, separator, map);
210
222
// break;
211
223
// case KnownCollectionRuleActions.SetEnvironmentVariable:
@@ -215,7 +227,15 @@ private static void MapCollectionRuleActionOptions_Settings(string type, object?
215
227
// MapGetEnvironmentVariableOptions(settings as GetEnvironmentVariableOptions, valueName, separator, map);
216
228
// break;
217
229
default :
218
- throw new NotSupportedException ( $ "Unknown action type: { type } ") ;
230
+ if ( _actionSettingsMap ? . TryGetValue ( type , out Action < object , string , string , IDictionary < string , string > > ? mapAction ) == true )
231
+ {
232
+ mapAction ( settings , valueName , separator , map ) ;
233
+ }
234
+ else
235
+ {
236
+ throw new NotSupportedException ( $ "Unknown action type: { type } ") ;
237
+ }
238
+ break ;
219
239
}
220
240
}
221
241
}
0 commit comments