diff options
author | Wouter van Oortmerssen <aardappel@gmail.com> | 2023-05-11 18:08:42 -0700 |
---|---|---|
committer | Wouter van Oortmerssen <aardappel@gmail.com> | 2023-05-11 18:14:27 -0700 |
commit | 950a71ab893e96147c30dd91735af6db73f72ae0 (patch) | |
tree | d785e704206a175b0456070e70b7f0a7ebc515da | |
parent | 86486a1735bc38f5629447251206934ef205f6e4 (diff) | |
download | flatbuffers-950a71ab893e96147c30dd91735af6db73f72ae0.tar.gz flatbuffers-950a71ab893e96147c30dd91735af6db73f72ae0.tar.bz2 flatbuffers-950a71ab893e96147c30dd91735af6db73f72ae0.zip |
Rename GenerateText
to make it a compile-time breaking change, to alert any users to the new meaning of the return value
-rw-r--r-- | include/flatbuffers/idl.h | 16 | ||||
-rw-r--r-- | include/flatbuffers/registry.h | 2 | ||||
-rw-r--r-- | include/flatbuffers/util.h | 2 | ||||
-rw-r--r-- | samples/sample_bfbs.cpp | 4 | ||||
-rw-r--r-- | samples/sample_text.cpp | 2 | ||||
-rw-r--r-- | src/idl_gen_text.cpp | 18 | ||||
-rw-r--r-- | tests/fuzz_test.cpp | 3 | ||||
-rw-r--r-- | tests/fuzzer/flatbuffers_monster_fuzzer.cc | 4 | ||||
-rw-r--r-- | tests/fuzzer/flatbuffers_scalar_fuzzer.cc | 7 | ||||
-rw-r--r-- | tests/json_test.cpp | 11 | ||||
-rw-r--r-- | tests/monster_test.cpp | 8 | ||||
-rw-r--r-- | tests/parser_test.cpp | 17 | ||||
-rw-r--r-- | tests/proto_test.cpp | 3 | ||||
-rw-r--r-- | tests/test.cpp | 14 |
14 files changed, 50 insertions, 61 deletions
diff --git a/include/flatbuffers/idl.h b/include/flatbuffers/idl.h index 2d7c8581..e4a56607 100644 --- a/include/flatbuffers/idl.h +++ b/include/flatbuffers/idl.h @@ -1210,14 +1210,14 @@ class Parser : public ParserState { // if it is less than 0, no linefeeds will be generated either. // See idl_gen_text.cpp. // strict_json adds "quotes" around field names if true. -// If the flatbuffer cannot be encoded in JSON (e.g., it contains non-UTF-8 -// byte arrays in String values), returns false. -extern const char *GenerateTextFromTable(const Parser &parser, - const void *table, - const std::string &tablename, - std::string *text); -extern const char *GenerateText(const Parser &parser, const void *flatbuffer, - std::string *text); +// These functions return nullptr on success, or an error string, +// which may happen if the flatbuffer cannot be encoded in JSON (e.g., +// it contains non-UTF-8 byte arrays in String values). +extern const char *GenTextFromTable(const Parser &parser, const void *table, + const std::string &tablename, + std::string *text); +extern const char *GenText(const Parser &parser, const void *flatbuffer, + std::string *text); // Generate GRPC Cpp interfaces. // See idl_gen_grpc.cpp. diff --git a/include/flatbuffers/registry.h b/include/flatbuffers/registry.h index ed3d922f..f59aa647 100644 --- a/include/flatbuffers/registry.h +++ b/include/flatbuffers/registry.h @@ -52,7 +52,7 @@ class Registry { Parser parser; if (!LoadSchema(ident, &parser)) return false; // Now we're ready to generate text. - auto err = GenerateText(parser, flatbuf, dest); + auto err = GenText(parser, flatbuf, dest); if (err) { lasterror_ = "unable to generate text for FlatBuffer binary: " + std::string(err); diff --git a/include/flatbuffers/util.h b/include/flatbuffers/util.h index 71afdc4d..1ccf3517 100644 --- a/include/flatbuffers/util.h +++ b/include/flatbuffers/util.h @@ -623,7 +623,7 @@ inline bool EscapeString(const char *s, size_t length, std::string *_text, // we previously checked for non-UTF-8, so we shouldn't reach // here. // - // 2) We reached here by someone calling GenerateText() + // 2) We reached here by someone calling GenText() // on a previously-serialized flatbuffer. The data might have // non-UTF-8 Strings, or might be corrupt. // diff --git a/samples/sample_bfbs.cpp b/samples/sample_bfbs.cpp index c0017bea..1178b0ef 100644 --- a/samples/sample_bfbs.cpp +++ b/samples/sample_bfbs.cpp @@ -59,13 +59,13 @@ int main(int /*argc*/, const char * /*argv*/[]) { // to ensure it is correct, we now generate text back from the binary, // and compare the two: std::string jsongen1; - if (GenerateText(parser1, parser1.builder_.GetBufferPointer(), &jsongen1)) { + if (GenText(parser1, parser1.builder_.GetBufferPointer(), &jsongen1)) { printf("Couldn't serialize parsed data to JSON!\n"); return 1; } std::string jsongen2; - if (GenerateText(parser2, parser2.builder_.GetBufferPointer(), &jsongen2)) { + if (GenText(parser2, parser2.builder_.GetBufferPointer(), &jsongen2)) { printf("Couldn't serialize parsed data to JSON!\n"); return 1; } diff --git a/samples/sample_text.cpp b/samples/sample_text.cpp index 8580b523..fb42563b 100644 --- a/samples/sample_text.cpp +++ b/samples/sample_text.cpp @@ -45,7 +45,7 @@ int main(int /*argc*/, const char * /*argv*/[]) { // to ensure it is correct, we now generate text back from the binary, // and compare the two: std::string jsongen; - if (GenerateText(parser, parser.builder_.GetBufferPointer(), &jsongen)) { + if (GenText(parser, parser.builder_.GetBufferPointer(), &jsongen)) { printf("Couldn't serialize parsed data to JSON!\n"); return 1; } diff --git a/src/idl_gen_text.cpp b/src/idl_gen_text.cpp index 17d51803..a34667c4 100644 --- a/src/idl_gen_text.cpp +++ b/src/idl_gen_text.cpp @@ -384,9 +384,8 @@ static const char *GenerateTextImpl(const Parser &parser, const Table *table, } // Generate a text representation of a flatbuffer in JSON format. -const char *GenerateTextFromTable(const Parser &parser, const void *table, - const std::string &table_name, - std::string *_text) { +const char *GenTextFromTable(const Parser &parser, const void *table, + const std::string &table_name, std::string *_text) { auto struct_def = parser.LookupStruct(table_name); if (struct_def == nullptr) { return "unknown struct"; } auto root = static_cast<const Table *>(table); @@ -394,8 +393,8 @@ const char *GenerateTextFromTable(const Parser &parser, const void *table, } // Generate a text representation of a flatbuffer in JSON format. -const char *GenerateText(const Parser &parser, const void *flatbuffer, - std::string *_text) { +const char *GenText(const Parser &parser, const void *flatbuffer, + std::string *_text) { FLATBUFFERS_ASSERT(parser.root_struct_def_); // call SetRootType() auto root = parser.opts.size_prefixed ? GetSizePrefixedRoot<Table>(flatbuffer) : GetRoot<Table>(flatbuffer); @@ -407,9 +406,8 @@ static std::string TextFileName(const std::string &path, return path + file_name + ".json"; } -static const char *GenerateTextFile(const Parser &parser, - const std::string &path, - const std::string &file_name) { +const char *GenTextFile(const Parser &parser, const std::string &path, + const std::string &file_name) { if (parser.opts.use_flexbuffers) { std::string json; parser.flex_root_.ToString(true, parser.opts.strict_json, json); @@ -420,7 +418,7 @@ static const char *GenerateTextFile(const Parser &parser, } if (!parser.builder_.GetSize() || !parser.root_struct_def_) return nullptr; std::string text; - auto err = GenerateText(parser, parser.builder_.GetBufferPointer(), &text); + auto err = GenText(parser, parser.builder_.GetBufferPointer(), &text); if (err) return err; return flatbuffers::SaveFile(TextFileName(path, file_name).c_str(), text, false) @@ -448,7 +446,7 @@ class TextCodeGenerator : public CodeGenerator { public: Status GenerateCode(const Parser &parser, const std::string &path, const std::string &filename) override { - auto err = GenerateTextFile(parser, path, filename); + auto err = GenTextFile(parser, path, filename); if (err) { status_detail = " (" + std::string(err) + ")"; return Status::ERROR; diff --git a/tests/fuzz_test.cpp b/tests/fuzz_test.cpp index 4e42549a..1c9adbc5 100644 --- a/tests/fuzz_test.cpp +++ b/tests/fuzz_test.cpp @@ -273,8 +273,7 @@ void FuzzTest2() { std::string jsongen; parser.opts.indent_step = 0; - auto result = - GenerateText(parser, parser.builder_.GetBufferPointer(), &jsongen); + auto result = GenText(parser, parser.builder_.GetBufferPointer(), &jsongen); TEST_NULL(result); if (jsongen != json) { diff --git a/tests/fuzzer/flatbuffers_monster_fuzzer.cc b/tests/fuzzer/flatbuffers_monster_fuzzer.cc index 4b8b7906..71a68027 100644 --- a/tests/fuzzer/flatbuffers_monster_fuzzer.cc +++ b/tests/fuzzer/flatbuffers_monster_fuzzer.cc @@ -78,8 +78,8 @@ std::string do_test(const flatbuffers::IDLOptions &opts, flatbuffers::Verifier verifier(parser_.builder_.GetBufferPointer(), parser_.builder_.GetSize()); TEST_EQ(true, MyGame::Example::VerifyMonsterBuffer(verifier)); - TEST_ASSERT( - GenerateText(parser_, parser_.builder_.GetBufferPointer(), &jsongen)); + TEST_NULL( + GenText(parser_, parser_.builder_.GetBufferPointer(), &jsongen)); } else if (check_parser) { TEST_OUTPUT_LINE("parser failed with JSON:\n%s", input_json.c_str()); TEST_EQ_STR("", parser_.error_.c_str()); diff --git a/tests/fuzzer/flatbuffers_scalar_fuzzer.cc b/tests/fuzzer/flatbuffers_scalar_fuzzer.cc index faa069e1..cd43506f 100644 --- a/tests/fuzzer/flatbuffers_scalar_fuzzer.cc +++ b/tests/fuzzer/flatbuffers_scalar_fuzzer.cc @@ -218,8 +218,7 @@ bool Parse(flatbuffers::Parser &parser, const std::string &json, std::string *_text) { auto done = parser.ParseJson(json.c_str()); if (done) { - TEST_EQ(GenerateText(parser, parser.builder_.GetBufferPointer(), _text), - true); + TEST_NULL(GenText(parser, parser.builder_.GetBufferPointer(), _text)); } else { *_text = parser.error_; } @@ -358,9 +357,9 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { } // Compare with print. std::string ref_string, def_string; - FLATBUFFERS_ASSERT(GenerateText( + FLATBUFFERS_ASSERT(!GenText( parser, parser.builder_.GetBufferPointer(), &ref_string)); - FLATBUFFERS_ASSERT(GenerateText( + FLATBUFFERS_ASSERT(!GenText( def_parser, def_parser.builder_.GetBufferPointer(), &def_string)); if (ref_string != def_string) { TEST_OUTPUT_LINE("Stage 3.2 failed: '%s' != '%s'", def_string.c_str(), diff --git a/tests/json_test.cpp b/tests/json_test.cpp index 86367eec..2a328638 100644 --- a/tests/json_test.cpp +++ b/tests/json_test.cpp @@ -36,7 +36,7 @@ void JsonDefaultTest(const std::string &tests_data_path) { color_monster.add_name(name); FinishMonsterBuffer(builder, color_monster.Finish()); std::string jsongen; - auto result = GenerateText(parser, builder.GetBufferPointer(), &jsongen); + auto result = GenText(parser, builder.GetBufferPointer(), &jsongen); TEST_NULL(result); // default value of the "color" field is Blue TEST_EQ(std::string::npos != jsongen.find("color: \"Blue\""), true); @@ -65,7 +65,7 @@ void JsonEnumsTest(const std::string &tests_data_path) { color_monster.add_color(Color(Color_Blue | Color_Red)); FinishMonsterBuffer(builder, color_monster.Finish()); std::string jsongen; - auto result = GenerateText(parser, builder.GetBufferPointer(), &jsongen); + auto result = GenText(parser, builder.GetBufferPointer(), &jsongen); TEST_NULL(result); TEST_EQ(std::string::npos != jsongen.find("color: \"Red Blue\""), true); // Test forward compatibility with 'output_enum_identifiers = true'. @@ -78,7 +78,7 @@ void JsonEnumsTest(const std::string &tests_data_path) { future_color.add_color( static_cast<Color>((1u << 2) | Color_Blue | Color_Red)); FinishMonsterBuffer(builder, future_color.Finish()); - result = GenerateText(parser, builder.GetBufferPointer(), &future_json); + result = GenText(parser, builder.GetBufferPointer(), &future_json); TEST_NULL(result); TEST_EQ(std::string::npos != future_json.find("color: 13"), true); } @@ -119,8 +119,7 @@ void JsonOptionalTest(const std::string &tests_data_path, // to ensure it is correct, we now generate text back from the binary, // and compare the two: std::string jsongen; - auto result = - GenerateText(parser, parser.builder_.GetBufferPointer(), &jsongen); + auto result = GenText(parser, parser.builder_.GetBufferPointer(), &jsongen); TEST_NULL(result); TEST_EQ_STR(jsongen.c_str(), jsonfile.c_str()); } @@ -199,7 +198,7 @@ root_type JsonUnionStructTest; // now generate text back from the binary, and compare the two: std::string json_generated; auto generate_result = - GenerateText(parser, parser.builder_.GetBufferPointer(), &json_generated); + GenText(parser, parser.builder_.GetBufferPointer(), &json_generated); TEST_NULL(generate_result); TEST_EQ_STR(json_source, json_generated.c_str()); } diff --git a/tests/monster_test.cpp b/tests/monster_test.cpp index d44f118e..8ec031a3 100644 --- a/tests/monster_test.cpp +++ b/tests/monster_test.cpp @@ -646,7 +646,7 @@ void TestMonsterExtraFloats(const std::string &tests_data_path) { TEST_EQ(def_extra->d2(), +infinity_d); TEST_EQ(def_extra->d3(), -infinity_d); std::string jsongen; - auto result = GenerateText(parser, def_obj, &jsongen); + auto result = GenText(parser, def_obj, &jsongen); TEST_NULL(result); // Check expected default values. TEST_EQ(std::string::npos != jsongen.find("f0: nan"), true); @@ -796,8 +796,7 @@ void ParseAndGenerateTextTest(const std::string &tests_data_path, bool binary) { // to ensure it is correct, we now generate text back from the binary, // and compare the two: std::string jsongen; - auto result = - GenerateText(parser, parser.builder_.GetBufferPointer(), &jsongen); + auto result = GenText(parser, parser.builder_.GetBufferPointer(), &jsongen); TEST_NULL(result); TEST_EQ_STR(jsongen.c_str(), jsonfile.c_str()); @@ -836,8 +835,7 @@ void ParseAndGenerateTextTest(const std::string &tests_data_path, bool binary) { // request natural printing for utf-8 strings parser.opts.natural_utf8 = true; parser.opts.strict_json = true; - TEST_NULL( - GenerateText(parser, parser.builder_.GetBufferPointer(), &jsongen_utf8)); + TEST_NULL(GenText(parser, parser.builder_.GetBufferPointer(), &jsongen_utf8)); TEST_EQ_STR(jsongen_utf8.c_str(), jsonfile_utf8.c_str()); } diff --git a/tests/parser_test.cpp b/tests/parser_test.cpp index 83c67e85..085c2fe0 100644 --- a/tests/parser_test.cpp +++ b/tests/parser_test.cpp @@ -484,8 +484,7 @@ T TestValue(const char *json, const char *type_name, // Check with print. std::string print_back; parser.opts.indent_step = -1; - TEST_NULL( - GenerateText(parser, parser.builder_.GetBufferPointer(), &print_back)); + TEST_NULL(GenText(parser, parser.builder_.GetBufferPointer(), &print_back)); // restore value from its default if (check_default) { TEST_EQ(parser.Parse(print_back.c_str()), true); } @@ -740,8 +739,7 @@ void UnicodeTest() { true); std::string jsongen; parser.opts.indent_step = -1; - auto result = - GenerateText(parser, parser.builder_.GetBufferPointer(), &jsongen); + auto result = GenText(parser, parser.builder_.GetBufferPointer(), &jsongen); TEST_NULL(result); TEST_EQ_STR(jsongen.c_str(), "{F: \"\\u20AC\\u00A2\\u30E6\\u30FC\\u30B6\\u30FC" @@ -760,8 +758,7 @@ void UnicodeTestAllowNonUTF8() { true); std::string jsongen; parser.opts.indent_step = -1; - auto result = - GenerateText(parser, parser.builder_.GetBufferPointer(), &jsongen); + auto result = GenText(parser, parser.builder_.GetBufferPointer(), &jsongen); TEST_NULL(result); TEST_EQ_STR( jsongen.c_str(), @@ -783,11 +780,10 @@ void UnicodeTestGenerateTextFailsOnNonUTF8() { true); std::string jsongen; parser.opts.indent_step = -1; - // Now, disallow non-UTF-8 (the default behavior) so GenerateText indicates + // Now, disallow non-UTF-8 (the default behavior) so GenText indicates // failure. parser.opts.allow_non_utf8 = false; - auto result = - GenerateText(parser, parser.builder_.GetBufferPointer(), &jsongen); + auto result = GenText(parser, parser.builder_.GetBufferPointer(), &jsongen); TEST_EQ_STR(result, "string contains non-utf8 bytes"); } @@ -825,8 +821,7 @@ void UnknownFieldsTest() { std::string jsongen; parser.opts.indent_step = -1; - auto result = - GenerateText(parser, parser.builder_.GetBufferPointer(), &jsongen); + auto result = GenText(parser, parser.builder_.GetBufferPointer(), &jsongen); TEST_NULL(result); TEST_EQ_STR(jsongen.c_str(), "{str: \"test\",i: 10}"); } diff --git a/tests/proto_test.cpp b/tests/proto_test.cpp index 6fb3ef35..90c70bdb 100644 --- a/tests/proto_test.cpp +++ b/tests/proto_test.cpp @@ -324,7 +324,8 @@ void ParseProtoBufAsciiTest() { TEST_EQ(parser.Parse("{ A [1 2] C { B:2 }}"), true); // Similarly, in text output, it should omit these. std::string text; - auto err = flatbuffers::GenerateText( + auto err = + flatbuffers::GenText( parser, parser.builder_.GetBufferPointer(), &text); TEST_NULL(err); TEST_EQ_STR(text.c_str(), diff --git a/tests/test.cpp b/tests/test.cpp index 49018d1b..5d483893 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -119,13 +119,13 @@ void GenerateTableTextTest(const std::string &tests_data_path) { TEST_EQ(abilities->Get(2)->distance(), 12); std::string jsongen; - auto result = GenerateTextFromTable(parser, monster, "MyGame.Example.Monster", + auto result = GenTextFromTable(parser, monster, "MyGame.Example.Monster", &jsongen); TEST_NULL(result); // Test sub table const Vec3 *pos = monster->pos(); jsongen.clear(); - result = GenerateTextFromTable(parser, pos, "MyGame.Example.Vec3", &jsongen); + result = GenTextFromTable(parser, pos, "MyGame.Example.Vec3", &jsongen); TEST_NULL(result); TEST_EQ_STR( jsongen.c_str(), @@ -133,13 +133,13 @@ void GenerateTableTextTest(const std::string &tests_data_path) { const Test &test3 = pos->test3(); jsongen.clear(); result = - GenerateTextFromTable(parser, &test3, "MyGame.Example.Test", &jsongen); + GenTextFromTable(parser, &test3, "MyGame.Example.Test", &jsongen); TEST_NULL(result); TEST_EQ_STR(jsongen.c_str(), "{a: 5,b: 6}"); const Test *test4 = monster->test4()->Get(0); jsongen.clear(); result = - GenerateTextFromTable(parser, test4, "MyGame.Example.Test", &jsongen); + GenTextFromTable(parser, test4, "MyGame.Example.Test", &jsongen); TEST_NULL(result); TEST_EQ_STR(jsongen.c_str(), "{a: 10,b: 20}"); } @@ -337,7 +337,7 @@ void UnionVectorTest(const std::string &tests_data_path) { // Generate text using parsed schema. std::string jsongen; - auto result = GenerateText(parser, fbb.GetBufferPointer(), &jsongen); + auto result = GenText(parser, fbb.GetBufferPointer(), &jsongen); TEST_NULL(result); TEST_EQ_STR(jsongen.c_str(), "{\n" @@ -957,7 +957,7 @@ void FixedLengthArrayJsonTest(const std::string &tests_data_path, bool binary) { // Export to JSON std::string jsonGen; TEST_NULL( - GenerateText(parserOrg, parserOrg.builder_.GetBufferPointer(), &jsonGen)); + GenText(parserOrg, parserOrg.builder_.GetBufferPointer(), &jsonGen)); // Import from JSON TEST_EQ(parserGen.Parse(jsonGen.c_str()), true); @@ -1083,7 +1083,7 @@ void TestEmbeddedBinarySchema(const std::string &tests_data_path) { // Export to JSON std::string jsonGen; TEST_NULL( - GenerateText(parserOrg, parserOrg.builder_.GetBufferPointer(), &jsonGen)); + GenText(parserOrg, parserOrg.builder_.GetBufferPointer(), &jsonGen)); // Import from JSON TEST_EQ(parserGen.Parse(jsonGen.c_str()), true); |