From 33a3f7e36a6506e556757d9ce08b2c3b4413c627 Mon Sep 17 00:00:00 2001 From: Poscat Date: Thu, 2 Apr 2020 08:39:58 +0800 Subject: [PATCH 1/2] Add instances for convenience --- src/Servant/Multipart.hs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/Servant/Multipart.hs b/src/Servant/Multipart.hs index 68aae04..1c819ed 100644 --- a/src/Servant/Multipart.hs +++ b/src/Servant/Multipart.hs @@ -173,6 +173,12 @@ data MultipartData tag = MultipartData , files :: [FileData tag] } +instance Semigroup (MultipartData tag) where + d1 <> d2 = MultipartData (inputs d1 <> inputs d2) (files d1 <> files d2) + +instance Monoid (MultipartData tag) where + mempty = MultipartData [] [] + fromRaw :: forall tag. ([Network.Wai.Parse.Param], [File (MultipartResult tag)]) -> MultipartData tag fromRaw (inputs, files) = MultipartData is fs @@ -274,6 +280,9 @@ class ToMultipart tag a where instance ToMultipart tag (MultipartData tag) where toMultipart = id +instance (ToMultipart tag a) => ToMultipart tag [a] where + toMultipart xs = foldMap toMultipart xs + -- | Upon seeing @MultipartForm a :> ...@ in an API type, --- servant-server will hand a value of type @a@ to your handler -- assuming the request body's content type is From 201c83bcac2fe843b54ac4b3d70425bb245a15be Mon Sep 17 00:00:00 2001 From: Poscat Date: Thu, 2 Apr 2020 08:43:38 +0800 Subject: [PATCH 2/2] Generalize --- src/Servant/Multipart.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Servant/Multipart.hs b/src/Servant/Multipart.hs index 1c819ed..6423193 100644 --- a/src/Servant/Multipart.hs +++ b/src/Servant/Multipart.hs @@ -280,7 +280,7 @@ class ToMultipart tag a where instance ToMultipart tag (MultipartData tag) where toMultipart = id -instance (ToMultipart tag a) => ToMultipart tag [a] where +instance (ToMultipart tag a, Foldable t) => ToMultipart tag (t a) where toMultipart xs = foldMap toMultipart xs -- | Upon seeing @MultipartForm a :> ...@ in an API type,