-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Support of google.protobuf.Any #435
Comments
This is not supported currently. Can you provide a link to the documentation passage on this? |
Sure, here comes the link: https://developers.google.com/protocol-buffers/docs/proto3#any |
Thanks! What you can do already is to include google/protobuf/any.proto in your project, and import it within the file in question. Basically, it's just: syntax = "proto3";
package google.protobuf;
message Any {
string type_url = 1;
bytes value = 2;
} You are then able to work with the Any type manually until a better mechanism is in place in protobuf.js. |
Thanks for the idea. |
I would highly appreciate, if someone could schedule this as a further enhancement. |
👍 Need this too. |
It's not very clear to me what's the optimal way to use the workaround mentioned above. I have a minimal working project below:
samples.js const ProtoBuf = require('protobufjs');
const builder = ProtoBuf.loadProtoFile('./myservice.proto');
const Any = builder.build('Any');
const SampleA = builder.build('SampleA');
const SampleB = builder.build('SampleB');
const Response = builder.build('Response');
const pack = (message, prefix) => {
return new Any({
type_url: path.join((prefix || ''), message.toString()),
value: message.encode()
});
};
const unpack = (message) => {
const b = builder.build(message.type_url.split('.')[1]);
return b.decode(message.value);
};
// Pack and create a response
const sampleA = new SampleA({fieldA: 'testa'});
const sampleB = new SampleA({fieldA: 'testa'});
const response = new Response({
id: '1234',
samples: [pack(sampleA), pack(sampleB)]
});
// Unpack
const samples = response.samples.map((sample) => {
return unpack(sample);
});
console.log(samples); This works and I get back what I expected. However, I have a couple of questions:
I appreciate your input on this! |
Closing this for now. Feel free to send a pull request if this is still a requirement. |
is this working now? |
It doesn't seem to work as expected yet: #764 |
loadProtoFile and build are not working in the latest version (matrix-io/matrix-creator-malos#53). So can someone please provide example to use google.protobuf.any. |
+1 |
1 similar comment
+1 |
+1 |
It's likely that I find ways to solve the Below are the detail ways. for example, i want to serialize a JS Object depending .proto file like this. syntax = "proto3";
import "google/protobuf/any.proto";
package request
message Message {
int32 version = 6;
google.protobuf.Any body = 7;
}
message Heartbeat {
int64 timestamp = 1;
int32 expires = 2;
} javascript snippet // serializition
const msg = new proto.request.Message()
const hb = new proto.request.Heartbeat()
hb.setTimestamp('12312421412414')
const any = new proto.google.protobuf.Any()
any.pack(hb.serializeBinary(), 'request.Heartbeat')
msg.setBody(any)
console.log(msg.serializeBinary())
// deserializition. input: byte stream
const msgInstance = proto.request.Message.deserializeBinary(bytes)
const msg = msgInstance.toObject()
let bodyInstance = null
const bodyPacked = msgInstance.getBody()
if (bodyPacked) {
bodyInstance = bodyPacked.unpack(proto.request.Heartbeat.deserializeBinary, 'request.Heartbeat')
}
console.log({
...msg,
body: bodyInstance && bodyInstance.toObject()
}) |
I used google proto buf official solution instead of |
Finally, I used
import { request } './compiled';
// serializition
const message = request.Message.create({ version: 1 });
const heartbeat = request.Heartbeat.create({ timestamp: '123', expires: 123});
message.body = {
type_url: 'type.googleapis.com/dispatcher.Heartbeat',
value: request.Heartbeat.encode(heartbeat).finish()
};
const buffer = request.Message.encode(message).finish();
// deserializition
const message = request.Message.decode(buffer);
const heartbeat = request.Heartbeat.decode(message.body.value); |
helped me a lot, thanks a lot |
While evaluating protobuf.js for a project, I also stumbled upon this issue in search for information how to use For packing a Message in a somewhat typesafe way in TypeScript, you could use this snippet: interface SomeProtoType {
new (...args: any[]): any;
getTypeUrl(prefix: string): string;
encode(message: InstanceType<this>): pbWriter;
}
function packAny<T extends SomeProtoType>(proto: T, instance: InstanceType<T> | ConstructorParameters<T>[0]) {
return <proto.google.protobuf.IAny>{
type_url: proto.getTypeUrl("prefix"),
value: proto.encode(instance).finish(),
};
};
// usage example:
import { google, HelloRequest } from "./messages";
let any: google.protobuf.IAny = packAny(HelloRequest, { name: "Alice" }); This is conceptually similar to what I also found wrappers for the Lines 39 to 102 in 0a0cdb6
|
I started using proto3, but it seems that "google.protobuf.Any" datatype is not yet supported. Please let me know what I am doing wrong, ori if that is still not yet implemented.
This is my proto file:
This is my code used to read the proto file:
This is the error I receive:
Error: unresolvable type reference in Message.Field .vnd.proto.Test1.number: google.protobuf.Any
Thanks in Advance.
The text was updated successfully, but these errors were encountered: