From 9bb88a026aa3423c2c97c5e74c60f404e62cc688 Mon Sep 17 00:00:00 2001 From: Tobias Oberstein Date: Fri, 27 Apr 2018 22:31:18 +0200 Subject: Include services in reflection data (fixes #4639) (#4713) * include service in reflection data (fixes #4639) * changes from review * regenerated test data --- src/flatc.cpp | 3 ++ src/idl_gen_grpc.cpp | 2 +- src/idl_parser.cpp | 84 +++++++++++++++++++++++++++++++++++++++++----------- 3 files changed, 71 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/flatc.cpp b/src/flatc.cpp index 8e4906f8..60fabd50 100644 --- a/src/flatc.cpp +++ b/src/flatc.cpp @@ -106,6 +106,7 @@ std::string FlatCompiler::GetUsageString(const char *program_name) const { " --grpc Generate GRPC interfaces for the specified languages\n" " --schema Serialize schemas instead of JSON (use with -b)\n" " --bfbs-comments Add doc comments to the binary schema files.\n" + " --bfbs-builtins Add builtin attributes to the binary schema files.\n" " --conform FILE Specify a schema the following schemas should be\n" " an evolution of. Gives errors if not.\n" " --conform-includes Include path for the schema given with --conform\n" @@ -253,6 +254,8 @@ int FlatCompiler::Compile(int argc, const char **argv) { grpc_enabled = true; } else if (arg == "--bfbs-comments") { opts.binary_schema_comments = true; + } else if (arg == "--bfbs-builtins") { + opts.binary_schema_builtins = true; } else if (arg == "--no-fb-import") { opts.skip_flatbuffers_import = true; } else if (arg == "--no-ts-reexport") { diff --git a/src/idl_gen_grpc.cpp b/src/idl_gen_grpc.cpp index 02994bda..b0159341 100644 --- a/src/idl_gen_grpc.cpp +++ b/src/idl_gen_grpc.cpp @@ -50,7 +50,7 @@ class FlatBufMethod : public grpc_generator::Method { grpc::string GetLeadingComments(const grpc::string) const { return ""; } grpc::string GetTrailingComments(const grpc::string) const { return ""; } std::vector GetAllComments() const { - return method_->rpc_comment; + return method_->doc_comment; } std::string name() const { return method_->name; } diff --git a/src/idl_parser.cpp b/src/idl_parser.cpp index c0fe5335..3102cd61 100644 --- a/src/idl_parser.cpp +++ b/src/idl_parser.cpp @@ -1724,7 +1724,7 @@ CheckedError Parser::ParseService() { ECHECK(ParseMetaData(&service_def.attributes)); EXPECT('{'); do { - std::vector rpc_comment = doc_comment_; + std::vector doc_comment = doc_comment_; auto rpc_name = attribute_; EXPECT(kTokenIdentifier); EXPECT('('); @@ -1740,7 +1740,7 @@ CheckedError Parser::ParseService() { rpc.name = rpc_name; rpc.request = reqtype.struct_def; rpc.response = resptype.struct_def; - rpc.rpc_comment = rpc_comment; + rpc.doc_comment = doc_comment; if (service_def.calls.Add(rpc_name, &rpc)) return Error("rpc already exists: " + rpc_name); ECHECK(ParseMetaData(&rpc.attributes)); @@ -2527,12 +2527,20 @@ void Parser::Serialize() { enum_offsets.push_back(offset); (*it)->serialized_location = offset.o; } + std::vector> service_offsets; + for (auto it = services_.vec.begin(); it != services_.vec.end(); ++it) { + auto offset = (*it)->Serialize(&builder_, *this); + service_offsets.push_back(offset); + (*it)->serialized_location = offset.o; + } auto schema_offset = reflection::CreateSchema( - builder_, builder_.CreateVectorOfSortedTables(&object_offsets), + builder_, + builder_.CreateVectorOfSortedTables(&object_offsets), builder_.CreateVectorOfSortedTables(&enum_offsets), builder_.CreateString(file_identifier_), builder_.CreateString(file_extension_), - root_struct_def_ ? root_struct_def_->serialized_location : 0); + (root_struct_def_ ? root_struct_def_->serialized_location : 0), + builder_.CreateVectorOfSortedTables(&service_offsets)); if (opts.size_prefixed) { builder_.FinishSizePrefixed(schema_offset, reflection::SchemaIdentifier()); } else { @@ -2549,9 +2557,12 @@ Offset StructDef::Serialize(FlatBufferBuilder *builder, } auto qualified_name = defined_namespace->GetFullyQualifiedName(name); return reflection::CreateObject( - *builder, builder->CreateString(qualified_name), - builder->CreateVectorOfSortedTables(&field_offsets), fixed, - static_cast(minalign), static_cast(bytesize), + *builder, + builder->CreateString(qualified_name), + builder->CreateVectorOfSortedTables(&field_offsets), + fixed, + static_cast(minalign), + static_cast(bytesize), SerializeAttributes(builder, parser), parser.opts.binary_schema_comments ? builder->CreateVectorOfStrings(doc_comment) @@ -2575,33 +2586,72 @@ Offset FieldDef::Serialize(FlatBufferBuilder *builder, // space by sharing it. Same for common values of value.type. } +Offset RPCCall::Serialize(FlatBufferBuilder *builder, + const Parser &parser) const { + return reflection::CreateRPCCall( + *builder, + builder->CreateString(name), + request->serialized_location, + response->serialized_location, + SerializeAttributes(builder, parser), + parser.opts.binary_schema_comments + ? builder->CreateVectorOfStrings(doc_comment) + : 0); +} + +Offset ServiceDef::Serialize(FlatBufferBuilder *builder, + const Parser &parser) const { + std::vector> servicecall_offsets; + for (auto it = calls.vec.begin(); it != calls.vec.end(); ++it) { + servicecall_offsets.push_back((*it)->Serialize(builder, parser)); + } + auto qualified_name = defined_namespace->GetFullyQualifiedName(name); + return reflection::CreateService( + *builder, + builder->CreateString(qualified_name), + builder->CreateVector(servicecall_offsets), + SerializeAttributes(builder, parser), + parser.opts.binary_schema_comments + ? builder->CreateVectorOfStrings(doc_comment) + : 0); +} + Offset EnumDef::Serialize(FlatBufferBuilder *builder, const Parser &parser) const { std::vector> enumval_offsets; for (auto it = vals.vec.begin(); it != vals.vec.end(); ++it) { - enumval_offsets.push_back((*it)->Serialize(builder)); + enumval_offsets.push_back((*it)->Serialize(builder, parser)); } auto qualified_name = defined_namespace->GetFullyQualifiedName(name); return reflection::CreateEnum( - *builder, builder->CreateString(qualified_name), - builder->CreateVector(enumval_offsets), is_union, - underlying_type.Serialize(builder), SerializeAttributes(builder, parser), + *builder, + builder->CreateString(qualified_name), + builder->CreateVector(enumval_offsets), + is_union, + underlying_type.Serialize(builder), + SerializeAttributes(builder, parser), parser.opts.binary_schema_comments ? builder->CreateVectorOfStrings(doc_comment) : 0); } -Offset EnumVal::Serialize( - FlatBufferBuilder *builder) const { +Offset EnumVal::Serialize(FlatBufferBuilder *builder, + const Parser &parser) const { return reflection::CreateEnumVal( - *builder, builder->CreateString(name), value, + *builder, + builder->CreateString(name), + value, union_type.struct_def ? union_type.struct_def->serialized_location : 0, - union_type.Serialize(builder)); + union_type.Serialize(builder), + parser.opts.binary_schema_comments + ? builder->CreateVectorOfStrings(doc_comment) + : 0); } Offset Type::Serialize(FlatBufferBuilder *builder) const { return reflection::CreateType( - *builder, static_cast(base_type), + *builder, + static_cast(base_type), static_cast(element), struct_def ? struct_def->index : (enum_def ? enum_def->index : -1)); } @@ -2614,7 +2664,7 @@ Definition::SerializeAttributes(FlatBufferBuilder *builder, for (auto kv = attributes.dict.begin(); kv != attributes.dict.end(); ++kv) { auto it = parser.known_attributes_.find(kv->first); FLATBUFFERS_ASSERT(it != parser.known_attributes_.end()); - if (!it->second) { // Custom attribute. + if (parser.opts.binary_schema_builtins || !it->second) { attrs.push_back(reflection::CreateKeyValue( *builder, builder->CreateString(kv->first), builder->CreateString(kv->second->constant))); -- cgit v1.2.3