Skip to content

Commit

Permalink
Add ToSchema instance for Map k ByteString (#140)
Browse files Browse the repository at this point in the history
Fixes #119
  • Loading branch information
Gabriella439 authored Sep 2, 2020
1 parent 185517b commit 0912c3e
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/Proto3/Suite/DotProto/Generate/Swagger.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@ import Control.Lens ((&), (?~))
#else
import Control.Lens ((&), (.~), (?~))
#endif
import Data.Aeson (Value (String))
import Data.Aeson (Value (String), ToJSONKey,
ToJSONKeyFunction(..))
import qualified Data.Aeson as Aeson
import Data.Aeson.Encode.Pretty (encodePretty)
import Data.ByteString (ByteString)
import qualified Data.ByteString.Lazy.Char8 as LC8
import Data.Hashable (Hashable)
import Data.HashMap.Strict.InsOrd (InsOrdHashMap)
import qualified Data.HashMap.Strict.InsOrd
import Data.Map (Map)
import Data.Swagger
import qualified Data.Text as T
import Data.Proxy
Expand Down Expand Up @@ -72,6 +75,17 @@ instance {-# OVERLAPPING #-} ToSchema (OverrideToSchema (V.Vector ByteString)) w
#endif
& items ?~ SwaggerItemsObject (Inline byteSchema)

instance {-# OVERLAPPING #-} (ToJSONKey k, ToSchema k) => ToSchema (OverrideToSchema (Map k ByteString)) where
declareNamedSchema _ = case Aeson.toJSONKey :: ToJSONKeyFunction k of
ToJSONKeyText _ _ -> do
return (NamedSchema Nothing schema_)
ToJSONKeyValue _ _ -> do
declareNamedSchema (Proxy :: Proxy [(k, (OverrideToSchema ByteString))])
where
schema_ = mempty
& type_ ?~ SwaggerObject
& additionalProperties ?~ AdditionalPropertiesSchema (Inline byteSchema)

{-| This is a convenience function that uses type inference to select the
correct instance of `ToSchema` to use for fields of a message
-}
Expand Down

0 comments on commit 0912c3e

Please sign in to comment.