简体   繁体   English

如何使用类型开关来确定 protoreflect.MessageDescriptor 的类型?

[英]How do I use a type switch to determine the type of a protoreflect.MessageDescriptor?

I am writing a protoc generation plugin using the protogen package.我正在使用protogen package 编写一个 protoc 生成插件。

I am looping the fields of a message and want to determine if a field is one of a few different message types.我正在循环消息的字段,并想确定一个字段是否是几种不同的消息类型之一。

It is possible to get the name of the message type as a string using:可以使用以下方法将消息类型的名称作为字符串获取:

field.Desc.Message().FullName() // mypackage.MyMessage

The problem with this approach is that I will need to switch against a string, which is error-prone:这种方法的问题是我需要切换一个容易出错的字符串:

switch field.Desc.Message().FullName(){
  case "mypackage.MyMessage":

  case "mypackage.MyMessage2":
}

Is there anyway to do this using a type assertion?无论如何使用类型断言来做到这一点? I tried to create an instance of the message using dynamicpc , but the type assertion does not work:我尝试使用dynamicpc创建消息的实例,但类型断言不起作用:

mt := dynamicpb.NewMessage(field.Desc.Message())

msg, ok := mt.(*mypackage.MyMessage) // ok is false despite field.Desc.Message().FullName() returning mypackage.MyMessage

The function dynamicpb.NewMessage doesn't create a Golang structure mypackage.MyMessage . dynamicpb.NewMessage dynamicpb.NewMessage 不会创建 Golang 结构mypackage.MyMessage Instead it creates a data structure that marshals into the same binary form as mypackage.MyMessage相反,它创建了一个数据结构,可以编组为与mypackage.MyMessage相同的二进制形式

Have a look inside the Message data structure :查看Message数据结构内部:

// Operations which modify a Message are not safe for concurrent use.
type Message struct {
    typ     messageType
    known   map[protoreflect.FieldNumber]protoreflect.Value
    ext     map[protoreflect.FieldNumber]protoreflect.FieldDescriptor
    unknown protoreflect.RawFields
}

It is just a storage for field values along with with fields' metadata.它只是字段值以及字段元数据的存储。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何确定类型是否为结构 - How to determine if type is a struct switch 语句中的泛型类型 - Generic type in a switch statement 如何在 Firebase Firestore 中查询 Flutter 中 Geopoint 类型的字段? - How do I Query Firebase Firestore for a field of type Geopoint in Flutter? 如何更改 Redshift 中的列数据类型? - How do I change column data type in Redshift? 如何通过 kubeadm 在 GCE 上使用 LoadBalancer 类型的服务? - How can I use LoadBalancer type service on GCE with kubeadm? 我如何获取 firebase 中 onSnapshot 中发生的更改类型 - how do i get the type of change that happened in onSnapshot in firebase 如何将我的项目从 github 存储库转移到 aws 服务器? 我可以使用哪种类型的 aws 服务(ec2 或 s3)? - how can i transfer my project from github repository to aws server? which type of aws services(ec2 or s3) i can use to do so? 我如何修改代码以获得 Stream<dynamic> 为 stream 生成器键入 stream?</dynamic> - How do i modify the code to have a Stream<dynamic> type stream for stream builder? 如何为具有 $oid 唯一标识符类型的嵌入式文档创建 Azure 认知搜索索引? - How do I create an Azure Cognitive Search index for embedded document with $oid unique identifier type? 如何使用反射知道切片数据类型? - how to use reflect know the slice data type?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM