How to set enum value in google-protobufjs
Hi have a proto file as follows:
enum Type
A = 0;
B = 1;
C = 2;
message Message
User to = 1;
User from = 2;
Type type = 3;
I use protoc compiler to convert it to javascript utility classes. Using this generated javascript file I set up Message object in my application as follows:
const message = new Main.Message()
message.setTo(this.user1) // saved user
message.setFrom(this.user3) // saved user
message.setType(0) // trying to set type to A
const buffer = message.serializeBinary()
socket.send(buffer)
The issue I am facing is in message object both to
and from
fields get updated properly, but type
remains null
! I am unable to figure out a solution for this.
Is this because in Message
proto, expected type is as Enum? But it javascript it is not possible. How to set expected enum values while creating protobuf for dispatch?
Note:
I've noticed that, this issue happens only when I try to set
zero
value to Enum type!
javascript protocol-buffers
add a comment |
Hi have a proto file as follows:
enum Type
A = 0;
B = 1;
C = 2;
message Message
User to = 1;
User from = 2;
Type type = 3;
I use protoc compiler to convert it to javascript utility classes. Using this generated javascript file I set up Message object in my application as follows:
const message = new Main.Message()
message.setTo(this.user1) // saved user
message.setFrom(this.user3) // saved user
message.setType(0) // trying to set type to A
const buffer = message.serializeBinary()
socket.send(buffer)
The issue I am facing is in message object both to
and from
fields get updated properly, but type
remains null
! I am unable to figure out a solution for this.
Is this because in Message
proto, expected type is as Enum? But it javascript it is not possible. How to set expected enum values while creating protobuf for dispatch?
Note:
I've noticed that, this issue happens only when I try to set
zero
value to Enum type!
javascript protocol-buffers
Which protobuf version (proto2, proto3)?
– jannis
Nov 19 '18 at 9:14
@jannis this is proto3
– demonofthemist
Nov 19 '18 at 9:15
1
Just a guess (never used protobuf), but can't you use the enum itself, likemessage.setType(Main.Type.A)
?
– Martin Adámek
Nov 19 '18 at 9:20
@MartinAdámek It doesn't matter! Effectively you are passing same value in both ways, i.e. a number.
– demonofthemist
Nov 19 '18 at 10:36
add a comment |
Hi have a proto file as follows:
enum Type
A = 0;
B = 1;
C = 2;
message Message
User to = 1;
User from = 2;
Type type = 3;
I use protoc compiler to convert it to javascript utility classes. Using this generated javascript file I set up Message object in my application as follows:
const message = new Main.Message()
message.setTo(this.user1) // saved user
message.setFrom(this.user3) // saved user
message.setType(0) // trying to set type to A
const buffer = message.serializeBinary()
socket.send(buffer)
The issue I am facing is in message object both to
and from
fields get updated properly, but type
remains null
! I am unable to figure out a solution for this.
Is this because in Message
proto, expected type is as Enum? But it javascript it is not possible. How to set expected enum values while creating protobuf for dispatch?
Note:
I've noticed that, this issue happens only when I try to set
zero
value to Enum type!
javascript protocol-buffers
Hi have a proto file as follows:
enum Type
A = 0;
B = 1;
C = 2;
message Message
User to = 1;
User from = 2;
Type type = 3;
I use protoc compiler to convert it to javascript utility classes. Using this generated javascript file I set up Message object in my application as follows:
const message = new Main.Message()
message.setTo(this.user1) // saved user
message.setFrom(this.user3) // saved user
message.setType(0) // trying to set type to A
const buffer = message.serializeBinary()
socket.send(buffer)
The issue I am facing is in message object both to
and from
fields get updated properly, but type
remains null
! I am unable to figure out a solution for this.
Is this because in Message
proto, expected type is as Enum? But it javascript it is not possible. How to set expected enum values while creating protobuf for dispatch?
Note:
I've noticed that, this issue happens only when I try to set
zero
value to Enum type!
javascript protocol-buffers
javascript protocol-buffers
edited Nov 19 '18 at 8:11
demonofthemist
asked Nov 15 '18 at 7:17
demonofthemistdemonofthemist
2,68531435
2,68531435
Which protobuf version (proto2, proto3)?
– jannis
Nov 19 '18 at 9:14
@jannis this is proto3
– demonofthemist
Nov 19 '18 at 9:15
1
Just a guess (never used protobuf), but can't you use the enum itself, likemessage.setType(Main.Type.A)
?
– Martin Adámek
Nov 19 '18 at 9:20
@MartinAdámek It doesn't matter! Effectively you are passing same value in both ways, i.e. a number.
– demonofthemist
Nov 19 '18 at 10:36
add a comment |
Which protobuf version (proto2, proto3)?
– jannis
Nov 19 '18 at 9:14
@jannis this is proto3
– demonofthemist
Nov 19 '18 at 9:15
1
Just a guess (never used protobuf), but can't you use the enum itself, likemessage.setType(Main.Type.A)
?
– Martin Adámek
Nov 19 '18 at 9:20
@MartinAdámek It doesn't matter! Effectively you are passing same value in both ways, i.e. a number.
– demonofthemist
Nov 19 '18 at 10:36
Which protobuf version (proto2, proto3)?
– jannis
Nov 19 '18 at 9:14
Which protobuf version (proto2, proto3)?
– jannis
Nov 19 '18 at 9:14
@jannis this is proto3
– demonofthemist
Nov 19 '18 at 9:15
@jannis this is proto3
– demonofthemist
Nov 19 '18 at 9:15
1
1
Just a guess (never used protobuf), but can't you use the enum itself, like
message.setType(Main.Type.A)
?– Martin Adámek
Nov 19 '18 at 9:20
Just a guess (never used protobuf), but can't you use the enum itself, like
message.setType(Main.Type.A)
?– Martin Adámek
Nov 19 '18 at 9:20
@MartinAdámek It doesn't matter! Effectively you are passing same value in both ways, i.e. a number.
– demonofthemist
Nov 19 '18 at 10:36
@MartinAdámek It doesn't matter! Effectively you are passing same value in both ways, i.e. a number.
– demonofthemist
Nov 19 '18 at 10:36
add a comment |
1 Answer
1
active
oldest
votes
Enums in protobuffer use 0 as a default (aka unknown) value; from Protocol Buffers
docs on Enums:
every enum definition must contain a constant that maps to zero as its
first element. This is because:
There must be a zero value, so that we can use 0 as a numeric
default value.
The zero value needs to be the first element, for compatibility
with the proto2 semantics where the first enum value is always the
default.
More on defaults:
When a message is parsed, if the encoded message does not contain a
particular singular element, the corresponding field in the parsed
object is set to the default value for that field:
- For enums, the default value is the first defined enum value, which must be 0.
Default enums are not even sent on wire. You can check that by examining your buffer
after serializing a message with a zero-value enum. Since default value is indistinguishable from an unset value, the way it's going to look after being deserialized is really on the decoder side.
You can't do much about it other than not defining any meaningful values as 0 in your enums and use throw-away values instead, like NONE
or UNSPECIFIED
:
enum Type
NONE = 0;
A = 1;
B = 2;
C = 3;
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53314212%2fhow-to-set-enum-value-in-google-protobufjs%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Enums in protobuffer use 0 as a default (aka unknown) value; from Protocol Buffers
docs on Enums:
every enum definition must contain a constant that maps to zero as its
first element. This is because:
There must be a zero value, so that we can use 0 as a numeric
default value.
The zero value needs to be the first element, for compatibility
with the proto2 semantics where the first enum value is always the
default.
More on defaults:
When a message is parsed, if the encoded message does not contain a
particular singular element, the corresponding field in the parsed
object is set to the default value for that field:
- For enums, the default value is the first defined enum value, which must be 0.
Default enums are not even sent on wire. You can check that by examining your buffer
after serializing a message with a zero-value enum. Since default value is indistinguishable from an unset value, the way it's going to look after being deserialized is really on the decoder side.
You can't do much about it other than not defining any meaningful values as 0 in your enums and use throw-away values instead, like NONE
or UNSPECIFIED
:
enum Type
NONE = 0;
A = 1;
B = 2;
C = 3;
add a comment |
Enums in protobuffer use 0 as a default (aka unknown) value; from Protocol Buffers
docs on Enums:
every enum definition must contain a constant that maps to zero as its
first element. This is because:
There must be a zero value, so that we can use 0 as a numeric
default value.
The zero value needs to be the first element, for compatibility
with the proto2 semantics where the first enum value is always the
default.
More on defaults:
When a message is parsed, if the encoded message does not contain a
particular singular element, the corresponding field in the parsed
object is set to the default value for that field:
- For enums, the default value is the first defined enum value, which must be 0.
Default enums are not even sent on wire. You can check that by examining your buffer
after serializing a message with a zero-value enum. Since default value is indistinguishable from an unset value, the way it's going to look after being deserialized is really on the decoder side.
You can't do much about it other than not defining any meaningful values as 0 in your enums and use throw-away values instead, like NONE
or UNSPECIFIED
:
enum Type
NONE = 0;
A = 1;
B = 2;
C = 3;
add a comment |
Enums in protobuffer use 0 as a default (aka unknown) value; from Protocol Buffers
docs on Enums:
every enum definition must contain a constant that maps to zero as its
first element. This is because:
There must be a zero value, so that we can use 0 as a numeric
default value.
The zero value needs to be the first element, for compatibility
with the proto2 semantics where the first enum value is always the
default.
More on defaults:
When a message is parsed, if the encoded message does not contain a
particular singular element, the corresponding field in the parsed
object is set to the default value for that field:
- For enums, the default value is the first defined enum value, which must be 0.
Default enums are not even sent on wire. You can check that by examining your buffer
after serializing a message with a zero-value enum. Since default value is indistinguishable from an unset value, the way it's going to look after being deserialized is really on the decoder side.
You can't do much about it other than not defining any meaningful values as 0 in your enums and use throw-away values instead, like NONE
or UNSPECIFIED
:
enum Type
NONE = 0;
A = 1;
B = 2;
C = 3;
Enums in protobuffer use 0 as a default (aka unknown) value; from Protocol Buffers
docs on Enums:
every enum definition must contain a constant that maps to zero as its
first element. This is because:
There must be a zero value, so that we can use 0 as a numeric
default value.
The zero value needs to be the first element, for compatibility
with the proto2 semantics where the first enum value is always the
default.
More on defaults:
When a message is parsed, if the encoded message does not contain a
particular singular element, the corresponding field in the parsed
object is set to the default value for that field:
- For enums, the default value is the first defined enum value, which must be 0.
Default enums are not even sent on wire. You can check that by examining your buffer
after serializing a message with a zero-value enum. Since default value is indistinguishable from an unset value, the way it's going to look after being deserialized is really on the decoder side.
You can't do much about it other than not defining any meaningful values as 0 in your enums and use throw-away values instead, like NONE
or UNSPECIFIED
:
enum Type
NONE = 0;
A = 1;
B = 2;
C = 3;
edited Nov 24 '18 at 5:11
answered Nov 24 '18 at 5:05
shkapershkaper
1,3611915
1,3611915
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53314212%2fhow-to-set-enum-value-in-google-protobufjs%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Which protobuf version (proto2, proto3)?
– jannis
Nov 19 '18 at 9:14
@jannis this is proto3
– demonofthemist
Nov 19 '18 at 9:15
1
Just a guess (never used protobuf), but can't you use the enum itself, like
message.setType(Main.Type.A)
?– Martin Adámek
Nov 19 '18 at 9:20
@MartinAdámek It doesn't matter! Effectively you are passing same value in both ways, i.e. a number.
– demonofthemist
Nov 19 '18 at 10:36