Skip to content

Commit

Permalink
Update ParameterGrouping to use a "Mapping" concept
Browse files Browse the repository at this point in the history
  • Loading branch information
matthchr committed Oct 20, 2015
1 parent 3eb59e3 commit e9d74c3
Show file tree
Hide file tree
Showing 37 changed files with 412 additions and 1,001 deletions.
64 changes: 53 additions & 11 deletions AutoRest/AutoRest.Core/ClientModel/Method.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public Method()
Parameters = new List<Parameter>();
RequestHeaders = new Dictionary<string, string>();
Responses = new Dictionary<HttpStatusCode, IType>();
ParameterExpansions = new Dictionary<string, Dictionary<Property, Parameter>>();
}

/// <summary>
Expand Down Expand Up @@ -58,24 +59,19 @@ public Method()
/// </summary>
public List<Parameter> Parameters { get; private set; }

/// <summary>
/// Gets the list of Parameter Expansions
/// </summary>
public Dictionary<string, Dictionary<Property, Parameter>> ParameterExpansions { get; private set; }

/// <summary>
/// Gets the parameter groups.
/// </summary>
public IEnumerable<string> ParameterGroups
{
get
{
List<string> parameterGroups = new List<string>();

foreach (Parameter parameter in this.Parameters)
{
if (!string.IsNullOrEmpty(parameter.ParameterGroup))
{
parameterGroups.Add(parameter.ParameterGroup);
}
}

return parameterGroups.Distinct();
return this.ParameterExpansions.Keys;
}
}

Expand Down Expand Up @@ -152,11 +148,57 @@ public object Clone()
newMethod.Parameters = new List<Parameter>();
newMethod.RequestHeaders = new Dictionary<string, string>();
newMethod.Responses = new Dictionary<HttpStatusCode, IType>();
newMethod.ParameterExpansions = new Dictionary<string, Dictionary<Property, Parameter>>();
this.Extensions.ForEach(e => newMethod.Extensions[e.Key] = e.Value);
this.Parameters.ForEach(p => newMethod.Parameters.Add((Parameter)p.Clone()));
this.ParameterExpansions.ForEach(p =>
{
newMethod.ParameterExpansions.Add(p.Key, new Dictionary<Property, Parameter>());
p.Value.ForEach(inner => newMethod.ParameterExpansions[p.Key].Add(inner.Key, inner.Value));
});
this.RequestHeaders.ForEach(r => newMethod.RequestHeaders[r.Key] = r.Value);
this.Responses.ForEach(r => newMethod.Responses[r.Key] = r.Value);
return newMethod;
}

/// <summary>
/// Adds a new grouped parameter.
/// </summary>
/// <param name="parameterGroupType">The composite type of the parameter group.</param>
/// <param name="parameterGroupProperty">The property on the parameter group which represents the grouped parameter.</param>
/// <param name="groupedParameter">The grouped parameter.</param>
public void AddGroupedParameter(string parameterGroupType, Property parameterGroupProperty, Parameter groupedParameter)
{
if (!this.ParameterExpansions.ContainsKey(parameterGroupType))
{
this.ParameterExpansions.Add(parameterGroupType, new Dictionary<Property, Parameter>());
}

Dictionary<Property, Parameter> propertyToParameterMapping = this.ParameterExpansions[parameterGroupType];
propertyToParameterMapping[parameterGroupProperty] = groupedParameter;
}

/// <summary>
/// Retrives a dictionary of Property -> Parameter mappings.
/// </summary>
/// <param name="parameterGroupType">The type of the parameter group.</param>
/// <returns>A dictionary of Property -> Parameter mappings.</returns>
public IReadOnlyDictionary<Property, Parameter> GetGroupedParameters(string parameterGroupType)
{
return this.ParameterExpansions[parameterGroupType];
}

/// <summary>
/// Updates a grouped parameter to have a new name.
/// </summary>
/// <param name="originalName">The original name of the grouped parameter.</param>
/// <param name="newName">The new name of the grouped parameter.</param>
public void UpdateGroupedParameterName(string originalName, string newName)
{
Dictionary<Property, Parameter> propertyToParameterMapping = this.ParameterExpansions[originalName];
this.ParameterExpansions.Remove(originalName);
this.ParameterExpansions.Add(newName, propertyToParameterMapping);
}

}
}
17 changes: 6 additions & 11 deletions AutoRest/AutoRest.Core/ClientModel/Parameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ public Parameter()
/// </summary>
public Property ClientProperty { get; set; }

/// <summary>
/// Gets or sets whether this parameter is a parameter group.
/// </summary>
public bool IsParameterGroup { get; set; }

/// <summary>
/// Gets or sets the constraints.
/// </summary>
Expand All @@ -70,17 +75,7 @@ public Parameter()
/// Gets or sets the documentation.
/// </summary>
public string Documentation { get; set; }

/// <summary>
/// Gets or sets the parameter group.
/// </summary>
public string ParameterGroup { get; set; }

/// <summary>
/// True if this parameter represents a parameter group, false otherwise
/// </summary>
public bool IsParameterGroup { get; set; }


