Skip to content

Commit

Permalink
Generate client factory for arm (#912)
Browse files Browse the repository at this point in the history
* generate client factory for arm

* store option with copy

* add clone process

* update azcore version
  • Loading branch information
tadelesh authored Mar 4, 2023
1 parent 278a866 commit 6f0aa61
Show file tree
Hide file tree
Showing 29 changed files with 1,103 additions and 34 deletions.
91 changes: 91 additions & 0 deletions src/generator/clientFactory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { Session } from '@autorest/extension-base';
import { CodeModel, Parameter} from '@autorest/codemodel';
import { length, values } from '@azure-tools/linq';
import { contentPreamble, formatCommentAsBulletItem, formatParameterTypeName, sortParametersByRequired } from './helpers';
import { ImportManager } from './imports';


// Creates the content for all <operation>.go files
export async function generateClientFactory(session: Session<CodeModel>): Promise<string> {
const azureARM = <boolean>session.model.language.go!.azureARM;
let result = '';
// generate client factory only for ARM
if (azureARM && length(session.model.operationGroups) > 0) {
// the list of packages to import
const imports = new ImportManager();

// there should be at most one client level param: subscriptionID for ARM, any exception is always a wrong swagger definition that we should fix
const allClientParams = new Array<Parameter>();
for (const group of values(session.model.operationGroups)) {
if (group.language.go!.clientParams) {
const clientParams = <Array<Parameter>>group.language.go!.clientParams;
for (const clientParam of values(clientParams)) {
if (values(allClientParams).where(cp => cp.language.go!.name === clientParam.language.go!.name).any()) {
continue;
}
allClientParams.push(clientParam);
}
}
}
allClientParams.sort(sortParametersByRequired);

// add factory type
imports.add('github.com/Azure/azure-sdk-for-go/sdk/azcore');
result += '// ClientFactory is a client factory used to create any client in this module.\n';
result += '// Don\'t use this type directly, use NewClientFactory instead.\n';
result += 'type ClientFactory struct {\n';
for (const clientParam of values(allClientParams)) {
result += `\t${clientParam.language.go!.name} ${formatParameterTypeName(clientParam)}\n`;
}
result += '\tcredential azcore.TokenCredential\n';
result += '\toptions *arm.ClientOptions\n';
result += '}\n\n';

// add factory CTOR
imports.add('github.com/Azure/azure-sdk-for-go/sdk/azcore/arm');
result += '// NewClientFactory creates a new instance of ClientFactory with the specified values.\n';
result += '// The parameter values will be propagated to any client created from this factory.\n';
for (const clientParam of values(allClientParams)) {
result += `${formatCommentAsBulletItem(`${clientParam.language.go!.name} - ${clientParam.language.go!.description}`)}\n`;
}
result += `${formatCommentAsBulletItem('credential - used to authorize requests. Usually a credential from azidentity.')}\n`;
result += `${formatCommentAsBulletItem('options - pass nil to accept the default values.')}\n`;

result += `func NewClientFactory(${allClientParams.map(p => {return `${p.language.go!.name} ${formatParameterTypeName(p)}`;}).join(', ')}, credential azcore.TokenCredential, options *arm.ClientOptions) (*ClientFactory, error) {\n`;
result += '\t_, err := arm.NewClient("armcompute.ClientFactory", moduleVersion, credential, options)\n';
result += '\tif err != nil {\n';
result += '\t\treturn nil, err\n';
result += '\t}\n';
result += '\treturn &ClientFactory{\n';
for (const clientParam of values(allClientParams)) {
result += `\t\t${clientParam.language.go!.name}: \t${clientParam.language.go!.name},`;
}
result += '\t\tcredential: credential,\n';
result += '\t\toptions: options.Clone(),\n';
result += '\t}, nil\n';
result += '}\n\n';

// add new sub client method for all operation groups
for (const group of values(session.model.operationGroups)) {
result += `func (c *ClientFactory) ${group.language.go!.clientCtorName}() *${group.language.go!.clientName} {\n`;
const clientParams = <Array<Parameter>>group.language.go!.clientParams;
if (clientParams) {
clientParams.sort(sortParametersByRequired);
result += `\tsubClient, _ := ${group.language.go!.clientCtorName}(${clientParams.map(p => {return `c.${p.language.go!.name}`;}).join(', ')}, c.credential, c.options)\n`;
} else {
result += `\tsubClient, _ := ${group.language.go!.clientCtorName}(c.credential, c.options)\n`;
}

result += '\treturn subClient\n';
result += '}\n\n';
}

result = await contentPreamble(session) + imports.text() + result;
}
return result;
}
10 changes: 10 additions & 0 deletions src/generator/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { serialize } from '@azure-tools/codegen';
import { AutorestExtensionHost, startSession } from '@autorest/extension-base';
import { codeModelSchema, CodeModel } from '@autorest/codemodel';
import { values } from '@azure-tools/linq';
import { generateClientFactory } from './clientFactory';
import { generateOperations } from './operations';
import { generateModels } from './models';
import { generateResponses } from './responses';
Expand Down Expand Up @@ -54,6 +55,15 @@ export async function protocolGen(host: AutorestExtensionHost) {
});
}

const clientFactory = await generateClientFactory(session);
if (clientFactory.length > 0) {
host.writeFile({
filename: `${filePrefix}client_factory.go`,
content: clientFactory,
artifactType: 'source-file-go'
});
}

