Protobuf.js: Support of google.protobuf.Any

Created on 26 May 2016  路  13Comments  路  Source: protobufjs/protobuf.js

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:

syntax = "proto3";

package vnd.proto;

message Test1 {
    uint32              key = 1;
    google.protobuf.Any value   = 2;
}

This is my code used to read the proto file:

var ProtoBuf = dcodeIO.ProtoBuf;

//Synchronously
var builder = ProtoBuf.loadProtoFile("test.proto"),
    proto = builder.build("vnd.proto");
    console.log(proto);

This is the error I receive:
Error: unresolvable type reference in Message.Field .vnd.proto.Test1.number: google.protobuf.Any

Thanks in Advance.

Most helpful comment

I would highly appreciate, if someone could schedule this as a further enhancement.

All 13 comments

This is not supported currently. Can you provide a link to the documentation passage on this?

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.
What timeframe you assume for the implementation of the Any support in protobuf.js?

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:

myservice.proto

syntax = "proto3";  

package myservice;

message Any {
  string type_url = 1;
  bytes value = 2;
}

message Request {
    string id = 1;
}

message SampleA {
    string fieldA = 1;
}

message SampleB {
    string fieldB = 1;
}

message Response {
    string id = 1;
    repeated Any samples = 2;
}

service MyService {
  rpc Get (Request) returns (Response);
}

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:

  1. Is there a better way to get the full name of a message to construct the type_url. I used .toString() in my code but I had a look at the implementations of other languages and messages seem to have some kind of getter for the name.
  2. When encoding and decoding an Any message should I use .encode and .decode or is there a better way?

I appreciate your input on this!

Closing this for now.

protobuf.js 6.0.0

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: https://github.com/dcodeIO/protobuf.js/issues/764

loadProtoFile and build are not working in the latest version (https://github.com/matrix-io/matrix-creator-malos/issues/53). So can someone please provide example to use google.protobuf.any.

+1

+1

Was this page helpful?
0 / 5 - 0 ratings