Skip to content

Commit

Permalink
change mgmt. example generation to use ClientFactory (#939)
Browse files Browse the repository at this point in the history
* switch example to ClientFactory

* bump version and add changelog

* format code

* format
  • Loading branch information
tadelesh authored Mar 21, 2023
1 parent d2c9632 commit db51bb9
Show file tree
Hide file tree
Showing 103 changed files with 1,167 additions and 1,106 deletions.
12 changes: 12 additions & 0 deletions packages/autorest.gotest/CHANGELOG.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
{
"name": "@autorest/gotest",
"entries": [
{
"version": "4.6.0",
"tag": "@autorest/gotest_v4.6.0",
"date": "Mon, 13 Mar 2023 09:28:18 GMT",
"comments": {
"minor": [
{
"comment": "Change example generation to use `ClientFactory`."
}
]
}
},
{
"version": "4.5.2",
"tag": "@autorest/gotest_v4.5.2",
Expand Down
9 changes: 8 additions & 1 deletion packages/autorest.gotest/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
# Change Log - @autorest/gotest

This log was last generated on Mon, 30 Jan 2023 08:26:19 GMT and should not be manually modified.
This log was last generated on Mon, 13 Mar 2023 09:28:18 GMT and should not be manually modified.

## 4.6.0
Mon, 13 Mar 2023 09:28:18 GMT

### Minor changes

- Change example generation to use `ClientFactory`.

## 4.5.2
Mon, 30 Jan 2023 08:26:19 GMT
Expand Down
2 changes: 1 addition & 1 deletion packages/autorest.gotest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ testmodeler:
version: 3.8.2

use-extension:
'@autorest/go': '4.0.0-preview.45'
'@autorest/go': '4.0.0-preview.47'
'@autorest/testmodeler': '2.3.2'

pipeline:
Expand Down
4 changes: 2 additions & 2 deletions packages/autorest.gotest/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@autorest/gotest",
"version": "4.5.2",
"version": "4.6.0",
"description": "Autorest extension for go test generation",
"main": "dist/index.js",
"scripts": {
Expand Down Expand Up @@ -47,7 +47,7 @@
"dependencies": {
"@autorest/codemodel": "~4.18.2",
"@autorest/extension-base": "~3.4.4",
"@autorest/go": "~4.0.0-preview.45",
"@autorest/go": "~4.0.0-preview.47",
"@autorest/testmodeler": "~2.3.2",
"@azure-tools/codegen": "~2.9.1",
"@azure-tools/linq": "~3.1.0",
Expand Down
73 changes: 55 additions & 18 deletions packages/autorest.gotest/src/generator/exampleGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,40 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { BaseCodeGenerator } from './baseGenerator';
import { Config } from '../common/constant';
import { ObjectSchema, Parameter, SchemaType } from '@autorest/codemodel';
import { sortParametersByRequired } from '@autorest/go/dist/generator/helpers';
import { ExampleModel, MockTestDefinitionModel } from '@autorest/testmodeler/dist/src/core/model';
import { MockTestDataRender } from './mockTestGenerator';
import { camelCase, trimEnd } from 'lodash';
import { Config } from '../common/constant';
import { ParameterOutput } from '../common/model';
import { camelCase } from 'lodash';
import _ = require('lodash');
import { BaseCodeGenerator } from './baseGenerator';
import { MockTestDataRender } from './mockTestGenerator';

export class ExampleDataRender extends MockTestDataRender {}
export class ExampleDataRender extends MockTestDataRender {
public renderData(): void {
super.renderData();
const allClientParams = new Array<Parameter>();
for (const group of this.context.codeModel.operationGroups) {
if (group.language.go!.clientParams) {
const clientParams = <Array<Parameter>>group.language.go!.clientParams;
for (const clientParam of clientParams) {
if (allClientParams.filter((cp) => cp.language.go!.name === clientParam.language.go!.name).length > 0) {
continue;
}
allClientParams.push(clientParam);
}
}
}
allClientParams.sort(sortParametersByRequired);
const clientFactoryParametersOutput = new Array<ParameterOutput>();
for (const clientParam of allClientParams) {
const isPolymophismValue = clientParam?.schema?.type === SchemaType.Object && (<ObjectSchema>clientParam.schema).discriminator?.property.isDiscriminator === true;
const isPtr: boolean = isPolymophismValue || !(clientParam.required || clientParam.language.go.byValue === true);
clientFactoryParametersOutput.push(new ParameterOutput(this.getLanguageName(clientParam), this.getDefaultValue(clientParam, isPtr)));
}
this.context.codeModel.testModel.mockTest['clientFactoryParametersOutput'] = clientFactoryParametersOutput;
}
}

export class ExampleCodeGenerator extends BaseCodeGenerator {
public generateCode(extraParam: Record<string, unknown> = {}): void {
Expand All @@ -32,19 +57,23 @@ export class ExampleCodeGenerator extends BaseCodeGenerator {
fileName = fileName.substring(0, fileName.length - 6) + '_client';
}

this.renderAndWrite({ exampleGroups: exampleGroups, swaggerCommit: this.context.swaggerCommit }, 'exampleTest.go.njk', `${this.getFilePrefix(Config.exampleFilePrefix)}${fileName}_example_test.go`, extraParam, {
getParamsValue: (params: Array<ParameterOutput>) => {
return params
.map((p) => {
return p.paramOutput;
})
.join(', ');
this.renderAndWrite(
{
clientFactoryParametersOutput: this.context.codeModel.testModel.mockTest['clientFactoryParametersOutput'],
exampleGroups: exampleGroups,
swaggerCommit: this.context.swaggerCommit,
},
getExampleSuffix: (exampleKey: string) => {
return camelCase(exampleKey);
'exampleTest.go.njk',
`${this.getFilePrefix(Config.exampleFilePrefix)}${fileName}_example_test.go`,
extraParam,
{
getParamsValue: this.getParamsValue,
getExampleSuffix: (exampleKey: string) => {
return camelCase(exampleKey);
},
getCommentResponseOutput: this.getCommentResponseOutput,
},
getCommentResponseOutput: this.getCommentResponseOutput,
});
);
}
}

Expand All @@ -71,6 +100,14 @@ export class ExampleCodeGenerator extends BaseCodeGenerator {
indentNum++;
}
}
return _.trimEnd(result, '\n');
return trimEnd(result, '\n');
}

public getParamsValue(params: Array<ParameterOutput>) {
return params
.map((p) => {
return p.paramOutput;
})
.join(', ');
}
}
12 changes: 6 additions & 6 deletions packages/autorest.gotest/src/template/exampleTest.go.njk
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ func Example{{example.operationGroup.language.go.clientName}}_{{example.opName}}
log.Fatalf("failed to obtain a credential: %v", err)
}
ctx := context.Background()
client, err := {{packageName}}.{{example.operationGroup.language.go.clientCtorName}}({{jsFunc.getParamsValue(example.clientParametersOutput) + ", " if example.clientParametersOutput.length>0 else ""}}cred, nil)
if err != nil {
clientFactory, err := {{packageName}}.NewClientFactory({{jsFunc.getParamsValue(clientFactoryParametersOutput) + ", " if clientFactoryParametersOutput.length>0 else ""}}cred, nil)
if err != nil {
log.Fatalf("failed to create client: %v", err)
}
{%- if example.returnInfo.length == 2 %}
{%- if example.isLRO %}
poller, err := client.{{example.opName}}({{jsFunc.getParamsValue(example.methodParametersOutput)}})
poller, err := clientFactory.{{example.operationGroup.language.go.clientCtorName}}().{{example.opName}}({{jsFunc.getParamsValue(example.methodParametersOutput)}})
{%- else %}
{% if example.checkResponse %}res{% else %}_{% endif %}, err {% if example.checkResponse %}:{% endif %}= client.{{example.opName}}({{jsFunc.getParamsValue(example.methodParametersOutput)}})
{% if example.checkResponse %}res{% else %}_{% endif %}, err {% if example.checkResponse %}:{% endif %}= clientFactory.{{example.operationGroup.language.go.clientCtorName}}().{{example.opName}}({{jsFunc.getParamsValue(example.methodParametersOutput)}})
{%- endif %}
if err != nil {
log.Fatalf("failed to finish the request: %v", err)
Expand All @@ -46,7 +46,7 @@ func Example{{example.operationGroup.language.go.clientName}}_{{example.opName}}
}
{%- endif %}
{%- elif (example.returnInfo.length == 1) and (example.isPageable) %}
pager := client.{{example.opName}}({{jsFunc.getParamsValue(example.methodParametersOutput)}})
pager := clientFactory.{{example.operationGroup.language.go.clientCtorName}}().{{example.opName}}({{jsFunc.getParamsValue(example.methodParametersOutput)}})
for pager.More() {
page, err := pager.NextPage(ctx)
if err != nil {
Expand All @@ -60,7 +60,7 @@ func Example{{example.operationGroup.language.go.clientName}}_{{example.opName}}
// page{% if not example.responseIsDiscriminator %}.{{example.responseType}}{% endif %} = {{jsFunc.getCommentResponseOutput(example.responseOutput)}}
}
{%- else %}
client.{{example.opName}}({{jsFunc.getParamsValue(example.methodParametersOutput)}})
clientFactory.{{example.operationGroup.language.go.clientCtorName}}().{{example.opName}}({{jsFunc.getParamsValue(example.methodParametersOutput)}})
{%- endif %}
{%- if example.isLRO and example.isPageable and example.checkResponse %}
for res.More() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12435,7 +12435,6 @@ language:
name: AzureAgFoodPlatformRPService
description: ''
azureARM: true
exportClients: false
groupParameters: true
hasTimeRFC3339: true
headAsBoolean: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8913,6 +8913,9 @@ security:
- https://management.azure.com/.default
testModel:
mockTest:
clientFactoryParametersOutput:
- paramName: subscriptionID
paramOutput: '"<subscription-id>"'
exampleGroups:
- operationId: Extensions_Create
examples:
Expand Down Expand Up @@ -13874,7 +13877,6 @@ language:
name: AzureAgFoodPlatformRPService
description: ''
azureARM: true
exportClients: false
groupParameters: true
hasTimeRFC3339: true
headAsBoolean: true
Expand Down

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

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

Loading

0 comments on commit db51bb9

Please sign in to comment.