Skip to content

Commit

Permalink
select convenience methods by extension decorator (#2666)
Browse files Browse the repository at this point in the history
* select convenience method by extension decorator

* Add PATCH handling

* Fix tests

* support Number

* resolve conflict

Co-authored-by: Alexander Sher <[email protected]>
  • Loading branch information
chunyu3 and AlexanderSher authored Sep 2, 2022
1 parent 847106f commit 38b656d
Show file tree
Hide file tree
Showing 34 changed files with 530 additions and 354 deletions.
57 changes: 39 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"devDependencies": {
"@cadl-lang/compiler": "0.34.0",
"@cadl-lang/rest": "0.16.0",
"@cadl-lang/openapi": "0.11.0",
"@azure-tools/cadl-csharp": "0.1.1",
"@microsoft.azure/autorest.testserver": "3.3.24",
"autorest": "3.6.1"
Expand Down
3 changes: 2 additions & 1 deletion src/AutoRest.CSharp/Common/Input/CodeModelConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ private InputOperation CreateOperation(ServiceRequest serviceRequest, Operation
RequestMediaTypes: operation.RequestMediaTypes?.Keys.ToList(),
BufferResponse: operation.Extensions?.BufferResponse ?? true,
LongRunning: CreateLongRunning(operation),
Paging: CreateOperationPaging(operation));
Paging: CreateOperationPaging(operation),
GenerateConvenienceMethod: false);

_inputOperationToOperationMap[inputOperation] = operation;
return inputOperation;
Expand Down
6 changes: 4 additions & 2 deletions src/AutoRest.CSharp/Common/Input/Inputs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ internal record InputOperation(
IReadOnlyList<string>? RequestMediaTypes,
bool BufferResponse,
OperationLongRunning? LongRunning,
OperationPaging? Paging)
OperationPaging? Paging,
bool GenerateConvenienceMethod)
{
public InputOperation() : this(
Name: string.Empty,
Expand All @@ -74,7 +75,8 @@ public InputOperation() : this(
RequestMediaTypes: Array.Empty<string>(),
BufferResponse: false,
LongRunning: null,
Paging: null)
Paging: null,
GenerateConvenienceMethod: false)
{ }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ private InputOperation CreateInputOperation(Operation operation)
RequestMediaTypes: operation.RequestMediaTypes?.Keys.ToList(),
BufferResponse: operation.Extensions?.BufferResponse ?? true,
LongRunning: null,
Paging: CreateOperationPaging(operation));
Paging: CreateOperationPaging(operation),
false);
}
return new InputOperation();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ private IEnumerable<LowLevelClientMethod> GetProtocolMethods(InputClient inputCl
.Select(m => m.Operation)
.Where(operation => IsProtocolMethodExists(operation, inputClient, context));

return LowLevelClient.BuildMethods(_context.TypeFactory, operations, Fields, GetClientName(inputClient, context), false);
return LowLevelClient.BuildMethods(_context.TypeFactory, operations, Fields, GetClientName(inputClient, context));
}

private static string GetClientName(InputClient inputClient, BuildContext<DataPlaneOutputLibrary> context)
Expand Down
13 changes: 6 additions & 7 deletions src/AutoRest.CSharp/LowLevel/Output/DpgOutputLibraryBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public DpgOutputLibrary Build(bool isCadlInput)
CreateEnums(enums, library.TypeFactory);
CreateModels(models, library.TypeFactory);
}
CreateClients(clients, topLevelClientInfos, library.TypeFactory, clientOptions, isCadlInput);
CreateClients(clients, topLevelClientInfos, library.TypeFactory, clientOptions);

return library;
}
Expand Down Expand Up @@ -229,9 +229,9 @@ private static void SetRequestToClient(ClientInfo clientInfo, InputOperation ope
clientInfo.Requests.Add(operation);
}

private void CreateClients(List<LowLevelClient> allClients, IEnumerable<ClientInfo> topLevelClientInfos, TypeFactory typeFactory, ClientOptionsTypeProvider clientOptions, bool isCadlInput)
private void CreateClients(List<LowLevelClient> allClients, IEnumerable<ClientInfo> topLevelClientInfos, TypeFactory typeFactory, ClientOptionsTypeProvider clientOptions)
{
var topLevelClients = CreateClients(topLevelClientInfos, typeFactory, clientOptions, null, isCadlInput);
var topLevelClients = CreateClients(topLevelClientInfos, typeFactory, clientOptions, null);

// Simple implementation of breadth first traversal
allClients.AddRange(topLevelClients);
Expand All @@ -241,7 +241,7 @@ private void CreateClients(List<LowLevelClient> allClients, IEnumerable<ClientIn
}
}

