aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/tools/proto_text
diff options
context:
space:
mode:
authorGravatar Patrick Nguyen <drpng@google.com>2018-06-06 09:41:29 -0700
committerGravatar Patrick Nguyen <drpng@google.com>2018-06-06 09:41:29 -0700
commitb458040c9620f959378c84dc816ccc1b7a7871c6 (patch)
tree25b262e37a791ee5f13b5f0c53018e7cea4a62e8 /tensorflow/tools/proto_text
parenta3ef4518d949df3675a85e444dd06769e186a6a4 (diff)
parent5c26ec27e5ac23a16d9037b102df8216f821c477 (diff)
Merge commit for internal changes
Diffstat (limited to 'tensorflow/tools/proto_text')
-rw-r--r--tensorflow/tools/proto_text/gen_proto_text_functions_lib.cc25
-rw-r--r--tensorflow/tools/proto_text/gen_proto_text_functions_lib_test.cc5
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 aa56cc676d..15d7c70281 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\" ");