Skip to content

Commit

Permalink
feat(dpg): rename top parameter in pagination method (#2708)
Browse files Browse the repository at this point in the history
- rename `top` parameter to `maxCount` if the name is not occupied
- add test

part of Azure/azure-sdk-for-net#29342
  • Loading branch information
archerzz authored Sep 21, 2022
1 parent 806f06e commit e4c8eda
Show file tree
Hide file tree
Showing 11 changed files with 1,896 additions and 41 deletions.

Large diffs are not rendered by default.

63 changes: 54 additions & 9 deletions src/AutoRest.CSharp/LowLevel/Output/DpgOutputLibraryBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ namespace AutoRest.CSharp.Output.Models
{
internal class DpgOutputLibraryBuilder
{
private const string MaxCountParameterName = "maxCount";

private readonly InputNamespace _rootNamespace;
private readonly SourceInputModel? _sourceInputModel;
private readonly string _defaultNamespace;
Expand All @@ -36,7 +38,7 @@ public DpgOutputLibraryBuilder(InputNamespace rootNamespace, SourceInputModel? s

public DpgOutputLibrary Build(bool isCadlInput)
{
var inputClients = UpdateListMethodNames();
var inputClients = UpdateOperations();

var clientInfosByName = inputClients
.Select(og => CreateClientInfo(og, _sourceInputModel, _rootNamespace.Name))
Expand Down Expand Up @@ -84,17 +86,60 @@ private void CreateModels(IDictionary<InputModelType, ModelTypeProvider> models,
}
}

private IEnumerable<InputClient> UpdateListMethodNames()
private IEnumerable<InputClient> UpdateOperations()
{
var defaultName = _rootNamespace.Name.ReplaceLast("Client", "");
// this map of old/new InputOperation is to update the lazy initialization of `Paging.NextLinkOperation`
var operationsMap = new Dictionary<InputOperation, InputOperation>();
foreach (var client in _rootNamespace.Clients)
{
var clientName = client.Name.IsNullOrEmpty() ? defaultName : client.Name;
yield return client with { Operations = client.Operations.Select(op => UpdateMethodName(op, clientName)).ToList() };
yield return client with { Operations = client.Operations.Select(op => UpdateOperation(op, clientName, operationsMap)).ToList() };
}
}

private static InputOperation UpdateOperation(InputOperation operation, string clientName, IDictionary<InputOperation, InputOperation> operationsMap)
{
InputOperation updatedOperation;
if (operation.Paging != null && !operation.Parameters.Any(p => p.Name.Equals(MaxCountParameterName, StringComparison.OrdinalIgnoreCase)))
{
updatedOperation = operation with
{
Name = UpdateOperationName(operation, clientName),
Parameters = UpdateOperationParameters(operation.Parameters),
// to update the lazy initialization of `Paging.NextLinkOperation`
Paging = operation.Paging with { NextLinkOperationRef = operation.Paging.NextLinkOperation != null ? () => operationsMap[operation.Paging.NextLinkOperation] : null }
};
}
else
{
updatedOperation = operation with { Name = UpdateOperationName(operation, clientName) };
}
operationsMap.Add(operation, updatedOperation);

return updatedOperation;

}

private static string UpdateOperationName(InputOperation operation, string clientName)
=> operation.Name.RenameGetMethod(clientName).RenameListToGet(clientName);

private static IReadOnlyList<InputParameter> UpdateOperationParameters(IReadOnlyList<InputParameter> operationParameters)
{
var parameters = new List<InputParameter>(operationParameters.Count);
foreach (var parameter in operationParameters)
{
if (parameter.Name.Equals("top", StringComparison.OrdinalIgnoreCase))
{
parameters.Add(parameter with { Name = MaxCountParameterName });
}
else
{
parameters.Add(parameter);
}
}

static InputOperation UpdateMethodName(InputOperation operation, string clientName)
=> operation with { Name = operation.Name.RenameGetMethod(clientName).RenameListToGet(clientName) };
return parameters;
}

private ClientOptionsTypeProvider CreateClientOptions(IReadOnlyList<ClientInfo> topLevelClientInfos)
Expand Down Expand Up @@ -169,7 +214,7 @@ private IReadOnlyList<ClientInfo> SetHierarchy(IReadOnlyDictionary<string, Clien
}
}

return new[] {topLevelClientInfo};
return new[] { topLevelClientInfo };
}

private static void AssignParents(in ClientInfo clientInfo, IReadOnlyDictionary<string, ClientInfo> clientInfosByName, SourceInputModel sourceInputModel)
Expand Down Expand Up @@ -216,7 +261,7 @@ private static void SetRequestToClient(ClientInfo clientInfo, InputOperation ope
clientInfo = clientInfo.Parent;
}
break;
case >1:
case > 1:
var requestParameters = operation.Parameters.ToHashSet();
while (clientInfo.Parent != null && !clientInfo.ResourceParameters.IsSubsetOf(requestParameters))
{
Expand Down Expand Up @@ -267,9 +312,9 @@ private IEnumerable<LowLevelClient> CreateClients(IEnumerable<ClientInfo> client
SubClients = subClients
};

subClients.AddRange(CreateClients(clientInfo.Children, typeFactory, clientOptions, client));
subClients.AddRange(CreateClients(clientInfo.Children, typeFactory, clientOptions, client));

yield return client;
yield return client;
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/AutoRest.CSharp/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,10 @@
"commandName": "Project",
"commandLineArgs": "--standalone $(SolutionDir)\\test\\TestProjects\\Pagination\\Generated"
},
"PaginationParams-LowLevel": {
"commandName": "Project",
"commandLineArgs": "--standalone $(SolutionDir)\\test\\TestProjects\\PaginationParams-LowLevel\\Generated"
},
"paging": {
"commandName": "Project",
"commandLineArgs": "--standalone $(SolutionDir)\\test\\TestServerProjects\\paging\\Generated"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using NUnit.Framework;
using PaginationParams_LowLevel;

namespace AutoRest.TestServer.Tests
{
public class PaginationParameterOverwriteTests
{
[Test]
public void OverwriteTop()
{
Assert.AreEqual(new string[] { "maxCount", "skip", "maxpagesize", "context" }, GetMethodParameterNames("GetPaginationParamsAsync"));
}

[Test]
public void NoOverwrite()
{
Assert.AreEqual(new string[] { "limit", "offset", "maxpagesize", "context" }, GetMethodParameterNames("Get2sAsync"));
}

[Test]
public void OverwriteTopCaseIncensitive()
{
Assert.AreEqual(new string[] { "maxCount", "skip", "maxpagesize", "context" }, GetMethodParameterNames("Get3sAsync"));
}

[Test]
public void NoOverwriteDueToOccupiedName()
{
Assert.AreEqual(new string[] { "top", "skip", "maxcount", "context" }, GetMethodParameterNames("Get4sAsync"));
}

private static IReadOnlyList<string> GetMethodParameterNames(string methodName)
{
var clazz = typeof(PaginationParamsClient);
TypeAsserts.HasPublicInstanceMethod(clazz, methodName);
var method = clazz.GetMethod(methodName, BindingFlags.Instance | BindingFlags.Public);
return method.GetParameters().Select(p => p.Name).ToList();
}
}
}
Loading

0 comments on commit e4c8eda

Please sign in to comment.