Skip to content

Add Enum to Schema generation - #20

Open
stevebell117 wants to merge 6 commits into
FebruaryBreeze:masterfrom
stevebell117:add-enum-to-class-generation
Open

Add Enum to Schema generation#20
stevebell117 wants to merge 6 commits into
FebruaryBreeze:masterfrom
stevebell117:add-enum-to-class-generation

Conversation

@stevebell117

@stevebell117 stevebell117 commented Feb 20, 2021

Copy link
Copy Markdown

Hello, noticed that the enum logic was setup but not fully implemented so I went ahead and implemented it. I'm not super familiar with this repository, so I don't know how to fully test this but it worked well with the file I used against it (which I can attach, it's a bit large), but here's an example of the output I am now receiving after implementing it.

"assetType": {
"type": "string",
"enum": [
    "EQUITY",
    "OPTION",
    "INDEX",
    "MUTUAL_FUND",
    "CASH_EQUIVALENT",
    "FIXED_INCOME",
    "CURRENCY"
]

generates (See latest comment)

class Assettype(Enum):
    Equity = "EQUITY"
    Option = "OPTION"
    Index = "INDEX"
    Mutual_fund = "MUTUAL_FUND"
    Cash_equivalent = "CASH_EQUIVALENT"
    Fixed_income = "FIXED_INCOME"
    Currency = "CURRENCY"

I am concerned about backwards compatibility, as those who have used this library previously may not be able to handle the conversion from str to Enum (since they'd need to do .value. Would you recommend adding a argument to explicitly enable this behavior?

Addresses #19

@stevebell117

Copy link
Copy Markdown
Author

The flake8 error that's being thrown doesn't make sense to me, but I'll continue looking in this more over the next few days.

1) If no default is specified, don't have the enum on the object since it'll fail Enum evaluation. Instead, set it to None and override it.
2) If "None" is a value in the provided Enum, lowercase it since None is a reserved word.
@stevebell117

Copy link
Copy Markdown
Author

Update:

The output now looks like:

Handles if no default is specified in the schema, and also includes a __str__ override to allow backwards compatibility.

No default:

self.session = None
if "session" in values:
    self.session = self.Session(values.get("session"))
class Session(Enum):
    Normal = "NORMAL"
    Am = "AM"
    Pm = "PM"
    Seamless = "SEAMLESS"

    def __str__(self):
        return str(self.value)

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

Successfully merging this pull request may close these issues.

1 participant