From 76b4937c99f131bbe52ef22b03a0bb7317280257 Mon Sep 17 00:00:00 2001 From: Sam T Date: Mon, 8 Jul 2013 22:34:16 +0400 Subject: ~ Allow passing raw dictionaries. We need this in Kademlia DHT -- there are method which return dictionaries with different keys depending on DHT server state. --- src/Remote/KRPC.hs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/Remote/KRPC.hs b/src/Remote/KRPC.hs index e1ad0853..1b4ae4b6 100644 --- a/src/Remote/KRPC.hs +++ b/src/Remote/KRPC.hs @@ -175,7 +175,9 @@ extractArgs :: BEncodable arg => [ParamName] -> Map ParamName BEncode -> Result arg extractArgs as d = fromBEncode =<< case as of - [] -> Right (BList []) + [] -> if M.null d + then Right (BList []) + else Right (BDict d) [x] -> f x xs -> BList <$> mapM f xs where @@ -184,12 +186,22 @@ extractArgs as d = fromBEncode =<< {-# INLINE extractArgs #-} injectVals :: BEncodable arg => [ParamName] -> arg -> [(ParamName, BEncode)] -injectVals [] (toBEncode -> BList []) = [] +injectVals [] (toBEncode -> be) + = case be of + BList [] -> [] + BDict d -> M.toList d + _ -> invalidParamList [] be + injectVals [p] (toBEncode -> arg) = [(p, arg)] injectVals ps (toBEncode -> BList as) = L.zip ps as -injectVals _ _ = error "KRPC.injectVals: impossible" +injectVals pl a = invalidParamList pl (toBEncode a) {-# INLINE injectVals #-} +invalidParamList :: [ParamName] -> BEncode -> a +invalidParamList pl be + = error $ "KRPC invalid parameter list: " ++ show pl ++ "\n" ++ + "while procedure args are: " ++ show be + -- | Alias to Socket, through might change in future. type Remote = Socket -- cgit v1.2.3