-
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
Conversation
current problems: - typenames are not correct - array types need to be deduced google#4360
Current problems: enum types not written correctly google#4360
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.
Looks great! just a few nits.
src/flatc_main.cpp
Outdated
@@ -96,7 +96,12 @@ int main(int argc, const char *argv[]) { | |||
flatbuffers::IDLOptions::kPhp, | |||
"Generate PHP files for tables/structs", | |||
flatbuffers::GeneralMakeRule }, | |||
}; | |||
{ flatbuffers::GenerateJsonSchema, "-S", "--jsonschema", "JsonSchema", true, |
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.
lets leave out the -S
?
src/idl_gen_json_schema.cpp
Outdated
return "number"; | ||
case BASE_TYPE_STRING: | ||
return "string"; | ||
default: { return "?"; } |
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.
is ?
a correct way to do this, or are we missing types?
src/idl_gen_json_schema.cpp
Outdated
} | ||
|
||
template <class T> | ||
std::string GenFullName(const T *enum_def) { |
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.
Try to follow the style of the rest of the code-base, thus template
on same line.
src/idl_gen_json_schema.cpp
Outdated
for (auto const &ns : nameSpaces) { | ||
fullName << ns << "_"; | ||
} | ||
|
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.
no need for blank line, here and elsewhere
src/idl_gen_json_schema.cpp
Outdated
std::string GenType(const Type &type) { | ||
switch (type.base_type) { | ||
case BASE_TYPE_VECTOR: { | ||
std::stringstream typeline; |
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.
not that I mind too much, but doesn't seem using stringstream
has any advantages here over using stting
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.
I thought it would but you are right it gives no benefits. See here: http://quick-bench.com/Xh4YAery7pHuO_AIg_ElnqTc3RU
I will remove them
src/idl_gen_json_schema.cpp
Outdated
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 comment
The reason will be displayed to describe this comment to others. Learn more.
this if-else can be simplified :)
src/idl_gen_json_schema.cpp
Outdated
|
||
auto props = s->fields.vec; | ||
std::vector<flatbuffers::FieldDef *> requiredProperties; | ||
std::copy_if(props.begin(), props.end(), |
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.
ooh fancy :)
src/idl_gen_json_schema.cpp
Outdated
code_ += " }"; // close properties | ||
} | ||
|
||
if (&s != &parser_.structs_.vec.back()) { |
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.
simplify
tests/monster_test.schema.json
Outdated
"definitions": { | ||
|
||
"MyGame_OtherNameSpace_FromInclude" : { | ||
"type" : "string", |
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.
maybe nice to add some indentation, for readability?
todo.md
Outdated
@@ -0,0 +1,11 @@ | |||
# TODOs for json schema generation |
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.
I don't think we want a todo in the root. Can you instead add this to a github issue?
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.
I planned to remove as soon as everything is done. Any thoughts on the enum with bit_flag problem? What is expected to be in the json. The name of the enum (=string) or a number? String is only possilbe when one flag is true so I assume that a number should be the right thing?
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.
bit_flags should be handled just like regular enums (they are numbers). all bit_flags
does is assign those numbers differently
Note you have some CI errors to address. |
- removed command line arg -S - removed new lines - fixed codestyle in template functions - removed usage of stringstream - idented json schema google#4360
src/idl_gen_json_schema.cpp
Outdated
@@ -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; |
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
src/idl_gen_json_schema.cpp
Outdated
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Why not for (auto it = namespaces.begin(); ...
?
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.
Was auto in vs2010? I do not have a copy installed.
src/idl_gen_json_schema.cpp
Outdated
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 comment
The reason will be displayed to describe this comment to others. Learn more.
use auto where possible
The CI runs through VS2010, so if it is happy, you know it works there. And yes, VS2010 supports auto and a partial set of other C++11 features. |
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.
Looking good, some minor comments
src/idl_gen_json_schema.cpp
Outdated
case BASE_TYPE_STRING: | ||
return "string"; | ||
default: | ||
std::cerr << "GenNativeType: Invalid base type " << type << "\n"; |
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.
There is no code anywhere else that outputs to cerr
, so this is not a good error handling mechanism. It be better to simply handle all types in some way. What happens with e.g. BASE_TYPE_STRUCT
, which is equivalent to a JSON object?
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.
It should never go there. So I removed the cerr part and return ""
src/idl_gen_json_schema.cpp
Outdated
|
||
template <class T> std::string GenFullName(const T *enum_def) { | ||
std::string full_name; | ||
std::vector<std::string> name_spaces = enum_def->defined_namespace->components; |
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 vector copied?
tests/monster_test.schema.json
Outdated
@@ -0,0 +1,120 @@ | |||
{ | |||
"$schema": "http://json-schema.org/draft-04/schema#", |
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.
indent these 2 lines?
tests/monster_test.schema.json
Outdated
|
||
"MyGame_Example_Test" : { | ||
"type" : "object", | ||
"description" : "", |
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.
if description is empty, should it maybe be left out to reduce clutter? Same of any other fields that are optional?
Thanks! |
Was a pleasure. |
New command line option jsonschema
--jsonschema Generate Json schema.
to generate a json schema.