-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Expanded documentation for TypeScript 😈 #666
Comments
Copying the slightly modified TypeScript paragraph here so we can work on it: Usage with TypeScriptThe library ships with its own type definitions and modern editors like Visual Studio Code should automatically detect and use them for code completion when following this pattern: // node.js
import * as protobuf from "protobufjs";
import * as Long from "long"; // optional
// browser only (alternatively)
import * as protobuf from "./node_modules/protobufjs/index.js";
import * as Long from "./node_modules/long/dist/long.js"; // optional
// example code
protobuf.load("awesome.proto", function(err, root) {
if (err)
throw err;
var AwesomeMessage = root.lookupType("AwesomeMessage");
var message = AwesomeMessage.create({ awesomeField: "hello" });
var buffer = AwesomeMessage.encode(message).finish();
...
}); To achieve the same with static code generated by pbjs, there is the pbts command line utility to generate type definitions from static code as well. Let's say you generated your static code to import * as root from "./bundle.js";
// example code
var AwesomeMessage = root.AwesomeMessage;
var message = AwesomeMessage.create({ awesomeField: "hello" });
var buffer = AwesomeMessage.encode(message).finish();
... |
Let me know if this leaves any open questions. |
@dcodeIO - Awesome, yeah -- so let's see if I can get all of this out in one go. Sometimes it's tough to know all problems in advance ;)
Apologies for the "Hi I'm new here"-ness of my questions. Once I know the right way to use this library, things will click. |
Not yet, unfortunately. Some day.
With reflection, you'll get runtime message instances, and you can replace the respective class with your own containing any custom functionality. |
Great, I think this gives me a loose idea of what I can do right away, with some decent wishlist candidates. Last round of questions (I think):
Only remaining thing might be to add points to address some of my "expectations" here in the documentation. It's all quite applied knowledge, but is the kind of stuff people will need to hunt down before they'll feel like they're up and running. Thanks so much for this library and your help! |
Well, that's completely up to you, but that's what I'd do:
|
I'm not sure I follow how "reflection" is being used in this specific scenario. Normally I understand it to be when you query and modify the type system at runtime - which JS/TS lack. What I'm seeing here is a fluent API to build metadata, as an alternative to JSON or protobuf files. One or more of what you've described so far might be what I'm after here, but I'm having a tough time aligning it to my understanding or picking out the correct approaches. So let's take a new scenario: Assume I don't want to use generated code, I have a bunch of models I've created in TS. Is there a way to load the metadata (whether through the fluent API, protobuf or JSON) and then use protobufjs as an infrastructure-service in my application to perform serialization and deserialization? |
I've added additional information on reflection-based and code-based use cases to the README. Basically, everything what's not static code is reflection based.
You could load a JSON descriptor / pbjs-generated JSON module, and then register your custom classes with the respective types. Let's say you have a class protobuf.Class.create(root.lookupType(".com.something.MyType"), MyType); From then on, deserialized messages of that type are |
Alright! Looks like I've got things more or less exactly how I'd like! The settings What an amazing job you've done here. I've got one minor suggestion to appease my linter which I'll open as another ticket. I don't know if you'd like to ask me any specific questions to help with further improving the documentation. But otherwise, feel free to close! |
protobuf.js version: 6.6.2
This is more of a documentation issue.. The documentation in
README.md
doesn't really give me a working understanding of what my workflows need to be with this library.If I'm authoring code in TypeScript and wish to make use of this library, what do I have to set up and what commands or
proto
files do I have to leverage to get everything to a point where I can go back to using domain classes?Again, it's a documentation thing, but if I'm not entirely clear on what I have to do, what generated code I need, where to put it or how to leverage it, perhaps I'm not alone? :)
The text was updated successfully, but these errors were encountered: