Replies: 4 comments
-
Suggestion for "what to do next": ultimately, for:
It's called TypedMsg, because currently I include a field with an enum to tell me what type was actually sent. Should be able to:
and if I actually want to confirm the type that was received, use a generated:
I suspect the main thing is to include an enum-valued hidden field like:
and for each field:
And either actually clear other fields OR arrange the field getters test _whichVariant and returns undefined, which suffice to ensure that serialize will only find/output one field. Maybe the protoc "extension library" has support for "oneof" ?? ... [edit]
|
Beta Was this translation helpful? Give feedback.
-
@jackpunt I just time to start working on this one. Creating a new enum for each of these cases and one-of-declarations seemed like overkill to me. I feel like we need something more practical My suggestion is to generate two more additional getters for an individual one-of-declaration given; message Person {
oneof name {
string nick = 1;
string real = 2;
string fake = 3;
}
} would look like below class Person {
get nick(): string;
set nick(): string;
get real(): string;
set real(): string;
get fake(): string;
set fake(): string;
get name(): string | string | string;
get which_name(): "nick" | "real" | "fake" | "none";
} Or class Person {
set nick(): string;
set real(): string;
set fake(): string;
get name(): string | string | string;
get which_name(): "nick" | "real" | "fake" | "none";
} |
Beta Was this translation helpful? Give feedback.
-
Ok, sure. |
Beta Was this translation helpful? Give feedback.
-
This issue is here to help people understand why I have created a new plugin that diversified the protocol buffer typescript community even more.
Q:
Why would you create a plugin from scratch when you can fix problems on the upstream compiler?A:
Well it is not that simple because there is a lot of bureaucracy to make any of the design changes that I have done with this plugin. Even if we managed to pass that, there would be breaking changes that will not make design changes like this.Q:
Why should I use this plugin when I can use https://github.com/improbable-eng/ts-protoc-gen?A:
Keep in mind that "ts-protoc-gen" generates onlyd.ts
binding which basically a type declaration file for the javascript output that has been generated by the official protoc compiler. This comes with a trade-off of not being able to customize thed.ts
output as much as we like. If we did. it wouldn't match with the javascript file and you’d get a runtime error even though the typescript compiler wouldn’t complain about the javascript file being different than thed.ts
file. Sots-protoc-gen
has to match the javascript file which limits its ability to compile proto files to modern typescript syntax.Q:
How doesprotoc-gen-ts
solve this problem?A:
By throwing away the javascript compiler of the official protoc compiler. Instead, we generate a typescript file (".ts" not "d.ts") which includes the runtime code that will run on the underlying javascript engine when it is running. Hence we have the ability to generate the proto files the way we want them.There are a number of things that we do differently than the official protoc compiler that complies to javascript such as "generating files as typescript getter setter" which in turn you get the exact names that you have defined in the proto message. For instance, when you have a field named
user_messages
with the official compiler you'll getgetUserMessages()
. however, if you use this plugin, you’ll just access the property as is likeuser_messages
.Also, your
enums
will be generated as a typescriptenum
instead of aninterface
orobject
with predefined keys.Beta Was this translation helpful? Give feedback.
All reactions