Skip to content

Commit

Permalink
Improve templates for C++ Restbed (#10543)
Browse files Browse the repository at this point in the history
* Improve templates for C++ Restbed

The templates now generate classes which have virtual functions that can
be overridden to implement handlers.

* Fix missing HTTP methods in generated restbed C++ code

There was a wrong handling of "x-codegen-other-methods".

Change-Id: If6526d2672434beb5ebb0871d84cb80d84c34c38
  • Loading branch information
LukasWoodtli committed Oct 13, 2021
1 parent 80c3a0e commit 0023f3b
Show file tree
Hide file tree
Showing 24 changed files with 3,351 additions and 1,293 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -315,13 +315,14 @@ public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> o
if (!foundInNewList) {
if (op1.path.equals(op.path)) {
foundInNewList = true;
List<CodegenOperation> currentOtherMethodList = (List<CodegenOperation>) op1.vendorExtensions.get("x-codegen-otherMethods");
final String X_CODEGEN_OTHER_METHODS = "x-codegen-other-methods";
List<CodegenOperation> currentOtherMethodList = (List<CodegenOperation>) op1.vendorExtensions.get(X_CODEGEN_OTHER_METHODS);
if (currentOtherMethodList == null) {
currentOtherMethodList = new ArrayList<CodegenOperation>();
}
op.operationIdCamelCase = op1.operationIdCamelCase;
currentOtherMethodList.add(op);
op1.vendorExtensions.put("x-codegen-other-methods", currentOtherMethodList);
op1.vendorExtensions.put(X_CODEGEN_OTHER_METHODS, currentOtherMethodList);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@
{{{defaultInclude}}}
#include <memory>
#include <utility>
#include <exception>

#include <corvusoft/restbed/session.hpp>
#include <corvusoft/restbed/resource.hpp>
#include <corvusoft/restbed/request.hpp>
#include <corvusoft/restbed/service.hpp>
#include <corvusoft/restbed/settings.hpp>

{{#imports}}{{{import}}}
{{/imports}}
Expand All @@ -25,6 +28,22 @@ namespace {{this}} {

using namespace {{modelNamespace}};

///
/// Exception to flag problems in the handlers
///
class {{declspec}} {{classname}}Exception: public std::exception
{
public:
{{classname}}Exception(int status_code, std::string what);

int getStatus() const;
const char* what() const noexcept override;

private:
int m_status;
std::string m_what;
};

{{#operation}}
/// <summary>
/// {{summary}}
Expand All @@ -35,60 +54,143 @@ using namespace {{modelNamespace}};
class {{declspec}} {{classname}}{{vendorExtensions.x-codegen-resource-name}}Resource: public restbed::Resource
{
public:
{{classname}}{{vendorExtensions.x-codegen-resource-name}}Resource();
{{classname}}{{vendorExtensions.x-codegen-resource-name}}Resource(const std::string& context = "{{contextPath}}");
virtual ~{{classname}}{{vendorExtensions.x-codegen-resource-name}}Resource();
void {{httpMethod}}_method_handler(const std::shared_ptr<restbed::Session> session);

protected:
//////////////////////////////////////////////////////////
// Override these to implement the server functionality //
//////////////////////////////////////////////////////////

virtual {{#returnType}}std::pair<int, {{{.}}}>{{/returnType}}{{^returnType}}int{{/returnType}} handler_{{httpMethod}}(
{{#allParams}}{{{dataType}}} const & {{{paramName}}}{{^-last}}, {{/-last}}{{/allParams}});

{{#vendorExtensions.x-codegen-other-methods}}
virtual {{#returnType}}std::pair<int, {{{.}}}>{{/returnType}}{{^returnType}}int{{/returnType}} handler_{{httpMethod}}(
{{#allParams}}{{{dataType}}} const & {{{paramName}}}{{^-last}}, {{/-last}}{{/allParams}});
{{/vendorExtensions.x-codegen-other-methods}}

protected:
//////////////////////////////////////
// Override these for customization //
//////////////////////////////////////

virtual std::string extractBodyContent(const std::shared_ptr<restbed::Session>& session);

{{#hasPathParams}}
{{#pathParams}}
{{#isPrimitiveType}}
virtual {{{dataType}}} getPathParam_{{{paramName}}}(const std::shared_ptr<const restbed::Request>& request)
{
return request->get_path_parameter("{{{paramName}}}", {{{defaultValue}}});
}

{{/isPrimitiveType}}
{{/pathParams}}
{{/hasPathParams}}
{{#hasQueryParams}}
{{#queryParams}}
{{#isPrimitiveType}}
virtual {{{dataType}}} getQueryParam_{{{paramName}}}(const std::shared_ptr<const restbed::Request>& request)
{
return request->get_query_parameter("{{{paramName}}}", {{{defaultValue}}});
}

{{/isPrimitiveType}}
{{/queryParams}}
{{/hasQueryParams}}
{{#hasHeaderParams}}
{{#headerParams}}
{{#isPrimitiveType}}
virtual {{{dataType}}} getHeader_{{{baseName}}}(const std::shared_ptr<const restbed::Request>& request)
{
return request->get_header("{{baseName}}", {{{defaultValue}}});
}

{{/isPrimitiveType}}
{{/headerParams}}
{{/hasHeaderParams}}

{{#vendorExtensions.x-codegen-other-methods}}
void {{httpMethod}}_method_handler(const std::shared_ptr<restbed::Session> session);
{{#hasPathParams}}
{{#pathParams}}
{{#isPrimitiveType}}
virtual {{{dataType}}} getPathParam_{{{paramName}}}_x_extension(const std::shared_ptr<const restbed::Request>& request)
{
return request->get_path_parameter("{{{paramName}}}", {{{defaultValue}}});
}
{{/isPrimitiveType}}
{{/pathParams}}
{{/hasPathParams}}
{{#hasQueryParams}}
{{#queryParams}}
{{#isPrimitiveType}}
virtual {{{dataType}}} getQueryParam_{{{paramName}}}_x_extension(const std::shared_ptr<const restbed::Request>& request)
{
return request->get_query_parameter("{{{paramName}}}", {{{defaultValue}}});
}
{{/isPrimitiveType}}
{{/queryParams}}
{{/hasQueryParams}}
{{#hasHeaderParams}}
{{#headerParams}}
{{#isPrimitiveType}}
virtual {{{dataType}}} getHeader_{{{baseName}}}_x_extension(const std::shared_ptr<const restbed::Request>& request)
{
return request->get_header("{{baseName}}", {{{defaultValue}}});
}
{{/isPrimitiveType}}
{{/headerParams}}
{{/hasHeaderParams}}
{{/vendorExtensions.x-codegen-other-methods}}

void set_handler_{{httpMethod}}(
std::function<std::pair<int, std::string>(
{{#allParams}}{{{dataType}}} const &{{^-last}}, {{/-last}}{{/allParams}}
)> handler
);
virtual std::pair<int, std::string> handle{{classname}}Exception(const {{classname}}Exception& e);
virtual std::pair<int, std::string> handleStdException(const std::exception& e);
virtual std::pair<int, std::string> handleUnspecifiedException();

virtual void setResponseHeader(const std::shared_ptr<restbed::Session>& session,
const std::string& header);

{{#vendorExtensions.x-codegen-other-methods}}
void set_handler_{{httpMethod}}(
std::function<std::pair<int, std::string>(
{{#allParams}}{{{dataType}}} const &{{^-last}}, {{/-last}}{{/allParams}}
)> handler
);
{{/vendorExtensions.x-codegen-other-methods}}

virtual void returnResponse(const std::shared_ptr<restbed::Session>& session,
const int status, const std::string& result, const std::string& contentType);
virtual void defaultSessionClose(const std::shared_ptr<restbed::Session>& session,
const int status, const std::string& result);

private:
std::function<std::pair<int, std::string>(
{{#allParams}}{{{dataType}}} const &{{^-last}}, {{/-last}}{{/allParams}}
)> handler_{{httpMethod}}_;

{{#vendorExtensions.x-codegen-other-methods}}
std::function<std::pair<int, std::string>(
{{#allParams}}{{{dataType}}} const &{{^-last}}, {{/-last}}{{/allParams}}
)> handler_{{httpMethod}}_;
{{/vendorExtensions.x-codegen-other-methods}}

{{#allParams}}
{{{dataType}}} {{paramName}}{};
{{/allParams}}
void handler_{{httpMethod}}_internal(const std::shared_ptr<restbed::Session> session);
{{#vendorExtensions.x-codegen-other-methods}}
void handler_{{httpMethod}}_internal(const std::shared_ptr<restbed::Session> session);
{{/vendorExtensions.x-codegen-other-methods}}
};


{{/operation}}

//
// The restbed service to actually implement the REST server
//
class {{declspec}} {{classname}}: public restbed::Service
class {{declspec}} {{classname}}
{
public:
{{classname}}();
~{{classname}}();
void startService(int const& port);
void stopService();
explicit {{classname}}(std::shared_ptr<restbed::Service> const& restbedService);
virtual ~{{classname}}();

{{#operation}}
virtual void set{{classname}}{{vendorExtensions.x-codegen-resource-name}}Resource(std::shared_ptr<{{classname}}{{vendorExtensions.x-codegen-resource-name}}Resource> sp{{classname}}{{vendorExtensions.x-codegen-resource-name}}Resource);
{{/operation}}

virtual void publishDefaultResources();

virtual std::shared_ptr<restbed::Service> service();

protected:
{{#operation}}
std::shared_ptr<{{classname}}{{vendorExtensions.x-codegen-resource-name}}Resource> m_sp{{classname}}{{vendorExtensions.x-codegen-resource-name}}Resource;
{{/operation}}

private:
std::shared_ptr<restbed::Service> m_service;
};


Expand Down
Loading

0 comments on commit 0023f3b

Please sign in to comment.