const constants = await generateConstants(session);
host.writeFile({
filename: `${filePrefix}constants.go`,
Expand Down
2 changes: 1 addition & 1 deletion src/generator/gomod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { lt, toSemver } from '@azure-tools/codegen';
export async function generateGoModFile(session: Session<CodeModel>, existingGoMod: string): Promise<string> {
// here we specify the minimum version of azcore as required by the code generator.
// the version can be overwritten by passing the --azcore-version switch during generation.
const version = await session.getValue('azcore-version', '1.3.0');
const version = await session.getValue('azcore-version', '1.4.0');
if (!version.match(/^\d+\.\d+\.\d+(?:-[a-zA-Z0-9_.-]+)?$/)) {
throw new Error(`azcore version ${version} must in the format major.minor.patch[-beta.N]`);
}
Expand Down
2 changes: 1 addition & 1 deletion test/acr/2021-07-01/azacr/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module azacr

go 1.18

require github.com/Azure/azure-sdk-for-go/sdk/azcore v1.3.1
require github.com/Azure/azure-sdk-for-go/sdk/azcore v1.4.0

require (
github.com/Azure/azure-sdk-for-go/sdk/internal v1.1.2 // indirect
Expand Down
4 changes: 2 additions & 2 deletions test/acr/2021-07-01/azacr/go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.3.1 h1:gVXuXcWd1i4C2Ruxe321aU+IKGaStvGB/S90PUPB/W8=
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.3.1/go.mod h1:DffdKW9RFqa5VgmsjUOsS7UE7eiA5iAvYUs63bhKQ0M=
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.4.0 h1:rTnT/Jrcm+figWlYz4Ixzt0SJVR2cMC8lvZcimipiEY=
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.4.0/go.mod h1:ON4tFdPTwRcgWEaVDrN3584Ef+b7GgSJaXxe5fW9t4M=
github.com/Azure/azure-sdk-for-go/sdk/internal v1.1.2 h1:+5VZ72z0Qan5Bog5C+ZkgSqUbeVUd9wgtHOrIKuc5b8=
github.com/Azure/azure-sdk-for-go/sdk/internal v1.1.2/go.mod h1:eWRD7oawr1Mu1sLCawqVc0CUiF43ia3qQMxLscsKQ9w=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
Expand Down
2 changes: 1 addition & 1 deletion test/compute/2019-12-01/armcompute/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module armcompute

go 1.18

require github.com/Azure/azure-sdk-for-go/sdk/azcore v1.3.1
require github.com/Azure/azure-sdk-for-go/sdk/azcore v1.4.0

require (
github.com/Azure/azure-sdk-for-go/sdk/internal v1.1.2 // indirect
Expand Down
4 changes: 2 additions & 2 deletions test/compute/2019-12-01/armcompute/go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.3.1 h1:gVXuXcWd1i4C2Ruxe321aU+IKGaStvGB/S90PUPB/W8=
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.3.1/go.mod h1:DffdKW9RFqa5VgmsjUOsS7UE7eiA5iAvYUs63bhKQ0M=
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.4.0 h1:rTnT/Jrcm+figWlYz4Ixzt0SJVR2cMC8lvZcimipiEY=
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.4.0/go.mod h1:ON4tFdPTwRcgWEaVDrN3584Ef+b7GgSJaXxe5fW9t4M=
github.com/Azure/azure-sdk-for-go/sdk/internal v1.1.2 h1:+5VZ72z0Qan5Bog5C+ZkgSqUbeVUd9wgtHOrIKuc5b8=
github.com/Azure/azure-sdk-for-go/sdk/internal v1.1.2/go.mod h1:eWRD7oawr1Mu1sLCawqVc0CUiF43ia3qQMxLscsKQ9w=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
Expand Down
190 changes: 190 additions & 0 deletions test/compute/2019-12-01/armcompute/zz_client_factory.go

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

2 changes: 1 addition & 1 deletion test/consumption/2019-10-01/armconsumption/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module armconsumption

go 1.18

require github.com/Azure/azure-sdk-for-go/sdk/azcore v1.3.1
require github.com/Azure/azure-sdk-for-go/sdk/azcore v1.4.0

require (
github.com/Azure/azure-sdk-for-go/sdk/internal v1.1.2 // indirect
Expand Down
4 changes: 2 additions & 2 deletions test/consumption/2019-10-01/armconsumption/go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.3.1 h1:gVXuXcWd1i4C2Ruxe321aU+IKGaStvGB/S90PUPB/W8=
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.3.1/go.mod h1:DffdKW9RFqa5VgmsjUOsS7UE7eiA5iAvYUs63bhKQ0M=
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.4.0 h1:rTnT/Jrcm+figWlYz4Ixzt0SJVR2cMC8lvZcimipiEY=
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.4.0/go.mod h1:ON4tFdPTwRcgWEaVDrN3584Ef+b7GgSJaXxe5fW9t4M=
github.com/Azure/azure-sdk-for-go/sdk/internal v1.1.2 h1:+5VZ72z0Qan5Bog5C+ZkgSqUbeVUd9wgtHOrIKuc5b8=
github.com/Azure/azure-sdk-for-go/sdk/internal v1.1.2/go.mod h1:eWRD7oawr1Mu1sLCawqVc0CUiF43ia3qQMxLscsKQ9w=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
Expand Down
Loading

0 comments on commit 6f0aa61

Please sign in to comment.