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 string needs to be exactly the same as enum name #15

Open
ajsaraujo opened this issue May 16, 2020 · 2 comments
Open

Enum string needs to be exactly the same as enum name #15

ajsaraujo opened this issue May 16, 2020 · 2 comments

Comments

@ajsaraujo
Copy link

ajsaraujo commented May 16, 2020

I want to use your package in my app, but right now it won't fit my needs, since one of my enums should be converted to the string 'PG-13'. However, you can't have an enum named PG-13, because you can't have a hyphen in your enum name.

It would be nice if you could mock enum string values, for example:

Enum Rating { G, PG13, R };
EnumToString.mockEnumValue(Rating.PG13, 'PG-13');
print(EnumToString.parse(TestEnum.PG13)); // 'PG-13'

I would be happy to submit a PR with this functionality if you would like.

@rknell
Copy link
Owner

rknell commented May 28, 2020

Hey @ajsaraujo

So I wasn't sure about this and wanted to think about it which is why I have taken so long to get back to you. Of course, I just came across this issue myself!

Because the EnumToString class is static, where were you going to store the mapping? We could consider making it a singleton and I'm not sure what implications that might have on the community. If its a singleton but the parse function is static that might also have some issues (cant reference instance members). None of this is insurmountable but this package is used incredibly widely so I want to make sure if we deliver a new function its done in an elegant way.

I really like the proposed syntax though!

Like I said, I need to do something similar myself, so I will have a go and see what learnings I get from it.

Any thoughts from the community?

@ajsaraujo
Copy link
Author

ajsaraujo commented May 29, 2020

It would be nice if EnumToString was a Singleton, so we could store state (the mapping) in it, but as you suggested we don't know what impacts that may have on the API usage.

Another solution would be storing this somewhere else. It could be a global variable inside the enum_to_string.dart file, or a separate singleton class responsible for storing state, so when we need it we would just instantiate it. While the first is more simple, the latter is more robust and gives us more options.

So in the second case the parse method would be like this:

static String parse(enumItem, {bool camelCase = false}) { 
    if (enumItem == null) return null;
    
    // Get mock will return null if a mock doesn't exist for enumItem. 
    final enumState = EnumGlobalState();
    final mock = enumState.getMock(enumItem); 
    if (mock != null) return mock;

    final _tmp = enumItem.toString().split('.')[1];
    return !camelCase ? _tmp : camelCaseToWords(_tmp);
  }

We could have separate functions to check mock existence and value, such as mockExists(enumItem) and getMock(enumItem), but this way we would be performing two queries when we could do the same thing with just one.

In the other hand I don't know what would be best for EnumGlobalState. I mean, I don't know if we should expose it's API to the users, or modify it through EnumToString. While the latter is less performant and straightforward for us, I think it provides a better experience for the users, since all the operations would remain in EnumToString. Also it would cause less impact if one day we decide to make EnumToString and EnumGlobalState into a single class.

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

2 participants