Browse Source

Metadata euqlity does not deend on the order of the lines

Marcos Dumay de Medeiros 7 years ago
parent
commit
e730704b39
1 changed files with 21 additions and 2 deletions
  1. 21 2
      src/Walrus/Backend/Metadata.hs

+ 21 - 2
src/Walrus/Backend/Metadata.hs

@@ -56,10 +56,22 @@ data MaybeMetadata = MaybeMetadata {_mclientId :: Maybe ClientIdentity, _mclient
                                     _mftchOffset :: Int, _mftchSize :: Maybe Int
                                    } deriving (Show, Ord, Eq)
 
+uq :: Eq a => (b -> a) -> b -> b -> Bool
+uq f a b = f a == f b
+
+sq :: (Eq a, Ord a) => (b -> [a]) -> b -> b -> Bool
+sq f a b = (List.sort . f $ a) == (List.sort . f $ b)
+
 -- | All the data of a backend metadata, as the walrus specification.
 data Metadata = Metadata {_clientId :: ClientIdentity, _auth :: Maybe ByteString,
                           _recvDate :: UTCTime, _unrecognized :: [ByteString],
-                          _dataSize :: Int, _actionData :: ActionData} deriving (Show, Ord, Eq)
+                          _dataSize :: Int, _actionData :: ActionData} deriving (Show, Ord)
+instance Eq Metadata where
+  a == b = let
+    u f = uq f a b
+    s f = sq f a b
+    in and [u _clientId, u _auth, u _recvDate, u _dataSize, u _actionData] &&
+       s _unrecognized
 
 data ActionData = Deliver DeliverData |
                   WillHandle AccountData |
@@ -71,7 +83,14 @@ data ActionData = Deliver DeliverData |
 data DeliverData = DeliverData {clientName :: ByteString,
                                 mailFrom :: Account, rcptTo :: [Account], rcptFailed :: [(Account, Response)],
                                 bodyEnc :: Mime.BodyEncoding, smtpUtf8 :: Bool
-                               }  deriving (Show, Ord, Eq)
+                               }  deriving (Show, Ord)
+instance Eq DeliverData where
+    a == b = let
+      u f = uq f a b
+      s f = sq f a b
+      in and [u clientName, u mailFrom, u bodyEnc, u smtpUtf8] &&
+         and [s rcptTo, s rcptFailed]
+
 data AccountData = AccountRequest Account |
                    AccountResponse (Account, Response) |
                    AccountOk deriving (Show, Ord, Eq)