Skip to content

Commit

Permalink
[PS] better handle special variables (OpenAPITools#5885)
Browse files Browse the repository at this point in the history
* add variable

* fix reserved words
  • Loading branch information
wing328 authored and MikailBag committed May 31, 2020
1 parent f8b5ebd commit 6637e06
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -372,39 +372,75 @@ public PowerShellExperimentalClientCodegen() {
"System.Nullable[Boolean]"
));

// https://richardspowershellblog.wordpress.com/2009/05/02/powershell-reserved-words/
// list of reserved words - must be in lower case
reservedWords = new HashSet<String>(Arrays.asList(
"Begin",
"Break",
"Catch",
"Continue",
"Data",
"Do",
"Dynamicparam",
"Else",
"Elseif",
"End",
"Exit",
"Filter",
"Finally",
"For",
"Foreach",
"From",
"Function",
"If",
"In",
"Param",
"Process",
"Return",
"Switch",
"Throw",
"Trap",
"Try",
"Until",
"While",
"Local",
"Private",
"Where"
// https://richardspowershellblog.wordpress.com/2009/05/02/powershell-reserved-words/
"begin",
"break",
"catch",
"continue",
"data",
"do",
"dynamicparam",
"else",
"elseif",
"end",
"exit",
"filter",
"finally",
"for",
"foreach",
"from",
"function",
"if",
"in",
"param",
"process",
"return",
"switch",
"throw",
"trap",
"try",
"until",
"while",
"local",
"private",
"where",
// special variables
"args",
"consolefilename",
"error",
"event",
"eventargs",
"eventsubscriber",
"executioncontext",
"false",
"foreach",
"home",
"host",
"input",
"lastexitcode",
"matches",
"myinvocation",
"nestedpromptlevel",
"null",
"pid",
"profile",
"pscmdlet",
"pscommandpath",
"psculture",
"psdebugcontext",
"pshome",
"psitem",
"psscriptroot",
"pssenderinfo",
"psuiculture",
"psversiontable",
"sender",
"shellid",
"stacktrace",
"this",
"true"
));

defaultIncludes = new HashSet<String>(Arrays.asList(
Expand Down Expand Up @@ -634,7 +670,7 @@ public String modelFileFolder() {

@Override
public String escapeReservedWord(String name) {
return "_" + name;
return "Var" + name;
}

/**
Expand All @@ -654,23 +690,23 @@ public String toModelName(String name) {
name = name + "_" + modelNameSuffix;
}

name = sanitizeName(name);
// camelize the model name
// phone_number => PhoneNumber
name = camelize(sanitizeName(name));

// model name cannot use reserved keyword, e.g. return
if (isReservedWord(name)) {
LOGGER.warn(name + " (reserved word) cannot be used as model name. Renamed to " + camelize("model_" + name));
name = "model_" + name; // e.g. return => ModelReturn (after camelize)
LOGGER.warn(name + " (reserved word or special variable name) cannot be used as model name. Renamed to " + camelize("model_" + name));
name = camelize("model_" + name); // e.g. return => ModelReturn (after camelize)
}

// model name starts with number
if (name.matches("^\\d.*")) {
LOGGER.warn(name + " (model name starts with number) cannot be used as model name. Renamed to " + camelize("model_" + name));
name = "model_" + name; // e.g. 200Response => Model200Response (after camelize)
name = camelize("model_" + name); // e.g. 200Response => Model200Response (after camelize)
}

// camelize the model name
// phone_number => PhoneNumber
return camelize(name);
return name;
}

@Override
Expand Down Expand Up @@ -736,27 +772,7 @@ public String toOperationId(String operationId) {

@Override
public String toParamName(String name) {
// sanitize name
name = sanitizeName(name);

// replace - with _ e.g. created-at => created_at
name = name.replaceAll("-", "_");

// if it's all upper case, do nothing
if (name.matches("^[A-Z_]*$")) {
return name;
}

// camelize the variable name
// pet_id => PetId
name = camelize(name, false);

// for reserved word or word starting with number, append _
if (isReservedWord(name) || name.matches("^\\d.*")) {
name = escapeReservedWord(name);
}

return name;
return toVarName(name);
}

@Override
Expand Down Expand Up @@ -835,17 +851,13 @@ public String toVarName(String name) {
// sanitize name
name = sanitizeName(name);

// if it's all uppper case, do nothing
if (name.matches("^[A-Z_]*$")) {
return name;
}

// camelize the variable name
// pet_id => PetId
name = camelize(name);

// for reserved word or word starting with number, append _
if (isReservedWord(name) || name.matches("^\\d.*")) {
LOGGER.warn(name + " (reserved word or special variable name) cannot be used in naming. Renamed to " + escapeReservedWord(name));
name = escapeReservedWord(name);
}

Expand Down Expand Up @@ -1028,4 +1040,5 @@ private String toMethodName(String operationId) {
public String toRegularExpression(String pattern) {
return escapeText(pattern);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#
# Generated by: OpenAPI Generator Team
#
# Generated on: 4/9/20
# Generated on: 4/10/20
#

@{
Expand Down

0 comments on commit 6637e06

Please sign in to comment.