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

Commit

Permalink
[Custom] Optimize custom function (#872)
Browse files Browse the repository at this point in the history
* support to remove empty property

* update test custom file

* add default value for argument which is not in parameter

* update tests
  • Loading branch information
kairu-ms authored Aug 9, 2021
1 parent 2c670a3 commit 2352caf
Show file tree
Hide file tree
Showing 15 changed files with 2,456 additions and 1,212 deletions.
167 changes: 115 additions & 52 deletions src/generate/renders/generated/CliCustom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ function ConstructMethodBodyParameter(model: CodeModelAz, needGeneric = false, r
if (model.SelectFirstMethodParameter(true)) {
const originalParameterStack: Parameter[] = [];
const originalParameterNameStack: string[] = [];
const originalParameterCheckEmpty: boolean[] = [];
const prefixIndent = ' ';

let skip = false;
Expand Down Expand Up @@ -146,6 +147,13 @@ function ConstructMethodBodyParameter(model: CodeModelAz, needGeneric = false, r
methodParameterHandler.MethodParameter.language?.['cli']?.cliFlattenTrace
?.length < originalParameterStack.length
) {
if (originalParameterCheckEmpty.pop()) {
const checks = ConstructEmptyCheck(
prefixIndent,
originalParameterNameStack,
);
outputBody = outputBody.concat(checks);
}
originalParameterStack.pop();
originalParameterNameStack.pop();
}
Expand All @@ -165,6 +173,13 @@ function ConstructMethodBodyParameter(model: CodeModelAz, needGeneric = false, r
methodHandler.Method.extensions?.['cli-split-operation-original-operation'],
)
) {
if (originalParameterCheckEmpty.pop()) {
const checks = ConstructEmptyCheck(
prefixIndent,
originalParameterNameStack,
);
outputBody = outputBody.concat(checks);
}
originalParameterStack.pop();
originalParameterNameStack.pop();
}
Expand All @@ -173,14 +188,15 @@ function ConstructMethodBodyParameter(model: CodeModelAz, needGeneric = false, r
allParams.indexOf(methodParameterHandler.MethodParameter.schema) === -1
) {
allParams.push(methodParameterHandler.MethodParameter.schema);
originalParameterStack.push(methodParameterHandler.MethodParameter);
originalParameterNameStack.push(methodParameterHandler.MethodParameter_Name);
} else if (originalParameterStack.length > 0) {
originalParameterStack.push(methodParameterHandler.MethodParameter);
originalParameterNameStack.push(methodParameterHandler.MethodParameter_Name);
} else {
} else if (originalParameterStack.length <= 0) {
continue;
}
originalParameterStack.push(methodParameterHandler.MethodParameter);
originalParameterNameStack.push(methodParameterHandler.MethodParameter_Name);
originalParameterCheckEmpty.push(
!needGeneric &&
!parameterHandler.Parameter_IsRequired(originalParameterStack.last),
);
if (!needGeneric) {
outputBody = outputBody.concat(
ConstructValuation(
Expand Down Expand Up @@ -229,6 +245,13 @@ function ConstructMethodBodyParameter(model: CodeModelAz, needGeneric = false, r
originalParameterStack.last.language?.['cli']?.cliM4Path,
) === -1
) {
if (originalParameterCheckEmpty.pop()) {
const checks = ConstructEmptyCheck(
prefixIndent,
originalParameterNameStack,
);
outputBody = outputBody.concat(checks);
}
originalParameterStack.pop();
originalParameterNameStack.pop();
}
Expand All @@ -244,8 +267,15 @@ function ConstructMethodBodyParameter(model: CodeModelAz, needGeneric = false, r
flattenedFrom,
);
newParam.language['cli'] = flattenedFrom.language['cli'];

originalParameterStack.push(newParam);
originalParameterNameStack.push(flattenedFrom.language.python.name);
originalParameterCheckEmpty.push(
!needGeneric &&
!parameterHandler.Parameter_IsRequired(
originalParameterStack.last,
),
);
if (!needGeneric) {
outputBody = outputBody.concat(
ConstructValuation(
Expand Down Expand Up @@ -292,7 +322,7 @@ function ConstructMethodBodyParameter(model: CodeModelAz, needGeneric = false, r
defaultValue = '{}';
}
if (!methodParameterHandler.MethodParameter_IsHidden) {
let needIfClause = true;
let needIfClause = !methodParameterHandler.MethodParameter_RequiredByMethod;
if (
methodParameterHandler.MethodParameter_Type === SchemaType.Constant
) {
Expand Down Expand Up @@ -359,8 +389,16 @@ function ConstructMethodBodyParameter(model: CodeModelAz, needGeneric = false, r
}
}
} else {
if (originalParameterCheckEmpty.pop()) {
const checks = ConstructEmptyCheck(
prefixIndent,
originalParameterNameStack,
);
outputBody = outputBody.concat(checks);
}
originalParameterStack.pop();
originalParameterNameStack.pop();

// if this parameter was popped out because of last flattened parameter has just finished construction,
// then we need to run construction logic for this parameter one more time.
if (
Expand All @@ -386,43 +424,65 @@ function ConstructValuation(
paramName: string,
value: string,
defaultValue: string = null,
needIfClause = true,
needIfClause = false,
): string[] {
const str = [];
let sentence = '';
if (isGeneric) {
if (
!(value.startsWith("'") && value.endsWith("'")) &&
!(value.startsWith('"') && value.endsWith('"'))
) {
str.push(prefix + 'if ' + value + ' is not None:');
prefix += ' ';
}
let property = '';

sentence = prefix + GetInstancePath(model, required) + '.';
if (isGeneric) {
property = GetInstancePath(model, required) + '.';
// sentence = prefix + GetInstancePath(model, required) + '.';
for (let i = 1; i < classNames.length; ++i) {
sentence += classNames[i] + '.';
property += classNames[i] + '.';
}
sentence += paramName;
property += paramName;
} else {
sentence = prefix + classNames[0];
property += classNames[0];
// sentence = prefix + classNames[0];
for (let i = 1; i < classNames.length; ++i) {
sentence += "['" + classNames[i] + "']";
property += "['" + classNames[i] + "']";
}

if (!isNullOrUndefined(paramName)) {
sentence += "['" + paramName + "']";
property += "['" + paramName + "']";
}
}

if (isNullOrUndefined(defaultValue)) {
sentence += ' = ' + value;
if (needIfClause) {
str.push(prefix + 'if ' + value + ' is not None:');
str.push(prefix + ' ' + property + ' = ' + value);
if (!isGeneric && !isNullOrUndefined(defaultValue)) {
str.push(prefix + 'else:');
str.push(prefix + ' ' + property + ' = ' + defaultValue);
}
} else {
sentence += ' = ' + defaultValue;
if (needIfClause) {
sentence += ' if ' + value + ' is None else ' + value;
if (!isNullOrUndefined(defaultValue)) {
str.push(prefix + property + ' = ' + defaultValue);
} else {
str.push(prefix + property + ' = ' + value);
}
}
return str;
}

function ConstructEmptyCheck(prefix: string, classNames: string[]): string[] {
const str = [];

if (classNames.length <= 1) {
// avoid to delete 'parameter' variable
return str;
}

let value = classNames[0];
for (let i = 1; i < classNames.length; ++i) {
value += "['" + classNames[i] + "']";
}

let sentence = prefix + 'if len(' + value + ') == 0:';

str.push(sentence);

prefix += ' ';
sentence = prefix + 'del ' + value;
str.push(sentence);
return str;
}
Expand Down Expand Up @@ -694,23 +754,6 @@ function GetSingleCommandBody(model: CodeModelAz, required: any) {
}
continue;
}
} else if (
methodParameterHandler.MethodParameter_DefaultValue !== undefined &&
methodParameterHandler.MethodParameter_Type !== SchemaType.Constant
) {
// model is simple type with default value
outputBody.push(
' if ' + methodParameterHandler.MethodParameter_MapsTo + ' is None:',
);
outputBody.push(
' ' +
methodParameterHandler.MethodParameter_MapsTo +
' = ' +
ToPythonString(
methodParameterHandler.MethodParameter_DefaultValue,
methodParameterHandler.MethodParameter_Type,
),
);
}
} while (skip || model.SelectNextMethodParameter());
}
Expand Down Expand Up @@ -971,11 +1014,10 @@ function GetSimpleCallItem(
const paramNamePython = parameterHandler.Parameter_NamePython(param);
const paramDefaultValue = parameterHandler.Parameter_DefaultValue(param);
if (parameterHandler.Parameter_IsHidden(param)) {
if (paramDefaultValue) {
if (!isNullOrUndefined(paramDefaultValue)) {
if (schemaHandler.Schema_Type(param.schema) === SchemaType.Object) {
parameterPair =
paramNamePython +
'=json.loads(' +
parameterPair = paramNamePython + `1`;
'=json.loads(' +
ToPythonString(paramDefaultValue, parameterHandler.Parameter_Type(param)) +
')';
required.json = true;
Expand All @@ -995,11 +1037,32 @@ function GetSimpleCallItem(
parameterPair = paramNamePython + '=None';
}
} else {
let valueName;
if (!isNullOrUndefined(optionName)) {
parameterPair = paramNamePython + '=' + optionName;
valueName = optionName;
// parameterPair = paramNamePython + '=' + optionName;
} else {
parameterPair = paramNamePython + '=' + parameterHandler.Parameter_MapsTo(param);
valueName = parameterHandler.Parameter_MapsTo(param);
// parameterPair = paramNamePython + '=' + parameterHandler.Parameter_MapsTo(param);
}

parameterPair = paramNamePython + '=';
if (!isNullOrUndefined(paramDefaultValue)) {
if (schemaHandler.Schema_Type(param.schema) === SchemaType.Object) {
parameterPair +=
'json.loads(' +
ToPythonString(paramDefaultValue, parameterHandler.Parameter_Type(param)) +
')';
required.json = true;
} else {
parameterPair += ToPythonString(
paramDefaultValue,
parameterHandler.Parameter_Type(param),
);
}
parameterPair += ' if ' + valueName + ' is None else ';
}
parameterPair += valueName;
}
}
return parameterPair;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,16 @@ def attestation_create_provider(client,
keys=None):
creation_params = {}
creation_params['location'] = location
creation_params['tags'] = tags
if tags is not None:
creation_params['tags'] = tags
creation_params['properties'] = {}
creation_params['properties']['attestation_policy'] = attestation_policy
if attestation_policy is not None:
creation_params['properties']['attestation_policy'] = attestation_policy
creation_params['properties']['policy_signing_certificates'] = {}
creation_params['properties']['policy_signing_certificates']['keys'] = keys
if keys is not None:
creation_params['properties']['policy_signing_certificates']['keys'] = keys
if len(creation_params['properties']['policy_signing_certificates']) == 0:
del creation_params['properties']['policy_signing_certificates']
return client.create(resource_group_name=resource_group_name,
offer_type="virtualmachine",
provider_name=provider_name,
Expand Down Expand Up @@ -54,7 +59,8 @@ def attestation_attestation_provider_update(client,
provider_name,
tags=None):
update_params = {}
update_params['tags'] = tags
if tags is not None:
update_params['tags'] = tags
return client.update(resource_group_name=resource_group_name,
offer_type="virtualmachine",
provider_name=provider_name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,21 +70,37 @@ def vm_virtual_machine_scale_set_vm_extension_create(client,
no_wait=False):
extension_parameters = {}
extension_parameters['location'] = location
extension_parameters['tags'] = tags
extension_parameters['force_update_tag'] = force_update_tag
extension_parameters['publisher'] = publisher
extension_parameters['type_properties_type'] = type_properties_type
extension_parameters['type_handler_version'] = type_handler_version
extension_parameters['auto_upgrade_minor_version'] = auto_upgrade_minor_version
extension_parameters['enable_automatic_upgrade'] = enable_automatic_upgrade
extension_parameters['settings'] = settings
extension_parameters['protected_settings'] = protected_settings
if tags is not None:
extension_parameters['tags'] = tags
if force_update_tag is not None:
extension_parameters['force_update_tag'] = force_update_tag
if publisher is not None:
extension_parameters['publisher'] = publisher
if type_properties_type is not None:
extension_parameters['type_properties_type'] = type_properties_type
if type_handler_version is not None:
extension_parameters['type_handler_version'] = type_handler_version
if auto_upgrade_minor_version is not None:
extension_parameters['auto_upgrade_minor_version'] = auto_upgrade_minor_version
if enable_automatic_upgrade is not None:
extension_parameters['enable_automatic_upgrade'] = enable_automatic_upgrade
if settings is not None:
extension_parameters['settings'] = settings
if protected_settings is not None:
extension_parameters['protected_settings'] = protected_settings
extension_parameters['instance_view'] = {}
extension_parameters['instance_view']['name'] = name
extension_parameters['instance_view']['type'] = type_
extension_parameters['instance_view']['type_handler_version'] = virtual_machine_extension_instance_view_type_handler_version_type_handler_version
extension_parameters['instance_view']['substatuses'] = substatuses
extension_parameters['instance_view']['statuses'] = statuses
if name is not None:
extension_parameters['instance_view']['name'] = name
if type_ is not None:
extension_parameters['instance_view']['type'] = type_
if virtual_machine_extension_instance_view_type_handler_version_type_handler_version is not None:
extension_parameters['instance_view']['type_handler_version'] = virtual_machine_extension_instance_view_type_handler_version_type_handler_version
if substatuses is not None:
extension_parameters['instance_view']['substatuses'] = substatuses
if statuses is not None:
extension_parameters['instance_view']['statuses'] = statuses
if len(extension_parameters['instance_view']) == 0:
del extension_parameters['instance_view']
return sdk_no_wait(no_wait,
client.create_or_update,
resource_group_name=resource_group_name,
Expand Down
Loading

0 comments on commit 2352caf

Please sign in to comment.