add option to allow writing the prefix as well

This commit is contained in:
Robert Schmidtke
2017-10-02 17:31:05 +02:00
parent f481bf3542
commit 081a8a1c24
3 changed files with 17 additions and 3 deletions

View File

@@ -376,6 +376,7 @@ struct IDLOptions {
std::string go_namespace; std::string go_namespace;
bool reexport_ts_modules; bool reexport_ts_modules;
bool protobuf_ascii_alike; bool protobuf_ascii_alike;
bool prefix_size;
// Possible options for the more general generator below. // Possible options for the more general generator below.
enum Language { enum Language {
@@ -428,6 +429,7 @@ struct IDLOptions {
skip_flatbuffers_import(false), skip_flatbuffers_import(false),
reexport_ts_modules(true), reexport_ts_modules(true),
protobuf_ascii_alike(false), protobuf_ascii_alike(false),
prefix_size(false),
lang(IDLOptions::kJava), lang(IDLOptions::kJava),
mini_reflect(IDLOptions::kNone), mini_reflect(IDLOptions::kNone),
lang_to_generate(0) {} lang_to_generate(0) {}

View File

@@ -113,6 +113,7 @@ std::string FlatCompiler::GetUsageString(const char* program_name) const {
" --no-ts-reexport Don't re-export imported dependencies for TypeScript.\n" " --no-ts-reexport Don't re-export imported dependencies for TypeScript.\n"
" --reflect-types Add minimal type reflection to code generation.\n" " --reflect-types Add minimal type reflection to code generation.\n"
" --reflect-names Add minimal type/name reflection.\n" " --reflect-names Add minimal type/name reflection.\n"
" --prefix-size Prefix size field to generated buffers.\n"
"FILEs may be schemas (must end in .fbs), or JSON files (conforming to preceding\n" "FILEs may be schemas (must end in .fbs), or JSON files (conforming to preceding\n"
"schema). FILEs after the -- must be binary flatbuffer format files.\n" "schema). FILEs after the -- must be binary flatbuffer format files.\n"
"Output files are named using the base file name of the input,\n" "Output files are named using the base file name of the input,\n"
@@ -249,6 +250,8 @@ int FlatCompiler::Compile(int argc, const char** argv) {
opts.mini_reflect = IDLOptions::kTypes; opts.mini_reflect = IDLOptions::kTypes;
} else if(arg == "--reflect-names") { } else if(arg == "--reflect-names") {
opts.mini_reflect = IDLOptions::kTypesAndNames; opts.mini_reflect = IDLOptions::kTypesAndNames;
} else if(arg == "--prefix-size") {
opts.prefix_size = true;
} else { } else {
for (size_t i = 0; i < params_.num_generators; ++i) { for (size_t i = 0; i < params_.num_generators; ++i) {
if (arg == params_.generators[i].generator_opt_long || if (arg == params_.generators[i].generator_opt_long ||

View File

@@ -2204,8 +2204,13 @@ CheckedError Parser::DoParse(const char *source,
} }
uoffset_t toff; uoffset_t toff;
ECHECK(ParseTable(*root_struct_def_, nullptr, &toff)); ECHECK(ParseTable(*root_struct_def_, nullptr, &toff));
builder_.Finish(Offset<Table>(toff), if (opts.prefix_size) {
file_identifier_.length() ? file_identifier_.c_str() : nullptr); builder_.FinishSizePrefixed(Offset<Table>(toff),
file_identifier_.length() ? file_identifier_.c_str() : nullptr);
} else {
builder_.Finish(Offset<Table>(toff),
file_identifier_.length() ? file_identifier_.c_str() : nullptr);
}
} else if (IsIdent("enum")) { } else if (IsIdent("enum")) {
ECHECK(ParseEnum(false, nullptr)); ECHECK(ParseEnum(false, nullptr));
} else if (IsIdent("union")) { } else if (IsIdent("union")) {
@@ -2317,7 +2322,11 @@ void Parser::Serialize() {
root_struct_def_ root_struct_def_
? root_struct_def_->serialized_location ? root_struct_def_->serialized_location
: 0); : 0);
builder_.Finish(schema_offset, reflection::SchemaIdentifier()); if (opts.prefix_size) {
builder_.FinishSizePrefixed(schema_offset, reflection::SchemaIdentifier());
} else {
builder_.Finish(schema_offset, reflection::SchemaIdentifier());
}
} }
Offset<reflection::Object> StructDef::Serialize(FlatBufferBuilder *builder, Offset<reflection::Object> StructDef::Serialize(FlatBufferBuilder *builder,