Skip to content

Commit

Permalink
Merge pull request Azure#1040 from matthchr/feature/param-group-comments
Browse files Browse the repository at this point in the history
Refactor parameter grouping and enable better comments
  • Loading branch information
tbombach committed May 16, 2016
2 parents f69f5ce + a0a620d commit 8124fb7
Show file tree
Hide file tree
Showing 36 changed files with 284 additions and 173 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ namespace Fixtures.Azure.AcceptanceTestsAzureParameterGrouping.Models
using Microsoft.Rest.Azure;

/// <summary>
/// Additional parameters for one or more operations
/// Additional parameters for a set of operations, such as:
/// parameterGrouping_postMultiParamGroups,
/// parameterGrouping_postSharedParameterGroupObject.
/// </summary>
public partial class FirstParameterGroup
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ namespace Fixtures.Azure.AcceptanceTestsAzureParameterGrouping.Models
using Microsoft.Rest.Azure;

/// <summary>
/// Additional parameters for one or more operations
/// Additional parameters for the parameterGrouping_postMultiParamGroups
/// operation.
/// </summary>
public partial class ParameterGroupingPostMultiParamGroupsSecondParamGroup
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Fixtures.Azure.AcceptanceTestsAzureParameterGrouping.Models
using Microsoft.Rest.Azure;

/// <summary>
/// Additional parameters for one or more operations
/// Additional parameters for the parameterGrouping_postOptional operation.
/// </summary>
public partial class ParameterGroupingPostOptionalParameters
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Fixtures.Azure.AcceptanceTestsAzureParameterGrouping.Models
using Microsoft.Rest.Azure;

/// <summary>
/// Additional parameters for one or more operations
/// Additional parameters for the parameterGrouping_postRequired operation.
/// </summary>
public partial class ParameterGroupingPostRequiredParameters
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Fixtures.Azure.AcceptanceTestsPaging.Models
using Microsoft.Rest.Azure;

/// <summary>
/// Additional parameters for one or more operations
/// Additional parameters for the Paging_getMultiplePages operation.
/// </summary>
public partial class PagingGetMultiplePagesOptions
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ namespace Fixtures.Azure.AcceptanceTestsPaging.Models
using Microsoft.Rest.Azure;

/// <summary>
/// Additional parameters for one or more operations
/// Additional parameters for the Paging_getMultiplePagesWithOffset
/// operation.
/// </summary>
public partial class PagingGetMultiplePagesWithOffsetOptions
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Fixtures.AcceptanceTestsModelFlattening.Models
using Microsoft.Rest.Serialization;

