How to set enum value in google-protobufjs
up vote
4
down vote
favorite
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 |
up vote
4
down vote
favorite
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 at 9:14
@jannis this is proto3
– demonofthemist
Nov 19 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 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 at 10:36
add a comment |
up vote
4
down vote
favorite
up vote
4
down vote
favorite
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 at 8:11
asked Nov 15 at 7:17
demonofthemist
2,70731232
2,70731232
Which protobuf version (proto2, proto3)?
– jannis
Nov 19 at 9:14
@jannis this is proto3
– demonofthemist
Nov 19 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 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 at 10:36
add a comment |
Which protobuf version (proto2, proto3)?
– jannis
Nov 19 at 9:14
@jannis this is proto3
– demonofthemist
Nov 19 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 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 at 10:36
Which protobuf version (proto2, proto3)?
– jannis
Nov 19 at 9:14
Which protobuf version (proto2, proto3)?
– jannis
Nov 19 at 9:14
@jannis this is proto3
– demonofthemist
Nov 19 at 9:15
@jannis this is proto3
– demonofthemist
Nov 19 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 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 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 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 at 10:36
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
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 |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
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 |
up vote
1
down vote
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 |
up vote
1
down vote
up vote
1
down vote
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 at 5:11
answered Nov 24 at 5:05
shkaper
765313
765313
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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 at 9:14
@jannis this is proto3
– demonofthemist
Nov 19 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 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 at 10:36