[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
This commit is contained in:
Mateusz Szychowski (Muttley) 2019-09-29 11:48:39 +02:00 committed by sunn
parent 5dcd959f30
commit 8212e80d0e
7 changed files with 118 additions and 6 deletions

View File

@ -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

View File

@ -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|

View File

@ -27,6 +27,8 @@ import org.apache.commons.lang3.StringUtils;
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 java.net.URL;
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 class CppPistacheServerCodegen extends AbstractCppCodegen {
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 class CppPistacheServerCodegen extends AbstractCppCodegen {
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 class CppPistacheServerCodegen extends AbstractCppCodegen {
} 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

View File

@ -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)

View File

@ -0,0 +1,40 @@
{{>licenseInfo}}
{{#models}}{{#model}}/*
* {{classname}}.h
*
* {{description}}
*/
#ifndef {{classname}}_H_
#define {{classname}}_H_
{{{defaultInclude}}}
{{#imports}}{{{this}}}
{{/imports}}
#include <nlohmann/json.hpp>
#include <pistache/optional.h>
{{#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}}

View File

@ -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}}

View File

@ -1 +1 @@
4.1.2-SNAPSHOT
4.1.3-SNAPSHOT