Skip to content

Commit 9ccc14c

Browse files
authored
client and operationGroup (#2854)
* resolve dotnet build issue * upgrade emitter version * support client and operationGroup defined in cadl * emit client meta-data * remove unused code * update test * set endpoint parameter and filter out monitor operation for LRO * update test * resolve comments * resolve comment * resolve comment * resolve comment
1 parent bea938b commit 9ccc14c

File tree

141 files changed

+8266
-6152
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

141 files changed

+8266
-6152
lines changed

package-lock.json

+10-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/AutoRest.CSharp/Common/Input/CodeModelConverter.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public InputClient CreateClient(OperationGroup operationGroup)
5252
=> new(
5353
Name: operationGroup.Language.Default.Name,
5454
Description: operationGroup.Language.Default.Description,
55-
Operations: CreateOperations(operationGroup.Operations).Values.ToArray())
55+
Operations: CreateOperations(operationGroup.Operations).Values.ToArray(), true, Array.Empty<InputParameter>(), null)
5656
{
5757
Key = operationGroup.Key,
5858
};

src/AutoRest.CSharp/Common/Input/Inputs.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ internal record InputOAuth2Auth(IReadOnlyCollection<string> Scopes)
3131
public InputOAuth2Auth() : this(Array.Empty<string>()) {}
3232
}
3333

34-
internal record InputClient(string Name, string Description, IReadOnlyList<InputOperation> Operations)
34+
internal record InputClient(string Name, string Description, IReadOnlyList<InputOperation> Operations, bool Creatable, IReadOnlyList<InputParameter> Parameters, string? Parent)
3535
{
3636
private readonly string? _key;
3737

@@ -41,7 +41,7 @@ public string Key
4141
init => _key = value;
4242
}
4343

44-
public InputClient() : this(string.Empty, string.Empty, Array.Empty<InputOperation>()) { }
44+
public InputClient() : this(string.Empty, string.Empty, Array.Empty<InputOperation>(), true, Array.Empty<InputParameter>(), null) { }
4545
}
4646