/// <summary>
/// Gets vendor extensions dictionary.
/// </summary>
Expand Down
12 changes: 12 additions & 0 deletions AutoRest/AutoRest.Core/CodeNamer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,18 @@ public virtual void NormalizeClientModel(ServiceClient client)
parameter.Name = GetParameterName(parameter.Name);
parameter.Type = NormalizeType(parameter.Type);
}

List<string> parameterGroupList = method.ParameterGroups.ToList();
foreach (string parameterGroupName in parameterGroupList)
{
foreach (Parameter p in method.GetGroupedParameters(parameterGroupName).Values)
{
p.Name = this.GetParameterName(p.Name);
}

method.UpdateGroupedParameterName(parameterGroupName, GetTypeName(parameterGroupName));
}

}
}

Expand Down
39 changes: 26 additions & 13 deletions AutoRest/Generators/Azure.Common/Azure.Common/AzureCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,8 @@ public static void AddParameterGroups(ServiceClient serviceClient)
foreach (Method method in serviceClient.Methods)
{
//This group name is normalized by each languages code generator later, so it need not happen here.
Dictionary<string, List<Property>> properties = new Dictionary<string, List<Property>>();
Dictionary<string, Dictionary<Property, Parameter>> parameterGroups = new Dictionary<string, Dictionary<Property, Parameter>>();

foreach (Parameter parameter in method.Parameters)
{
if (parameter.Extensions.ContainsKey(ParameterGroupExtension))
Expand All @@ -231,14 +232,12 @@ public static void AddParameterGroups(ServiceClient serviceClient)
string parameterGroupName = method.Group + "-" + method.Name + "-" + "Parameters";
parameterGroupName = extensionObject.Value<string>("name") ?? parameterGroupName;

parameter.ParameterGroup = parameterGroupName;

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

properties[parameterGroupName].Add(new Property()
Property groupProperty = new Property()
{
IsReadOnly = false, //Since these properties are used as parameters they are never read only
Name = parameter.Name,
Expand All @@ -248,38 +247,52 @@ public static void AddParameterGroups(ServiceClient serviceClient)
Documentation = parameter.Documentation,
Type = parameter.Type,
SerializedName = null //Parameter is never serialized directly
});
};

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

foreach (string parameterGroupName in method.ParameterGroups)
foreach (string parameterGroupName in parameterGroups.Keys)
{
//Define the new parameter group type (it's always a composite type)
CompositeType parameterGroupType = new CompositeType()
{
Name = parameterGroupName,
Documentation = "Additional parameters for the " + method.Name + " operation."
};

foreach (Property property in properties[parameterGroupName])
//Populate the parameter group type with properties.
foreach (Property property in parameterGroups[parameterGroupName].Keys)
{
parameterGroupType.Properties.Add(property);
}

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

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

method.Parameters.Add(groupedParameter);
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];

method.AddGroupedParameter(parameterGroupType.Name, property, p);
method.Parameters.Remove(p);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,7 @@ public void ParameterGroupingTests()
//Valid required parameters
ParameterGroupingPostRequiredParameters requiredParameters = new ParameterGroupingPostRequiredParameters(bodyParameter, pathParameter)
{
Header = headerParameter,
CustomHeader = headerParameter,
Query = queryParameter
};

Expand All @@ -724,7 +724,7 @@ public void ParameterGroupingTests()
//Valid optional parameters
ParameterGroupingPostOptionalParameters optionalParameters = new ParameterGroupingPostOptionalParameters()
{
Header = headerParameter,
CustomHeader = headerParameter,
Query = queryParameter
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@ public partial class ParameterGroupingPostOptionalParameters
/// Initializes a new instance of the
/// ParameterGroupingPostOptionalParameters class.
/// </summary>
/// <param name='header'>
/// <param name='customHeader'>
/// </param>
/// <param name='query'>
/// Query parameter with default
/// </param>
public ParameterGroupingPostOptionalParameters(string header = default(string), int? query = default(int?))
public ParameterGroupingPostOptionalParameters(string customHeader = default(string), int? query = default(int?))
{
Header = header;
CustomHeader = customHeader;
Query = query;
}

/// <summary>
/// </summary>
public string Header {get;set;}
public string CustomHeader {get;set;}

/// <summary>
/// Query parameter with default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ public partial class ParameterGroupingPostRequiredParameters
/// </summary>
/// <param name='body'>
/// </param>
/// <param name='header'>
/// <param name='customHeader'>
/// </param>
/// <param name='query'>
/// Query parameter with default
/// </param>
/// <param name='path'>
/// Path parameter
/// </param>
public ParameterGroupingPostRequiredParameters(int? body, string path, string header = default(string), int? query = default(int?))
public ParameterGroupingPostRequiredParameters(int? body, string path, string customHeader = default(string), int? query = default(int?))
{
Body = body;
Header = header;
CustomHeader = customHeader;
Query = query;
Path = path;
}
Expand All @@ -43,7 +43,7 @@ public partial class ParameterGroupingPostRequiredParameters

/// <summary>
/// </summary>
public string Header {get;set;}
public string CustomHeader {get;set;}

/// <summary>
/// Query parameter with default
Expand Down
Loading

0 comments on commit e9d74c3

Please sign in to comment.