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

How to access a Message and use google.protobuf.Any #430

Closed
psaelango opened this issue Jul 10, 2018 · 6 comments
Closed

How to access a Message and use google.protobuf.Any #430

psaelango opened this issue Jul 10, 2018 · 6 comments

Comments

@psaelango
Copy link

psaelango commented Jul 10, 2018

ProtoFile - gateway.proto

import "google/protobuf/timestamp.proto";
package test.io.gateway;

service Gateway
{
  rpc UserLogin (UserLoginCmd) returns (UserLoginRsp) {}
}
message UserLoginCmd
{
    string username= 1;
    string password = 2;
}
message UserLoginRsp
{
    string accesstoken= 1;
}
message Something
{
    google.protobuf.Any data = 1;
}

Javascript File - some.js

PROTO_PATH = "C:\\Users\\firstname.lastname\\Documents\\io\\gateway.proto";
protoDefinition = protoLoader.loadSync(PROTO_PATH,{
    keepCase: true,
    longs: String,
    enums: String,
    defaults: true,
    oneofs: true
  });
proto = grpc.loadPackageDefinition(protoDefinition).test.io.gateway;
console.log(proto); //Has only service (Gateway) 

The above code runs fine and prints only the service. But I want access the message (Something) to achieve this - protocolbuffers/protobuf#2612.

Please kindly help me here to understand the way to access the message and google.protobuf.Any.

Thanks in advance.

@murgatroid99 - Can you please kindly share your thoughts. Also I couldn't able to use includeDirs properly.

PROTO_PATH = "C:\\Users\\firstname.lastname\\Documents\\io\\gateway.proto";
protoDefinition = protoLoader.loadSync(PROTO_PATH,{
    keepCase: true,
    longs: String,
    enums: String,
    defaults: true,
    oneofs: true,
    includeDirs: ["C:\\Users\\firstname.lastname\\Documents\\io\\*.proto"]
  });
proto = grpc.loadPackageDefinition(protoDefinition).test.io.gateway; // Get error - io of undefined

The above code crashes & gives error io of undefined

@murgatroid99
Copy link
Member

First of all, the @grpc/proto-loader package is intended to be used for loading service definitions in .proto files for use with other grpc libraries. In your .proto file, you do not use the Something message type in a service, so you should not expect to be able to access it using the proto loader library.

Second, you seem to be saying that you want to use the solution in the issue you linked to. That issue is about the package google-protobuf, so you should use that package. If your goal is to use a google.protobuf.Any object with @grpc/proto-loader, you are going to have some difficulty. That package does not have any special support for the Any type, so you will have to explicitly use a protobuf implementation to serialize and deserialize the binary blob in the object.

Third, the includeDirs option has to be a list of directory names. The value you passed is not a directory name but a glob, which is not valid there.

@psaelango
Copy link
Author

psaelango commented Jul 10, 2018

@murgatroid99 - My apologies. Something message is an example for google.protobuf.Any. But I couldn't able to access UserLoginCmd as well. So can you please tell how to access UserLoginCmd message. Also for includeDirs - I have tried "C:\\Users\\firstname.lastname\\Documents\\io" which didn't work

@nicolasnoble
Copy link
Member

You don't access the messages using @grpc/proto-loader. That's not its purpose.

@psaelango
Copy link
Author

psaelango commented Jul 10, 2018

@murgatroid99 @nicolasnoble - Can you guys please give an example implementation of google.protobuf.Any for javascript.

@nicolasnoble
Copy link
Member

I'm not familiar with google.protobuf.Any, and you're asking questions about protobuf's API, not grpc's. I'm not properly equipped to answer this.

@murgatroid99
Copy link
Member

If you are using the google-protobuf library, you should follow along with the issue you linked in the original post. If you are using @grpc/proto-loader, that library does not have any special handling for google.protobuf.Any, and I would suggest looking at https://github.com/dcodeIO/protobuf.js to figure out how to manually handle it. Either way, this question does not really seem to be about grpc, so I am closing it.

@lock lock bot locked as resolved and limited conversation to collaborators Oct 8, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants