From 1de3fa1e9a9adeeda2690168eea980c3e157c529 Mon Sep 17 00:00:00 2001 From: "Mateusz Szychowski (Muttley)" Date: Sun, 29 Sep 2019 11:48:39 +0200 Subject: [PATCH] [C++][Pistache] Simplified model template (#3417) * [C++][Pistache] Simplified model template * [C++][Pistache] Fix for addExternalLibs option CMake would fail with addExternalLibs set to false since it'd try to add depenency to not-existing targets * [C++][Pistache] Update cpp-pistache-server-petstore.sh * [C++][Pistache] Update Petstore sample * [C++][Pistache] Update documentation --- bin/cpp-pistache-server-petstore.sh | 2 +- docs/generators/cpp-pistache-server.md | 1 + .../languages/CppPistacheServerCodegen.java | 28 +++++++++-- .../cpp-pistache-server/cmake.mustache | 2 +- .../model-struct-header.mustache | 40 +++++++++++++++ .../model-struct-source.mustache | 49 +++++++++++++++++++ .../cpp-pistache/.openapi-generator/VERSION | 2 +- 7 files changed, 118 insertions(+), 6 deletions(-) create mode 100644 modules/openapi-generator/src/main/resources/cpp-pistache-server/model-struct-header.mustache create mode 100644 modules/openapi-generator/src/main/resources/cpp-pistache-server/model-struct-source.mustache diff --git a/bin/cpp-pistache-server-petstore.sh b/bin/cpp-pistache-server-petstore.sh index d0273a9d76c0..7f2c734b6894 100755 --- a/bin/cpp-pistache-server-petstore.sh +++ b/bin/cpp-pistache-server-petstore.sh @@ -27,6 +27,6 @@ fi # if you've executed sbt assembly previously it will use that instead. export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties" -ags="generate -g cpp-pistache-server -t modules/openapi-generator/src/main/resources/cpp-pistache-server -i modules/openapi-generator/src/test/resources/2_0/petstore.yaml --additional-properties addExternalLibs=true -o samples/server/petstore/cpp-pistache $@" +ags="generate -g cpp-pistache-server -t modules/openapi-generator/src/main/resources/cpp-pistache-server -i modules/openapi-generator/src/test/resources/2_0/petstore.yaml --additional-properties addExternalLibs=true --additional-properties useStructModel=false -o samples/server/petstore/cpp-pistache $@" java $JAVA_OPTS -jar $executable $ags diff --git a/docs/generators/cpp-pistache-server.md b/docs/generators/cpp-pistache-server.md index ac4abee691e6..2ffeb59cc7db 100644 --- a/docs/generators/cpp-pistache-server.md +++ b/docs/generators/cpp-pistache-server.md @@ -9,3 +9,4 @@ sidebar_label: cpp-pistache-server | ------ | ----------- | ------ | ------- | |addExternalLibs|Add the Possibility to fetch and compile external Libraries needed by this Framework.| |true| |helpersPackage|Specify the package name to be used for the helpers (e.g. org.openapitools.server.helpers).| |org.openapitools.server.helpers| +|useStructModel|Use struct-based model template instead of get/set-based model template| |false| diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CppPistacheServerCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CppPistacheServerCodegen.java index cf9d78ed5dd9..3de0cbf5b91e 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CppPistacheServerCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CppPistacheServerCodegen.java @@ -27,6 +27,8 @@ import org.openapitools.codegen.*; import org.openapitools.codegen.utils.ModelUtils; import org.openapitools.codegen.utils.URLPathUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.io.File; import java.util.*; @@ -35,10 +37,15 @@ import static org.openapitools.codegen.utils.StringUtils.*; public class CppPistacheServerCodegen extends AbstractCppCodegen { + private static final Logger LOGGER = LoggerFactory.getLogger(CppPistacheServerCodegen.class); + protected String implFolder = "impl"; protected boolean isAddExternalLibs = true; + protected boolean isUseStructModel = false; public static final String OPTIONAL_EXTERNAL_LIB = "addExternalLibs"; public static final String OPTIONAL_EXTERNAL_LIB_DESC = "Add the Possibility to fetch and compile external Libraries needed by this Framework."; + public static final String OPTION_USE_STRUCT_MODEL = "useStructModel"; + public static final String OPTION_USE_STRUCT_MODEL_DESC = "Use struct-based model template instead of get/set-based model template"; public static final String HELPERS_PACKAGE_NAME = "helpersPackage"; public static final String HELPERS_PACKAGE_NAME_DESC = "Specify the package name to be used for the helpers (e.g. org.openapitools.server.helpers)."; protected final String PREFIX = ""; @@ -68,9 +75,6 @@ public CppPistacheServerCodegen() { apiPackage = "org.openapitools.server.api"; modelPackage = "org.openapitools.server.model"; - modelTemplateFiles.put("model-header.mustache", ".h"); - modelTemplateFiles.put("model-source.mustache", ".cpp"); - apiTemplateFiles.put("api-header.mustache", ".h"); apiTemplateFiles.put("api-source.mustache", ".cpp"); apiTemplateFiles.put("api-impl-header.mustache", ".h"); @@ -81,6 +85,7 @@ public CppPistacheServerCodegen() { cliOptions.clear(); addSwitch(OPTIONAL_EXTERNAL_LIB, OPTIONAL_EXTERNAL_LIB_DESC, this.isAddExternalLibs); addOption(HELPERS_PACKAGE_NAME, HELPERS_PACKAGE_NAME_DESC, this.helpersPackage); + addSwitch(OPTION_USE_STRUCT_MODEL, OPTION_USE_STRUCT_MODEL_DESC, this.isUseStructModel); reservedWords = new HashSet<>(); @@ -144,6 +149,23 @@ public void processOpts() { } else { additionalProperties.put(OPTIONAL_EXTERNAL_LIB, isAddExternalLibs); } + + setupModelTemplate(); + } + + private void setupModelTemplate() { + if (additionalProperties.containsKey(OPTION_USE_STRUCT_MODEL)) + isUseStructModel = convertPropertyToBooleanAndWriteBack(OPTION_USE_STRUCT_MODEL); + + if (isUseStructModel) { + LOGGER.info("Using struct-based model template"); + modelTemplateFiles.put("model-struct-header.mustache", ".h"); + modelTemplateFiles.put("model-struct-source.mustache", ".cpp"); + } else { + LOGGER.info("Using get/set-based model template"); + modelTemplateFiles.put("model-header.mustache", ".h"); + modelTemplateFiles.put("model-source.mustache", ".cpp"); + } } @Override diff --git a/modules/openapi-generator/src/main/resources/cpp-pistache-server/cmake.mustache b/modules/openapi-generator/src/main/resources/cpp-pistache-server/cmake.mustache index a822ae28ae59..2ad6f17868fd 100644 --- a/modules/openapi-generator/src/main/resources/cpp-pistache-server/cmake.mustache +++ b/modules/openapi-generator/src/main/resources/cpp-pistache-server/cmake.mustache @@ -35,5 +35,5 @@ file(GLOB SRCS ) add_executable(${PROJECT_NAME} ${SRCS} ) -add_dependencies(${PROJECT_NAME} PISTACHE NLOHMANN) +{{#addExternalLibs}}add_dependencies(${PROJECT_NAME} PISTACHE NLOHMANN){{/addExternalLibs}} target_link_libraries(${PROJECT_NAME} pistache pthread) diff --git a/modules/openapi-generator/src/main/resources/cpp-pistache-server/model-struct-header.mustache b/modules/openapi-generator/src/main/resources/cpp-pistache-server/model-struct-header.mustache new file mode 100644 index 000000000000..a49415db26de --- /dev/null +++ b/modules/openapi-generator/src/main/resources/cpp-pistache-server/model-struct-header.mustache @@ -0,0 +1,40 @@ +{{>licenseInfo}} +{{#models}}{{#model}}/* + * {{classname}}.h + * + * {{description}} + */ + +#ifndef {{classname}}_H_ +#define {{classname}}_H_ + +{{{defaultInclude}}} +{{#imports}}{{{this}}} +{{/imports}} +#include +#include + +{{#modelNamespaceDeclarations}} +namespace {{this}} { +{{/modelNamespaceDeclarations}} + +struct {{classname}} +{ + {{#vars}} + {{^required}}Pistache::Optional<{{/required}}{{{dataType}}}{{^required}}>{{/required}} {{baseName}}; + {{/vars}} + + nlohmann::json to_json() const; + static {{classname}} from_json(const nlohmann::json& j); +}; + +void to_json(nlohmann::json& j, const {{classname}}& o); +void from_json(const nlohmann::json& j, {{classname}}& o); + +{{#modelNamespaceDeclarations}} +} // {{this}} +{{/modelNamespaceDeclarations}} + +#endif /* {{classname}}_H_ */ +{{/model}} +{{/models}} diff --git a/modules/openapi-generator/src/main/resources/cpp-pistache-server/model-struct-source.mustache b/modules/openapi-generator/src/main/resources/cpp-pistache-server/model-struct-source.mustache new file mode 100644 index 000000000000..2e4f114cc73c --- /dev/null +++ b/modules/openapi-generator/src/main/resources/cpp-pistache-server/model-struct-source.mustache @@ -0,0 +1,49 @@ +{{>licenseInfo}} +{{#models}}{{#model}} + +#include "{{classname}}.h" + +{{#modelNamespaceDeclarations}} +namespace {{this}} { +{{/modelNamespaceDeclarations}} + +nlohmann::json {{classname}}::to_json() const +{ + nlohmann::json j; + {{#modelNamespaceDeclarations}}::{{this}}{{/modelNamespaceDeclarations}}::to_json(j, *this); + return j; +} + +{{classname}} {{classname}}::from_json(const nlohmann::json& j) +{ + {{classname}} o{}; + {{#modelNamespaceDeclarations}}::{{this}}{{/modelNamespaceDeclarations}}::from_json(j, o); + return o; +} + +void to_json(nlohmann::json& j, const {{classname}}& o) +{ + {{#vars}} + {{^required}}if (!o.{{baseName}}.isEmpty()){{/required}} + j["{{baseName}}"] = o.{{baseName}}{{^required}}.get(){{/required}}; + {{/vars}} +} + +void from_json(const nlohmann::json& j, {{classname}}& o) +{ + {{#vars}} + {{#required}}j.at("{{baseName}}").get_to(o.{{baseName}});{{/required}} + {{^required}}if (j.find("{{baseName}}") != j.end()) { + {{{dataType}}} temporary_{{baseName}}; + j.at("{{baseName}}").get_to(temporary_{{baseName}}); + o.{{baseName}} = Pistache::Some(temporary_{{baseName}}); + }{{/required}} + {{/vars}} +} + +{{#modelNamespaceDeclarations}} +} // {{this}} +{{/modelNamespaceDeclarations}} + +{{/model}} +{{/models}} diff --git a/samples/server/petstore/cpp-pistache/.openapi-generator/VERSION b/samples/server/petstore/cpp-pistache/.openapi-generator/VERSION index d1a8f58b3884..0e97bd19efbf 100644 --- a/samples/server/petstore/cpp-pistache/.openapi-generator/VERSION +++ b/samples/server/petstore/cpp-pistache/.openapi-generator/VERSION @@ -1 +1 @@ -4.1.2-SNAPSHOT \ No newline at end of file +4.1.3-SNAPSHOT \ No newline at end of file