Closed topofstack closed 1 year ago
I’m not sure why you would want to use dynamicpb
when you have the actual message type?
var msg pb.MyMessage
if err := proto.Unmarshal(bytes, &msg) {
return nil, err
}
goTime := msg.Tstamp.AsTime() // get the Go time.Time object
Thanks for response!
I'm trying to write some kind of an abstract proto parser which should extract fields from different protos/messages
It should work like:
1) User passes MessageDescriptor to the Parser. It acts as a template for incoming binary messages. Now I can inspect internal representation of the incoming byte arrays using protoreflect
.
2) User calls something like Parser.parse(byte[])
and in this method passed byte array will be dynamically inspected without knowing it's actual underlying message type. All I know is that this byte array corresponds with the MessageDescriptor
i got on the step 1. I need to extract all fields and cast them into Go types for later usage.
The conversion from google.protobuf.Timestamp
to time.Time
from the code above doesn't look good. So, I'm wondering, if there's a more clear way to perform it.
I’m unsure, but a type switch might enable you to go from .Message()
to the timestamppb
and then you should be able to .AsTime()
at that point…
Sadly, all out-of-the-box conversions are defined in https://github.com/protocolbuffers/protobuf-go/blob/v1.30.0/reflect/protoreflect/value_union.go. So, it looks like the only way is to treat timestamp as a message and parse it by hand like I initially did.
I'm pretty new to protobuf reflection and the question may sound pretty dumb. Is there any other way I can get access to underlying Go type using
protoreflect
?Here's small proto which includes another widely used proto:
Here's the code I use to parse incoming byte array on the basis of the model generated by proto-compiler
Is there any smarter way to implement it?