by bos · 11y ago · 0 comments
It doesn't always fit (typically when your data structures don't resemble the wire encoding), but it kills all the boilerplate when it does.
Since you say that you can magically get away without boilerplate in Ruby in this case, I would expect that the Generics approach will give exactly the same result.
Here's a Pusher message.
{
"event": "pusher:error",
"data": {
"message": String,
"code": Integer
}
}
Here's the corresponding Haskell. {-# LANGUAGE DeriveGeneric #-}
import GHC.Generics
-- a generic wrapper type for all Pusher events
data Event a = Event {
eventType :: Text,
eventData :: a
} deriving (Generic)
instance ToJSON a => ToJSON (Event a)
instance FromJSON a => FromJSON (Event a)
data Error = Error {
message :: Text,
code :: Integer
} deriving (Generic)
instance ToJSON Error
instance FromJSON Error