diff options
author | 2018-06-06 05:39:15 -0700 | |
---|---|---|
committer | 2018-06-06 05:43:26 -0700 | |
commit | abd8348587b765aa6a72469a92d03c02802dbcef (patch) | |
tree | 4393842a854fe6c43c59be166cec6bbd9e280c62 /tensorflow/tools/proto_text | |
parent | 76c9358e344a4d454784faccfbff4a73d9c0a04a (diff) |
Tensorflow protos allow enum values outside of the listed constants; this is now properly supported in the Text Format.
PiperOrigin-RevId: 199450074
Diffstat (limited to 'tensorflow/tools/proto_text')
-rw-r--r-- | tensorflow/tools/proto_text/gen_proto_text_functions_lib.cc | 25 | ||||
-rw-r--r-- | tensorflow/tools/proto_text/gen_proto_text_functions_lib_test.cc | 5 |
2 files changed, 22 insertions, 8 deletions
diff --git a/tensorflow/tools/proto_text/gen_proto_text_functions_lib.cc b/tensorflow/tools/proto_text/gen_proto_text_functions_lib.cc index 62e29b5128..29add6d5ea 100644 --- a/tensorflow/tools/proto_text/gen_proto_text_functions_lib.cc +++ b/tensorflow/tools/proto_text/gen_proto_text_functions_lib.cc @@ -279,8 +279,13 @@ void Generator::AppendFieldValueAppend(const FieldDescriptor& field, if (omit_default) { Print("if (", field_expr, " != 0) {").Nest(); } - Print("o->AppendEnumName(\"", field.name(), "\", ", - GetQualifiedEnumNameFn(*field.enum_type()), "(", field_expr, "));"); + Print("const char* enum_name = ", + GetQualifiedEnumNameFn(*field.enum_type()), "(", field_expr, ");"); + Print("if (enum_name[0]) {").Nest(); + Print("o->AppendEnumName(\"", field.name(), "\", enum_name);"); + Unnest().Print("} else {").Nest(); + Print("o->AppendNumeric(\"", field.name(), "\", ", field_expr, ");"); + Unnest().Print("}"); if (omit_default) { Unnest().Print("}"); } @@ -540,18 +545,24 @@ void Generator::AppendParseMessageFunction(const Descriptor& md) { for (int enum_i = 0; enum_i < enum_d->value_count(); ++enum_i) { const auto* value_d = enum_d->value(enum_i); const string& value_name = value_d->name(); - string condition = StrCat("value == \"", value_name, - "\" || value == \"", value_d->number(), "\""); - if (value_d->number() == 0) { - StrAppend(&condition, " || value == \"-0\""); - } + string condition = StrCat("value == \"", value_name, "\""); Print(enum_i == 0 ? "" : "} else ", "if (", condition, ") {"); Nest(); Print(set_value_prefix, "(", value_prefix, value_name, ");"); Unnest(); } + Print("} else {"); + Nest(); + // Proto3 allows all numeric values. + Print("int32 int_value;"); + Print("if (strings::SafeStringToNumeric(value, &int_value)) {"); + Nest(); + Print(set_value_prefix, "(static_cast<", GetQualifiedName(*enum_d), + ">(int_value));"); + Unnest(); Print("} else {").Nest().Print("return false;").Unnest().Print("}"); + Unnest().Print("}"); } else { Print(field->cpp_type_name(), " value;"); switch (field->cpp_type()) { diff --git a/tensorflow/tools/proto_text/gen_proto_text_functions_lib_test.cc b/tensorflow/tools/proto_text/gen_proto_text_functions_lib_test.cc index 6f0b4f47de..e67add72de 100644 --- a/tensorflow/tools/proto_text/gen_proto_text_functions_lib_test.cc +++ b/tensorflow/tools/proto_text/gen_proto_text_functions_lib_test.cc @@ -455,7 +455,10 @@ TEST(CreateProtoDebugStringLibTest, Enums) { "repeated_nested_enum: 1")); EXPECT_PARSE_SUCCESS("", "optional_nested_enum: -0"); - EXPECT_PARSE_FAILURE("optional_nested_enum: 6"); + // TODO(amauryfa): restore the line below when protobuf::TextFormat also + // supports unknonwn enum values. + // EXPECT_PARSE_SUCCESS("optional_nested_enum: 6", "optional_nested_enum: 6"); + EXPECT_PARSE_FAILURE("optional_nested_enum: 2147483648"); // > INT32_MAX EXPECT_PARSE_FAILURE("optional_nested_enum: BARNONE"); EXPECT_PARSE_FAILURE("optional_nested_enum: 'BAR'"); EXPECT_PARSE_FAILURE("optional_nested_enum: \"BAR\" "); |