-
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
Simplify imports for enums. #576
Comments
The reason why there aren't any magically created object trees anymore basically is that there is no way to make this work the same with typed dialects like TypeScript, which requires proper types on everything. What do you think about adding
so you can do the following (if Type is a direct child of MyMessage): if (something === MyMessage.getEnum("Type").CAT) {
} Side note: You can also use |
Thanks that's better. |
These methods throw: Does this help? |
¯_(ツ)_/¯ Still a runtime check. I am thinking if there is something I can add so that the whole thing barfs during babel bundling. |
Interesting idea to use static checking for this somehow, I like the idea but I'm not quite sure how we can implement that. |
This idea is very raw: I feel like there is a middle ground between the current static-module (3.2MB in our case) and json-module (56KB) I think you can create exports within json-module by not just export $root but the entire hierarchy as symbols. For all Types, Services and Enums. I mean it is easy for me to do the following, Therefore I believe we can do something like that in the generated json-module itself whereby we can make these import statements work. import * as protobuf from 'protobufjs';
const root = protobuf.Root.fromJSON(require('core/dist/core.json'));
const protos = {
...root.lookup('tocktix.messages').nested
};
module.exports = protos; And for bonus, if we can somehow eliminate subsets of protos what is not in the dependency tree, the whole compiled bundle will be smaller. We have a huge proto tree for the server side. But our clients depending on what APIs they call use different subsets of them. If we can somehow atomize and let babel/uglify be able to eliminate dead-code, that would be wonderful, But not required. I would take syntax and safety over this. |
So why not just generate Typescript definitions for the static code, and actually generate + execute the static code at runtime? By that I mean, generate a strongly typed TypeScript tree for the expected output of protobuf.Root.fromJSON. Then don't actually compile that to code. Just use |
@paralin Could you mock a example for me. I would like to see what that looks like. I am trying to convert a large react codebase from v5-v6. I am also trying to automate this conversion, examples help me visuale the refactor. |
This would have to be a few steps, I think. Given a proto: message MyMessage {
string my_string = 1;
} Step 1 is to generate typescript interfaces for these:
Then step 2 is to allow
The T would be used for the return type of Next, generate a tree: {
MyNamespace: {
MyMessage: <Type<IMyMessage>>root.nested.MyNamespace.nested.MyMessage,
},
} Then usage would be:
The |
Yes! The generated tree portion. This is exactly what I had in my mind, which I started the conversation when I said shadow hierarchy. The trouble we have right now is nesting Types, Enums together, so we will have to create such trees independently for Types, Services and Enums. And we can do that in JS too... Doesn't have to be just typescript. |
Well, now that you mention it, it's theoretically possible to expose everything uppercase (there are no other uppercased properties) on the reflection objects so that a .d.ts from a static module could be used with reflection. |
❤️ I strongly support that theoretical possibility and would encourage to be made a reality. A clean API goes a long way; this is one of the last remaining kinks with v6. |
Give it a shot! Requires calling |
Damn, you're quick.
…On Mon, Dec 19, 2016 at 2:53 PM, Daniel Wirtz ***@***.***> wrote:
Give it a shot! Might still be missing something.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#576 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAgpZ5YNWCBNNjAYaSaL1ki0DpVTE_6dks5rJwrkgaJpZM4LQbza>
.
|
It looks like the generated json is missing that type entirely.
|
robinanil 21:14:10 :~/work/core$ cat src/main/model/proto/*.proto |grep "message BusinessOnboarding"
message BusinessOnboarding {
robinanil 21:14:34 :~/work/core$ ./node_modules/protobufjs/bin/pbjs src/main/model/proto/*.proto -s proto -t json -w commonjs -p src/main/model/proto/*.proto -p node_modules/protobufjs/ -o dist/core.json
robinanil 21:14:37 :~/work/core$ cat dist/core.json |grep "BusinessOnboarding: {" |
This does not happen at v6.2.1 Just at head. -t static fails too
|
That's probably because v6.2.1 didn't call |
Yes, Let me know if you need additional information for figuring out a fix |
The .proto file is syntax = "proto3"; |
@r-tock: I don't know if this last issue actually requires a fix. If I understood you correctly, there are missing definitions, which should, to my understanding, cause an error. |
Your means if a enum type is included in the .proto file, we can't serialize it use protobufjs now ? |
@Rranran: Sorry, forgot to mention r-tock. My reply was directed to him. Regarding your question: I am not quite sure what your issue is. You can just look up the enum and access its values: var PhoneTypeValues = root.lookup("person.PhoneType").values Or create a PhoneNumber from JSON and enum strings using var PhoneNumber = root.lookup("PhoneNumber");
var phoneNumber = PhoneNumber.from({
number: "01234",
type: "WORK"
}); Or you could rename "person" to "Person" to benefit from what we talked about above. |
@dcodeIO the missing definition is caused by the pbjs compiler. All the Protos are in the valid path. See my grep command. The compiler is the one that is skipping those definitions |
@dcodeIO |
Reopening this, there is one more case needs to be handled. which are Enums nested in a namespace not within a message, needs to be resolved into the namespace |
Erm, are those missing on top of the namespace after |
They are present in a different form. Where to access the values I need to do Type.values.CAT instead of Type.CAT |
Hmm, that's strange. This is what happens there for any subclass of namespace alike: https://github.com/dcodeIO/protobuf.js/blob/master/src/namespace.js#L306 Any idea? |
Ha found it! I was exporting this root.resolveAll().lookup('tocktix.messages').nested instead of root.resolveAll().lookup('tocktix.messages') |
protobuf.js version: 6.2.1
Enums are a common thing used across our code. in v5. I would be able to do things like
The same thing now is
I like the lookup pattern for messages, I understands its pros. But for enums its very clunky when you have values in between.
Can enums live in its own shadow hierarchy? which makes it easy to do something like.
The text was updated successfully, but these errors were encountered: