diff options
author | Sam T <pxqr.sta@gmail.com> | 2013-07-08 22:34:16 +0400 |
---|---|---|
committer | Sam T <pxqr.sta@gmail.com> | 2013-07-08 22:34:16 +0400 |
commit | 76b4937c99f131bbe52ef22b03a0bb7317280257 (patch) | |
tree | 4154022e200461f3c1cdbd0a9e036b935e56b639 | |
parent | a437e18badb78bd4946ce4ecec830acdf000abee (diff) |
~ Allow passing raw dictionaries.
We need this in Kademlia DHT -- there are method which return
dictionaries with different keys depending on DHT server state.
-rw-r--r-- | krpc.cabal | 1 | ||||
-rw-r--r-- | src/Remote/KRPC.hs | 18 | ||||
-rw-r--r-- | tests/Client.hs | 8 | ||||
-rw-r--r-- | tests/Server.hs | 1 | ||||
-rw-r--r-- | tests/Shared.hs | 6 |
5 files changed, 30 insertions, 4 deletions
@@ -57,6 +57,7 @@ test-suite test-client | |||
57 | other-modules: Shared | 57 | other-modules: Shared |
58 | build-depends: base == 4.* | 58 | build-depends: base == 4.* |
59 | , bytestring | 59 | , bytestring |
60 | , containers | ||
60 | , process | 61 | , process |
61 | , filepath | 62 | , filepath |
62 | 63 | ||
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 | |||
175 | => [ParamName] -> Map ParamName BEncode -> Result arg | 175 | => [ParamName] -> Map ParamName BEncode -> Result arg |
176 | extractArgs as d = fromBEncode =<< | 176 | extractArgs as d = fromBEncode =<< |
177 | case as of | 177 | case as of |
178 | [] -> Right (BList []) | 178 | [] -> if M.null d |
179 | then Right (BList []) | ||
180 | else Right (BDict d) | ||
179 | [x] -> f x | 181 | [x] -> f x |
180 | xs -> BList <$> mapM f xs | 182 | xs -> BList <$> mapM f xs |
181 | where | 183 | where |
@@ -184,12 +186,22 @@ extractArgs as d = fromBEncode =<< | |||
184 | {-# INLINE extractArgs #-} | 186 | {-# INLINE extractArgs #-} |
185 | 187 | ||
186 | injectVals :: BEncodable arg => [ParamName] -> arg -> [(ParamName, BEncode)] | 188 | injectVals :: BEncodable arg => [ParamName] -> arg -> [(ParamName, BEncode)] |
187 | injectVals [] (toBEncode -> BList []) = [] | 189 | injectVals [] (toBEncode -> be) |
190 | = case be of | ||
191 | BList [] -> [] | ||
192 | BDict d -> M.toList d | ||
193 | _ -> invalidParamList [] be | ||
194 | |||
188 | injectVals [p] (toBEncode -> arg) = [(p, arg)] | 195 | injectVals [p] (toBEncode -> arg) = [(p, arg)] |
189 | injectVals ps (toBEncode -> BList as) = L.zip ps as | 196 | injectVals ps (toBEncode -> BList as) = L.zip ps as |
190 | injectVals _ _ = error "KRPC.injectVals: impossible" | 197 | injectVals pl a = invalidParamList pl (toBEncode a) |
191 | {-# INLINE injectVals #-} | 198 | {-# INLINE injectVals #-} |
192 | 199 | ||
200 | invalidParamList :: [ParamName] -> BEncode -> a | ||
201 | invalidParamList pl be | ||
202 | = error $ "KRPC invalid parameter list: " ++ show pl ++ "\n" ++ | ||
203 | "while procedure args are: " ++ show be | ||
204 | |||
193 | -- | Alias to Socket, through might change in future. | 205 | -- | Alias to Socket, through might change in future. |
194 | type Remote = Socket | 206 | type Remote = Socket |
195 | 207 | ||
diff --git a/tests/Client.hs b/tests/Client.hs index 1b9ef8d2..313cd56e 100644 --- a/tests/Client.hs +++ b/tests/Client.hs | |||
@@ -5,6 +5,7 @@ import Control.Concurrent | |||
5 | import Control.Exception | 5 | import Control.Exception |
6 | import qualified Data.ByteString as B | 6 | import qualified Data.ByteString as B |
7 | import Data.BEncode | 7 | import Data.BEncode |
8 | import Data.Map | ||
8 | import System.Environment | 9 | import System.Environment |
9 | import System.Process | 10 | import System.Process |
10 | import System.FilePath | 11 | import System.FilePath |
@@ -69,4 +70,11 @@ tests = | |||
69 | 70 | ||
70 | , testCase "raw method" $ | 71 | , testCase "raw method" $ |
71 | BInteger 10 ==? call addr rawM (BInteger 10) | 72 | BInteger 10 ==? call addr rawM (BInteger 10) |
73 | |||
74 | , testCase "raw dict" $ | ||
75 | let dict = BDict $ fromList | ||
76 | [ ("some_int", BInteger 100) | ||
77 | , ("some_list", BList [BInteger 10]) | ||
78 | ] | ||
79 | in dict ==? call addr rawDictM dict | ||
72 | ] | 80 | ] |
diff --git a/tests/Server.hs b/tests/Server.hs index 7cd6a5d6..aaf6d9f2 100644 --- a/tests/Server.hs +++ b/tests/Server.hs | |||
@@ -15,4 +15,5 @@ main = server 6000 | |||
15 | , reverseM ==> return . reverse | 15 | , reverseM ==> return . reverse |
16 | , shiftR ==> \(a, b, c) -> return (c, a, b) | 16 | , shiftR ==> \(a, b, c) -> return (c, a, b) |
17 | , rawM ==> return | 17 | , rawM ==> return |
18 | , rawDictM ==> return | ||
18 | ] | 19 | ] |
diff --git a/tests/Shared.hs b/tests/Shared.hs index a04b6093..f64112da 100644 --- a/tests/Shared.hs +++ b/tests/Shared.hs | |||
@@ -7,6 +7,7 @@ module Shared | |||
7 | , reverseM | 7 | , reverseM |
8 | , shiftR | 8 | , shiftR |
9 | , rawM | 9 | , rawM |
10 | , rawDictM | ||
10 | ) where | 11 | ) where |
11 | 12 | ||
12 | import Data.ByteString (ByteString) | 13 | import Data.ByteString (ByteString) |
@@ -32,4 +33,7 @@ shiftR :: Method ((), Int, [Int]) ([Int], (), Int) | |||
32 | shiftR = method "shiftR" ["x", "y", "z"] ["a", "b", "c"] | 33 | shiftR = method "shiftR" ["x", "y", "z"] ["a", "b", "c"] |
33 | 34 | ||
34 | rawM :: Method BEncode BEncode | 35 | rawM :: Method BEncode BEncode |
35 | rawM = method "rawM" [""] [""] \ No newline at end of file | 36 | rawM = method "rawM" [""] [""] |
37 | |||
38 | rawDictM :: Method BEncode BEncode | ||
39 | rawDictM = method "m" [] [] \ No newline at end of file | ||