1, Remove unkown field support in jave TextFormat
2, Verifies the EnumValueDscriptor for DynamicMessage.Builder#setField
This commit is contained in:
parent
9eda53a6f1
commit
db9f7dd1ac
3 changed files with 19 additions and 118 deletions
|
@ -31,6 +31,7 @@
|
|||
package com.google.protobuf;
|
||||
|
||||
import com.google.protobuf.Descriptors.Descriptor;
|
||||
import com.google.protobuf.Descriptors.EnumValueDescriptor;
|
||||
import com.google.protobuf.Descriptors.FieldDescriptor;
|
||||
import com.google.protobuf.Descriptors.OneofDescriptor;
|
||||
|
||||
|
@ -482,6 +483,9 @@ public final class DynamicMessage extends AbstractMessage {
|
|||
public Builder setField(FieldDescriptor field, Object value) {
|
||||
verifyContainingType(field);
|
||||
ensureIsMutable();
|
||||
if (field.getType() == FieldDescriptor.Type.ENUM) {
|
||||
verifyEnumType(field, value);
|
||||
}
|
||||
OneofDescriptor oneofDescriptor = field.getContainingOneof();
|
||||
if (oneofDescriptor != null) {
|
||||
int index = oneofDescriptor.getIndex();
|
||||
|
@ -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() {
|
||||
if (fields.isImmutable()) {
|
||||
fields = fields.clone();
|
||||
|
|
|
@ -1211,21 +1211,6 @@ public final class TextFormat {
|
|||
private SingularOverwritePolicy singularOverwritePolicy =
|
||||
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.
|
||||
*/
|
||||
|
|
|
@ -119,9 +119,6 @@ public class TextFormatTest extends TestCase {
|
|||
" i: 456\n" +
|
||||
"}\n";
|
||||
|
||||
private final TextFormat.Parser parserAllowingUnknownFields =
|
||||
TextFormat.Parser.newBuilder().setAllowUnknownFields(true).build();
|
||||
|
||||
private final TextFormat.Parser parserWithOverwriteForbidden =
|
||||
TextFormat.Parser.newBuilder()
|
||||
.setSingularOverwritePolicy(
|
||||
|
@ -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,
|
||||
String text) {
|
||||
TestAllTypes.Builder builder = TestAllTypes.newBuilder();
|
||||
|
@ -922,87 +900,6 @@ public class TextFormatTest extends TestCase {
|
|||
.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 {
|
||||
assertParseSuccessWithOverwriteForbidden(
|
||||
"repeated_int32: 1\n" +
|
||||
|
|
Loading…
Add table
Reference in a new issue