找回密码
 立即注册
查看: 251|回复: 0

Proto3:Any

[复制链接]
发表于 2022-11-18 18:04 | 显示全部楼层 |阅读模式
The Any message type lets you use messages as embedded types without having their .proto definition. An Anycontains an arbitrary serialized message as bytes, along with a URL that acts as a globally unique identifier for and resolves to that message's type. To use the Any type, you need to importgoogle/protobuf/any.proto.
Any message类型允许我们在没有.proto定义的情况下,作为内嵌类型使用message类型。一个Any类型包含了一个任意序列化的消息作为bytes(字节),以及一个URL代表着该message类型的全局唯一标识符,并解析为该message的类型。为了使用Any类型,你需要导入google/protobuf/any.proto。
import "google/protobuf/any.proto";message ErrorStatus {  string message = 1;  repeated google.protobuf.Any details = 2;}
The default type URL for a given message type is type.googleapis.com/_packagename_._messagename_.
给定message类型的默认值类型URL是type.googleapis.com/_packagename_._messagename_。

Different language implementations will support runtime library helpers to pack and unpack Any values in a typesafe manner – for example, in Java, the Any type will have special pack() and unpack() accessors, while in C++ there are PackFrom() and UnpackTo() methods:
不同的语言实现将支持运行时库来帮助以类型安全的方式打包或解包Any数据 - 举个例子,在Java中,Any类型会有特殊的pack()和unpack()访问器,而C++提供了PackFrom()和UnpackTo()方法:
// Storing an arbitrary message type in Any.NetworkErrorDetails details = ...;ErrorStatus status;status.add_details()->PackFrom(details);// Reading an arbitrary message from Any.ErrorStatus status = ...;for (const google::protobuf::Any& detail : status.details()) {  if (detail.Is<NetworkErrorDetails>()) {    NetworkErrorDetails network_error;    detail.UnpackTo(&network_error);    ... processing network_error ...  }}
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-11-24 20:00 , Processed in 0.089780 second(s), 25 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表