-
Notifications
You must be signed in to change notification settings - Fork 807
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Casting Any to proto.Message #476
Comments
Hey. |
given the message: message EventMessage {
string event = 1;
google.protobuf.Any value = 2;
} This will be compiled to: type EventMessage struct {
Event string `protobuf:"bytes,1,opt,name=event,proto3" json:"event,omitempty"`
Value *google_protobuf.Any `protobuf:"bytes,2,opt,name=value" json:"value,omitempty"`
} It will be nice if the |
So the And So you will be able to use the value where you need a proto.Message type. func foo() {
em := &EventMessage{}
myProtoMessageFunc(em.Value)
}
func myProtoMessageFunc(pb proto.Message) {
fmt.Printf("Here is a pb message: %+v\n", pb)
} Is that what you meant? With regards to un/marshaling a any type. Here are some helper functions: Edit:
Did you mean if you unmarshal a type EventMessage struct {
Event string `protobuf:"bytes,1,opt,name=event,proto3" json:"event,omitempty"`
Value proto.Message `protobuf:"bytes,2,opt,name=value" json:"myany,omitempty"`
}
And then the ```Value``` field would be the Unmarshaled message which was inside the ```Any``` ? |
Exactly- when you write: message EventMessage {
string event = 1;
google.protobuf.Any value = 2 [gogo.anyprotofmt=true];
} It'll be compiled to: type EventMessage struct {
Event string `protobuf:"bytes,1,opt,name=event,proto3" json:"event,omitempty"`
Value *proto.Message `protobuf:"bytes,2,opt,name=value" json:"value,omitempty"`
} And you'll be able to use it as: msg := EventMessage{}
msg.Value = &SomeOtherMessage{} And behind the scene- it'll use Since the |
To be exact- the Idea is that when you do |
Ah ok, Thanks for the clarification. |
It's exactly like the stdtime issue- we need to add a few lines to convert the google timestamp to the standard time... I think it can be useful to do that seamlessly by gogo. If you can hint a little maybe ill be able to draft something |
Hey, sorry for the wait, I had to think about it a bit. I am still unsure about the boilerplate you are experiencing. Here is a simple use case: message EventMessage {
string name = 1;
google.protobuf.Any myany = 2;
}
message Foo {
string bar = 1;
} simple go example: event := &EventMessage{Name: "firstevent"}
myAnyMessage := &Foo{
Bar: "bar",
}
marshaledAny, err := types.MarshalAny(myAnyMessage)
if err != nil {
panic(err)
}
event.Myany = marshaledAny
b, err := proto.Marshal(event)
if err != nil {
panic(err)
}
receivedEvent := &EventMessage{}
proto.Unmarshal(b, receivedEvent)
newReceivedAny := &Foo{}
err = types.UnmarshalAny(receivedEvent.Myany, newReceivedAny)
if err != nil {
panic(err)
}
fmt.Printf("AnyMessage: %#v\n", newReceivedAny)
pbMessage(newReceivedAny) Currently you have to unmarshal the newReceivedAny := &Foo{}
err = types.UnmarshalAny(receivedEvent.Myany, newReceivedAny)
if err != nil {
panic(err)
} However, even if we "auto" unmarshal it to a switch t := protoMessage.(type) {
case *Foo:
fmt.Printf("Foo message: %v\n", pb)
default:
fmt.Printf("Unknown message of type: %v, %T\n", t, pb)
} So you would just get different "boilerplate" you have to write? |
Is there a way to cast the WKT
Any
to aproto.Message
like we doing with thestdtime
in order to reduce boilerplate?The text was updated successfully, but these errors were encountered: