com.google.protobuf.Any.unpack()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(6.9k)|赞(0)|评价(0)|浏览(753)

本文整理了Java中com.google.protobuf.Any.unpack()方法的一些代码示例,展示了Any.unpack()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Any.unpack()方法的具体详情如下:
包路径:com.google.protobuf.Any
类名称:Any
方法名:unpack

Any.unpack介绍

暂无

代码示例

代码示例来源:origin: googleapis/google-cloud-java

private static <T extends Message> T unpack(Any response, Class<T> clazz)
  throws SpannerException {
 try {
  return response.unpack(clazz);
 } catch (InvalidProtocolBufferException e) {
  throw SpannerExceptionFactory.newSpannerException(
    ErrorCode.INTERNAL, "Error unpacking response", e);
 }
}

代码示例来源:origin: googleapis/google-cloud-java

@Override
 public String parseMetadata(Any metadata) {
  try {
   return metadata.unpack(CreateDatabaseMetadata.class).getDatabase();
  } catch (InvalidProtocolBufferException e) {
   return null;
  }
 }
}

代码示例来源:origin: googleapis/google-cloud-java

@Override
public Database parseResult(Any response) {
 try {
  return Database.fromProto(
    response.unpack(com.google.spanner.admin.database.v1.Database.class), dbClient);
 } catch (InvalidProtocolBufferException e) {
  return null;
 }
}

代码示例来源:origin: Netflix/conductor

@Test
  public void testSimpleMapping() throws JsonGenerationException, JsonMappingException, IOException {
    ObjectMapper m = new JsonMapperProvider().get();
    assertTrue(m.canSerialize(Any.class));

    Struct struct1 = Struct.newBuilder().putFields(
        "some-key", Value.newBuilder().setStringValue("some-value").build()
    ).build();

    Any source = Any.pack(struct1);

    StringWriter buf = new StringWriter();
    m.writer().writeValue(buf, source);

    Any dest = m.reader().forType(Any.class).readValue(buf.toString());
    assertEquals(source.getTypeUrl(), dest.getTypeUrl());

    Struct struct2 = dest.unpack(Struct.class);
    assertTrue(struct2.containsFields("some-key"));
    assertEquals(
        struct1.getFieldsOrThrow("some-key").getStringValue(),
        struct2.getFieldsOrThrow("some-key").getStringValue()
    );
  }
}

代码示例来源:origin: ks-no/eventstore2

public static <T extends Message> T unPackAny(String type, Any any) {
  try {
    return (T) any.unpack(deserializeClasses.get(type));
  } catch (Exception e) {
    throw new RuntimeException("Klarte ikke å pakke opp type " + type + " any " + any,e);
  }
}

代码示例来源:origin: com.google.cloud/google-cloud-spanner

private static <T extends Message> T unpack(Any response, Class<T> clazz)
  throws SpannerException {
 try {
  return response.unpack(clazz);
 } catch (InvalidProtocolBufferException e) {
  throw SpannerExceptionFactory.newSpannerException(
    ErrorCode.INTERNAL, "Error unpacking response", e);
 }
}

代码示例来源:origin: com.google.api/gax-grpc

@Override
 public PackedT apply(Any input) {
  try {
   return input == null || packedClass == null ? null : input.unpack(packedClass);
  } catch (InvalidProtocolBufferException | ClassCastException e) {
   throw new IllegalStateException(
     "Failed to unpack object from 'any' field. Expected "
       + packedClass.getName()
       + ", found "
       + input.getTypeUrl());
  }
 }
}

代码示例来源:origin: googleapis/gax-java

@Override
 public PackedT apply(Any input) {
  try {
   return input == null || packedClass == null ? null : input.unpack(packedClass);
  } catch (InvalidProtocolBufferException | ClassCastException e) {
   throw new IllegalStateException(
     "Failed to unpack object from 'any' field. Expected "
       + packedClass.getName()
       + ", found "
       + input.getTypeUrl());
  }
 }
}

代码示例来源:origin: com.athaydes.protobuf/protobuf-tcp-rpc

static Object convert(Any any, Class<?> type) throws InvalidProtocolBufferException {
  if (Message.class.isAssignableFrom(type)) {
    Class<? extends Message> messageType = type.asSubclass(Message.class);
    if (any.is(messageType)) {
      return any.unpack(messageType);
    }
  }
  // not a protobuf type, try a Java type
  return convertJavaType(any, type);
}

