Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump to LTS 14 (GHC 8.6.5) #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions aeson-injector.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ library
Data.Aeson.WithField

build-depends:
base >= 4.7 && < 4.11
, aeson >= 0.11 && < 1.3
base >= 4.7 && < 5
, aeson >= 0.11 && < 1.5
, bifunctors >= 5.2 && < 6
, deepseq >= 1.4 && < 2
, hashable >= 1.0 && < 2.0
Expand Down
26 changes: 13 additions & 13 deletions src/Data/Aeson/Unit.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ Maintainer : [email protected]
Stability : experimental
Portability : Portable

Common problem in REST interfaces when you need to return nothing as result,
Common problem in REST interfaces when you need to return nothing as result,
usage of `()` will produce `[]` JSON. That causes problems in some JSON parsers
in other languages.
in other languages.

So, `Unit` serialises into empty JSON object:

Expand All @@ -20,12 +20,12 @@ So, `Unit` serialises into empty JSON object:
-}
module Data.Aeson.Unit(
Unit(..)
) where
) where

import Control.Lens
import Data.Aeson
import Data.Swagger
import GHC.Generics
import Control.Lens
import Data.Aeson
import Data.Swagger
import GHC.Generics

-- | Data type that serialise into empty object in aeson
--
Expand All @@ -39,14 +39,14 @@ import GHC.Generics
data Unit = Unit
deriving (Generic, Eq, Show, Read, Enum, Bounded)

instance ToJSON Unit where
instance ToJSON Unit where
toJSON _ = object []

-- | Always a success parse
instance FromJSON Unit where
-- | Always a success parse
instance FromJSON Unit where
parseJSON _ = pure Unit

instance ToSchema Unit where
declareNamedSchema _ = do
instance ToSchema Unit where
declareNamedSchema _ = do
return $ NamedSchema Nothing $ mempty
& type_ .~ SwaggerObject
& type_ ?~ SwaggerObject
74 changes: 37 additions & 37 deletions src/Data/Aeson/WithField.hs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE OverloadedLists #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE OverloadedLists #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE TypeFamilies #-}
{-|
Module : Data.Aeson.WithField
Description : Provides utility to inject fields into aeson values.
Expand Down Expand Up @@ -160,24 +160,24 @@ module Data.Aeson.WithField(
, OnlyId
) where

import Control.Applicative
import Control.DeepSeq
import Control.Lens hiding ((.=))
import Control.Monad
import Data.Aeson
import Data.Aeson.Types (typeMismatch)
import Data.Hashable
import Data.Monoid
import Data.Proxy
import Data.Swagger
import GHC.Generics
import GHC.TypeLits
import Servant.Docs

import qualified Data.Foldable as F
import Control.Applicative
import Control.DeepSeq
import Control.Lens hiding ((.=))
import Control.Monad
import Data.Aeson
import Data.Aeson.Types (typeMismatch)
import Data.Hashable
import Data.Monoid
import Data.Proxy
import Data.Swagger
import GHC.Generics
import GHC.TypeLits
import Servant.Docs

import qualified Data.Foldable as F
import qualified Data.HashMap.Strict as H
import qualified Data.List as L
import qualified Data.Text as T
import qualified Data.List as L
import qualified Data.Text as T

-- | Injects field 'a' into 'b' with tag 's'. It has
-- special instances for 'ToJSON' and 'FromJSON' for
Expand Down Expand Up @@ -262,15 +262,15 @@ instance (KnownSymbol s, FromJSON a, FromJSON b) => FromJSON (WithField s a b) w
instance (KnownSymbol s, ToSchema a, ToSchema b) => ToSchema (WithField s a b) where
declareNamedSchema _ = do
NamedSchema n s <- declareNamedSchema (Proxy :: Proxy b)
if s ^. type_ == SwaggerObject then inline n s
if s ^. type_ == pure SwaggerObject then inline n s
else wrapper n s
where
field = T.pack $ symbolVal (Proxy :: Proxy s)
namePrefix = "WithField '" <> field <> "' "
wrapper n s = do
indexSchema <- declareSchema (Proxy :: Proxy a)
return $ NamedSchema (fmap (namePrefix <>) n) $ mempty
& type_ .~ SwaggerObject
& type_ ?~ SwaggerObject
& properties .~
[ ("value", Inline s)
, (field, Inline indexSchema)
Expand Down Expand Up @@ -336,11 +336,11 @@ instance (ToJSON a, ToJSON b) => ToJSON (WithFields a b) where
in case jsonb of
Object bvs -> case jsona of
Object avs -> Object $ H.union avs bvs
_ -> Object $ H.insert "injected" jsona bvs
_ -> Object $ H.insert "injected" jsona bvs
_ -> case jsona of
Object avs -> Object $ case H.lookup "value" avs of
Nothing -> H.insert "value" jsonb avs
Just _ -> avs
Just _ -> avs
_ -> object [
"injected" .= jsona
, "value" .= jsonb
Expand Down Expand Up @@ -369,7 +369,7 @@ instance (ToJSON a, FromJSON a, FromJSON b) => FromJSON (WithFields a b) where
extractFields :: ToJSON a => a -> [T.Text]
extractFields a = case toJSON a of
Object vs -> H.keys vs
_ -> []
_ -> []
parseJSON wat = typeMismatch "Expected JSON Object" wat

-- | Note: the instance tries to generate schema of the json as object with
Expand All @@ -381,18 +381,18 @@ instance (ToSchema a, ToSchema b) => ToSchema (WithFields a b) where
NamedSchema na sa <- declareNamedSchema (Proxy :: Proxy a)
let newName = combinedName <$> na <*> nb
return . NamedSchema newName $ case (sa ^. type_ , sb ^. type_) of
(SwaggerObject, SwaggerObject) -> sb <> sa
(SwaggerObject, _) -> bwrapper sb <> sa
(_, SwaggerObject) -> sb <> awrapper sa
_ -> bwrapper sb <> awrapper sa
(Just SwaggerObject, Just SwaggerObject) -> sb <> sa
(Just SwaggerObject, Just _) -> bwrapper sb <> sa
(Just _, Just SwaggerObject) -> sb <> awrapper sa
_ -> bwrapper sb <> awrapper sa
where
combinedName a b = "WithFields_" <> a <> "_" <> b
awrapper nas = mempty
& type_ .~ SwaggerObject
& type_ ?~ SwaggerObject
& properties .~ [ ("injected", Inline nas) ]
& required .~ [ "injected" ]
bwrapper nbs = mempty
& type_ .~ SwaggerObject
& type_ ?~ SwaggerObject
& properties .~ [ ("value", Inline nbs) ]
& required .~ [ "value" ]

Expand Down Expand Up @@ -437,7 +437,7 @@ instance (KnownSymbol s, ToSchema a) => ToSchema (OnlyField s a) where
declareNamedSchema _ = do
NamedSchema an as <- declareNamedSchema (Proxy :: Proxy a)
return $ NamedSchema (fmap ("OnlyField" <>) an) $ mempty
& type_ .~ SwaggerObject
& type_ ?~ SwaggerObject
& properties .~ [(field, Inline as)]
& required .~ [field]
where
Expand Down
4 changes: 1 addition & 3 deletions stack.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
resolver: lts-9.6
#resolver: nightly-2017-10-04
resolver: lts-14.2

packages:
- '.'
extra-deps: []

# system-ghc: true
# nix:
Expand Down