Skip to content
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

Stringification of Enum broken in 6.7.0 (first in 6.6.4) #727

Closed
joscha opened this issue Mar 24, 2017 · 2 comments
Closed

Stringification of Enum broken in 6.7.0 (first in 6.6.4) #727

joscha opened this issue Mar 24, 2017 · 2 comments

Comments

@joscha
Copy link

joscha commented Mar 24, 2017

protobuf.js version: master HEAD (6.7.0)

EDIT: Still works in 6.6.3, first broken version is 6.6.4

When transforming the following file:

syntax = "proto3";

package com.example;

message MyMessage {
    uint32 humidity = 1;
    AdsGain gain = 2;

    enum AdsGain {
        GAIN_TWOTHIRDS = 0;
        GAIN_ONE = 2;
        GAIN_TWO = 4;
        GAIN_FOUR = 6;
        GAIN_EIGHT = 8;
        GAIN_SIXTEEN = 10;
    }
}

via:

pbjs --target=static-module --wrap=commonjs --out=bla.js ./test.proto

The following .toObject code gets generated:

            MyMessage.toObject = function toObject(message, options) {
                if (!options)
                    options = {};
                var object = {};
                if (options.defaults) {
                    object.humidity = 0;
                    object.gain = options.enums === String ? "GAIN_TWOTHIRDS" : 0;
                }
                if (message.humidity !== undefined && message.humidity !== null && message.hasOwnProperty("humidity"))
                    object.humidity = message.humidity;
                if (message.gain !== undefined && message.gain !== null && message.hasOwnProperty("gain"))
                    object.gain = options.enums === String ? $types[1][message.gain] : message.gain;
                return object;
            };

when using:

.toObject(..., { enums: String})

the enum does not appear at all on the output. The reason for that is, that the $types[1] is actually a map from string to int, so searching for the int value in it returns undefined.

@joscha joscha changed the title Stringification of Enum broken in 6.7.0 Stringification of Enum broken in 6.7.0 (first in 6.6.4) Mar 24, 2017
@dcodeIO
Copy link
Member

dcodeIO commented Mar 24, 2017

Should be fixed within #720 - isn't it? The intended behavior is to emit TypeScript-like enums with both string and number keys with the respective other as the values, i.e.:

values[valuesById[102] = "GET_MISSION"] = 102;

@joscha
Copy link
Author

joscha commented Mar 24, 2017

Yes, it's working with 8a60174 - I must have been a few commits older...

@joscha joscha closed this as completed Mar 24, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants