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

[cpprestsdk] fix string conversion, add integer enum support #10531

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,55 @@ namespace {{this}} {

{{#isEnum}}

namespace
{
using EnumUnderlyingType = {{#isNumeric}}int64_t{{/isNumeric}}{{^isNumeric}}utility::string_t{{/isNumeric}};

{{classname}}::e{{classname}} toEnum(const EnumUnderlyingType& val)
{
{{#allowableValues}}
{{#isNumeric}}
switch (val)
{
{{#enumVars}}
case {{value}}:
return {{classname}}::e{{classname}}::{{classname}}_{{name}};
{{#-last}}
default:
break;
{{/-last}}
{{/enumVars}}
}
{{/isNumeric}}
{{^isNumeric}}
{{#enumVars}}
if (val == utility::conversions::to_string_t(U("{{{value}}}")))
return {{classname}}::e{{classname}}::{{classname}}_{{name}};
{{/enumVars}}
{{/isNumeric}}
{{/allowableValues}}
return {};
}

EnumUnderlyingType fromEnum({{classname}}::e{{classname}} e)
{
{{#allowableValues}}
switch (e)
{
{{#enumVars}}
case {{classname}}::e{{classname}}::{{classname}}_{{name}}:
return {{#isNumeric}}{{value}}{{/isNumeric}}{{^isNumeric}}U("{{value}}"){{/isNumeric}};
{{#-last}}
default:
break;
{{/-last}}
{{/enumVars}}
}
{{/allowableValues}}
return {};
}
}

{{classname}}::{{classname}}()
{
}
Expand All @@ -25,56 +74,44 @@ void {{classname}}::validate()

web::json::value {{classname}}::toJson() const
{
web::json::value val = web::json::value::object();

{{#allowableValues}}{{#enumVars}}
if (m_value == e{{classname}}::{{classname}}_{{name}}) val = web::json::value::string(U({{{value}}}));{{/enumVars}}{{/allowableValues}}

return val;
auto val = fromEnum(m_value);
return web::json::value(val);
}

bool {{classname}}::fromJson(const web::json::value& val)
{
auto s = val.as_string();

{{#allowableValues}}{{#enumVars}}
if (s == utility::conversions::to_string_t({{{value}}})) m_value = e{{classname}}::{{classname}}_{{name}};{{/enumVars}}{{/allowableValues}}
m_value = toEnum({{#isNumeric}}val.as_number().to_int64(){{/isNumeric}}{{^isNumeric}}val.as_string(){{/isNumeric}});
return true;
}

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("."))
if (!namePrefix.empty() && namePrefix.back() != U('.'))
{
namePrefix += utility::conversions::to_string_t(".");
namePrefix.push_back(U('.'));
}

utility::string_t s;

{{#allowableValues}}{{#enumVars}}
if (m_value == e{{classname}}::{{classname}}_{{name}}) s = utility::conversions::to_string_t({{{value}}});{{/enumVars}}{{/allowableValues}}

multipart->add(ModelBase::toHttpContent(namePrefix, s));
auto e = fromEnum(m_value);
multipart->add(ModelBase::toHttpContent(namePrefix, e));
}

bool {{classname}}::fromMultiPart(std::shared_ptr<MultipartFormData> multipart, const utility::string_t& prefix)
{
bool ok = true;
utility::string_t namePrefix = prefix;
if(namePrefix.size() > 0 && namePrefix.substr(namePrefix.size() - 1) != utility::conversions::to_string_t("."))
if (!namePrefix.empty() && namePrefix.back() != U('.'))
{
namePrefix += utility::conversions::to_string_t(".");
namePrefix.push_back(U('.'));
}
{
utility::string_t s;
ok = ModelBase::fromHttpContent(multipart->getContent(namePrefix), s);
e{{classname}} v;

{{#allowableValues}}{{#enumVars}}
if (s == utility::conversions::to_string_t({{{value}}})) v = e{{classname}}::{{classname}}_{{name}};{{/enumVars}}{{/allowableValues}}

setValue(v);
EnumUnderlyingType e;
ok = ModelBase::fromHttpContent(multipart->getContent(namePrefix), e);
if (ok)
{
auto v = toEnum(e);
setValue(v);
}
}
return ok;
}
Expand Down Expand Up @@ -134,7 +171,7 @@ web::json::value {{classname}}::toJson() const
{{#vars}}{{^isInherited}}
if(m_{{name}}IsSet)
{
val[utility::conversions::to_string_t("{{baseName}}")] = ModelBase::toJson(m_{{name}});
val[utility::conversions::to_string_t(U("{{baseName}}"))] = ModelBase::toJson(m_{{name}});
}{{/isInherited}}{{/vars}}

return val;
Expand All @@ -147,9 +184,9 @@ bool {{classname}}::fromJson(const web::json::value& val)
ok &= this->{{{.}}}::fromJson(val);
{{/parent}}
{{#vars}}{{^isInherited}}
if(val.has_field(utility::conversions::to_string_t("{{baseName}}")))
if(val.has_field(utility::conversions::to_string_t(U("{{baseName}}"))))
{
const web::json::value& fieldValue = val.at(utility::conversions::to_string_t("{{baseName}}"));
const web::json::value& fieldValue = val.at(utility::conversions::to_string_t(U("{{baseName}}")));
if(!fieldValue.is_null())
{
{{{dataType}}} refVal_{{baseName}};
Expand All @@ -163,14 +200,14 @@ bool {{classname}}::fromJson(const web::json::value& val)
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("."))
if(namePrefix.size() > 0 && namePrefix.substr(namePrefix.size() - 1) != utility::conversions::to_string_t(U(".")))
{
namePrefix += utility::conversions::to_string_t(".");
namePrefix += utility::conversions::to_string_t(U("."));
}
{{#vars}}
if(m_{{name}}IsSet)
{
multipart->add(ModelBase::toHttpContent(namePrefix + utility::conversions::to_string_t("{{baseName}}"), m_{{name}}));
multipart->add(ModelBase::toHttpContent(namePrefix + utility::conversions::to_string_t(U("{{baseName}}")), m_{{name}}));
}
{{/vars}}
}
Expand All @@ -179,16 +216,16 @@ bool {{classname}}::fromMultiPart(std::shared_ptr<MultipartFormData> multipart,
{
bool ok = true;
utility::string_t namePrefix = prefix;
if(namePrefix.size() > 0 && namePrefix.substr(namePrefix.size() - 1) != utility::conversions::to_string_t("."))
if(namePrefix.size() > 0 && namePrefix.substr(namePrefix.size() - 1) != utility::conversions::to_string_t(U(".")))
{
namePrefix += utility::conversions::to_string_t(".");
namePrefix += utility::conversions::to_string_t(U("."));
}

{{#vars}}
if(multipart->hasContent(utility::conversions::to_string_t("{{baseName}}")))
if(multipart->hasContent(utility::conversions::to_string_t(U("{{baseName}}"))))
{
{{{dataType}}} refVal_{{baseName}};
ok &= ModelBase::fromHttpContent(multipart->getContent(utility::conversions::to_string_t("{{baseName}}")), refVal_{{baseName}} );
ok &= ModelBase::fromHttpContent(multipart->getContent(utility::conversions::to_string_t(U("{{baseName}}"))), refVal_{{baseName}} );
{{setter}}(refVal_{{baseName}});
}
{{/vars}}
Expand Down
44 changes: 22 additions & 22 deletions samples/client/petstore/cpp-restsdk/client/model/ApiResponse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ web::json::value ApiResponse::toJson() const

if(m_CodeIsSet)
{
val[utility::conversions::to_string_t("code")] = ModelBase::toJson(m_Code);
val[utility::conversions::to_string_t(U("code"))] = ModelBase::toJson(m_Code);
}
if(m_TypeIsSet)
{
val[utility::conversions::to_string_t("type")] = ModelBase::toJson(m_Type);
val[utility::conversions::to_string_t(U("type"))] = ModelBase::toJson(m_Type);
}
if(m_MessageIsSet)
{
val[utility::conversions::to_string_t("message")] = ModelBase::toJson(m_Message);
val[utility::conversions::to_string_t(U("message"))] = ModelBase::toJson(m_Message);
}

return val;
Expand All @@ -65,29 +65,29 @@ bool ApiResponse::fromJson(const web::json::value& val)
{
bool ok = true;

if(val.has_field(utility::conversions::to_string_t("code")))
if(val.has_field(utility::conversions::to_string_t(U("code"))))
{
const web::json::value& fieldValue = val.at(utility::conversions::to_string_t("code"));
const web::json::value& fieldValue = val.at(utility::conversions::to_string_t(U("code")));
if(!fieldValue.is_null())
{
int32_t refVal_code;
ok &= ModelBase::fromJson(fieldValue, refVal_code);
setCode(refVal_code);
}
}
if(val.has_field(utility::conversions::to_string_t("type")))
if(val.has_field(utility::conversions::to_string_t(U("type"))))
{
const web::json::value& fieldValue = val.at(utility::conversions::to_string_t("type"));
const web::json::value& fieldValue = val.at(utility::conversions::to_string_t(U("type")));
if(!fieldValue.is_null())
{
utility::string_t refVal_type;
ok &= ModelBase::fromJson(fieldValue, refVal_type);
setType(refVal_type);
}
}
if(val.has_field(utility::conversions::to_string_t("message")))
if(val.has_field(utility::conversions::to_string_t(U("message"))))
{
const web::json::value& fieldValue = val.at(utility::conversions::to_string_t("message"));
const web::json::value& fieldValue = val.at(utility::conversions::to_string_t(U("message")));
if(!fieldValue.is_null())
{
utility::string_t refVal_message;
Expand All @@ -101,49 +101,49 @@ bool ApiResponse::fromJson(const web::json::value& val)
void ApiResponse::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("."))
if(namePrefix.size() > 0 && namePrefix.substr(namePrefix.size() - 1) != utility::conversions::to_string_t(U(".")))
{
namePrefix += utility::conversions::to_string_t(".");
namePrefix += utility::conversions::to_string_t(U("."));
}
if(m_CodeIsSet)
{
multipart->add(ModelBase::toHttpContent(namePrefix + utility::conversions::to_string_t("code"), m_Code));
multipart->add(ModelBase::toHttpContent(namePrefix + utility::conversions::to_string_t(U("code")), m_Code));
}
if(m_TypeIsSet)
{
multipart->add(ModelBase::toHttpContent(namePrefix + utility::conversions::to_string_t("type"), m_Type));
multipart->add(ModelBase::toHttpContent(namePrefix + utility::conversions::to_string_t(U("type")), m_Type));
}
if(m_MessageIsSet)
{
multipart->add(ModelBase::toHttpContent(namePrefix + utility::conversions::to_string_t("message"), m_Message));
multipart->add(ModelBase::toHttpContent(namePrefix + utility::conversions::to_string_t(U("message")), m_Message));
}
}

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

if(multipart->hasContent(utility::conversions::to_string_t("code")))
if(multipart->hasContent(utility::conversions::to_string_t(U("code"))))
{
int32_t refVal_code;
ok &= ModelBase::fromHttpContent(multipart->getContent(utility::conversions::to_string_t("code")), refVal_code );
ok &= ModelBase::fromHttpContent(multipart->getContent(utility::conversions::to_string_t(U("code"))), refVal_code );
setCode(refVal_code);
}
if(multipart->hasContent(utility::conversions::to_string_t("type")))
if(multipart->hasContent(utility::conversions::to_string_t(U("type"))))
{
utility::string_t refVal_type;
ok &= ModelBase::fromHttpContent(multipart->getContent(utility::conversions::to_string_t("type")), refVal_type );
ok &= ModelBase::fromHttpContent(multipart->getContent(utility::conversions::to_string_t(U("type"))), refVal_type );
setType(refVal_type);
}
if(multipart->hasContent(utility::conversions::to_string_t("message")))
if(multipart->hasContent(utility::conversions::to_string_t(U("message"))))
{
utility::string_t refVal_message;
ok &= ModelBase::fromHttpContent(multipart->getContent(utility::conversions::to_string_t("message")), refVal_message );
ok &= ModelBase::fromHttpContent(multipart->getContent(utility::conversions::to_string_t(U("message"))), refVal_message );
setMessage(refVal_message);
}
return ok;
Expand Down
32 changes: 16 additions & 16 deletions samples/client/petstore/cpp-restsdk/client/model/Category.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ web::json::value Category::toJson() const

if(m_IdIsSet)
{
val[utility::conversions::to_string_t("id")] = ModelBase::toJson(m_Id);
val[utility::conversions::to_string_t(U("id"))] = ModelBase::toJson(m_Id);
}
if(m_NameIsSet)
{
val[utility::conversions::to_string_t("name")] = ModelBase::toJson(m_Name);
val[utility::conversions::to_string_t(U("name"))] = ModelBase::toJson(m_Name);
}

return val;
Expand All @@ -59,19 +59,19 @@ bool Category::fromJson(const web::json::value& val)
{
bool ok = true;

if(val.has_field(utility::conversions::to_string_t("id")))
if(val.has_field(utility::conversions::to_string_t(U("id"))))
{
const web::json::value& fieldValue = val.at(utility::conversions::to_string_t("id"));
const web::json::value& fieldValue = val.at(utility::conversions::to_string_t(U("id")));
if(!fieldValue.is_null())
{
int64_t refVal_id;
ok &= ModelBase::fromJson(fieldValue, refVal_id);
setId(refVal_id);
}
}
if(val.has_field(utility::conversions::to_string_t("name")))
if(val.has_field(utility::conversions::to_string_t(U("name"))))
{
const web::json::value& fieldValue = val.at(utility::conversions::to_string_t("name"));
const web::json::value& fieldValue = val.at(utility::conversions::to_string_t(U("name")));
if(!fieldValue.is_null())
{
utility::string_t refVal_name;
Expand All @@ -85,39 +85,39 @@ bool Category::fromJson(const web::json::value& val)
void Category::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("."))
if(namePrefix.size() > 0 && namePrefix.substr(namePrefix.size() - 1) != utility::conversions::to_string_t(U(".")))
{
namePrefix += utility::conversions::to_string_t(".");
namePrefix += utility::conversions::to_string_t(U("."));
}
if(m_IdIsSet)
{
multipart->add(ModelBase::toHttpContent(namePrefix + utility::conversions::to_string_t("id"), m_Id));
multipart->add(ModelBase::toHttpContent(namePrefix + utility::conversions::to_string_t(U("id")), m_Id));
}
if(m_NameIsSet)
{
multipart->add(ModelBase::toHttpContent(namePrefix + utility::conversions::to_string_t("name"), m_Name));
multipart->add(ModelBase::toHttpContent(namePrefix + utility::conversions::to_string_t(U("name")), m_Name));
}
}

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

if(multipart->hasContent(utility::conversions::to_string_t("id")))
if(multipart->hasContent(utility::conversions::to_string_t(U("id"))))
{
int64_t refVal_id;
ok &= ModelBase::fromHttpContent(multipart->getContent(utility::conversions::to_string_t("id")), refVal_id );
ok &= ModelBase::fromHttpContent(multipart->getContent(utility::conversions::to_string_t(U("id"))), refVal_id );
setId(refVal_id);
}
if(multipart->hasContent(utility::conversions::to_string_t("name")))
if(multipart->hasContent(utility::conversions::to_string_t(U("name"))))
{
utility::string_t refVal_name;
ok &= ModelBase::fromHttpContent(multipart->getContent(utility::conversions::to_string_t("name")), refVal_name );
ok &= ModelBase::fromHttpContent(multipart->getContent(utility::conversions::to_string_t(U("name"))), refVal_name );
setName(refVal_name);
}
return ok;
Expand Down
Loading