代码示例来源:origin: envoyproxy/java-control-plane

/**
 * Returns the name of the given resource message.
 *
 * @param anyResource the resource message
 * @throws RuntimeException if the passed Any doesn't correspond to an xDS resource
 */
public static String getResourceName(Any anyResource) {
 Class<? extends Message> clazz = RESOURCE_TYPE_BY_URL.get(anyResource.getTypeUrl());
 Preconditions.checkNotNull(clazz, "cannot unpack non-xDS message type");
 try {
  return getResourceName(anyResource.unpack(clazz));
 } catch (InvalidProtocolBufferException e) {
  throw new RuntimeException(e);
 }
}

代码示例来源:origin: tronprotocol/wallet-cli

case AccountCreateContract:
 owner = contract.getParameter()
   .unpack(org.tron.protos.Contract.AccountCreateContract.class).getOwnerAddress();
 break;
case TransferContract:
 owner = contract.getParameter().unpack(org.tron.protos.Contract.TransferContract.class)
   .getOwnerAddress();
 break;
case TransferAssetContract:
 owner = contract.getParameter()
   .unpack(org.tron.protos.Contract.TransferAssetContract.class).getOwnerAddress();
 break;
case VoteAssetContract:
 owner = contract.getParameter().unpack(org.tron.protos.Contract.VoteAssetContract.class)
   .getOwnerAddress();
 break;
case VoteWitnessContract:
 owner = contract.getParameter().unpack(org.tron.protos.Contract.VoteWitnessContract.class)
   .getOwnerAddress();
 break;
case WitnessCreateContract:
 owner = contract.getParameter()
   .unpack(org.tron.protos.Contract.WitnessCreateContract.class).getOwnerAddress();
 break;
case AssetIssueContract:
 owner = contract.getParameter().unpack(org.tron.protos.Contract.AssetIssueContract.class)
   .getOwnerAddress();
 break;
case ParticipateAssetIssueContract:

代码示例来源:origin: com.xorlev.grpc-jersey/jersey-rpc-support

public static Response createJerseyResponse(Throwable t) {
  GrpcErrorUtil.GrpcError grpcError = GrpcErrorUtil.throwableToStatus(t);
  int httpStatusCode = GrpcErrorUtil.grpcToHttpStatus(grpcError.getStatus());
  Response.ResponseBuilder httpResponse = Response.status(httpStatusCode);
  try {
    for (Any extra : grpcError.getPayload().getDetailsList()) {
      if (extra.is(RetryInfo.class)) {
        RetryInfo retryInfo = extra.unpack(RetryInfo.class);
        if (retryInfo.hasRetryDelay()) {
          httpResponse.header("Retry-After", Durations.toSeconds(retryInfo.getRetryDelay()));
        }
      }
    }
    httpResponse.entity(JsonFormat.printer().print(grpcError.getPayload().toBuilder().clearDetails().build()));
  } catch (InvalidProtocolBufferException e) {
    // this should never happen
    throw new RuntimeException(e);
  }
  return httpResponse.build();
}

代码示例来源:origin: tronprotocol/wallet-cli

case AccountCreateContract:
 AccountCreateContract accountCreateContract = contract.getParameter()
   .unpack(AccountCreateContract.class);
 result += "type: ";
 result += accountCreateContract.getType();
case AccountUpdateContract:
 AccountUpdateContract accountUpdateContract = contract.getParameter()
   .unpack(AccountUpdateContract.class);
 if (accountUpdateContract.getAccountName() != null
   && !accountUpdateContract.getAccountName().isEmpty()) {
case TransferContract:
 TransferContract transferContract = contract.getParameter()
   .unpack(TransferContract.class);
 result += "owner_address: ";
 result += WalletApi
case TransferAssetContract:
 TransferAssetContract transferAssetContract = contract.getParameter()
   .unpack(TransferAssetContract.class);
 result += "asset_name: ";
 result += new String(transferAssetContract.getAssetName().toByteArray(),
case VoteAssetContract:
 VoteAssetContract voteAssetContract = contract.getParameter()
   .unpack(VoteAssetContract.class);
 break;
case VoteWitnessContract:

相关文章