Skip to content

Commit

Permalink
[C++][RESTSDK] support enums (#2749)
Browse files Browse the repository at this point in the history
* Support enums

* Updating petstore sample

* Use enum class instead of just enum

* Use string_t for g++ compatibility

* Add enum descriptions

* Fix string parsing. Make g++ compatible.
  • Loading branch information
dan-drl authored and etherealjoy committed May 1, 2019
1 parent 26c493b commit 40759a5
Show file tree
Hide file tree
Showing 14 changed files with 166 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,48 @@
namespace {{this}} {
{{/modelNamespaceDeclarations}}

{{#isEnum}}
class {{declspec}} {{classname}}
: public {{#parent}}{{{parent}}}{{/parent}}{{^parent}}ModelBase{{/parent}}
{
public:
{{classname}}();
virtual ~{{classname}}();

/////////////////////////////////////////////
/// ModelBase overrides

void validate() override;

web::json::value toJson() const override;
void fromJson(const web::json::value& json) override;

void toMultipart(std::shared_ptr<MultipartFormData> multipart, const utility::string_t& namePrefix) const override;
void fromMultiPart(std::shared_ptr<MultipartFormData> multipart, const utility::string_t& namePrefix) override;

enum class e{{classname}}
{
{{#allowableValues}}
{{#values}}
{{#enumDescription}}
/// <summary>
/// {{enumDescription}}
/// </summary>
{{/enumDescription}}
{{classname}}_{{.}}{{^last}},{{/last}}
{{/values}}
{{/allowableValues}}
};

e{{classname}} getValue() const;
void setValue(e{{classname}} const value);

protected:
e{{classname}} m_value;
};
{{/isEnum}}
{{^isEnum}}

/// <summary>
/// {{description}}
/// </summary>
Expand Down Expand Up @@ -75,6 +117,8 @@ protected:
{{/vars}}
};

{{/isEnum}}

{{#modelNamespaceDeclarations}}
}
{{/modelNamespaceDeclarations}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,90 @@
namespace {{this}} {
{{/modelNamespaceDeclarations}}


{{#isEnum}}

{{classname}}::{{classname}}()
{
}

{{classname}}::~{{classname}}()
{
}

void {{classname}}::validate()
{
// TODO: implement validation
}

web::json::value {{classname}}::toJson() const
{
web::json::value val = web::json::value::object();
{{#allowableValues}}{{#values}}
if (m_value == e{{classname}}::{{classname}}_{{.}}) val = web::json::value::string(U("{{.}}"));{{/values}}{{/allowableValues}}

return val;
}

void {{classname}}::fromJson(const web::json::value& val)
{
auto s = val.as_string();
{{#allowableValues}}{{#values}}
if (s == utility::conversions::to_string_t("{{.}}")) m_value = e{{classname}}::{{classname}}_{{.}};{{/values}}{{/allowableValues}}
}

void {{classname}}::toMultipart(std::shared_ptr<MultipartFormData> multipart, const utility::string_t& prefix) const
{
utility::string_t namePrefix = prefix;
if(namePrefix.size() > 0 && namePrefix.substr(namePrefix.size() - 1) != utility::conversions::to_string_t("."))
{
namePrefix += utility::conversions::to_string_t(".");
}

utility::string_t s;

{{#allowableValues}}{{#values}}
if (m_value == e{{classname}}::{{classname}}_{{.}}) s = utility::conversions::to_string_t("{{.}}");{{/values}}{{/allowableValues}}

multipart->add(ModelBase::toHttpContent(namePrefix, s));
}

void {{classname}}::fromMultiPart(std::shared_ptr<MultipartFormData> multipart, const utility::string_t& prefix)
{
utility::string_t namePrefix = prefix;
if(namePrefix.size() > 0 && namePrefix.substr(namePrefix.size() - 1) != utility::conversions::to_string_t("."))
{
namePrefix += utility::conversions::to_string_t(".");
}

{
utility::string_t s;
s = ModelBase::stringFromHttpContent(multipart->getContent(namePrefix));
e{{classname}} v;

{{#allowableValues}}{{#values}}
if (s == utility::conversions::to_string_t("{{.}}")) v = e{{classname}}::{{classname}}_{{.}};{{/values}}{{/allowableValues}}

setValue(v);
}
}

{{classname}}::e{{classname}} {{classname}}::getValue() const
{
return m_value;
}

void {{classname}}::setValue({{classname}}::e{{classname}} const value)
{
m_value = value;
}

{{/isEnum}}

{{^isEnum}}

{{classname}}::{{classname}}()
{
{{#vars}}
Expand Down Expand Up @@ -629,9 +713,11 @@ void {{classname}}::unset{{name}}()
{{/required}}
{{/isInherited}}
{{/vars}}
{{/isEnum}}
{{#modelNamespaceDeclarations}}
}
{{/modelNamespaceDeclarations}}


{{/model}}
{{/models}}
4 changes: 4 additions & 0 deletions samples/client/petstore/cpp-restsdk/model/ApiResponse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ namespace openapitools {
namespace client {
namespace model {




ApiResponse::ApiResponse()
{
m_Code = 0;
Expand Down Expand Up @@ -197,3 +200,4 @@ void ApiResponse::unsetMessage()
}
}


2 changes: 2 additions & 0 deletions samples/client/petstore/cpp-restsdk/model/ApiResponse.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ namespace openapitools {
namespace client {
namespace model {


/// <summary>
/// Describes the result of uploading an image resource
/// </summary>
Expand Down Expand Up @@ -89,6 +90,7 @@ class ApiResponse
bool m_MessageIsSet;
};


}
}
}
Expand Down
4 changes: 4 additions & 0 deletions samples/client/petstore/cpp-restsdk/model/Category.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ namespace openapitools {
namespace client {
namespace model {




Category::Category()
{
m_Id = 0L;
Expand Down Expand Up @@ -154,3 +157,4 @@ void Category::unsetName()
}
}


2 changes: 2 additions & 0 deletions samples/client/petstore/cpp-restsdk/model/Category.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ namespace openapitools {
namespace client {
namespace model {


/// <summary>
/// A category for a pet
/// </summary>
Expand Down Expand Up @@ -78,6 +79,7 @@ class Category
bool m_NameIsSet;
};


}
}
}
Expand Down
4 changes: 4 additions & 0 deletions samples/client/petstore/cpp-restsdk/model/Order.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ namespace openapitools {
namespace client {
namespace model {




Order::Order()
{
m_Id = 0L;
Expand Down Expand Up @@ -326,3 +329,4 @@ void Order::unsetComplete()
}
}


2 changes: 2 additions & 0 deletions samples/client/petstore/cpp-restsdk/model/Order.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ namespace openapitools {
namespace client {
namespace model {


/// <summary>
/// An order for a pets from the pet store
/// </summary>
Expand Down Expand Up @@ -122,6 +123,7 @@ class Order
bool m_CompleteIsSet;
};


}
}
}
Expand Down
4 changes: 4 additions & 0 deletions samples/client/petstore/cpp-restsdk/model/Pet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ namespace openapitools {
namespace client {
namespace model {




Pet::Pet()
{
m_Id = 0L;
Expand Down Expand Up @@ -352,3 +355,4 @@ void Pet::unsetStatus()
}
}


2 changes: 2 additions & 0 deletions samples/client/petstore/cpp-restsdk/model/Pet.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ namespace openapitools {
namespace client {
namespace model {


/// <summary>
/// A pet for sale in the pet store
/// </summary>
Expand Down Expand Up @@ -119,6 +120,7 @@ class Pet
bool m_StatusIsSet;
};


}
}
}
Expand Down
4 changes: 4 additions & 0 deletions samples/client/petstore/cpp-restsdk/model/Tag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ namespace openapitools {
namespace client {
namespace model {




Tag::Tag()
{
m_Id = 0L;
Expand Down Expand Up @@ -154,3 +157,4 @@ void Tag::unsetName()
}
}


2 changes: 2 additions & 0 deletions samples/client/petstore/cpp-restsdk/model/Tag.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ namespace openapitools {
namespace client {
namespace model {


/// <summary>
/// A tag for a pet
/// </summary>
Expand Down Expand Up @@ -78,6 +79,7 @@ class Tag
bool m_NameIsSet;
};


}
}
}
Expand Down
4 changes: 4 additions & 0 deletions samples/client/petstore/cpp-restsdk/model/User.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ namespace openapitools {
namespace client {
namespace model {




User::User()
{
m_Id = 0L;
Expand Down Expand Up @@ -412,3 +415,4 @@ void User::unsetUserStatus()
}
}


2 changes: 2 additions & 0 deletions samples/client/petstore/cpp-restsdk/model/User.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ namespace openapitools {
namespace client {
namespace model {


/// <summary>
/// A User who is purchasing from the pet store
/// </summary>
Expand Down Expand Up @@ -144,6 +145,7 @@ class User
bool m_UserStatusIsSet;
};


}
}
}
Expand Down

0 comments on commit 40759a5

Please sign in to comment.