git.iko.soy/typeable/bloodhound
on master help sign in
Drop homespun query rendering, fix tests
commit270aa8b5f7a8ed957361ee63a5c984b2e9a9f725 authorMichael Xavier <michael@michaelxavier.net> dateFri 10 Jul 2015, 12:09 (11 years ago) parent2d8be0d
3 files changed+18−11
bloodhound.cabal+2−1 src/Database/Bloodhound/Client.hs+10−5 tests/tests.hs+6−5
bloodhound.cabal+2−1
@@ -37,7 +37,8 @@ library
3737 vector >= 0.10.9 && <0.11,
3838 uri-bytestring >= 0.1 && <0.2,
3939 exceptions,
40− data-default-class
40+ data-default-class,
41+ blaze-builder
4142 default-language: Haskell2010
4243
4344test-suite tests
src/Database/Bloodhound/Client.hs+10−5
@@ -54,6 +54,7 @@ module Database.Bloodhound.Client
5454 )
5555 where
5656
57+import qualified Blaze.ByteString.Builder as BB
5758import Control.Applicative
5859import Control.Monad
5960import Control.Monad.Catch
@@ -72,6 +73,7 @@ import qualified Data.Vector as V
7273import Network.HTTP.Client
7374import qualified Network.HTTP.Types.Method as NHTM
7475import qualified Network.HTTP.Types.Status as NHTS
76+import qualified Network.HTTP.Types.URI as NHTU
7577import Prelude hiding (filter, head)
7678import URI.ByteString hiding (Query)
7779
@@ -168,11 +170,12 @@ joinPath ps = do
168170
169171-- | Severely dumbed down query renderer. Assumes your data doesn't
170172-- need any encoding
171−addQuery :: [(Text, Text)] -> Text -> Text
172−addQuery ps u = u <> "?" <> params
173+addQuery :: [(Text, Maybe Text)] -> Text -> Text
174+addQuery q u = u <> rendered
173175 where
174− params = T.intercalate "&" (uncurry mkParam <$> ps)
175− mkParam k v = k <> "=" <> v
176+ rendered =
177+ T.decodeUtf8 $ BB.toByteString $ NHTU.renderQueryText prependQuestionMark q
178+ prependQuestionMark = True
176179
177180
178181bindM2 :: (Applicative m, Monad m) => (a -> b -> m c) -> m a -> m b -> m c
@@ -346,7 +349,9 @@ indexDocument (IndexName indexName)
346349 ExternalGTE (ExternalDocVersion v) -> versionParams v "external_gte"
347350 ForceVersion (ExternalDocVersion v) -> versionParams v "force"
348351 vt = T.pack . show . docVersionNumber
349− versionParams v t = [("version", vt v), ("version_type", t)]
352+ versionParams v t = [ ("version", Just $ vt v)
353+ , ("version_type", Just t)
354+ ]
350355 body = Just (encode document)
351356
352357-- | 'deleteDocument' is the primary way to delete a single document.
tests/tests.hs+6−5
@@ -5,6 +5,7 @@
55module Main where
66
77import Control.Applicative
8+import Control.Exception
89import Control.Monad
910import Control.Monad.Reader
1011import Data.Aeson
@@ -688,10 +689,10 @@ main = hspec $ do
688689
689690 describe "Enum DocVersion" $ do
690691 it "follows the laws of Enum, Bounded" $ do
691− return (succ maxBound :: DocVersion) `shouldThrow` anyErrorCall
692− return (pred minBound :: DocVersion) `shouldThrow` anyErrorCall
693− return (toEnum 0 :: DocVersion) `shouldThrow` anyErrorCall
694− return (toEnum 9200000000000000001 :: DocVersion) `shouldThrow` anyErrorCall
692+ evaluate (succ maxBound :: DocVersion) `shouldThrow` anyErrorCall
693+ evaluate (pred minBound :: DocVersion) `shouldThrow` anyErrorCall
694+ evaluate (toEnum 0 :: DocVersion) `shouldThrow` anyErrorCall
695+ evaluate (toEnum 9200000000000000001 :: DocVersion) `shouldThrow` anyErrorCall
695696 enumFrom (pred maxBound :: DocVersion) `shouldBe` [pred maxBound, maxBound]
696697 enumFrom (pred maxBound :: DocVersion) `shouldBe` [pred maxBound, maxBound]
697− enumFromThen minBound (pred maxBound :: DocVersion) `shouldBe` [minBound, pred maxBound, maxBound]
698+ enumFromThen minBound (pred maxBound :: DocVersion) `shouldBe` [minBound, pred maxBound]