Skip to content
This repository has been archived by the owner on Jul 14, 2023. It is now read-only.

Commit

Permalink
Polymorphism multi layer split (#848)
Browse files Browse the repository at this point in the history
* hider update

* poly as resource

* polymorphism fix

* merger update for poly

* reserve work

* discriminator value

* comment out debug info

* for wsl path

* reserve work

* fix bugs

* remove duplicated autogen test scenario

* Revert "fix bugs"

This reverts commit fac6981.

* fix datafactory test

* fix managedNetwork

* check discriminator value in test

* add middle group in help

* fix action bug

* check schema for checking dup names

* remove resource prefix in param

* use clicommon 0.6.2

Co-authored-by: qiaozha <qiaozha@microsoft.com>
  • Loading branch information
changlong-liu and qiaozha authored Jun 1, 2021
1 parent 6475700 commit 114cc5e
Show file tree
Hide file tree
Showing 31 changed files with 311 additions and 213 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ debug-output-folder: $(az-output-folder)/_az_debug

use-extension:
"@autorest/python": "5.4.0"
"@autorest/clicommon": "0.6.1"
"@autorest/clicommon": "0.6.2"
#"@autorest/python": "latest"

require:
Expand Down
23 changes: 17 additions & 6 deletions src/aznamer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CodeModel, codeModelSchema, Language } from '@azure-tools/codemodel';
import { AnySchema, CodeModel, codeModelSchema, Language } from '@azure-tools/codemodel';
import { Session, startSession, Host, Channel } from '@autorest/extension-base';
import { serialize } from '@azure-tools/codegen';
import { values } from '@azure-tools/linq';
Expand All @@ -20,8 +20,12 @@ export class AzNamer {
let ons: Array<string> = [];
if (operationNameOri.indexOf('#') > -1) {
ons = operationNameOri.split('#');
if (ons && ons.length === 2) {
subOperationGroupName = changeCamelToDash(ons[1]);
if (ons && ons.length >= 2) {
const subgroups = [];
for (let i = 1; i < ons.length; i++) {
subgroups.push(changeCamelToDash(ons[i]));
}
subOperationGroupName = subgroups.join(' ');
operationName = ons[0].toLowerCase();
}
}
Expand Down Expand Up @@ -80,7 +84,7 @@ export class AzNamer {
return this.codeModel;
}

getAzName(obj) {
getAzName(obj, methodKey?) {
if (obj.language['az']) {
return;
}
Expand All @@ -90,6 +94,13 @@ export class AzNamer {
: obj.language.python.name;
obj.language['az'].name = changeCamelToDash(obj.language['az'].name);
obj.language['az'].mapsto = obj.language['az'].name.replace(/-/g, '_');
if (
!isNullOrUndefined(obj['nameBaseParam']) &&
!isNullOrUndefined(methodKey) &&
!isNullOrUndefined(obj['nameBaseParam'].subParams?.[methodKey])
) {
obj['nameBaseParam'].subParams[methodKey] = obj.language['az'].mapsto;
}
obj.language['az'].description = obj.language['cli']
? obj.language['cli'].description
: obj.language.python.description;
Expand Down Expand Up @@ -272,7 +283,7 @@ export class AzNamer {
}
operation.parameters.forEach((parameter) => {
if (!isNullOrUndefined(parameter.language['cli'])) {
this.getAzName(parameter);
this.getAzName(parameter, operation.language['cli'].name);
for (const k of ['alias', 'positionalKeys']) {
this.addAttributes(parameter, false, k);
this.addAttributes(parameter, true, k);
Expand All @@ -287,7 +298,7 @@ export class AzNamer {
if (request.parameters) {
request.parameters.forEach((parameter) => {
if (!isNullOrUndefined(parameter.language['cli'])) {
this.getAzName(parameter);
this.getAzName(parameter, operation.language['cli'].name);
for (const k of ['alias', 'positionalKeys']) {
this.addAttributes(parameter, false, k);
this.addAttributes(parameter, true, k);
Expand Down
47 changes: 31 additions & 16 deletions src/generate/codemodel/CodeModelAzImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,11 @@ export class CodeModelCliImpl implements CodeModelAz {
do {
if (
!paramRequired.has(
this.methodParameterHandler.MethodParameter_Name,
this.methodParameterHandler.MethodParameter_MapsTo,
)
) {
paramRequired.set(
this.methodParameterHandler.MethodParameter_Name,
this.methodParameterHandler.MethodParameter_MapsTo,
this.methodParameterHandler.MethodParameter_IsRequired
? 1
: 0,
Expand All @@ -207,9 +207,9 @@ export class CodeModelCliImpl implements CodeModelAz {
this.methodParameterHandler.MethodParameter_IsRequired
) {
paramRequired.set(
this.methodParameterHandler.MethodParameter_Name,
this.methodParameterHandler.MethodParameter_MapsTo,
paramRequired.get(
this.methodParameterHandler.MethodParameter_Name,
this.methodParameterHandler.MethodParameter_MapsTo,
) + 1,
);
}
Expand All @@ -221,11 +221,11 @@ export class CodeModelCliImpl implements CodeModelAz {
do {
if (
!paramRequired.has(
this.methodParameterHandler.MethodParameter_Name,
this.methodParameterHandler.MethodParameter_MapsTo,
)
) {
paramRequired.set(
this.methodParameterHandler.MethodParameter_Name,
this.methodParameterHandler.MethodParameter_MapsTo,
this.methodParameterHandler
.MethodParameter_IsRequired
? 1
Expand All @@ -235,10 +235,10 @@ export class CodeModelCliImpl implements CodeModelAz {
this.methodParameterHandler.MethodParameter_IsRequired
) {
paramRequired.set(
this.methodParameterHandler.MethodParameter_Name,
this.methodParameterHandler.MethodParameter_MapsTo,
paramRequired.get(
this.methodParameterHandler
.MethodParameter_Name,
.MethodParameter_MapsTo,
) + 1,
);
}
Expand Down Expand Up @@ -276,7 +276,7 @@ export class CodeModelCliImpl implements CodeModelAz {
'RequiredByMethod'
] =
paramRequired.get(
this.methodParameterHandler.MethodParameter_Name,
this.methodParameterHandler.MethodParameter_MapsTo,
) === paramTime;
}
if (
Expand Down Expand Up @@ -322,7 +322,7 @@ export class CodeModelCliImpl implements CodeModelAz {
] =
paramRequired.get(
this.methodParameterHandler
.MethodParameter_Name,
.MethodParameter_MapsTo,
) === paramTime;
}
if (
Expand Down Expand Up @@ -450,6 +450,7 @@ export class CodeModelCliImpl implements CodeModelAz {
this.allActions = new Map<Parameter, string>();
const nameActionReference: Map<string, ActionParam> = new Map<string, ActionParam>();
const pythonReserveWord = ['all', 'id', 'format', 'type', 'filter'];

if (this.SelectFirstCommandGroup()) {
do {
if (this.SelectFirstCommand()) {
Expand All @@ -460,6 +461,10 @@ export class CodeModelCliImpl implements CodeModelAz {
>();
if (this.SelectFirstMethod()) {
do {
const methodParamMaptos: Map<string, Schema> = new Map<
string,
Schema
>();
if (this.SelectFirstMethodParameter()) {
do {
const paramName = this.methodParameterHandler
Expand Down Expand Up @@ -512,12 +517,13 @@ export class CodeModelCliImpl implements CodeModelAz {
paramFlattenedName = paramName;
}
if (names.length > 1) {
let subgroup: string = names[0];
subgroup = subgroup.replace(/-/g, '_');
if (paramFlattenedName.startsWith(subgroup)) {
paramFlattenedName = paramFlattenedName.substr(
subgroup.length + 1,
);
for (let subgroup of names.slice(0, names.length - 1)) {
subgroup = subgroup.replace(/-/g, '_');
if (paramFlattenedName.startsWith(subgroup)) {
paramFlattenedName = paramFlattenedName.substr(
subgroup.length + 1,
);
}
}
}
if (
Expand Down Expand Up @@ -576,6 +582,7 @@ export class CodeModelCliImpl implements CodeModelAz {
param,
);
nameParamReference.set(paramName, param);
methodParamMaptos.set(paramName, param.schema);
} else {
// if the full flattenedName within one command is the same but has two different reference. there's no way to split them.
this.session.message({
Expand All @@ -594,6 +601,14 @@ export class CodeModelCliImpl implements CodeModelAz {
) {
paramFlattenedName += '_';
}
while (
methodParamMaptos.has(paramFlattenedName) &&
methodParamMaptos.get(paramFlattenedName).type !==
param.schema.type
) {
paramFlattenedName += '_';
}
methodParamMaptos.set(paramFlattenedName, param.schema);
this.parameterHandler.Parameter_SetAzNameMapsTo(
paramFlattenedName,
param,
Expand Down
65 changes: 48 additions & 17 deletions src/generate/codemodel/Example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,10 +292,21 @@ export class ExampleModelImpl implements ExampleModel {
public GetExampleParameters(exampleObj): ExampleParam[] {
const parameters: ExampleParam[] = [];
const methodParamList: MethodParam[] = this.GetMethodParametersList();
let flattenSucceed = true;
Object.entries(exampleObj.parameters).forEach(([paramName, paramValue]) => {
this.FlattenExampleParameter(methodParamList, parameters, paramName, paramValue, []);
if (
!this.FlattenExampleParameter(
methodParamList,
parameters,
paramName,
paramValue,
[],
)
) {
flattenSucceed = false;
}
});
return parameters;
return flattenSucceed ? parameters : undefined;
}

private isDiscriminator(param: any): boolean {
Expand Down Expand Up @@ -676,7 +687,7 @@ export class ExampleModelImpl implements ExampleModel {
name: string,
value: any,
ancestors: string[],
) {
): boolean {
for (const methodParam of this.matchMethodParam(methodParamList, name, ancestors)) {
let polySubParam: MethodParam = null;
let netValue =
Expand All @@ -691,15 +702,27 @@ export class ExampleModelImpl implements ExampleModel {
if (polySubParamObj.schema.extensions) {
const valueToMatch =
polySubParamObj.schema.extensions['x-ms-discriminator-value'];
if (netValue[keyToMatch] === valueToMatch) {
if (
isNullOrUndefined(netValue[keyToMatch]) ||
netValue[keyToMatch] === valueToMatch
) {
polySubParam = methodParam;
delete netValue[keyToMatch];
break;
} else {
return false;
}
}
}
}
} else if (
!isNullOrUndefined(methodParam.value.extensions?.['cli-discriminator-value'])
) {
if (value !== methodParam.value.extensions?.['cli-discriminator-value']) {
return false;
}
}

if (polySubParam) {
netValue = this.FlattenProperty(polySubParam.value?.schema, netValue);
rawValue = this.FlattenProperty(polySubParam.value?.schema, rawValue);
Expand All @@ -722,7 +745,7 @@ export class ExampleModelImpl implements ExampleModel {
ancestors,
rawValue,
);
return;
return true;
}
} else if (
'targetProperty' in methodParam.value &&
Expand All @@ -741,7 +764,7 @@ export class ExampleModelImpl implements ExampleModel {
ancestors,
rawValue,
);
return;
return true;
}
} else if (ancestors.length === 0) {
// exampleParam.set(name, value);
Expand All @@ -755,21 +778,26 @@ export class ExampleModelImpl implements ExampleModel {
rawValue,
)
)
return;
return true;
}
}

if (!isNullOrUndefined(value) && typeof value === 'object') {
for (const subName in value) {
this.FlattenExampleParameter(
methodParamList,
exampleParam,
subName,
value[subName],
ancestors.concat(name),
);
if (
!this.FlattenExampleParameter(
methodParamList,
exampleParam,
subName,
value[subName],
ancestors.concat(name),
)
) {
return false;
}
}
}
return true;
}

public ConvertToCliParameters(
Expand Down Expand Up @@ -831,7 +859,7 @@ export class ExampleModelImpl implements ExampleModel {
}

// check polymophism here
const originalOperation = this.methodHandler.Method_GetOriginalOperation;
const originalOperation = this.methodHandler.Method_GetOriginalOperation();
if (!isNullOrUndefined(originalOperation)) {
if (this.baseHandler.SelectFirstMethodParameter()) {
do {
Expand Down Expand Up @@ -917,6 +945,9 @@ export class ExampleModelImpl implements ExampleModel {
example.HttpMethod = this.methodHandler.Method_HttpMethod;
example.ResourceClassName = this.commandGroupHandler.CommandGroup_Key;
const params = this.GetExampleParameters(exampleObj);
if (isNullOrUndefined(params)) {
return undefined;
}
example.Parameters = this.ConvertToCliParameters(
params,
this.commandGroupHandler.CommandGroup_Key,
Expand All @@ -932,13 +963,13 @@ export class ExampleModelImpl implements ExampleModel {
this.commandHandler.Command_MethodName.toLowerCase() === 'update' &&
!forUpdate(this.baseHandler, id)
) {
return;
return undefined;
}
if (
this.commandHandler.Command_MethodName.toLowerCase() !== 'update' &&
forUpdate(this.baseHandler, id)
) {
return;
return undefined;
}
}
if (this.filterExampleByPoly(exampleObj, example)) {
Expand Down
Loading

0 comments on commit 114cc5e

Please sign in to comment.