4747
internal record InputOperation(

src/AutoRest.CSharp/LowLevel/Output/DpgOutputLibraryBuilder.cs

+44-5
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public DpgOutputLibrary Build(bool isCadlInput)
4343
var clientInfosByName = inputClients
4444
.Select(og => CreateClientInfo(og, _sourceInputModel, _rootNamespace.Name))
4545
.ToDictionary(ci => ci.Name);
46-
46+
AssignParentClients(inputClients, clientInfosByName);
4747
var topLevelClientInfos = SetHierarchy(clientInfosByName);
4848
var clientOptions = CreateClientOptions(topLevelClientInfos);
4949

@@ -223,7 +223,7 @@ private static ClientInfo CreateClientInfo(InputClient ns, SourceInputModel? sou
223223
var operations = ns.Operations;
224224
var clientParameters = RestClientBuilder.GetParametersFromOperations(operations).ToList();
225225
var resourceParameters = clientParameters.Where(cp => cp.IsResourceParameter).ToHashSet();
226-
var isSubClient = Configuration.SingleTopLevelClient && !string.IsNullOrEmpty(ns.Name) || resourceParameters.Any();
226+
var isSubClient = Configuration.SingleTopLevelClient && !string.IsNullOrEmpty(ns.Name) || resourceParameters.Any() || !string.IsNullOrEmpty(ns.Parent);
227227
var clientName = isSubClient ? clientNamePrefix : clientNamePrefix + ClientBuilder.GetClientSuffix();
228228

229229
INamedTypeSymbol? existingType;
@@ -276,6 +276,32 @@ private IReadOnlyList<ClientInfo> SetHierarchy(IReadOnlyDictionary<string, Clien
276276
return new[] { topLevelClientInfo };
277277
}
278278

279+
// assign parent according to the information in the input Model
280+
private static void AssignParentClients(in IEnumerable<InputClient> clients, IReadOnlyDictionary<string, ClientInfo> clientInfosByName)
281+
{
282+
foreach (var client in clients)
283+
{
284+
if (!String.IsNullOrEmpty(client.Parent))
285+
{
286+
ClientInfo? targetClient = null;
287+
ClientInfo? targetParent = null;
288+
foreach (var info in clientInfosByName.Values)
289+
{
290+
if (info.OperationGroupKey == client.Name)
291+
targetClient = info;
292+
if (info.OperationGroupKey == client.Parent)
293+
targetParent = info;
294+
}
295+
if (targetClient != null && targetParent != null)
296+
{
297+
targetClient.Parent = targetParent;
298+
targetParent.Children.Add(targetClient);
299+
}
300+
}
301+
}
302+
}
303+
304+
//Assgin parent according to the customized inputModel
279305
private static void AssignParents(in ClientInfo clientInfo, IReadOnlyDictionary<string, ClientInfo> clientInfosByName, SourceInputModel sourceInputModel)
280306
{
281307
var child = clientInfo;
@@ -385,7 +411,20 @@ private class ClientInfo
385411
public string Description { get; }
386412
public INamedTypeSymbol? ExistingType { get; }
387413
public IReadOnlyList<InputOperation> Operations { get; }
388-
public IReadOnlyList<InputParameter> ClientParameters { get; }
414+
415+
private IReadOnlyList<InputParameter>? _clientParameters;
416+
private IReadOnlyList<InputParameter> _initClientParameters;
417+
public IReadOnlyList<InputParameter> ClientParameters => _clientParameters ??= EnsureClientParameters();
418+
419+
private IReadOnlyList<InputParameter> EnsureClientParameters()
420+
{
421+
if (_initClientParameters.Count == 0)
422+
{
423+
var endpointParameter = this.Children.SelectMany(c => c.ClientParameters).FirstOrDefault(p => p.IsEndpoint);
424+
return endpointParameter != null ? new[] { endpointParameter } : Array.Empty<InputParameter>();
425+
}
426+
return _initClientParameters;
427+
}
389428
public ISet<InputParameter> ResourceParameters { get; }
390429

391430
public ClientInfo? Parent { get; set; }
@@ -405,7 +444,7 @@ public ClientInfo(string operationGroupKey, string clientName, string clientName
405444
Description = clientDescription;
406445
ExistingType = existingType;
407446
Operations = operations;
408-
ClientParameters = clientParameters;
447+
_initClientParameters = clientParameters;
409448
ResourceParameters = resourceParameters;
410449
Children = new List<ClientInfo>();
411450
Requests = new List<InputOperation>();
@@ -419,7 +458,7 @@ public ClientInfo(string clientName, string clientNamespace, IReadOnlyList<Input
419458
Description = string.Empty;
420459
ExistingType = null;
421460
Operations = Array.Empty<InputOperation>();
422-
ClientParameters = clientParameters;
461+
_initClientParameters = clientParameters;
423462
ResourceParameters = new HashSet<InputParameter>();
424463
Children = new List<ClientInfo>();
425464
Requests = new List<InputOperation>();

src/AutoRest.CSharp/Properties/launchSettings.json

+4
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,10 @@
180180
"commandName": "Project",
181181
"commandLineArgs": "--standalone $(SolutionDir)\\test\\CadlRanchProjects\\property-types\\Generated"
182182
},
183+
"ClientAndOperationGroup-Cadl": {
184+
"commandName": "Project",
185+
"commandLineArgs": "--standalone $(SolutionDir)\\test\\TestProjects\\ClientAndOperationGroup-Cadl\\Generated"
186+
},
183187
"CognitiveSearch": {
184188
"commandName": "Project",
185189
"commandLineArgs": "--standalone $(SolutionDir)\\samples\\CognitiveSearch\\Generated"

src/CADL.Extension/Emitter.Csharp/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@azure-tools/cadl-csharp",
3-
"version": "0.1.6",
3+
"version": "0.1.7-dev.1",
44
"author": "Microsoft Corporation",
55
"description": "Cadl library for emitting csharp model from the Cadl REST protocol binding",
66
"homepage": "https://github.com/Microsoft/cadl",
@@ -50,7 +50,7 @@
5050
"@autorest/csharp": "3.0.0-beta.20221025.3",
5151
"@azure-tools/cadl-autorest": "0.21.0",
5252
"@azure-tools/cadl-azure-core": "0.8.0",
53-
"@azure-tools/cadl-dpg": "0.1.0",
53+
"@azure-tools/cadl-dpg": "0.2.0",
5454
"@cadl-lang/compiler": "0.36.1",
5555
"@cadl-lang/eslint-config-cadl": "0.4.1",
5656
"@cadl-lang/eslint-plugin": "0.1.1",

0 commit comments

Comments
 (0)