/// <summary>
/// Additional parameters for one or more operations
/// Additional parameters for the putSimpleProductWithGrouping operation.
/// </summary>
[JsonTransformation]
public partial class FlattenParameterGroup
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public static void NormalizeAzureClientModel(ServiceClient serviceClient, Settin
ParseODataExtension(serviceClient);
FlattenModels(serviceClient);
FlattenMethodParameters(serviceClient, settings);
AddParameterGroups(serviceClient);
ParameterGroupExtensionHelper.AddParameterGroups(serviceClient);
AddLongRunningOperations(serviceClient);
AddAzureProperties(serviceClient);
SetDefaultResponses(serviceClient);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<Compile Include="ClientModelHelpers.cs" />
<Compile Include="Extensions.cs" />
<Compile Include="GlobalSuppressions.cs" />
<Compile Include="ParameterGroupExtensionHelper.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen>
Expand Down
137 changes: 1 addition & 136 deletions AutoRest/Generators/Extensions/Extensions/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static void NormalizeClientModel(ServiceClient serviceClient, Settings se
{
FlattenModels(serviceClient);
FlattenMethodParameters(serviceClient, settings);
AddParameterGroups(serviceClient);
ParameterGroupExtensionHelper.AddParameterGroups(serviceClient);
ProcessParameterizedHost(serviceClient, settings);
}

Expand Down Expand Up @@ -333,141 +333,6 @@ public static void RemoveUnreferencedTypes(ServiceClient serviceClient, HashSet<
}
}

/// <summary>
/// Adds the parameter groups to operation parameters.
/// </summary>
/// <param name="serviceClient"></param>
public static void AddParameterGroups(ServiceClient serviceClient)
{
if (serviceClient == null)
{
throw new ArgumentNullException("serviceClient");
}

HashSet<CompositeType> generatedParameterGroups = new HashSet<CompositeType>();

foreach (Method method in serviceClient.Methods)
{
//Copy out flattening transformations as they should be the last
List<ParameterTransformation> flatteningTransformations = method.InputParameterTransformation.ToList();
method.InputParameterTransformation.Clear();

//This group name is normalized by each languages code generator later, so it need not happen here.
Dictionary<string, Dictionary<Property, Parameter>> parameterGroups = new Dictionary<string, Dictionary<Property, Parameter>>();

foreach (Parameter parameter in method.Parameters)
{
if (parameter.Extensions.ContainsKey(ParameterGroupExtension))
{
JContainer extensionObject = parameter.Extensions[ParameterGroupExtension] as JContainer;
if (extensionObject != null)
{
string specifiedGroupName = extensionObject.Value<string>("name");
string parameterGroupName;
if (specifiedGroupName == null)
{
string postfix = extensionObject.Value<string>("postfix") ?? "Parameters";
parameterGroupName = method.Group + "-" + method.Name + "-" + postfix;
}
else
{
parameterGroupName = specifiedGroupName;
}

if (!parameterGroups.ContainsKey(parameterGroupName))
{
parameterGroups.Add(parameterGroupName, new Dictionary<Property, Parameter>());
}

Property groupProperty = new Property()
{
IsReadOnly = false, //Since these properties are used as parameters they are never read only
Name = parameter.Name,
IsRequired = parameter.IsRequired,
DefaultValue = parameter.DefaultValue,
//Constraints = parameter.Constraints, Omit these since we don't want to perform parameter validation
Documentation = parameter.Documentation,
Type = parameter.Type,
SerializedName = null //Parameter is never serialized directly
};
// Copy over extensions
foreach (var key in parameter.Extensions.Keys)
{
groupProperty.Extensions[key] = parameter.Extensions[key];
}

parameterGroups[parameterGroupName].Add(groupProperty, parameter);
}
}
}

foreach (string parameterGroupName in parameterGroups.Keys)
{
CompositeType parameterGroupType =
generatedParameterGroups.FirstOrDefault(item => item.Name == parameterGroupName);
if (parameterGroupType == null)
{
parameterGroupType = new CompositeType
{
Name = parameterGroupName,
Documentation = "Additional parameters for one or more operations"
};
generatedParameterGroups.Add(parameterGroupType);

//Add to the service client
serviceClient.ModelTypes.Add(parameterGroupType);
}
foreach (Property property in parameterGroups[parameterGroupName].Keys)
{
Property matchingProperty = parameterGroupType.Properties.FirstOrDefault(
item => item.Name == property.Name &&
item.IsReadOnly == property.IsReadOnly &&
item.DefaultValue == property.DefaultValue &&
item.SerializedName == property.SerializedName);
if (matchingProperty == null)
{
parameterGroupType.Properties.Add(property);
}
}

bool isGroupParameterRequired = parameterGroupType.Properties.Any(p => p.IsRequired);

//Create the new parameter object based on the parameter group type
Parameter parameterGroup = new Parameter()
{
Name = parameterGroupName,
IsRequired = isGroupParameterRequired,
Location = ClientModel.ParameterLocation.None,
SerializedName = string.Empty,
Type = parameterGroupType,
Documentation = "Additional parameters for the operation"
};

method.Parameters.Add(parameterGroup);

//Link the grouped parameters to their parent, and remove them from the method parameters
foreach (Property property in parameterGroups[parameterGroupName].Keys)
{
Parameter p = parameterGroups[parameterGroupName][property];

var parameterTransformation = new ParameterTransformation
{
OutputParameter = p
};
parameterTransformation.ParameterMappings.Add(new ParameterMapping
{
InputParameter = parameterGroup,
InputParameterProperty = property.GetClientName()
});
method.InputParameterTransformation.Add(parameterTransformation);
method.Parameters.Remove(p);
}
}

// Copy back flattening transformations if any
flatteningTransformations.ForEach(t => method.InputParameterTransformation.Add(t));
}
}

/// <summary>
/// Flattens the request payload if the number of properties of the
Expand Down
Loading

0 comments on commit 8124fb7

Please sign in to comment.