Commit db9f7dd1 authored by jieluo@google.com's avatar jieluo@google.com

1, Remove unkown field support in jave TextFormat

2, Verifies the EnumValueDscriptor for DynamicMessage.Builder#setField
parent 9eda53a6
...@@ -31,6 +31,7 @@ ...@@ -31,6 +31,7 @@
package com.google.protobuf; package com.google.protobuf;
import com.google.protobuf.Descriptors.Descriptor; import com.google.protobuf.Descriptors.Descriptor;
import com.google.protobuf.Descriptors.EnumValueDescriptor;
import com.google.protobuf.Descriptors.FieldDescriptor; import com.google.protobuf.Descriptors.FieldDescriptor;
import com.google.protobuf.Descriptors.OneofDescriptor; import com.google.protobuf.Descriptors.OneofDescriptor;
...@@ -482,6 +483,9 @@ public final class DynamicMessage extends AbstractMessage { ...@@ -482,6 +483,9 @@ public final class DynamicMessage extends AbstractMessage {
public Builder setField(FieldDescriptor field, Object value) { public Builder setField(FieldDescriptor field, Object value) {
verifyContainingType(field); verifyContainingType(field);
ensureIsMutable(); ensureIsMutable();
if (field.getType() == FieldDescriptor.Type.ENUM) {
verifyEnumType(field, value);
}
OneofDescriptor oneofDescriptor = field.getContainingOneof(); OneofDescriptor oneofDescriptor = field.getContainingOneof();
if (oneofDescriptor != null) { if (oneofDescriptor != null) {
int index = oneofDescriptor.getIndex(); int index = oneofDescriptor.getIndex();
...@@ -568,6 +572,21 @@ public final class DynamicMessage extends AbstractMessage { ...@@ -568,6 +572,21 @@ public final class DynamicMessage extends AbstractMessage {
} }
} }
/** Verifies that the value is EnumValueDescriptor and matchs Enum Type. */
private void verifyEnumType(FieldDescriptor field, Object value) {
if (value == null) {
throw new NullPointerException();
}
if (!(value instanceof EnumValueDescriptor)) {
throw new IllegalArgumentException(
"DynamicMessage should use EnumValueDescriptor to set Enum Value.");
}
if (field.getEnumType() != ((EnumValueDescriptor) value).getType()) {
throw new IllegalArgumentException(
"EnumValueDescriptor doesn't much Enum Field.");
}
}
private void ensureIsMutable() { private void ensureIsMutable() {
if (fields.isImmutable()) { if (fields.isImmutable()) {
fields = fields.clone(); fields = fields.clone();
......
...@@ -1211,21 +1211,6 @@ public final class TextFormat { ...@@ -1211,21 +1211,6 @@ public final class TextFormat {
private SingularOverwritePolicy singularOverwritePolicy = private SingularOverwritePolicy singularOverwritePolicy =
SingularOverwritePolicy.ALLOW_SINGULAR_OVERWRITES; SingularOverwritePolicy.ALLOW_SINGULAR_OVERWRITES;
/**
* Set whether this parser will allow unknown fields. By default, an
* exception is thrown if an unknown field is encountered. If this is
* set, the parser will only log a warning.
*
* <p>Use of this parameter is discouraged. See:
* https://sites.google.com/a/google.com/protocol-buffers/migration/
* proto2-faq#How_do_I_ignore_unknown_fields_w
* for more details.
*/
public Builder setAllowUnknownFields(boolean allowUnknownFields) {
this.allowUnknownFields = allowUnknownFields;
return this;
}
/** /**
* Sets parser behavior when a non-repeated field appears more than once. * Sets parser behavior when a non-repeated field appears more than once.
*/ */
......
...@@ -119,9 +119,6 @@ public class TextFormatTest extends TestCase { ...@@ -119,9 +119,6 @@ public class TextFormatTest extends TestCase {
" i: 456\n" + " i: 456\n" +
"}\n"; "}\n";
private final TextFormat.Parser parserAllowingUnknownFields =
TextFormat.Parser.newBuilder().setAllowUnknownFields(true).build();
private final TextFormat.Parser parserWithOverwriteForbidden = private final TextFormat.Parser parserWithOverwriteForbidden =
TextFormat.Parser.newBuilder() TextFormat.Parser.newBuilder()
.setSingularOverwritePolicy( .setSingularOverwritePolicy(
...@@ -463,25 +460,6 @@ public class TextFormatTest extends TestCase { ...@@ -463,25 +460,6 @@ public class TextFormatTest extends TestCase {
} }
} }
private void assertParseErrorWithUnknownFields(String error, String text) {
TestAllTypes.Builder builder = TestAllTypes.newBuilder();
try {
parserAllowingUnknownFields.merge(
text, TestUtil.getExtensionRegistry(), builder);
fail("Expected parse exception.");
} catch (TextFormat.ParseException e) {
assertEquals(error, e.getMessage());
}
}
private TestAllTypes assertParseSuccessWithUnknownFields(String text)
throws TextFormat.ParseException {
TestAllTypes.Builder builder = TestAllTypes.newBuilder();
parserAllowingUnknownFields.merge(
text, TestUtil.getExtensionRegistry(), builder);
return builder.build();
}
private void assertParseErrorWithOverwriteForbidden(String error, private void assertParseErrorWithOverwriteForbidden(String error,
String text) { String text) {
TestAllTypes.Builder builder = TestAllTypes.newBuilder(); TestAllTypes.Builder builder = TestAllTypes.newBuilder();
...@@ -922,87 +900,6 @@ public class TextFormatTest extends TestCase { ...@@ -922,87 +900,6 @@ public class TextFormatTest extends TestCase {
.build())); .build()));
} }
public void testParseUnknownFields() throws Exception {
assertParseSuccessWithUnknownFields("unknown_field: 12345");
assertParseSuccessWithUnknownFields("unknown_field: -12345");
assertParseSuccessWithUnknownFields("unknown_field: 1.2345");
assertParseSuccessWithUnknownFields("unknown_field: -1.2345");
assertParseSuccessWithUnknownFields("unknown_field: 1.2345f");
assertParseSuccessWithUnknownFields("unknown_field: -1.2345f");
assertParseSuccessWithUnknownFields("unknown_field: inf");
assertParseSuccessWithUnknownFields("unknown_field: -inf");
assertParseSuccessWithUnknownFields("unknown_field: TYPE_STRING");
assertParseSuccessWithUnknownFields("unknown_field: \"string value\"");
// Invalid field value
assertParseErrorWithUnknownFields(
"1:16: Invalid field value: -TYPE_STRING",
"unknown_field: -TYPE_STRING");
// Two or more unknown fields
assertParseSuccessWithUnknownFields("unknown_field1: TYPE_STRING\n" +
"unknown_field2: 12345");
// Unknown nested message
assertParseSuccessWithUnknownFields("unknown_message1: {}\n" +
"unknown_message2 {\n" +
" unknown_field: 12345\n" +
"}\n" +
"unknown_message3 <\n" +
" unknown_nested_message {\n" +
" unknown_field: 12345\n" +
" }\n" +
">");
// Unmatched delimeters for message body
assertParseErrorWithUnknownFields(
"1:19: Expected \"}\".", "unknown_message: {>");
// Unknown extension
assertParseSuccessWithUnknownFields(
"[somewhere.unknown_extension1]: 12345\n" +
"[somewhere.unknown_extension2] {\n" +
" unknown_field: 12345\n" +
"}");
// Unknown fields between known fields.
TestAllTypes expected = TestAllTypes.newBuilder()
.setOptionalInt32(1)
.setOptionalString("string")
.setOptionalNestedMessage(NestedMessage.newBuilder()
.setBb(2))
.build();
assertEquals(expected, assertParseSuccessWithUnknownFields(
"optional_int32: 1\n" +
"unknown_field: 12345\n" +
"optional_string: \"string\"\n" +
"unknown_message { unknown : 0 }\n" +
"optional_nested_message { bb: 2 }"));
// Nested unknown extensions.
assertParseSuccessWithUnknownFields(
"[test.extension1] <\n" +
" unknown_nested_message <\n" +
" [test.extension2] <\n" +
" unknown_field: 12345\n" +
" >\n" +
" >\n" +
">");
assertParseSuccessWithUnknownFields(
"[test.extension1] {\n" +
" unknown_nested_message {\n" +
" [test.extension2] {\n" +
" unknown_field: 12345\n" +
" }\n" +
" }\n" +
"}");
assertParseSuccessWithUnknownFields(
"[test.extension1] <\n" +
" some_unknown_fields: <\n" +
" unknown_field: 12345\n" +
" >\n" +
">");
assertParseSuccessWithUnknownFields(
"[test.extension1] {\n" +
" some_unknown_fields: {\n" +
" unknown_field: 12345\n" +
" }\n" +
"}");
}
public void testParseNonRepeatedFields() throws Exception { public void testParseNonRepeatedFields() throws Exception {
assertParseSuccessWithOverwriteForbidden( assertParseSuccessWithOverwriteForbidden(
"repeated_int32: 1\n" + "repeated_int32: 1\n" +
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment