top | item 9349159

(no title)

bos | 11 years ago

I know nothing about the Pusher protocol, but the standard approach to boilerplate encoding/decoding problems these days is to use GHC.Generics.

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.

discuss

order

bos|11 years ago

In fact, it turns out the Pusher protocol is all JSON, so you can autogenerate the code.

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