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

Commit

Permalink
Set default value if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
LianwMS committed May 12, 2020
1 parent 14c1a60 commit 7351b59
Show file tree
Hide file tree
Showing 9 changed files with 211 additions and 144 deletions.
8 changes: 5 additions & 3 deletions src/plugins/azgenerator/CodeModelAz.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ export interface CodeModelAz
Method_GenericSetterParameter(Operation): Parameter;


SelectFirstMethodParameter(): boolean;
SelectNextMethodParameter(): boolean;
EnterSubMethodParameters(): boolean;
SelectFirstMethodParameter(containHidden?: boolean): boolean;
SelectNextMethodParameter(containHidden?: boolean): boolean;
EnterSubMethodParameters(param?: Parameter): boolean;
ExitSubMethodParameters(): boolean;

MethodParameter_Name: string;
Expand All @@ -132,6 +132,7 @@ export interface CodeModelAz
MethodParameter_RequiredByMethod: boolean;
MethodParameter_EnumValues: string[];
MethodParameters_AddPolySubClass(oriParam, para): boolean;
MethodParameters_DefaultValue: any | undefined;
Parameter_Type(Parameter): string;
Schema_Type(Schema): string;
Parameter_IsList(Parameter): boolean;
Expand All @@ -148,6 +149,7 @@ export interface CodeModelAz
Parameter_NameAz(Parameter): string;
Parameter_NamePython(Parameter): string;
Parameter_Description(Parameter): string;
Parameter_DefaultValue(Parameter): any | undefined;
Schema_Description(Schema): string;

