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

enum definitions and ruby constants clashing #326

Open
onethirtyfive opened this issue Jun 6, 2016 · 6 comments
Open

enum definitions and ruby constants clashing #326

onethirtyfive opened this issue Jun 6, 2016 · 6 comments

Comments

@onethirtyfive
Copy link
Contributor

Hello!

I'm writing a parser for Dota 2 game replay files, which are just game data over time stored as protobufs.

I get my definitions from upstream reverse engineering projects, and their contents don't agree with ruby constantizing rules. Consequently, I have to preprocess them often. Here's the issue for me:

enum EDOTAChatWheelMessage {
    k_EDOTA_CW_Ok = 0;
    k_EDOTA_CW_Care = 1;
    k_EDOTA_CW_GetBack = 2;
    k_EDOTA_CW_NeedWards = 3;
    k_EDOTA_CW_Stun = 4;
        ...
    k_EDOTA_CW_Zeus_Ult = 85;
}

Protobuf is using const_set to define these constants, so the lib barfs with uncapitalized constants. It seems like it might be good to have the code generator be more tolerant of bad input. Maybe:

  • some option or default to prefix 'bad' constants (perhaps trying to constantize and prefixing something sensible on exception)
  • a different mechanism for persisting the enum values than constants (idk what that would be, but it's a thought)

Right now, I'm having to sed all the files to handle all this bad input:

    sed -i \
        -e '/k_EHero/ s/k_EHero/K_EHero/' \
        -e '/kPVLS/ s/kPVLS/KPVLS/' \
        -e '/k_EDOTA/ s/k_EDOTA/K_EDOTA/' \
        -e '/k_eResult/ s/k_eResult/K_eResult/' \
        -e '/ePE_/ s/ePE_/EPE_/' \
        -e '/net_/ s/net_/NET_/' \
        -e '/svc_/ s/svc_/SVC_/' \
        #{lots_of_files}.proto

If you have a preference for how to deal with this, I'd be happy to whip up a PR. :)

Thanks!

@embark
Copy link
Contributor

embark commented Jun 8, 2016

@onethirtyfive Hi, thanks for the issue. We should get this documented, but you can set the ENV variable PB_UPCASE_ENUMS, which will upcase all the letters in your enum constants, making them valid constants in Ruby. Does this solution work for you?

9a99921

@onethirtyfive
Copy link
Contributor Author

Yep sorry for not looking deeper into the code! Thanks!
On Jun 8, 2016 4:34 PM, "embark" notifications@github.com wrote:

@onethirtyfive https://github.com/onethirtyfive Hi, thanks for the
issue. We could have this be better documented, but you can set the ENV
variable PB_UPCASE_ENUMS, which will upcase all the letters in your enum
constants and solve this problem. Does this solution work for you?

9a99921
9a99921


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#326 (comment),
or mute the thread
https://github.com/notifications/unsubscribe/AAAQHxm2pUUiG6LLEhwywpKfIg46nEM9ks5qJ1F8gaJpZM4IulgX
.

@embark
Copy link
Contributor

embark commented Jun 8, 2016

No worries, we need to add documentation for this! Closing this out.

@onethirtyfive
Copy link
Contributor Author

I just wanted to add that this change doesn't appear to be in the released version of 3.6.9, although it is definitely in master. Since I'm using this gem in a gemspec, I must additionally supply a gem directive in the Gemfile to fetch master from this repo.

Thought I'd let you know!

@embark
Copy link
Contributor

embark commented Jun 9, 2016

CC @film42 about releases

@film42
Copy link
Member

film42 commented Jun 13, 2016

Sorry for the delay, I've been AFK for the last week. There is a pre-release that has already been published to rubygems: https://rubygems.org/gems/protobuf/versions/3.7.0.pre1.

If you set your Gemfile to be greater than 3.7.0.pre you'll be able to pick up all updates as we work through getting 3.7.0 completed. Thanks for your patience, @onethirtyfive!

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

3 participants