Skip to content
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 for Any.pack #72

Closed
long1eu opened this issue Oct 15, 2020 · 2 comments
Closed

Support for Any.pack #72

long1eu opened this issue Oct 15, 2020 · 2 comments

Comments

@long1eu
Copy link

long1eu commented Oct 15, 2020

Hi!

Tha google.protobuf.Any type has a pack and unpack method. If is possible to added them? These are the js definitions.

/**
 * Packs the given message instance into this Any.
 * For binary format usage only.
 * @param {!Uint8Array} serialized The serialized data to pack.
 * @param {string} name The type name of this message object.
 * @param {string=} opt_typeUrlPrefix the type URL prefix.
 */
proto.google.protobuf.Any.prototype.pack = function(serialized, name,
                                                    opt_typeUrlPrefix) {
  if (!opt_typeUrlPrefix) {
    opt_typeUrlPrefix = 'type.googleapis.com/';
  }

  if (opt_typeUrlPrefix.substr(-1) != '/') {
    this.setTypeUrl(opt_typeUrlPrefix + '/' + name);
  } else {
    this.setTypeUrl(opt_typeUrlPrefix + name);
  }

  this.setValue(serialized);
};


/**
 * @template T
 * Unpacks this Any into the given message object.
 * @param {function(Uint8Array):T} deserialize Function that will deserialize
 *     the binary data properly.
 * @param {string} name The expected type name of this message object.
 * @return {?T} If the name matched the expected name, returns the deserialized
 *     object, otherwise returns null.
 */
proto.google.protobuf.Any.prototype.unpack = function(deserialize, name) {
  if (this.getTypeName() == name) {
    return deserialize(this.getValue_asU8());
  } else {
    return null;
  }
};
@agreatfool
Copy link
Owner

Hi @long1eu,

I have made some test.

  1. git clone git@github.com:agreatfool/grpc_tools_node_protoc_ts.git
  2. cd grpc_tools_node_protoc_ts/examples
  3. add file examples/proto/goolge/protobuf/any.proto, source comes from here
    anyproto
  4. edit book.proto file, add import "google/protobuf/any.proto"; and:
    anymessage
  5. run examples/bash/build.sh, see what changed in file: examples/src/grpc/proto/book_pb.js

There is no pack & unpack generated.
nopack

And js codes generated come from grpc-tools. You'd better ask them about this.

@long1eu
Copy link
Author

long1eu commented Oct 16, 2020

Hey!

Thanks for this. As you can see the pack and unpack functions just combines the two already existing method. Good to know that this is not a ts issue. Thanks again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants