agreatfool / grpc_tools_node_protoc_ts

Generate TypeScript d.ts definitions for generated js files from grpc_tools_node_protoc
MIT License
498 stars 56 forks source link

Is there a way to create messages easily for testing instead of calling setter one by one? #101

Closed kamok closed 3 years ago

kamok commented 3 years ago

For example, in your example repo, you have this:

            const reply = new Book();
            reply.setTitle(`Book${request.getIsbn()}`);
            reply.setAuthor(`Author${request.getIsbn()}`);
            reply.setIsbn(request.getIsbn());
            log(`[getBooks] Write: ${JSON.stringify(reply.toObject())}`);

Is there a way to generate a new Book that has all the corresponding getter methods? I'm trying to test a piece of code that directly accesses Book.getTitle.

agreatfool commented 3 years ago

I didn't get your idea. new Book() already give you the instance with all the getters and setters.

kamok commented 3 years ago

It means this: protocolbuffers/protobuf-javascript#96

It looks like it's been a very popular open issue for developers to be able to create a new Book() from a JSON object

agreatfool commented 3 years ago

Check this: protocolbuffers/protobuf-javascript#96

This one is the official maintainer of repo: https://github.com/grpc/grpc-node , https://github.com/grpc/grpc-node/tree/master/packages/grpc-tools

And d.ts signatures are keeping consistent with generated js codes. We need grpc-tools to support this first. Seems it's not supported yet.

kamok commented 3 years ago

Ok so basically unless grpc-tools library implement the fromObject, this library will not have the change. Thanks, that what i wanted to know. In case other people want this feature, I read on here: https://github.com/grpc/grpc-node/issues/1730#issuecomment-815137482

That you can serialize + deserialize which would give you the instance of Message you need. Or you can write your own method that takes in a object of a specific type, and imperatively call every single one of those setter methods to build it.

Closing for now!