Skip to content

Commit 76c6608

Browse files
committed
Implemented reserved parameter and alias auto-resolution.
1 parent 40f3fff commit 76c6608

File tree

3 files changed

+131
-3
lines changed

3 files changed

+131
-3
lines changed

generator/AWSPSGeneratorLib/Analysis/OperationAnalyzer.cs

Lines changed: 97 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,13 @@ public HashSet<string> GetAllParameterAliases(SimplePropertyInfo property)
9696
var globalAliases = CurrentModel.CustomParameters[property.AnalyzedName].Aliases;
9797
foreach (var a in globalAliases)
9898
{
99-
aliases.Add(a);
99+
var aliasToUse = ResolveAliasWithReservedParameterName(a);
100+
101+
// If new alias is not equivalent to CmdLet parameter name that would be used, then add it to aliases list.
102+
if (aliasToUse != property.CmdletParameterName)
103+
{
104+
aliases.Add(aliasToUse);
105+
}
100106
}
101107
}
102108

@@ -109,7 +115,13 @@ public HashSet<string> GetAllParameterAliases(SimplePropertyInfo property)
109115
var propAliases = property.Customization.Aliases;
110116
foreach (var a in propAliases)
111117
{
112-
aliases.Add(a);
118+
var aliasToUse = ResolveAliasWithReservedParameterName(a);
119+
120+
// If new alias is not equivalent to CmdLet parameter name that would be used, then add it to aliases list.
121+
if (aliasToUse != property.CmdletParameterName)
122+
{
123+
aliases.Add(aliasToUse);
124+
}
113125
}
114126

115127
if (property.Customization.AutoApplyAlias && property.CmdletParameterName != property.AnalyzedName)
@@ -124,7 +136,15 @@ public HashSet<string> GetAllParameterAliases(SimplePropertyInfo property)
124136
{
125137
var iterAlias = autoIteration.GetIterationParameterAlias(property.AnalyzedName);
126138
if (!string.IsNullOrEmpty(iterAlias))
127-
aliases.Add(iterAlias);
139+
{
140+
var aliasToUse = ResolveAliasWithReservedParameterName(iterAlias);
141+
142+
// If new alias is not equivalent to CmdLet parameter name that would be used, then add it to aliases list.
143+
if (aliasToUse != property.CmdletParameterName)
144+
{
145+
aliases.Add(aliasToUse);
146+
}
147+
}
128148
}
129149

130150
return aliases;
@@ -2407,6 +2427,8 @@ private void FinalizeParameterNames()
24072427
}
24082428
}
24092429

2430+
// In auto-rename, we attempt to shorten the name. In case there is a new service where there are similar end properties
2431+
// under different paths (e.g. EmailSettings_EmailMessage for Amplify Backend), then this could generate duplicate parameters.
24102432
if (attemptAutoRename)
24112433
{
24122434
// inspect the name to determine if it can be reduced in length if we flattened a deep
@@ -2434,9 +2456,81 @@ private void FinalizeParameterNames()
24342456
RecordParameterRename(property, alternateName);
24352457
}
24362458
}
2459+
2460+
// Attempt to automatically resolve reserved parameter name conflicts.
2461+
ResolveReservedParameterName(property);
24372462
}
24382463
}
24392464

