I have some sort of complex protobuf object. It’s a request sent to my GRPC endpoint. I want to just process it if I didn’t before. So I want to hash the object to some string and store it in my Redis database. I used ObjectHash-Proto but with new versions of protobuf-compiler I got this error:
got an unexpected struct of type 'impl.MessageState' for field {Name:state PkgPath:... Type:impl.MessageState Tag: Offset:0 Index:[0] Anonymous:false}
Seems it does not support structs and new version of protobuf-compiler generates a code containing struct.
I can’t generate some sort of ID for each request. The ID is actually the hash of the whole of the object.
2
Answers
If you have a
proto.Message
[1], then you get a Marshal function [2] for free.So after you Marshal the message, you can just pass the bytes to
base64
, ormd5
or whatever you want:Proto serialization is not stable, thus you can’t rely on marshalling and hashing the output to produce the same hash for the same message.
From https://developers.google.com/protocol-buffers/docs/reference/go/faq#hash
The closest solution I could find is deepmind objecthash-proto but there have been no contributions in last 4 years so I assume it might be outdated