Skip to content

Commit 9fc934b

Browse files
committed
encode and decode Event and Payload correctly
1 parent 50455a7 commit 9fc934b

File tree

3 files changed

+32
-12
lines changed

3 files changed

+32
-12
lines changed

Web/SocketIO/Types/Event.hs

+11-4
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ import Web.SocketIO.Types.String
2020
import Control.Applicative
2121
import Data.Aeson
2222
import Data.Aeson.Encode (encodeToTextBuilder)
23+
import Data.List (intersperse)
2324
import Data.Text.Internal.Builder (toLazyText)
24-
import Data.Vector (toList)
25+
import qualified Data.Text.Lazy as TL
26+
import Data.Vector (toList)
2527
--------------------------------------------------------------------------------
2628
-- | Name of an Event
2729
type EventName = Text
@@ -30,14 +32,19 @@ type EventName = Text
3032
-- | Payload carried by an Event
3133
data Payload = Payload [Text] deriving (Eq, Show)
3234

35+
instance Serializable Payload where
36+
serialize (Payload payload) = serialize $ '[' `TL.cons` (TL.concat $ intersperse "," payload) `TL.snoc` ']'
37+
3338
--------------------------------------------------------------------------------
3439
-- | Event
3540
data Event = Event EventName Payload
3641
| NoEvent -- ^ some malformed shit
3742
deriving (Show, Eq)
3843

3944
instance Serializable Event where
40-
serialize = serialize . encode
45+
serialize (Event name (Payload [])) = serialize $ "{\"name\":\"" `TL.append` name `TL.append` "\"}"
46+
serialize (Event name payload) = serialize $ "{\"name\":\"" `TL.append` name `TL.append` "\",\"args\":" `TL.append` serialize payload `TL.append` "}"
47+
serialize NoEvent = ""
4148

4249
instance FromJSON Event where
4350
parseJSON (Object v) = Event <$>
@@ -51,8 +58,8 @@ instance FromJSON Event where
5158
parseJSON _ = return NoEvent
5259

5360
instance ToJSON Event where
54-
toJSON (Event name (Payload [])) = object ["name" .= name]
55-
toJSON (Event name (Payload args)) = object ["name" .= name, "args" .= args]
61+
toJSON (Event name (Payload [])) = object ["name" .= name]
62+
toJSON (Event name (Payload payload)) = object ["name" .= name, "args" .= payload]
5663
toJSON NoEvent = object []
5764

5865
--------------------------------------------------------------------------------

test/Test/Instances/Value.hs

+13-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
{-# LANGUAGE TypeSynonymInstances #-}
55
{-# OPTIONS_GHC -fno-warn-orphans #-}
66

7-
module Test.Instances.Value (Value(..)) where
7+
module Test.Instances.Value (Value(..), arbitraryJSONString) where
88

99
import Web.SocketIO.Types.String
1010

@@ -14,6 +14,7 @@ import Data.Aeson.Types
1414
import Data.Aeson.Encode (encode)
1515
import qualified Data.HashMap.Strict as H
1616
import Data.Scientific (Scientific, scientific)
17+
import qualified Data.Text.Lazy as TL
1718
import Data.Vector (fromList)
1819
import Test.QuickCheck
1920

@@ -28,6 +29,9 @@ instance Serializable Value where
2829
instance Arbitrary StrictText where
2930
arbitrary = fmap fromString arbitrary
3031

32+
instance Arbitrary Text where
33+
arbitrary = fmap fromString arbitrary
34+
3135
instance Arbitrary Scientific where
3236
arbitrary = do
3337
c <- arbitrary
@@ -41,9 +45,15 @@ instance Arbitrary Value where
4145
s <- arbitrary
4246
n <- arbitrary
4347
b <- arbitrary
44-
elements [Object o, Array a, String s, Number n, Bool b, Null]
48+
elements [Object o, Array a, String s, Number n, Bool b]
4549

4650
where arbitraryPair = do
4751
k <- arbitrary
4852
v <- arbitrary
49-
return (k, v)
53+
return (k, v)
54+
55+
arbitraryJSONString :: Gen Text
56+
arbitraryJSONString = TL.filter badthings <$> arbitrary
57+
where badthings '"' = False
58+
badthings '\\' = False
59+
badthings _ = True

test/Test/Protocol.hs

+8-5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ module Test.Protocol (test) where
1010
import Web.SocketIO.Types
1111
import Web.SocketIO.Protocol
1212

13+
import Data.Aeson
1314
import Test.Instances.Value
1415
--------------------------------------------------------------------------------
1516
import Control.Applicative ((<$>))
@@ -37,14 +38,16 @@ instance Arbitrary Data where
3738
d <- friendlyStringGen
3839
elements [NoData, Data d]
3940

40-
instance Arbitrary Text where
41-
arbitrary = fmap fromString arbitrary
41+
instance Arbitrary Payload where
42+
arbitrary = do
43+
payload <- map serialize <$> listOf (arbitrary :: Gen Value)
44+
return $ Payload payload
4245

4346
instance Arbitrary Event where
4447
arbitrary = do
45-
eventName <- arbitrary
46-
--payload <- arbitrary
47-
elements [NoEvent, Event eventName (Payload [])]
48+
eventName <- arbitraryJSONString
49+
payload <- arbitrary
50+
elements [NoEvent, Event eventName payload]
4851

4952
instance Arbitrary Message where
5053
arbitrary = do

0 commit comments

Comments
 (0)