本文整理匯總了Java中com.google.protobuf.Message.Builder類的典型用法代碼示例。如果您正苦於以下問題:Java Builder類的具體用法?Java Builder怎麽用?Java Builder使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
Builder類屬於com.google.protobuf.Message包,在下文中一共展示了Builder類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: setTargetField
import com.google.protobuf.Message.Builder; //導入依賴的package包/類
private static void setTargetField(final Builder target, final Object sourceObject, final String targetField)
throws IllegalArgumentException {
Descriptors.FieldDescriptor fieldDescriptor = target.getDescriptorForType().findFieldByName(targetField);
if (null == fieldDescriptor) {
throw new RuntimeException("Unknown target field in protobuf: " + targetField);
}
if (fieldDescriptor.isRepeated()) {
target.addRepeatedField(fieldDescriptor, sourceObject);
} else {
target.setField(fieldDescriptor, sourceObject);
}
}
示例2: buildMessage
import com.google.protobuf.Message.Builder; //導入依賴的package包/類
@SuppressWarnings("unchecked")
private static Object buildMessage(Builder builder, Map<String, Object> fields) {
Descriptor descriptor = builder.getDescriptorForType();
for (Map.Entry<String, Object> entry : fields.entrySet()) {
if (entry.getValue() == null) {
continue;
}
FieldDescriptor field = getField(descriptor, entry.getKey());
if (entry.getValue() instanceof List<?>) {
List<Object> values = (List<Object>) entry.getValue();
for (Object value : values) {
builder.addRepeatedField(field, buildValue(builder, field, value));
}
} else {
builder.setField(field, buildValue(builder, field, entry.getValue()));
}
}
return builder.build();
}
示例3: buildSaslNegotiateResponse
import com.google.protobuf.Message.Builder; //導入依賴的package包/類
/**
* Process the Sasl's Negotiate request, including the optimization of
* accelerating token negotiation.
* @return the response to Negotiate request - the list of enabled
* authMethods and challenge if the TOKENS are supported.
* @throws SaslException - if attempt to generate challenge fails.
* @throws IOException - if it fails to create the SASL server for Tokens
*/
private RpcSaslProto buildSaslNegotiateResponse()
throws InterruptedException, SaslException, IOException {
RpcSaslProto negotiateMessage = negotiateResponse;
// accelerate token negotiation by sending initial challenge
// in the negotiation response
if (enabledAuthMethods.contains(AuthMethod.TOKEN)) {
saslServer = createSaslServer(AuthMethod.TOKEN);
byte[] challenge = saslServer.evaluateResponse(new byte[0]);
RpcSaslProto.Builder negotiateBuilder =
RpcSaslProto.newBuilder(negotiateResponse);
negotiateBuilder.getAuthsBuilder(0) // TOKEN is always first
.setChallenge(ByteString.copyFrom(challenge));
negotiateMessage = negotiateBuilder.build();
}
sentNegotiate = true;
return negotiateMessage;
}
示例4: buildNegotiateResponse
import com.google.protobuf.Message.Builder; //導入依賴的package包/類
private RpcSaslProto buildNegotiateResponse(List<AuthMethod> authMethods)
throws IOException {
RpcSaslProto.Builder negotiateBuilder = RpcSaslProto.newBuilder();
if (authMethods.contains(AuthMethod.SIMPLE) && authMethods.size() == 1) {
// SIMPLE-only servers return success in response to negotiate
negotiateBuilder.setState(SaslState.SUCCESS);
} else {
negotiateBuilder.setState(SaslState.NEGOTIATE);
for (AuthMethod authMethod : authMethods) {
SaslRpcServer saslRpcServer = new SaslRpcServer(authMethod);
SaslAuth.Builder builder = negotiateBuilder.addAuthsBuilder()
.setMethod(authMethod.toString())
.setMechanism(saslRpcServer.mechanism);
if (saslRpcServer.protocol != null) {
builder.setProtocol(saslRpcServer.protocol);
}
if (saslRpcServer.serverId != null) {
builder.setServerId(saslRpcServer.serverId);
}
}
}
return negotiateBuilder.build();
}
示例5: buildValue
import com.google.protobuf.Message.Builder; //導入依賴的package包/類
@SuppressWarnings("unchecked")
private static Object buildValue(
Message.Builder parentBuilder, FieldDescriptor field, Object value) {
if (field.getType() == FieldDescriptor.Type.MESSAGE) {
if (field.isRepeated()) {}
Message.Builder fieldBuilder = parentBuilder.newBuilderForField(field);
return buildMessage(fieldBuilder, (Map<String, Object>) value);
} else if (field.getType() == FieldDescriptor.Type.ENUM) {
return field.getEnumType().findValueByName((String) value);
} else {
switch (field.getType()) {
case FLOAT: // float is a special case
return Float.valueOf(value.toString());
default:
return value;
}
}
}
示例6: parserAcceptsStringForNumericField
import com.google.protobuf.Message.Builder; //導入依賴的package包/類
@Test
public void parserAcceptsStringForNumericField() throws Exception {
TestAllTypes.Builder builder = TestAllTypes.newBuilder();
mergeFromJson(
"{\n"
+ " \"optionalInt32\": \"1234\",\n"
+ " \"optionalUint32\": \"5678\",\n"
+ " \"optionalSint32\": \"9012\",\n"
+ " \"optionalFixed32\": \"3456\",\n"
+ " \"optionalSfixed32\": \"7890\",\n"
+ " \"optionalFloat\": \"1.5\",\n"
+ " \"optionalDouble\": \"1.25\",\n"
+ " \"optionalBool\": \"true\"\n"
+ "}",
builder);
TestAllTypes message = builder.build();
assertEquals(1234, message.getOptionalInt32());
assertEquals(5678, message.getOptionalUint32());
assertEquals(9012, message.getOptionalSint32());
assertEquals(3456, message.getOptionalFixed32());
assertEquals(7890, message.getOptionalSfixed32());
assertEquals(1.5f, message.getOptionalFloat(), 0.000001);
assertEquals(1.25, message.getOptionalDouble(), 0.000001);
assertEquals(true, message.getOptionalBool());
}
示例7: mapNullValueIsRejected
import com.google.protobuf.Message.Builder; //導入依賴的package包/類
@Test
public void mapNullValueIsRejected() throws Exception {
TestMap.Builder builder = TestMap.newBuilder();
assertThatThrownBy(
() ->
mergeFromJson(
"{\n"
+ " \"int32ToInt32Map\": {null: 1},\n"
+ " \"int32ToMessageMap\": {null: 2}\n"
+ "}",
builder))
.isInstanceOf(InvalidProtocolBufferException.class);
TestMap.Builder builder2 = TestMap.newBuilder();
assertThatThrownBy(
() ->
mergeFromJson(
"{\n"
+ " \"int32ToInt32Map\": {\"1\": null},\n"
+ " \"int32ToMessageMap\": {\"2\": null}\n"
+ "}",
builder2))
.isInstanceOf(InvalidProtocolBufferException.class);
}
示例8: anyInMaps
import com.google.protobuf.Message.Builder; //導入依賴的package包/類
@Test
public void anyInMaps() throws Exception {
TestAny.Builder testAny = TestAny.newBuilder();
testAny.putAnyMap("int32_wrapper", Any.pack(Int32Value.newBuilder().setValue(123).build()));
testAny.putAnyMap("int64_wrapper", Any.pack(Int64Value.newBuilder().setValue(456).build()));
testAny.putAnyMap("timestamp", Any.pack(Timestamps.parse("1969-12-31T23:59:59Z")));
testAny.putAnyMap("duration", Any.pack(Durations.parse("12345.1s")));
testAny.putAnyMap("field_mask", Any.pack(FieldMaskUtil.fromString("foo.bar,baz")));
Value numberValue = Value.newBuilder().setNumberValue(1.125).build();
Struct.Builder struct = Struct.newBuilder();
struct.putFields("number", numberValue);
testAny.putAnyMap("struct", Any.pack(struct.build()));
Value nullValue = Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build();
testAny.putAnyMap(
"list_value",
Any.pack(ListValue.newBuilder().addValues(numberValue).addValues(nullValue).build()));
testAny.putAnyMap("number_value", Any.pack(numberValue));
testAny.putAnyMap("any_value_number", Any.pack(Any.pack(numberValue)));
testAny.putAnyMap("any_value_default", Any.pack(Any.getDefaultInstance()));
testAny.putAnyMap("default", Any.getDefaultInstance());
assertMatchesUpstream(testAny.build(), TestAllTypes.getDefaultInstance());
}
示例9: preservingProtoFieldNames
import com.google.protobuf.Message.Builder; //導入依賴的package包/類
@Test
public void preservingProtoFieldNames() throws Exception {
TestAllTypes message = TestAllTypes.newBuilder().setOptionalInt32(12345).build();
assertMatchesUpstream(message);
assertMatchesUpstream(message, false, true, false);
// The json_name field option is ignored when configured to use original proto field names.
TestCustomJsonName messageWithCustomJsonName =
TestCustomJsonName.newBuilder().setValue(12345).build();
assertMatchesUpstream(message, false, true, false);
// Parsers accept both original proto field names and lowerCamelCase names.
TestAllTypes.Builder builder = TestAllTypes.newBuilder();
mergeFromJson("{\"optionalInt32\": 12345}", builder);
assertEquals(12345, builder.getOptionalInt32());
builder.clear();
mergeFromJson("{\"optional_int32\": 54321}", builder);
assertEquals(54321, builder.getOptionalInt32());
}
示例10: mergeFromJson
import com.google.protobuf.Message.Builder; //導入依賴的package包/類
private void mergeFromJson(
String json, boolean ignoringUnknownFields, Builder builder, Message... additionalTypes)
throws IOException {
MessageMarshaller.Builder marshallerBuilder =
MessageMarshaller.builder()
.register(builder.getDefaultInstanceForType())
.ignoringUnknownFields(ignoringUnknownFields);
for (Message prototype : additionalTypes) {
marshallerBuilder.register(prototype);
}
MessageMarshaller marshaller = marshallerBuilder.build();
marshaller.mergeValue(json, builder);
Message.Builder builder2 = builder.build().newBuilderForType();
marshaller.mergeValue(json.getBytes(StandardCharsets.UTF_8), builder2);
assertThat(builder2.build()).isEqualTo(builder.build());
Message.Builder builder3 = builder.build().newBuilderForType();
try (ByteArrayInputStream bis =
new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8))) {
marshaller.mergeValue(bis, builder3);
}
assertThat(builder3.build()).isEqualTo(builder.build());
}
示例11: buildSaslNegotiateResponse
import com.google.protobuf.Message.Builder; //導入依賴的package包/類
private RpcSaslProto buildSaslNegotiateResponse()
throws IOException, InterruptedException {
RpcSaslProto negotiateMessage = negotiateResponse;
// accelerate token negotiation by sending initial challenge
// in the negotiation response
if (enabledAuthMethods.contains(AuthMethod.TOKEN)) {
saslServer = createSaslServer(AuthMethod.TOKEN);
byte[] challenge = saslServer.evaluateResponse(new byte[0]);
RpcSaslProto.Builder negotiateBuilder =
RpcSaslProto.newBuilder(negotiateResponse);
negotiateBuilder.getAuthsBuilder(0) // TOKEN is always first
.setChallenge(ByteString.copyFrom(challenge));
negotiateMessage = negotiateBuilder.build();
}
sentNegotiate = true;
return negotiateMessage;
}
示例12: getUserInfo
import com.google.protobuf.Message.Builder; //導入依賴的package包/類
private synchronized UserInformation getUserInfo(UserGroupInformation ugi) {
if (ugi == null || authMethod == AuthMethod.DIGEST) {
// Don't send user for token auth
return null;
}
UserInformation.Builder userInfoPB = UserInformation.newBuilder();
if (authMethod == AuthMethod.KERBEROS) {
// Send effective user for Kerberos auth
userInfoPB.setEffectiveUser(ugi.getUserName());
} else if (authMethod == AuthMethod.SIMPLE) {
//Send both effective user and real user for simple auth
userInfoPB.setEffectiveUser(ugi.getUserName());
if (ugi.getRealUser() != null) {
userInfoPB.setRealUser(ugi.getRealUser().getUserName());
}
}
return userInfoPB.build();
}
示例13: convertJsonToProto
import com.google.protobuf.Message.Builder; //導入依賴的package包/類
@SuppressWarnings("unchecked")
public <T extends Message> T convertJsonToProto(T prototype, String json, String extensionName) {
try {
Builder builder = prototype.newBuilderForType();
JsonFormat.parser().merge(json, builder);
return (T) builder.build();
} catch (InvalidProtocolBufferException ex) {
diagCollector.addDiag(
Diag.error(
new SimpleLocation(extensionName),
"Extension %s cannot be converted into proto type %s. Details: %s",
extensionName,
prototype.getDescriptorForType().getFullName(),
ex.getMessage()));
return prototype;
}
}
示例14: getUserInfo
import com.google.protobuf.Message.Builder; //導入依賴的package包/類
private UserInformation getUserInfo(UserGroupInformation ugi) {
if (ugi == null || authMethod == AuthMethod.DIGEST) {
// Don't send user for token auth
return null;
}
UserInformation.Builder userInfoPB = UserInformation.newBuilder();
if (authMethod == AuthMethod.KERBEROS) {
// Send effective user for Kerberos auth
userInfoPB.setEffectiveUser(ugi.getUserName());
} else if (authMethod == AuthMethod.SIMPLE) {
//Send both effective user and real user for simple auth
userInfoPB.setEffectiveUser(ugi.getUserName());
if (ugi.getRealUser() != null) {
userInfoPB.setRealUser(ugi.getRealUser().getUserName());
}
}
return userInfoPB.build();
}
示例15: channelRead
import com.google.protobuf.Message.Builder; //導入依賴的package包/類
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
//System.out.println("channelRead");
RpcCall call = (RpcCall)msg;
if(call == null) {
return;
}
LOG.debug("RpcServer read, call ID: " + call.getCallId() + ", local server:" + ctx.channel().localAddress().toString());
try {
Message response = service.callBlockingMethod(call.getMd(), null, call.getMessage());
if(response != null) {
ResponseHeader.Builder builder = ResponseHeader.newBuilder();
builder.setId(call.getCallId());
builder.setResponseName(call.getMd().getName());
ResponseHeader header = builder.build();
call.setHeader(header);
call.setMessage(response);
ctx.writeAndFlush(call);
callCounter.getAndIncrement();
}
} catch(ServiceException e) {
LOG.error("Rpc Server channelRead exception:" + e.getMessage(), e);
}
}