GetModuleOperationName(): string;
Expand Down
74 changes: 52 additions & 22 deletions src/plugins/azgenerator/CodeModelAzImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -772,11 +772,11 @@ export class CodeModelCliImpl implements CodeModelAz {
//
// This interface is designed to enumerate all parameters of the selected method and their mapping to Python SDK.
//=================================================================================================================
public SelectFirstMethodParameter(): boolean {
public SelectFirstMethodParameter(containHidden: boolean = false): boolean {
if (this.submethodparameters != null) {
this.currentSubOptionIndex = 0;
let parameter = this.submethodparameters[this.currentSubOptionIndex];
if (parameter?.language['cli']?.hidden || parameter?.language['cli']?.removed) {
if (this.Parameter_IsHidden(parameter) && !containHidden) {
if (!this.SelectNextMethodParameter()) {
return false;
}
Expand All @@ -785,7 +785,7 @@ export class CodeModelCliImpl implements CodeModelAz {
}
if (this.MethodParameters.length > 0) {
this.currentParameterIndex = 0;
if (this.MethodParameter_IsHidden || this.codeModel.globalParameters.indexOf(this.MethodParameter) > -1) {
if ((this.MethodParameter_IsHidden && !containHidden) || this.codeModel.globalParameters.indexOf(this.MethodParameter) > -1) {
if (this.SelectNextMethodParameter()) {
return true;
} else {
Expand All @@ -798,15 +798,15 @@ export class CodeModelCliImpl implements CodeModelAz {
}
}

public SelectNextMethodParameter(): boolean {
public SelectNextMethodParameter(containHidden: boolean = false): boolean {
if (this.submethodparameters != null) {
this.currentSubOptionIndex++;

if (this.currentSubOptionIndex >= this.submethodparameters.length) {
return false;
}
let parameter = this.submethodparameters[this.currentSubOptionIndex];
if (parameter?.language['cli']?.hidden || parameter?.language['cli']?.removed) {
if (this.Parameter_IsHidden(parameter) && !containHidden) {
if (!this.SelectNextMethodParameter()) {
return false;
}
Expand All @@ -815,7 +815,7 @@ export class CodeModelCliImpl implements CodeModelAz {
}
if (this.currentParameterIndex < this.MethodParameters.length - 1) {
this.currentParameterIndex++;
if (this.MethodParameter_IsHidden || this.codeModel.globalParameters.indexOf(this.MethodParameter) > -1) {
if ((this.MethodParameter_IsHidden && !containHidden) || this.codeModel.globalParameters.indexOf(this.MethodParameter) > -1) {
if (this.SelectNextMethodParameter()) {
return true;
} else {
Expand All @@ -828,12 +828,15 @@ export class CodeModelCliImpl implements CodeModelAz {
}
}

public EnterSubMethodParameters(): boolean {
this.submethodparameters = this.GetSubParameters(this.MethodParameter);
if(isNullOrUndefined(this.submethodparameters)) {
return false;
public EnterSubMethodParameters(param: Parameter = this.MethodParameter): boolean {
let subParams = this.GetSubParameters(param);
if (isNullOrUndefined(subParams)) {
return false;
}
else {
this.submethodparameters = subParams;
return true;
}
return true;
}

public GetSubParameters(param: Parameter = this.MethodParameter): Parameter[] {
Expand Down Expand Up @@ -1216,16 +1219,35 @@ export class CodeModelCliImpl implements CodeModelAz {
}

public get MethodParameter_IsHidden(): boolean {
return this.Parameter_IsHidden(this.MethodParameter);
}

let parameter = this.MethodParameter;

public Parameter_IsHidden(parameter: Parameter): boolean {
if (!parameter.language['az'].hasOwnProperty('hidden')) {
// Handle complex
let shouldHidden = undefined;
if (this.EnterSubMethodParameters(parameter))
{
shouldHidden = true;
if (this.SelectFirstMethodParameter()) {
do {
if (this.Parameter_Type(this.SubMethodParameter) != SchemaType.Constant
&& this.SubMethodParameter['readOnly'] != true) {
shouldHidden = false;
break;
}
} while (this.SelectNextMethodParameter())
}
this.ExitSubMethodParameters();
}

// Handle simple parameter
if (parameter?.language?.['cli']?.removed || parameter?.language?.['cli']?.hidden) {
if (parameter.schema.defaultValue == undefined && parameter.required == true) {
if (this.Parameter_DefaultValue(parameter) == undefined && parameter.required == true) {
parameter.language['az'].hidden = false;
this.session.message({
Channel: Channel.Warning,
Text: "OperationGroup " + this.GetModuleOperationName()
Text: "OperationGroup " + this.CommandGroup.language['az'].name
+ " operation " + this.Method_Name
+ " parameter " + parameter.language['az'].name
+ " should not be hidden while it is required without default value"
Expand All @@ -1235,19 +1257,27 @@ export class CodeModelCliImpl implements CodeModelAz {
parameter.language['az'].hidden = true;
}
} else {
parameter.language['az'].hidden = this.MethodParameter['hidden'] ? true : false;
parameter.language['az'].hidden = this.MethodParameter['hidden'] ?? shouldHidden ?? false;
}
}

return parameter.language['az'].hidden;
}

public Parameter_IsHidden(parameter: Parameter): boolean {
if (parameter.language['cli'].removed || parameter.language['cli'].hidden) {
return true;
} else {
return this.MethodParameter['hidden'] ? true : false;
}
public get MethodParameters_DefaultValue(): string | undefined {
return this.Parameter_DefaultValue(this.MethodParameter);
}

public Parameter_DefaultValue(parameter: Parameter): string | undefined{
if (!parameter.language['az'].hasOwnProperty('default-value')) {
if (parameter?.language?.['cli']?.hasOwnProperty('default-value')) {
parameter.language['az']['default-value'] = parameter.language['cli']['default-value'];
} else {
parameter.language['az']['default-value'] = parameter.schema.defaultValue;
}
}

return parameter.language['az']['default-value'];
}

public Parameter_Description(param: Parameter = this.MethodParameter): string {
Expand Down
14 changes: 13 additions & 1 deletion src/plugins/azgenerator/TemplateAzureCliActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*--------------------------------------------------------------------------------------------*/

import { CodeModelAz } from "./CodeModelAz"
import { ToCamelCase, Capitalize } from "../../utils/helper";
import { ToCamelCase, Capitalize, ToPythonString } from "../../utils/helper";
import { SchemaType, Parameter } from "@azure-tools/codemodel";
import { stringify } from "querystring";
import { HeaderGenerator } from "./Header";
Expand Down Expand Up @@ -96,6 +96,18 @@ function GetAction(model: CodeModelAz, actionName: string, param: Parameter, key
output.push(" except ValueError:");
output.push(" raise CLIError('usage error: {} [KEY=VALUE ...]'.format(option_string))");
output.push(" d = {}");

if (model.EnterSubMethodParameters()) {
if (model.SelectFirstMethodParameter(true)) {
do {
if (model.Parameter_DefaultValue(model.SubMethodParameter) !== undefined)
{
output.push(" d['" + model.Parameter_NamePython(model.SubMethodParameter) + "'] = " + ToPythonString(model.Parameter_DefaultValue(model.SubMethodParameter), model.Parameter_Type(model.SubMethodParameter)));
}
} while (model.SelectNextMethodParameter(true));
}
}

output.push(" for k in properties:");
output.push(" kl = k.lower()");
output.push(" v = properties[k]");
Expand Down
Loading

0 comments on commit 7351b59

Please sign in to comment.