Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[C++][Pistache] Simplified model template #3417

Merged
merged 5 commits into from
Sep 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bin/cpp-pistache-server-petstore.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions docs/generators/cpp-pistache-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -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 = "";
Expand Down Expand Up @@ -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");
Expand All @@ -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<>();

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Original file line number Diff line number Diff line change
@@ -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}}
Original file line number Diff line number Diff line change
@@ -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}}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.1.2-SNAPSHOT
4.1.3-SNAPSHOT