-
-
Notifications
You must be signed in to change notification settings - Fork 14
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
Oneof/union types don't work very well #42
Comments
+1 I'm using I have a protobuf that looks like this:
and the generated constructor sets default values for each field (rather than nulls):
And the encoder sets all three fields.
I think a good solution would be defaulting the fields to null and modifying the encode function to only set non-null fields. |
Unfortunately, there is no concept of |
I realize this is a bigger issue than just I think it's ok for the fields to be set to a default value, but there needs to be a way to tell if the field is set. This could also be used to not write the unset field to the wire protocol. Since you can't null a primitive type, I'd suggest keeping a setting boolean value You might need to get rid of the constructor to make that possible. I think it's worthwhile though as settings default values like Perhaps this could be done with The generated class could look something like:
@piotr-oles What do you think about this Fixing |
Yes, I agree it's a bigger issue. I'm wondering if we should use |
class Message {
value: int32 = 0;
private hasMask1: i32 = 0;
setValue(value: int32): void {
this.value = value;
this.hasMask1 &= 0x1;
}
hasValue(): boolean {
return this.hasMask1 & 0x1 !== 0;
}
getValue(): int32 {
return this.value;
}
static encode(message: Message, writer: Writer): void {
if (message.hasValue()) {
writer.uint32(8);
writer.i32(message.value);
}
}
} |
That makes sense. I'm actually surprised I haven't run into this being a blocker for me yet. I don't have time/motivation to take this on myself (at least for now), but we should probably spin out a separate issue for this. |
I'm about to stop using My proto created in AssemblyScript:
The decoding code in Java:
Looks like the Java decoder assumes only one of the fields in a So if I have a boolean value set, then |
It's kind of a problem with AssemblyScript, not
as-proto
per se, that it doesn't support union types or undefined, but protobufs often contain "oneof" fields andas-proto
currently doesn't do a great job of transpiling them.Given this:
as-proto
will generate this:The "payload" field isn't even referenced, and the constructor forces the
Simple
type to have all of the "oneof" properties (not one of them).If you try it with
ts-proto
instead it can generate a union type, e.g.but that won't work in AssemblyScript. It would need to be something like this, which I think is what is meant by the very cursory comment on union types in the language guide (https://www.assemblyscript.org/status.html#language-features):
The text was updated successfully, but these errors were encountered: