[ 
https://issues.apache.org/jira/browse/THRIFT-546?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Todd Lipcon updated THRIFT-546:
-------------------------------

    Attachment: thrift-546.txt

This patch adds TO_STRING and FROM_STRING dictionaries to generated code. From 
the generated code for tutorial.thrift:

{code:python}
class Operation(object):
  """
  You can define enums, which are just 32 bit integers. Values are optional
  and start at 1 if not supplied, C style again.
  """
  ADD = 1
  SUBTRACT = 2
  MULTIPLY = 3
  DIVIDE = 4

  TO_STRING = {
    1: "ADD",
    2: "SUBTRACT",
    3: "MULTIPLY",
    4: "DIVIDE",
  }

  FROM_STRING = {
    "ADD": 1,
    "SUBTRACT": 2,
    "MULTIPLY": 3,
    "DIVIDE": 4,
  }
{code}

This has major downside, which is that you can't name an enum constant 
"FROM_STRING" or "TO_STRING" - somewhat annoying. Two alternatives:

a) name them something less likely to conflict - eg make them functions 
"@staticmethod def from_string(i): ...". This is still bad, but I think maybe 
less bad.

b) It would be sort of nice to do an overhaul of enum handling completely

{code:python}
class Operation(object):
  def __init__(self, value, name):
    self.value = value
    self.name = name

  def __str__(self):
    return self.name

Operation.ADD = Operation(1, "ADD")
Operation.SUBTRACT = Operation(2, "SUBTRACT")
...
{code}

This would make operation values more "typesafe" though would not be 
backwards-compatible.

> Python generated code should give the ability to convert to/from string names 
> of enums
> --------------------------------------------------------------------------------------
>
>                 Key: THRIFT-546
>                 URL: https://issues.apache.org/jira/browse/THRIFT-546
>             Project: Thrift
>          Issue Type: New Feature
>          Components: Compiler (Python)
>            Reporter: Todd Lipcon
>            Assignee: Todd Lipcon
>         Attachments: thrift-546.txt
>
>
> The current code just generates enums as int constants namespaced within a 
> class. This patch adds dictionaries in that class called TO_STRING and 
> FROM_STRING, so enum values can be nicely printed in log messages, etc.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

Reply via email to