Skip to content

How do you work with enum types? #28

Answered by tchoutri
tonyalaribe asked this question in Q&A
Discussion options

You must be logged in to vote

Here is what I usually do:

data NotificationStatusEnum = NotificationRead | NotificationUnread deriving (Eq, Generic, Show)

instance ToField NotificationStatusEnum where
  toField NotificationRead   = Escape "Read"
  toField NotificationUnread = Escape "Unread"

instance FromField NotificationStatusEnum where
  fromField f mdata =
    case mdata of
      Nothing -> returnError UnexpectedNull f ""
      Just bs ->
        case parseNotificationStatusEnum bs of
          Just a  -> pure a
          Nothing -> returnError ConversionFailed f $ "Conversion error: Expected 'status' enum, got " <> decodeUtf8 bs <> " instead."

parseNotificationStatusEnum :: (Eq s, IsString s) => s -> Maybe Noti…

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
2 replies
@tonyalaribe
Comment options

Answer selected by tonyalaribe
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants