本文整理汇总了Java中com.google.protobuf.Any.newBuilder方法的典型用法代码示例。如果您正苦于以下问题:Java Any.newBuilder方法的具体用法?Java Any.newBuilder怎么用?Java Any.newBuilder使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.protobuf.Any
的用法示例。
在下文中一共展示了Any.newBuilder方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: parserUnexpectedTypeUrl
import com.google.protobuf.Any; //导入方法依赖的package包/类
@Test
public void parserUnexpectedTypeUrl() throws Exception {
Any.Builder builder = Any.newBuilder();
assertThatThrownBy(
() ->
mergeFromJson(
"{\n"
+ " \"@type\": \"type.googleapis.com/json_test.TestAllTypes\",\n"
+ " \"optionalInt32\": 12345\n"
+ "}",
builder))
.isInstanceOf(InvalidProtocolBufferException.class);
}
示例2: emptyWrapperTypesInAny
import com.google.protobuf.Any; //导入方法依赖的package包/类
@Test
public void emptyWrapperTypesInAny() throws Exception {
Any.Builder builder = Any.newBuilder();
mergeFromJson(
"{\n"
+ " \"@type\": \"type.googleapis.com/google.protobuf.BoolValue\",\n"
+ " \"value\": false\n"
+ "}\n",
builder,
TestAllTypes.getDefaultInstance());
Any any = builder.build();
assertEquals(0, any.getValue().size());
}
示例3: parserMissingTypeUrl
import com.google.protobuf.Any; //导入方法依赖的package包/类
@Test
public void parserMissingTypeUrl() throws Exception {
Any.Builder builder = Any.newBuilder();
assertThatThrownBy(() -> mergeFromJson("{\n" + " \"optionalInt32\": 1234\n" + "}", builder))
.isInstanceOf(InvalidProtocolBufferException.class);
}
示例4: toAnyType
import com.google.protobuf.Any; //导入方法依赖的package包/类
/**
* Convert a protocol buffer field value to {@link Any} with the below rule:
* <ul>
* <li> If the field is a primitive type and can be mapped to a wrapper message type
* defined in //tech/type/proto/wrappers.proto, its value is boxed into a wrapper
* and goes to Any.value, the type name of the wrapper goes to Any.type_url.
* <li> If the field is already a message, its type name goes to Any.type_url,
* its value directly goes to Any.value
* <li> If the field is an enum value, the name of the enum value is boxed into
* tech.type.String and put into Any.value, and tech.type.String goes to Any.type_url.
*/
private static Any toAnyType(FieldDescriptor.Type protobufType, Object value) {
Any.Builder builder = Any.newBuilder();
java.lang.String typeFullName;
Message wrapperMessage;
switch (protobufType) {
case MESSAGE:
wrapperMessage = (Message) value;
typeFullName = wrapperMessage.getDescriptorForType().getFullName();
break;
case ENUM:
// NOTE: Erasing the enum type to the String wrapper is currently intentional, to avoid
// the need to add an enum wrapper type. This may change in the future.
typeFullName = StringValue.getDescriptor().getFullName();
wrapperMessage =
StringValue.newBuilder().setValue(((EnumValueDescriptor) value).getName()).build();
break;
case BOOL:
typeFullName = BoolValue.getDescriptor().getFullName();
wrapperMessage = BoolValue.newBuilder().setValue((Boolean) value).build();
break;
case DOUBLE:
typeFullName = DoubleValue.getDescriptor().getFullName();
wrapperMessage = DoubleValue.newBuilder().setValue((java.lang.Double) value).build();
break;
case FLOAT:
typeFullName = FloatValue.getDescriptor().getFullName();
wrapperMessage = FloatValue.newBuilder().setValue((java.lang.Float) value).build();
break;
case STRING:
typeFullName = StringValue.getDescriptor().getFullName();
wrapperMessage = StringValue.newBuilder().setValue((java.lang.String) value).build();
break;
case SINT32:
case SFIXED32:
case INT32:
typeFullName = Int32Value.getDescriptor().getFullName();
wrapperMessage = Int32Value.newBuilder().setValue((Integer) value).build();
break;
case SINT64:
case SFIXED64:
case INT64:
typeFullName = Int64Value.getDescriptor().getFullName();
wrapperMessage = Int64Value.newBuilder().setValue((Long) value).build();
break;
case UINT32:
case FIXED32:
typeFullName = UInt32Value.getDescriptor().getFullName();
wrapperMessage = UInt32Value.newBuilder().setValue((Integer) value).build();
break;
case UINT64:
case FIXED64:
typeFullName = UInt64Value.getDescriptor().getFullName();
wrapperMessage = UInt64Value.newBuilder().setValue((Long) value).build();
break;
case BYTES:
typeFullName = BytesValue.getDescriptor().getFullName();
wrapperMessage = BytesValue.newBuilder().setValue(ByteString.copyFrom((byte[]) value))
.build();
break;
default:
throw new IllegalArgumentException("Type " + protobufType.name()
+ " cannot be converted to Any type.");
}
return builder.setTypeUrl(TYPE_SERVICE_BASE_URL + "/" + typeFullName)
.setValue(wrapperMessage.toByteString()).build();
}