下面列出了怎么用com.google.protobuf.Message.Builder的API类实例代码及写法,或者点击链接到github查看源代码。
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();
}
@SuppressWarnings("InconsistentOverloads")
private static void mergeFromJson(
boolean ignoringUnknownFields, String json, 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());
}
Map<String, Object> getCtxMap(Builder b, WebContextData ctx, DefaultWebReq req) {
Map<String, Object> m = null;
for (FieldDescriptor field : b.getDescriptorForType().getFields()) {
String name = field.getName();
Object value = getValue(ctx, req, name);
if (value != null) {
if (m == null) m = new HashMap<>();
m.put(name, value);
req.getParameters().putIfAbsent(name,value);
}
}
return m;
}
@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;
}
}
static Builder getFieldBuilder(Builder b, Descriptors.FieldDescriptor f) {
try {
if( b instanceof MapEntry.Builder ) {
MapEntry.Builder bb = (MapEntry.Builder)b;
return bb.newBuilderForField(f);
}
String fieldName = f.getName();
String methodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1) + "Builder";
Method method = b.getClass().getDeclaredMethod(methodName, dummyTypes);
Builder builder = (Builder) method.invoke(b, dummyParameters);
return builder;
} catch (Exception e) {
throw new RuntimeException("getFieldBuilder exception", e);
}
}
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;
}
static public Message toMessage(Builder b, Map<String, Object> params, Map<String, Object> ctx) {
for (FieldDescriptor field : b.getDescriptorForType().getFields()) {
String name = field.getName();
Object value = getValue(params, ctx, name);
if (value == null) continue;
if (field.isMapField()) {
objToMap(b, field, value);
} else if (field.isRepeated()) {
objToMessageObjRepeated(b, value, field);
} else {
if (value instanceof List) {
value = ((List) value).get(0);
if (value == null) continue;
}
objToMessageObj(b, value, field);
}
}
return b.build();
}
static Builder getFieldBuilder(Builder b, FieldDescriptor f) {
try {
if( b instanceof MapEntry.Builder ) {
MapEntry.Builder bb = (MapEntry.Builder)b;
return bb.newBuilderForField(f);
}
String fieldName = f.getName();
String methodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1) + "Builder";
Method method = b.getClass().getDeclaredMethod(methodName, dummyTypes);
Builder builder = (Builder) method.invoke(b, dummyParameters);
return builder;
} catch (Exception e) {
throw new RuntimeException("getFieldBuilder exception", e);
}
}
static public Message toMessage(Builder b, Object bean) {
try {
for (Descriptors.FieldDescriptor field : b.getDescriptorForType().getFields()) {
String name = field.getName();
Object value = getValue(bean, name);
if (value == null) continue;
if (field.isMapField()) {
objToMap(b, field, value);
} else if (field.isRepeated()) {
objToMessageObjRepeated(b, value, field);
} else {
objToMessageObj(b, value, field);
}
}
return b.build();
} catch(Exception e) {
log.error("toMessage exception, e="+e.getMessage());
return null;
}
}
static Builder getFieldBuilder(Builder b, Descriptors.FieldDescriptor f) {
try {
if( b instanceof MapEntry.Builder ) {
MapEntry.Builder bb = (MapEntry.Builder)b;
return bb.newBuilderForField(f);
}
String fieldName = f.getName();
String methodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1) + "Builder";
Method method = b.getClass().getDeclaredMethod(methodName, dummyTypes);
Builder builder = (Builder) method.invoke(b, dummyParameters);
return builder;
} catch (Exception e) {
throw new RuntimeException("getFieldBuilder exception", e);
}
}
@Test
public void sortedMapKeys() throws Exception {
TestMap.Builder mapBuilder = TestMap.newBuilder();
mapBuilder.putStringToInt32Map("\ud834\udd20", 3); // utf-8 F0 9D 84 A0
mapBuilder.putStringToInt32Map("foo", 99);
mapBuilder.putStringToInt32Map("xxx", 123);
mapBuilder.putStringToInt32Map("\u20ac", 1); // utf-8 E2 82 AC
mapBuilder.putStringToInt32Map("abc", 20);
mapBuilder.putStringToInt32Map("19", 19);
mapBuilder.putStringToInt32Map("8", 8);
mapBuilder.putStringToInt32Map("\ufb00", 2); // utf-8 EF AC 80
mapBuilder.putInt32ToInt32Map(3, 3);
mapBuilder.putInt32ToInt32Map(10, 10);
mapBuilder.putInt32ToInt32Map(5, 5);
mapBuilder.putInt32ToInt32Map(4, 4);
mapBuilder.putInt32ToInt32Map(1, 1);
mapBuilder.putInt32ToInt32Map(2, 2);
mapBuilder.putInt32ToInt32Map(-3, -3);
TestMap mapMessage = mapBuilder.build();
assertMatchesUpstream(mapMessage, false, false, false, false, true);
TestMap emptyMap = TestMap.getDefaultInstance();
assertMatchesUpstream(emptyMap, false, false, false, false, true);
}
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;
}
@SuppressWarnings("unchecked")
private static Object buildValue(
Message.Builder parentBuilder, FieldDescriptor field, Object value) {
if (field == null) {
return 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;
}
}
}
@Test
public void unknownEnumValue() throws Exception {
TestAllTypes message =
TestAllTypes.newBuilder()
.setOptionalNestedEnumValue(12345)
.addRepeatedNestedEnumValue(12345)
.addRepeatedNestedEnumValue(0)
.build();
assertMatchesUpstream(message);
TestMap.Builder mapBuilder = TestMap.newBuilder();
mapBuilder.putInt32ToEnumMapValue(1, 0);
Map<Integer, Integer> mapWithInvalidValues = new HashMap<>();
mapWithInvalidValues.put(2, 12345);
mapBuilder.putAllInt32ToEnumMapValue(mapWithInvalidValues);
TestMap mapMessage = mapBuilder.build();
assertMatchesUpstream(mapMessage);
}
@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());
}
@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);
}
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();
}
@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());
}
@Test
public void parserSupportAliasEnums() throws Exception {
TestAllTypes.Builder builder = TestAllTypes.newBuilder();
String json = "{\n" + " \"optionalAliasedEnum\": \"QUX\"\n" + "}";
mergeFromJson(json, builder);
assertThat(builder.getOptionalAliasedEnum()).isEqualTo(AliasedEnum.ALIAS_BAZ);
builder = TestAllTypes.newBuilder();
json = "{\n" + " \"optionalAliasedEnum\": \"qux\"\n" + "}";
mergeFromJson(json, builder);
assertThat(builder.getOptionalAliasedEnum()).isEqualTo(AliasedEnum.ALIAS_BAZ);
builder = TestAllTypes.newBuilder();
json = "{\n" + " \"optionalAliasedEnum\": \"bAz\"\n" + "}";
mergeFromJson(json, builder);
assertThat(builder.getOptionalAliasedEnum()).isEqualTo(AliasedEnum.ALIAS_BAZ);
}
public static Builder generateBuilder(Class<?> messageCls) {
try {
Method method = messageCls.getDeclaredMethod("newBuilder", dummyTypes);
Builder builder = (Builder) method.invoke(null, dummyParameters);
return builder;
} catch (Exception e) {
log.error("generateBuilder exception, e="+e.getMessage()+",messageCls="+messageCls);
return null;
}
}
/**
* Generates a random protocol buffer, filling in all required fields but
* with a p chance of not setting an optional field and p chance of having
* an empty repeated field.
*/
@SuppressWarnings("unchecked")
public E generate(double p) {
Builder builder = instance.newBuilderForType();
Descriptor descriptor = instance.getDescriptorForType();
for (FieldDescriptor field : descriptor.getFields()) {
if (!field.isRequired() && random.nextDouble() < p) {
continue;
}
builder.setField(field, getRandomValue(field, p));
}
return (E) builder.build();
}
private static void objToMap(Builder b, Descriptors.FieldDescriptor field, Object v0) {
if( !(v0 instanceof Collection) ) return;
Descriptors.Descriptor type = field.getMessageType();
Descriptors.FieldDescriptor keyField = type.findFieldByName("key");
Descriptors.FieldDescriptor valueField = type.findFieldByName("value");
if (keyField != null && valueField != null) {
Collection v0c = (Collection)v0;
for(Object e: v0c ) {
MapEntry entry = (MapEntry)e;
Object key = entry.getKey();
Object value = entry.getValue();
com.google.protobuf.Message.Builder entryBuilder = b.newBuilderForField(field);
Object k = objToMessageObjInner(entryBuilder,key,keyField,false);
Object v = objToMessageObjInner(entryBuilder,value,valueField,false);
if(k == null || v == null ) continue;
entryBuilder.setField(keyField, k);
entryBuilder.setField(valueField, v);
b.addRepeatedField(field, entryBuilder.build());
}
} else {
throw new RuntimeException("Invalid map field");
}
}
static void objToMessageObjRepeated(Builder b, Object value, Descriptors.FieldDescriptor field) {
List<Object> list = TypeSafe.anyToList(value);
if (list == null) return;
for (Object o : list) {
Object newObj = objToMessageObjInner(b, o, field, true);
if (newObj != null)
b.addRepeatedField(field, newObj);
}
}
private static <T extends Message> Builder newBuilder(Class<T> messageType) {
try {
return (Builder) messageType.getMethod("newBuilder").invoke(null);
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}
static public Message toMessage(Builder b, String json) {
try {
JsonFormat.parser().merge(json, b);
return b.build();
} catch(Exception e) {
log.error("toMessage exception, e="+e.getMessage());
return null;
}
}
public static Builder generateBuilder(Class<?> messageCls) {
try {
Method method = messageCls.getDeclaredMethod("newBuilder", dummyTypes);
Builder builder = (Builder) method.invoke(null, dummyParameters);
return builder;
} catch (Exception e) {
log.error("generateBuilder exception, e="+e.getMessage()+",messageCls="+messageCls);
return null;
}
}
private static void objToMap(Builder b, FieldDescriptor field, Object map0) {
if( !(map0 instanceof Map) ) return;
Descriptors.Descriptor type = field.getMessageType();
FieldDescriptor keyField = type.findFieldByName("key");
FieldDescriptor valueField = type.findFieldByName("value");
if (keyField != null && valueField != null) {
Map map = (Map)map0;
for(Object e: map.entrySet() ) {
Map.Entry entry = (Map.Entry)e;
Object key = entry.getKey();
Object value = entry.getValue();
com.google.protobuf.Message.Builder entryBuilder = b.newBuilderForField(field);
Object k = objToMessageObjInner(entryBuilder,key,keyField,false);
Object v = objToMessageObjInner(entryBuilder,value,valueField,false);
if(k == null || v == null ) continue;
entryBuilder.setField(keyField, k);
entryBuilder.setField(valueField, v);
b.addRepeatedField(field, entryBuilder.build());
}
} else {
throw new RuntimeException("Invalid map field");
}
}
static void objToMessageObjRepeated(Builder b, Object value, FieldDescriptor field) {
List<Object> list = TypeSafe.anyToList(value);
if (list == null) return;
for (Object o : list) {
Object newObj = objToMessageObjInner(b, o, field, true);
if (newObj != null)
b.addRepeatedField(field, newObj);
}
}
static Builder getRepeatedFieldBuilder(Builder b, String fieldName) {
try {
String methodName = "add" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1) + "Builder";
Method method = b.getClass().getDeclaredMethod(methodName, dummyTypes);
Object builder = method.invoke(b, dummyParameters);
return (Builder) builder;
} catch (Exception e) {
throw new RuntimeException("getFieldBuilder exception", e);
}
}
public static Builder generateBuilder(Class<?> messageCls) {
try {
Method method = messageCls.getDeclaredMethod("newBuilder", dummyTypes);
Builder builder = (Builder) method.invoke(null, dummyParameters);
return builder;
} catch (Exception e) {
log.error("generateBuilder exception, e="+e.getMessage()+",messageCls="+messageCls);
return null;
}
}