Thrift
  1. Thrift
  2. THRIFT-526

Generated Ruby enums have no good way to get the names back out once you have a number.

    Details

    • Type: Improvement Improvement
    • Status: Closed
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 0.2
    • Component/s: Ruby - Compiler
    • Labels:
      None
    • Patch Info:
      Patch Available

      Description

      Thrift enums are compiled into Ruby modules as Ruby has no native enum type. This works well except that without maintaining a separate map somewhere else or using reflection, it is impossible to get the names of the values back out from the values themselves. Essentially with this setup:

      module MyModule
      DATAPOINT = 1
      OTHERDATA = 2
      end

      If I have 2, it is very difficult to get "OTHERDATA" back out. It would be easier if thrift generated a static map from constant value to constant name. I am going to try and work on a patch for this.

        Activity

        Hide
        Adam Coffman added a comment - - edited

        The patch I submitted generates a hash called VALUE_MAP that maps values back to their names (as strings) in ruby enums. It also includes a test for the added functionality.

        Show
        Adam Coffman added a comment - - edited The patch I submitted generates a hash called VALUE_MAP that maps values back to their names (as strings) in ruby enums. It also includes a test for the added functionality.
        Hide
        Bryan Duxbury added a comment -

        Is there some advantage to the Hash[...] syntax of defining a map over the usual {} approach? Otherwise, the patch looks great.

        Show
        Bryan Duxbury added a comment - Is there some advantage to the Hash [...] syntax of defining a map over the usual {} approach? Otherwise, the patch looks great.
        Hide
        Adam Coffman added a comment -

        No particular advantages or disadvantages that I'm aware of. I just use it because it reads more like a constructor to me.

        Show
        Adam Coffman added a comment - No particular advantages or disadvantages that I'm aware of. I just use it because it reads more like a constructor to me.
        Hide
        Bryan Duxbury added a comment -

        Well, if there's no advantage, then I am going to change it to {} for consistency and commit.

        Show
        Bryan Duxbury added a comment - Well, if there's no advantage, then I am going to change it to {} for consistency and commit.
        Hide
        Bryan Duxbury added a comment -

        I just committed this. Thanks for the patch!

        Show
        Bryan Duxbury added a comment - I just committed this. Thanks for the patch!

          People

          • Assignee:
            Adam Coffman
            Reporter:
            Adam Coffman
          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development