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 /src | |
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.
Diffstat (limited to 'src')
-rw-r--r-- | src/Remote/KRPC.hs | 18 |
1 files changed, 15 insertions, 3 deletions
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 | ||