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

EnumToString.fromString does not work with String that include hyphen #16

Closed
geek-sajjad opened this issue Jun 24, 2020 · 3 comments
Closed

Comments

@geek-sajjad
Copy link

Hi I use your package in my project , but i have this problem :

status: EnumToString.fromString(TicketStatus.values, json["status"]),

enum TicketStatus {
  open,
  customerReply,
}

and my REST API return "status": "Customer-Reply",
and i can't use hyphen in the Enum values ! what can i do ??

@rknell
Copy link
Owner

rknell commented Jun 25, 2020

Hey @geek-sajjad

Right now there isn't a way. The other open ticket is also about the same thing so there is definitely a need for it. Unfortunately I have had the flood gates open since covid has cleared up near me to haven't had time - or an incident that has caused me to write it.

feel free to jump over to #15 to discuss the best way to implement it. The big hold up is that its not a global singleton and doesn't hold a state, so you need to store the mapping somehow.

Ideally I would like something like in JSON serializable which has the fieldRename parameter which handles camelcase, snake and skewer case, but your example with the uppercase first letter wouldn't work there either.

@geek-sajjad
Copy link
Author

geek-sajjad commented Jun 27, 2020

thank you for your responding , i solved with this way :

final EnumValues<TicketStatus> ticketStatusValues =
    EnumValues<TicketStatus>(<String, TicketStatus>{
  'Open': TicketStatus.open,
  'Customer-Reply': TicketStatus.customerReply,
});

class EnumValues<T> {
  Map<String, T> map;
  Map<T, String> reverseMap;

  EnumValues(this.map);

  Map<T, String> get reverse {
    if (reverseMap == null) {
      reverseMap = map.map((k, v) => new MapEntry(v, k));
    }
    return reverseMap;
  }
}

enum TicketStatus {
  open,
  customerReply,
}


Ticket.fromJson(Map<String, dynamic> json) {
   return Ticket(
      status: ticketStatusValues.map[json["status"]],
    );
}

@rknell
Copy link
Owner

rknell commented Jul 11, 2020

Closing as we will deal with this in #15

@rknell rknell closed this as completed Jul 11, 2020
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