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

The new 'Any' type and JavaScript #2612

Closed
cristianmad opened this issue Jan 20, 2017 · 10 comments
Closed

The new 'Any' type and JavaScript #2612

cristianmad opened this issue Jan 20, 2017 · 10 comments

Comments

@cristianmad
Copy link

cristianmad commented Jan 20, 2017

Hi,

I recently started using protobuf for JavaScript. I have been using protobuf for the past few years but mostly with C#/C++.
Everything seems to go well, except I was not able to find a way to work with the new 'Any' type (defined in protobuf3). I am using the binary encoding which according to the doc, should be supported in JavaScript.

I have used protoc to define the JavaScript equivalent of my proto files. I can make instances of all my types, set properties, encode them (binary), etc.

Basically, I do not seem to be able to access (see them) the Pack/Unpack functions.

Would anyone know how to do that? Any examples?

PS: I should add that my JavaScript expertise is very basic so the solution may be something obvious :)

Thanks!

@liujisi
Copy link
Contributor

liujisi commented Jan 20, 2017

@haberman

@cristianmad
Copy link
Author

cristianmad commented Jan 20, 2017

I may have found a work around.

Basically the JavaScript implementation of Any does not seem to have the Pack/Unpack functions defined. Instead I manually made instances of the Any type (where required) and then called the setType and setValue functions. I do not know if this would be acceptable...

proto:
message Message
{
Any AnyType = 1;
}

JavaScript

var message = new Message();
var value = new proto.google.protobuf.Any;
value.setValue(objectToPack.serializeBinary());
value.setTypeUrl("type.googleapis.com/FullyQualifedTypeNameOfTheObjectBeingPacked");

message.setAnyType(value);

There may be a better way of achieving this, but this worked for me.

Enjoy!

@haberman
Copy link
Member

@cristianmad
Copy link
Author

cristianmad commented Jan 23, 2017

Thanks @haberman,

I think that there may be an issue with my generated code since it does not contain the pack and unpack functions. It does have the other ones though (set/getType and set/getValue).

From the source code that you linked I see that the implementation is very similar to my workaround.

I tried calling pack on one of my Any instances and I got:
"0x800a01b6 - JavaScript runtime error: Object doesn't support property or method 'pack' occurred"

Thank you

@cristianmad
Copy link
Author

cristianmad commented Jan 23, 2017

Also, note that when generating the code, I have followed the instructions documented here for CommonJS:
https://github.com/google/protobuf/tree/master/js

$ protoc --js_out=import_style=commonjs,binary:. messages.proto base.proto

After that I browserify it (since the current protoc does not support generating browser targeted files -- well it does define support for browsers but it does not implement it -- see https://github.com/google/protobuf/issues/2556)

The resulting file does not seem to contain the pack/unpack functions...(unless I am doing something wrong). If I am right (and there is a good chance that I am doing something wrong), there may be an issue with the code generator.

@haberman
Copy link
Member

I should have mentioned that the well-known type support is not currently released (it's only on GitHub) but will be part of the upcoming 3.2 release.

@cristianmad
Copy link
Author

Aha! :)

OK, thank you for your time! I will use my workaround until then.

@haberman
Copy link
Member

haberman commented Mar 6, 2017

This was released as part of 3.2! Resolving this issue.

@psaelango
Copy link

psaelango commented Jul 10, 2018

@cristianmad @haberman
Can you please kindly share the updated solution for using protobuf.Any in javascript.

@pcj
Copy link
Contributor

pcj commented Nov 29, 2018

Here's an example of deserializing an any. Couldn't find the pack/unpack methods.

    /**
     * @param {!proto.google.protobuf.Any} any 
     */
    handleAny(any) {
        switch (any.getTypeUrl()) {
            case "type.example.com/package_name.Special":
                const special = Special.deserializeBinary(any.getValue());
                this.handleSpecial(special);
            default:
                console.warn("Don't know how to deal with this any type");
        }
    }

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

5 participants