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

Add Enum to Schema generation #20

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

stevebell117
Copy link

@stevebell117 stevebell117 commented Feb 20, 2021

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
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
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