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

Json schema #4369

merged 23 commits into from
Jul 10, 2017

Conversation

schoetbi
Copy link
Contributor

@schoetbi schoetbi commented Jun 28, 2017

New command line option jsonschema
--jsonschema Generate Json schema.

to generate a json schema.

Copy link
Collaborator

@aardappel aardappel left a 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.

@@ -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,
Copy link
Collaborator

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 ?

return "number";
case BASE_TYPE_STRING:
return "string";
default: { return "?"; }
Copy link
Collaborator

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?

}

template <class T>
std::string GenFullName(const T *enum_def) {
Copy link
Collaborator

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.

for (auto const &ns : nameSpaces) {
fullName << ns << "_";
}

Copy link
Collaborator

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

std::string GenType(const Type &type) {
switch (type.base_type) {
case BASE_TYPE_VECTOR: {
std::stringstream typeline;
Copy link
Collaborator

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

Copy link
Contributor Author

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

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 :)


auto props = s->fields.vec;
std::vector<flatbuffers::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 :)

code_ += " }"; // close properties
}

if (&s != &parser_.structs_.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.

simplify

"definitions": {

"MyGame_OtherNameSpace_FromInclude" : {
"type" : "string",
Copy link
Collaborator

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
Copy link
Collaborator

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?

Copy link
Contributor Author

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?

Copy link
Collaborator

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

@aardappel
Copy link
Collaborator

Note you have some CI errors to address.

schoetbi added 2 commits July 3, 2017 14:04
- removed command line arg -S
- removed new lines
- fixed codestyle in template functions
- removed usage of stringstream
- idented json schema
google#4360
@@ -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

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++) {
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.

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

@aardappel
Copy link
Collaborator

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.

Copy link
Collaborator

@aardappel aardappel left a 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

case BASE_TYPE_STRING:
return "string";
default:
std::cerr << "GenNativeType: Invalid base type " << type << "\n";
Copy link
Collaborator

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?

Copy link
Contributor Author

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 ""


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;
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 vector copied?

@@ -0,0 +1,120 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
Copy link
Collaborator

Choose a reason for hiding this comment

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

indent these 2 lines?


"MyGame_Example_Test" : {
"type" : "object",
"description" : "",
Copy link
Collaborator

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?

@aardappel
Copy link
Collaborator

Thanks!

@aardappel aardappel merged commit f202041 into google:master Jul 10, 2017
@schoetbi
Copy link
Contributor Author

Was a pleasure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants