I am writing a protoc generation plugin using the protogen package.
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:
mt := dynamicpb.NewMessage(field.Desc.Message())
msg, ok := mt.(*mypackage.MyMessage) // ok is false despite field.Desc.Message().FullName() returning mypackage.MyMessage
Aucun commentaire:
Enregistrer un commentaire