How can I check if I can unpack an any protobuf message to certain message type in Javascript?

21 views
Skip to first unread message

avi...@verloop.io

unread,
Dec 28, 2018, 1:43:35 AM12/28/18
to grpc.io

Consider the following proto:


message Payload {
  google.protobuf.Any Object = 1;
}

message HelloMsg {
    string Name = 1;
}

message HiMsg {
    string Result = 1;
}


So the Object field in Payload can have any valid protobuf message.


My consumer is written in Javacript. The method which receives the message, knows which kind of protobuf message it wants. So, when this handler method receives the Payload, I want to know if I can unpack it to the protobuf message it expects. But how?


function myHandler(payload) {
    expectedPaylaodType = HelloMsg;
    // now how do I unpack, payload.getObject() to HelloMsg?

}


I tried following, but it failed:


let anyObj = payload.getObject();
let targetObj = expectedPaylaodType();
let unpackedObj = anyObj.unpack(anyObj.deserializeBinary, targetObj.getTypeName());


It is because, targetObj.getTypeName is undefined.


If above is not possible, then I can check with the typeURL field? The any object has this information. But I am not able to figure out against which field of targetObj I can check this. Something like this:


if (anyObj.getTypeUrl() == targetObj.typeUrl) {
}


targetObj.typeUrl does not exist, but I am not sure against which value I can compare it to.

Reply all
Reply to author
Forward
0 new messages