2465+
/// <summary>
2466+
/// Automatically resolves reserved parameter name conflict.
2467+
/// </summary>
2468+
/// <param name="property">Property representing parameter.</param>
2469+
/// <returns>Flag indicating if resolution was done.</returns>
2470+
private bool ResolveReservedParameterName(SimplePropertyInfo property)
2471+
{
2472+
string parameterNameToResolve = property.Customization?.NewName ?? property.AnalyzedName;
2473+
2474+
// Parameter Name conflicts with one of the reserved parameter names. We want to use reserved parameter name from existing list for proper casing.
2475+
string reservedParameterName = ReservedParameterNames.FirstOrDefault(r => r.Equals(parameterNameToResolve, StringComparison.OrdinalIgnoreCase));
2476+
if (reservedParameterName != null)
2477+
{
2478+
// We want to use reserved parameter name from existing list for proper casing.
2479+
if (AllModels.ReservedParameterNameMappings.TryGetValue(reservedParameterName, out var newResolvedName))
2480+
{
2481+
newResolvedName = newResolvedName.Replace("{Noun}", CurrentOperation.SelectedNoun).Replace("{Verb}", CurrentOperation.SelectedVerb);
2482+
2483+
var customization = property.Customization;
2484+
if (customization == null)
2485+
{
2486+
customization = new Param
2487+
{
2488+
Origin = Param.CustomizationOrigin.DuringGeneration,
2489+
Name = property.AnalyzedName
2490+
};
2491+
property.Customization = customization;
2492+
}
2493+
2494+
property.Customization.NewName = newResolvedName;
2495+
2496+
// We should not auto apply alias since it would apply an alias of the original name which we have already transformed.
2497+
property.Customization.AutoApplyAlias = false;
2498+
2499+
// We should override this even if there was NewName specified in customization.
2500+
property.Customization.Origin = Param.CustomizationOrigin.DuringGeneration;
2501+
2502+
return true;
2503+
}
2504+
}
2505+
2506+
return false;
2507+
}
2508+
2509+
/// <summary>
2510+
/// Automatically resolves alias reserved parameter name conflict.
2511+
/// </summary>
2512+
/// <param name="alias">Parameter alias.</param>
2513+
/// <returns>Resolved alias.</returns>
2514+
private string ResolveAliasWithReservedParameterName(string alias)
2515+
{
2516+
string newAlias = alias;
2517+
2518+
if (!string.IsNullOrEmpty(alias))
2519+
{
2520+
// Alias conflicts with one of the reserved parameter names. We want to use reserved parameter name from existing list for proper casing.
2521+
string reservedParameterName = ReservedParameterNames.FirstOrDefault(r => r.Equals(alias, StringComparison.OrdinalIgnoreCase));
2522+
if (reservedParameterName != null)
2523+
{
2524+
// We want to use reserved parameter name from existing list for proper casing.
2525+
if (AllModels.ReservedParameterNameMappings.TryGetValue(reservedParameterName, out var newResolvedName))
2526+
{
2527+
newAlias = newResolvedName.Replace("{Noun}", CurrentOperation.SelectedNoun).Replace("{Verb}", CurrentOperation.SelectedVerb);
2528+
}
2529+
}
2530+
}
2531+
2532+
return newAlias;
2533+
}
24402534

24412535
/// <summary>
24422536
/// Register fields derived from the SDK's ConstantClass 'enum' type

generator/AWSPSGeneratorLib/Config/Configs.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,20 @@
1313
<Map From="ListTagsForResource" To="GetResourceTag" />
1414
<Map From="GetAccessPolicies" To="GetAccessPolicyCollection" />
1515
</OperationNameMappings>
16+
17+
<ReservedParameterNameMappings>
18+
<Map From="Credential" To="{Noun}Credential" />
19+
<Map From="AccessKey" To="{Noun}AccessKey" />
20+
<Map From="SecretKey" To="{Noun}SecretKey" />
21+
<Map From="SecretAccessKey" To="{Noun}SecretAccessKey" />
22+
<Map From="SessionToken" To="{Noun}SessionToken" />
23+
<Map From="Region" To="{Noun}Region" />
24+
<Map From="Select" To="{Noun}Select" />
25+
<Map From="Force" To="Force{Verb}" />
26+
<Map From="Verbose" To="VerboseOutput" />
27+
<Map From="ProfileName" To="Profile" />
28+
</ReservedParameterNameMappings>
29+
1630
<!-- Verb transformation patterns for special operation naming patterns -->
1731
<VerbNounTransformationPatterns>
1832
<Pattern Verb="Advertise" TargetVerb="Start" NounTransformation="{Noun}Advertisement" />

generator/AWSPSGeneratorLib/ConfigModel/ConfigModel.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,26 @@ public Dictionary<string, string> OperationNameMappings
156156
}
157157
}
158158

159+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
160+
[XmlArray("ReservedParameterNameMappings")]
161+
public List<Map> ReservedParameterNameMappingsList = new List<Map>();
162+
163+
Dictionary<string, string> _reservedParameterNameMappings;
164+
/// <summary>
165+
/// Reserved parameter name remaps
166+
/// </summary>
167+
[XmlIgnore]
168+
public Dictionary<string, string> ReservedParameterNameMappings
169+
{
170+
get
171+
{
172+
if (_reservedParameterNameMappings == null)
173+
_reservedParameterNameMappings = ReservedParameterNameMappingsList.ToDictionary(m => m.From, m => m.To);
174+
175+
return _reservedParameterNameMappings;
176+
}
177+
}
178+
159179
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
160180
[XmlArray("NounMappings")]
161181
public List<Map> NounMappingsList = new List<Map>();

0 commit comments

Comments
 (0)