-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
Json schema #4369
Json schema #4369
Changes from 1 commit
2b79e11
5a4e0f8
85edf5b
7a73168
352afa9
16e199e
146107f
8b6ce58
323294e
8ea50de
453e730
7d868a8
05aee5e
00100e3
c1ded2e
915e277
61bc633
4f3a461
1ac0859
4e600de
4875f3b
90e976e
2d2fb87
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,8 +53,9 @@ std::string GenNativeType(BaseType type) { | |
|
||
template <class T> std::string GenFullName(const T *enum_def) { | ||
std::string fullName; | ||
auto nameSpaces = enum_def->defined_namespace->components; | ||
for (auto const &ns : nameSpaces) { | ||
std::vector<std::string> nameSpaces = enum_def->defined_namespace->components; | ||
for (int nsindex = 0; nsindex < nameSpaces.size(); nsindex++) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Was auto in vs2010? I do not have a copy installed. |
||
std::string ns = nameSpaces[nsindex]; | ||
fullName.append(ns + "_"); | ||
} | ||
fullName.append(enum_def->name); | ||
|
@@ -88,7 +89,8 @@ std::string GenType(const Type &type) { | |
} | ||
case BASE_TYPE_UNION: { | ||
std::string unionTypes("\"anyOf\": ["); | ||
for (auto const &ut : type.enum_def->vals.vec) { | ||
for (int index = 0; index < type.enum_def->vals.vec.size(); ++index) { | ||
EnumVal* &ut = type.enum_def->vals.vec[index]; | ||
if (ut->union_type.base_type == BASE_TYPE_NONE) { | ||
continue; | ||
} | ||
|
@@ -129,11 +131,14 @@ class JsonSchemaGenerator : public BaseGenerator { | |
code_ += "{"; | ||
code_ += "\"$schema\": \"http://json-schema.org/draft-04/schema#\","; | ||
code_ += "\"definitions\": {"; | ||
for (auto &e : parser_.enums_.vec) { | ||
for (int index = 0; index < parser_.enums_.vec.size(); ++index) { | ||
EnumDef *e = parser_.enums_.vec[index]; | ||
code_ += " \"" + GenFullName(e) + "\" : {"; | ||
code_ += " " + GenType("string") + ","; | ||
std::string enumdef(" \"enum\": ["); | ||
for (auto &enumval : e->vals.vec) { | ||
|
||
for (int valindex = 0; valindex < e->vals.vec.size(); ++valindex) { | ||
EnumVal *enumval = e->vals.vec[valindex]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use auto where possible |
||
enumdef.append("\"" + enumval->name + "\""); | ||
if (&enumval != &e->vals.vec.back()) { | ||
enumdef.append(", "); | ||
|
@@ -143,7 +148,9 @@ class JsonSchemaGenerator : public BaseGenerator { | |
code_ += enumdef; | ||
code_ += " },"; // close type | ||
} | ||
for (auto &s : parser_.structs_.vec) { | ||
|
||
for (int structindex = 0; structindex < parser_.structs_.vec.size(); structindex++) { | ||
StructDef * const &s = parser_.structs_.vec[structindex]; | ||
code_ += ""; | ||
code_ += "\"" + GenFullName(s) + "\" : {"; | ||
code_ += " " + GenType("object") + ","; | ||
|
@@ -153,22 +160,25 @@ class JsonSchemaGenerator : public BaseGenerator { | |
} | ||
code_ += " \"description\" : \"" + comment + "\","; | ||
code_ += " \"properties\" : {"; | ||
for (auto const &prop : s->fields.vec) { | ||
|
||
for (int propindex = 0; propindex < s->fields.vec.size(); propindex++) { | ||
FieldDef* &prop = s->fields.vec[propindex]; | ||
std::string typeLine(" \"" + prop->name + "\" : { " + GenType(prop->value.type) + " }"); | ||
if (&prop != &s->fields.vec.back()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this if-else can be simplified :) |
||
typeLine.append(","); | ||
} | ||
code_ += typeLine; | ||
} | ||
auto props = s->fields.vec; | ||
std::vector<FieldDef*> props = s->fields.vec; | ||
std::vector<FieldDef *> requiredProperties; | ||
std::copy_if(props.begin(), props.end(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ooh fancy :) |
||
back_inserter(requiredProperties), | ||
[](FieldDef *prop) { return prop->required; }); | ||
if (requiredProperties.size() > 0) { | ||
code_ += " },"; // close properties | ||
std::string requiredString("\"required\" : [ "); | ||
for (const auto &reqProp : requiredProperties) { | ||
for (int reqindex = 0; reqindex < requiredProperties.size(); reqindex++) { | ||
FieldDef*const &reqProp = requiredProperties[reqindex]; | ||
requiredString.append("\"" + reqProp->name + "\""); | ||
if (&reqProp != &requiredProperties.back()) { | ||
requiredString.append(", "); | ||
|
@@ -194,8 +204,8 @@ class JsonSchemaGenerator : public BaseGenerator { | |
GenFullName(parser_.root_struct_def_) + "\""; | ||
|
||
code_ += "}"; // close schema root | ||
const auto file_path = GeneratedFileName(path_, file_name_); | ||
const auto final_code = code_.ToString(); | ||
const std::string file_path = GeneratedFileName(path_, file_name_); | ||
const std::string final_code = code_.ToString(); | ||
return SaveFile(file_path.c_str(), final_code, false); | ||
} | ||
}; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this making a copy? you can still use
auto &
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, use snake_case instead of camelCase