Skip to content

Commit

Permalink
[C++] Add an option to allow having lowercase variables in models, Pi…
Browse files Browse the repository at this point in the history
…stache: allow using reservedWords in models (OpenAPITools#5434)

* [C++][Pistache] Use reserved words to replace incorrect names

discard old decision to truncate reservedWords

* [C++][Pistache] Update struct model to use name instead of baseName

* [C++][Pistache] Update Petstore sample

* [C++] Add option to have lowercase variables

* [C++] Update generated docs
muttleyxd authored and MikailBag committed Mar 23, 2020
1 parent ee22b88 commit 1985d11
Showing 11 changed files with 119 additions and 12 deletions.
88 changes: 88 additions & 0 deletions docs/generators/cpp-pistache-server.md
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@ sidebar_label: cpp-pistache-server
|helpersPackage|Specify the package name to be used for the helpers (e.g. org.openapitools.server.helpers).| |org.openapitools.server.helpers|
|reservedWordPrefix|Prefix to prepend to reserved words in order to avoid conflicts| |r_|
|useStructModel|Use struct-based model template instead of get/set-based model template| |false|
|variableNameFirstCharacterUppercase|Make first character of variable name uppercase (eg. value -> Value)| |true|

## IMPORT MAPPING

@@ -42,6 +43,93 @@ sidebar_label: cpp-pistache-server
## RESERVED WORDS

<ul class="column-ul">
<li>alignas</li>
<li>alignof</li>
<li>and</li>
<li>and_eq</li>
<li>asm</li>
<li>auto</li>
<li>bitand</li>
<li>bitor</li>
<li>bool</li>
<li>break</li>
<li>case</li>
<li>catch</li>
<li>char</li>
<li>char16_t</li>
<li>char32_t</li>
<li>class</li>
<li>compl</li>
<li>concept</li>
<li>const</li>
<li>const_cast</li>
<li>constexpr</li>
<li>continue</li>
<li>decltype</li>
<li>default</li>
<li>delete</li>
<li>do</li>
<li>double</li>
<li>dynamic_cast</li>
<li>else</li>
<li>enum</li>
<li>explicit</li>
<li>export</li>
<li>extern</li>
<li>false</li>
<li>float</li>
<li>for</li>
<li>friend</li>
<li>goto</li>
<li>if</li>
<li>inline</li>
<li>int</li>
<li>linux</li>
<li>long</li>
<li>mutable</li>
<li>namespace</li>
<li>new</li>
<li>noexcept</li>
<li>not</li>
<li>not_eq</li>
<li>nullptr</li>
<li>operator</li>
<li>or</li>
<li>or_eq</li>
<li>private</li>
<li>protected</li>
<li>public</li>
<li>register</li>
<li>reinterpret_cast</li>
<li>requires</li>
<li>return</li>
<li>short</li>
<li>signed</li>
<li>sizeof</li>
<li>static</li>
<li>static_assert</li>
<li>static_cast</li>
<li>struct</li>
<li>switch</li>
<li>template</li>
<li>this</li>
<li>thread_local</li>
<li>throw</li>
<li>true</li>
<li>try</li>
<li>typedef</li>
<li>typeid</li>
<li>typename</li>
<li>union</li>
<li>unsigned</li>
<li>using</li>
<li>virtual</li>
<li>void</li>
<li>volatile</li>
<li>wchar_t</li>
<li>while</li>
<li>xor</li>
<li>xor_eq</li>
</ul>

## FEATURE SET
1 change: 1 addition & 0 deletions docs/generators/cpp-qt5-client.md
Original file line number Diff line number Diff line change
@@ -15,6 +15,7 @@ sidebar_label: cpp-qt5-client
|reservedWordPrefix|Prefix to prepend to reserved words in order to avoid conflicts| |r_|
|sortModelPropertiesByRequiredFlag|Sort model properties to place required parameters before optional parameters.| |true|
|sortParamsByRequiredFlag|Sort method arguments to place required parameters before optional parameters.| |true|
|variableNameFirstCharacterUppercase|Make first character of variable name uppercase (eg. value -&gt; Value)| |true|

## IMPORT MAPPING

1 change: 1 addition & 0 deletions docs/generators/cpp-qt5-qhttpengine-server.md
Original file line number Diff line number Diff line change
@@ -14,6 +14,7 @@ sidebar_label: cpp-qt5-qhttpengine-server
|reservedWordPrefix|Prefix to prepend to reserved words in order to avoid conflicts| |r_|
|sortModelPropertiesByRequiredFlag|Sort model properties to place required parameters before optional parameters.| |true|
|sortParamsByRequiredFlag|Sort method arguments to place required parameters before optional parameters.| |true|
|variableNameFirstCharacterUppercase|Make first character of variable name uppercase (eg. value -&gt; Value)| |true|

## IMPORT MAPPING

1 change: 1 addition & 0 deletions docs/generators/cpp-restsdk.md
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@ sidebar_label: cpp-restsdk
|modelPackage|C++ namespace for models (convention: name.space.model).| |org.openapitools.client.model|
|packageVersion|C++ package version.| |1.0.0|
|reservedWordPrefix|Prefix to prepend to reserved words in order to avoid conflicts| |r_|
|variableNameFirstCharacterUppercase|Make first character of variable name uppercase (eg. value -&gt; Value)| |true|

## IMPORT MAPPING

1 change: 1 addition & 0 deletions docs/generators/cpp-tizen.md
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@ sidebar_label: cpp-tizen
|reservedWordPrefix|Prefix to prepend to reserved words in order to avoid conflicts| |r_|
|sortModelPropertiesByRequiredFlag|Sort model properties to place required parameters before optional parameters.| |true|
|sortParamsByRequiredFlag|Sort method arguments to place required parameters before optional parameters.| |true|
|variableNameFirstCharacterUppercase|Make first character of variable name uppercase (eg. value -&gt; Value)| |true|

## IMPORT MAPPING

Original file line number Diff line number Diff line change
@@ -44,6 +44,9 @@ abstract public class AbstractCppCodegen extends DefaultCodegen implements Codeg
protected static final String RESERVED_WORD_PREFIX_OPTION = "reservedWordPrefix";
protected static final String RESERVED_WORD_PREFIX_DESC = "Prefix to prepend to reserved words in order to avoid conflicts";
protected String reservedWordPrefix = "r_";
protected static final String VARIABLE_NAME_FIRST_CHARACTER_UPPERCASE_OPTION = "variableNameFirstCharacterUppercase";
protected static final String VARIABLE_NAME_FIRST_CHARACTER_UPPERCASE_DESC = "Make first character of variable name uppercase (eg. value -> Value)";
protected boolean variableNameFirstCharacterUppercase = true;

public AbstractCppCodegen() {
super();
@@ -145,6 +148,9 @@ public AbstractCppCodegen() {
addOption(RESERVED_WORD_PREFIX_OPTION,
RESERVED_WORD_PREFIX_DESC,
this.reservedWordPrefix);
addOption(VARIABLE_NAME_FIRST_CHARACTER_UPPERCASE_OPTION,
VARIABLE_NAME_FIRST_CHARACTER_UPPERCASE_DESC,
Boolean.toString(this.variableNameFirstCharacterUppercase));
}

@Override
@@ -191,7 +197,7 @@ public String toVarName(String name) {
return escapeReservedWord(name);
}

if (name.length() > 1) {
if (variableNameFirstCharacterUppercase && name.length() > 1) {
return sanitizeName(Character.toUpperCase(name.charAt(0)) + name.substring(1));
}

@@ -275,6 +281,11 @@ public void processOpts() {
}

additionalProperties.put(RESERVED_WORD_PREFIX_OPTION, reservedWordPrefix);

if (additionalProperties.containsKey(VARIABLE_NAME_FIRST_CHARACTER_UPPERCASE_OPTION))
variableNameFirstCharacterUppercase =
convertPropertyToBooleanAndWriteBack(VARIABLE_NAME_FIRST_CHARACTER_UPPERCASE_OPTION);
additionalProperties.put(VARIABLE_NAME_FIRST_CHARACTER_UPPERCASE_OPTION, variableNameFirstCharacterUppercase);
}

@Override
Original file line number Diff line number Diff line change
@@ -107,8 +107,9 @@ public CppPistacheServerCodegen() {
addOption(HELPERS_PACKAGE_NAME, HELPERS_PACKAGE_NAME_DESC, this.helpersPackage);
addOption(RESERVED_WORD_PREFIX_OPTION, RESERVED_WORD_PREFIX_DESC, this.reservedWordPrefix);
addSwitch(OPTION_USE_STRUCT_MODEL, OPTION_USE_STRUCT_MODEL_DESC, this.isUseStructModel);

reservedWords = new HashSet<>();
addOption(VARIABLE_NAME_FIRST_CHARACTER_UPPERCASE_OPTION,
VARIABLE_NAME_FIRST_CHARACTER_UPPERCASE_DESC,
Boolean.toString(this.variableNameFirstCharacterUppercase));

supportingFiles.add(new SupportingFile("helpers-header.mustache", "model", modelNamePrefix + "Helpers.h"));
supportingFiles.add(new SupportingFile("helpers-source.mustache", "model", modelNamePrefix + "Helpers.cpp"));
Original file line number Diff line number Diff line change
@@ -132,6 +132,9 @@ public CppRestSdkClientCodegen() {
addOption(RESERVED_WORD_PREFIX_OPTION,
RESERVED_WORD_PREFIX_DESC,
this.reservedWordPrefix);
addOption(VARIABLE_NAME_FIRST_CHARACTER_UPPERCASE_OPTION,
VARIABLE_NAME_FIRST_CHARACTER_UPPERCASE_DESC,
Boolean.toString(this.variableNameFirstCharacterUppercase));

supportingFiles.add(new SupportingFile("modelbase-header.mustache", "", "ModelBase.h"));
supportingFiles.add(new SupportingFile("modelbase-source.mustache", "", "ModelBase.cpp"));
Original file line number Diff line number Diff line change
@@ -21,7 +21,7 @@ namespace {{this}} {
struct {{classname}}
{
{{#vars}}
{{^required}}Pistache::Optional<{{/required}}{{{dataType}}}{{^required}}>{{/required}} {{baseName}};
{{^required}}Pistache::Optional<{{/required}}{{{dataType}}}{{^required}}>{{/required}} {{name}};
{{/vars}}

bool operator==(const {{classname}}& other) const;
Original file line number Diff line number Diff line change
@@ -23,7 +23,7 @@ nlohmann::json {{classname}}::to_json() const

bool {{classname}}::operator==(const {{classname}}& other) const
{
return {{#vars}}{{baseName}} == other.{{baseName}}{{#hasMore}} && {{/hasMore}}{{/vars}};
return {{#vars}}{{name}} == other.{{name}}{{#hasMore}} && {{/hasMore}}{{/vars}};
}

bool {{classname}}::operator!=(const {{classname}}& other) const
@@ -34,19 +34,19 @@ bool {{classname}}::operator!=(const {{classname}}& other) const
void to_json(nlohmann::json& j, const {{classname}}& o)
{
{{#vars}}
{{^required}}if (!o.{{baseName}}.isEmpty()){{/required}}
j["{{baseName}}"] = o.{{baseName}}{{^required}}.get(){{/required}};
{{^required}}if (!o.{{name}}.isEmpty()){{/required}}
j["{{baseName}}"] = o.{{name}}{{^required}}.get(){{/required}};
{{/vars}}
}

void from_json(const nlohmann::json& j, {{classname}}& o)
{
{{#vars}}
{{#required}}j.at("{{baseName}}").get_to(o.{{baseName}});{{/required}}
{{#required}}j.at("{{baseName}}").get_to(o.{{name}});{{/required}}
{{^required}}if (j.find("{{baseName}}") != j.end()) {
{{{dataType}}} temporary_{{baseName}};
j.at("{{baseName}}").get_to(temporary_{{baseName}});
o.{{baseName}} = Pistache::Some(temporary_{{baseName}});
{{{dataType}}} temporary_{{name}};
j.at("{{baseName}}").get_to(temporary_{{name}});
o.{{name}} = Pistache::Some(temporary_{{name}});
}{{/required}}
{{/vars}}
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.2.3-SNAPSHOT
4.3.0-SNAPSHOT

0 comments on commit 1985d11

Please sign in to comment.