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

Json schema #4369

Merged
merged 23 commits into from
Jul 10, 2017
Merged
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
2b79e11
Added empty generator for json schema (idl_gen_json_schema.cpp)
schoetbi Jun 23, 2017
5a4e0f8
JsonSchemaGenerator: output of tables implemented
schoetbi Jun 23, 2017
85edf5b
JsonSchemaGenerator: Corrected generation of typenames
schoetbi Jun 23, 2017
7a73168
JsonSchemaGenerator: Added generation of enum types
schoetbi Jun 23, 2017
352afa9
idl_gen_json_schema.cpp: Write required properties to schema
schoetbi Jun 27, 2017
16e199e
idl_gen_json_schema.cpp: Export Types including namespace
schoetbi Jun 27, 2017
146107f
idl_gen_json_schema.cpp: Fixed Json format
schoetbi Jun 28, 2017
8b6ce58
idl_gen_json_schema.cpp: Formatted according to google code style
schoetbi Jun 28, 2017
323294e
Checked in monster_test.bfbs with changes from master
schoetbi Jun 28, 2017
8ea50de
Added idl_gen_json_schema.cpp in CMakeLists.txt
schoetbi Jun 28, 2017
453e730
generate_code.bat: Added generation of json schema
schoetbi Jun 28, 2017
7d868a8
Added todo.md
schoetbi Jun 28, 2017
05aee5e
generate_code.sh: Added generation of json schema
schoetbi Jun 28, 2017
00100e3
Addressed some review issues
schoetbi Jul 3, 2017
c1ded2e
removed auto in idl_gen_json_schema.cpp
schoetbi Jul 3, 2017
915e277
idl_gen_json_schema.cpp: changed iterator declarations to auto
schoetbi Jul 4, 2017
61bc633
deleted todo.md
schoetbi Jul 4, 2017
4f3a461
idl_gen_json_schema.cpp: Removed keyword "override" so that vs2010 ca…
schoetbi Jul 6, 2017
1ac0859
idl_gen_json_schema.cpp: switch statement in GenType handeles all enu…
schoetbi Jul 7, 2017
4e600de
idl_gen_json_schema.cpp: Removed cerr output
schoetbi Jul 10, 2017
4875f3b
idl_gen_json_schema.cpp: Avoid vector copying
schoetbi Jul 10, 2017
90e976e
idl_gen_json_schema.cpp: Fixed identation of json schema output
schoetbi Jul 10, 2017
2d2fb87
idl_gen_json_schema.cpp: Do not output empty descriptions
schoetbi Jul 10, 2017
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
32 changes: 21 additions & 11 deletions src/idl_gen_json_schema.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Collaborator

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 &

Copy link
Collaborator

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

for (int nsindex = 0; nsindex < nameSpaces.size(); nsindex++) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not for (auto it = namespaces.begin(); ... ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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);
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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];
Copy link
Collaborator

Choose a reason for hiding this comment

The 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(", ");
Expand All @@ -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") + ",";
Expand All @@ -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()) {
Copy link
Collaborator

Choose a reason for hiding this comment

The 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(),
Copy link
Collaborator

Choose a reason for hiding this comment

The 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(", ");
Expand All @@ -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);
}
};
Expand Down