private IEnumerable<LowLevelClient> CreateClients(IEnumerable<ClientInfo> clientInfos, TypeFactory typeFactory, ClientOptionsTypeProvider clientOptions, LowLevelClient? parentClient, bool isCadlInput)
private IEnumerable<LowLevelClient> CreateClients(IEnumerable<ClientInfo> clientInfos, TypeFactory typeFactory, ClientOptionsTypeProvider clientOptions, LowLevelClient? parentClient)
{
foreach (var clientInfo in clientInfos)
{
Expand All @@ -262,13 +262,12 @@ private IEnumerable<LowLevelClient> CreateClients(IEnumerable<ClientInfo> client
_rootNamespace.Auth,
_sourceInputModel,
clientOptions,
typeFactory,
isCadlInput)
typeFactory)
{
SubClients = subClients
};

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

yield return client;
}
Expand Down
8 changes: 4 additions & 4 deletions src/AutoRest.CSharp/LowLevel/Output/LowLevelClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ internal class LowLevelClient : TypeProvider
public bool IsSubClient { get; }
public bool IsResourceClient { get; }

public LowLevelClient(string name, string ns, string description, string libraryName, LowLevelClient? parentClient, IEnumerable<InputOperation> operations, IEnumerable<InputParameter> clientParameters, InputAuth authorization, SourceInputModel? sourceInputModel, ClientOptionsTypeProvider clientOptions, TypeFactory typeFactory, bool isCadlInput)
public LowLevelClient(string name, string ns, string description, string libraryName, LowLevelClient? parentClient, IEnumerable<InputOperation> operations, IEnumerable<InputParameter> clientParameters, InputAuth authorization, SourceInputModel? sourceInputModel, ClientOptionsTypeProvider clientOptions, TypeFactory typeFactory)
: base(ns, sourceInputModel)
{
DefaultName = name;
Expand All @@ -63,7 +63,7 @@ public LowLevelClient(string name, string ns, string description, string library

(PrimaryConstructors, SecondaryConstructors) = BuildPublicConstructors(Parameters);

var clientMethods = BuildMethods(typeFactory, operations, Fields, Declaration.Name, isCadlInput).ToArray();
var clientMethods = BuildMethods(typeFactory, operations, Fields, Declaration.Name).ToArray();

ClientMethods = clientMethods
.OrderBy(m => m.LongRunning != null ? 2 : m.PagingInfo != null ? 1 : 0) // Temporary sorting to minimize amount of changed files. Will be removed when new LRO is implemented
Expand All @@ -79,9 +79,9 @@ public LowLevelClient(string name, string ns, string description, string library
SubClients = Array.Empty<LowLevelClient>();
}

public static IEnumerable<LowLevelClientMethod> BuildMethods(TypeFactory typeFactory, IEnumerable<InputOperation> operations, ClientFields fields, string clientName, bool isCadlInput)
public static IEnumerable<LowLevelClientMethod> BuildMethods(TypeFactory typeFactory, IEnumerable<InputOperation> operations, ClientFields fields, string clientName)
{
var builders = operations.ToDictionary(o => o, o => new OperationMethodChainBuilder(o, clientName, fields, typeFactory, isCadlInput));
var builders = operations.ToDictionary(o => o, o => new OperationMethodChainBuilder(o, clientName, fields, typeFactory));
foreach (var (_, builder) in builders)
{
builder.BuildNextPageMethod(builders);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ internal class OperationMethodChainBuilder
["If-Unmodified-Since"] = RequestConditionHeaders.IfUnmodifiedSince
};

private readonly bool _isCadlInput;
private readonly string _clientName;
private readonly ClientFields _fields;
private readonly TypeFactory _typeFactory;
Expand All @@ -45,12 +44,11 @@ internal class OperationMethodChainBuilder

public InputOperation Operation { get; }

public OperationMethodChainBuilder(InputOperation operation, string clientName, ClientFields fields, TypeFactory typeFactory, bool isCadlInput)
public OperationMethodChainBuilder(InputOperation operation, string clientName, ClientFields fields, TypeFactory typeFactory)
{
_clientName = clientName;
_fields = fields;
_typeFactory = typeFactory;
_isCadlInput = isCadlInput;
_orderedParameters = new List<ParameterChain>();
_requestParts = new List<RequestPartSource>();

Expand Down Expand Up @@ -84,7 +82,7 @@ public LowLevelClientMethod BuildOperationMethodChain()
var returnTypeChain = BuildReturnTypes();
var protocolMethodParameters = _orderedParameters.Select(p => p.Protocol).WhereNotNull().ToArray();
var protocolMethodSignature = new MethodSignature(_restClientMethod.Name, _restClientMethod.Summary, _restClientMethod.Description, _restClientMethod.Accessibility | Virtual, returnTypeChain.Protocol, null, protocolMethodParameters);
var convenienceMethod = _isCadlInput ? BuildConvenienceMethod(returnTypeChain) : null;
var convenienceMethod = Operation.GenerateConvenienceMethod ? BuildConvenienceMethod(returnTypeChain) : null;

var diagnostic = new Diagnostic($"{_clientName}.{_restClientMethod.Name}");

Expand Down
Loading

0 comments on commit 38b656d

Please sign in to comment.