Rename GenerateText

to make it a compile-time breaking change, to alert any users to the new meaning of the return value
This commit is contained in:
Wouter van Oortmerssen 2023-05-11 18:08:42 -07:00
parent 86486a1735
commit 950a71ab89
14 changed files with 50 additions and 61 deletions

View File

@ -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.

View File

@ -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);

View File

@ -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.
//

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;

View File

@ -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) {

View File

@ -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());

View File

@ -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(),

View File

@ -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());
}

View File

@ -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());
}

View File

@ -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}");
}

View File

@ -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(),

View File

@ -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);