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

Allow aeson 2.0 #2315

Merged
merged 3 commits into from
Nov 2, 2021
Merged
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
3 changes: 2 additions & 1 deletion dhall-json/dhall-json.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Library
Hs-Source-Dirs: src
Build-Depends:
base >= 4.11.0.0 && < 5 ,
aeson >= 1.4.6.0 && < 1.6 ,
aeson >= 1.4.6.0 && < 2.1 ,
aeson-pretty < 0.9 ,
aeson-yaml >= 1.1.0 && < 1.2 ,
bytestring < 0.12,
Expand All @@ -59,6 +59,7 @@ Library
Dhall.JSON.Yaml
Dhall.DhallToYaml.Main
Other-Modules:
Dhall.JSON.Compat
Dhall.JSON.Util
GHC-Options: -Wall
Default-Language: Haskell2010
Expand Down
10 changes: 5 additions & 5 deletions dhall-json/src/Dhall/JSON.hs
Original file line number Diff line number Diff line change
Expand Up @@ -230,14 +230,14 @@ import Prettyprinter (Pretty)

import qualified Data.Aeson as Aeson
import qualified Data.Foldable as Foldable
import qualified Data.HashMap.Strict as HashMap
import qualified Data.List
import qualified Data.Map
import qualified Data.Ord
import qualified Data.Text
import qualified Data.Vector as Vector
import qualified Dhall.Core as Core
import qualified Dhall.Import
import qualified Dhall.JSON.Compat as JSON.Compat
import qualified Dhall.Map
import qualified Dhall.Optics
import qualified Dhall.Parser
Expand Down Expand Up @@ -536,7 +536,7 @@ dhallToJSON e0 = loop (Core.alphaNormalize (Core.normalize e0))

ys <- traverse inner (Foldable.toList xs)

return (Aeson.Object (HashMap.fromList ys))
return (Aeson.Object (JSON.Compat.objectFromList ys))
outer (Core.App (Core.Field (V 0) (FA "number")) (Core.DoubleLit (DhallDouble n))) =
return (Aeson.toJSON n)
outer (Core.App (Core.Field (V 0) (FA "string")) (Core.TextLit (Core.Chunks [] text))) =
Expand Down Expand Up @@ -581,7 +581,7 @@ dhallToJSON e0 = loop (Core.alphaNormalize (Core.normalize e0))

ys <- traverse inner (Foldable.toList xs)

return (Aeson.Object (HashMap.fromList ys))
return (Aeson.Object (JSON.Compat.objectFromList ys))
outer (Core.App (Core.Field (V 0) (FA "double")) (Core.DoubleLit (DhallDouble n))) =
return (Aeson.toJSON n)
outer (Core.App (Core.Field (V 0) (FA "integer")) (Core.IntegerLit n)) =
Expand Down Expand Up @@ -635,7 +635,7 @@ toOrderedList =
omitNull :: Value -> Value
omitNull (Object object) = Object fields
where
fields =HashMap.filter (/= Null) (fmap omitNull object)
fields = JSON.Compat.filterObject (/= Null) (fmap omitNull object)
omitNull (Array array) =
Array (fmap omitNull array)
omitNull (String string) =
Expand All @@ -654,7 +654,7 @@ omitEmpty :: Value -> Value
omitEmpty (Object object) =
if null fields then Null else Object fields
where
fields = HashMap.filter (/= Null) (fmap omitEmpty object)
fields = JSON.Compat.filterObject (/= Null) (fmap omitEmpty object)
omitEmpty (Array array) =
if null elems then Null else Array elems
where
Expand Down
79 changes: 79 additions & 0 deletions dhall-json/src/Dhall/JSON/Compat.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
{-# LANGUAGE CPP #-}

-- | Compatibility helpers for the @aeson-2@ migration.
module Dhall.JSON.Compat (
objectFromList
, mapToAscList
, filterObject
, lookupObject
, traverseObjectWithKey
, objectKeys
, textToKey
) where

import Data.Aeson (Object, Value)
import Data.Text (Text)

#if MIN_VERSION_aeson(2,0,0)
import Data.Aeson.Key (Key)
import qualified Data.Aeson.Key as Key
import Data.Aeson.KeyMap (KeyMap)
import qualified Data.Aeson.KeyMap as KeyMap
import Data.Bifunctor (first)
#else
import Data.HashMap.Strict (HashMap)
import qualified Data.HashMap.Strict as HashMap
import qualified Data.List as List
#endif

objectFromList :: [(Text, Value)] -> Object
#if MIN_VERSION_aeson(2,0,0)
objectFromList = KeyMap.fromList . map (first Key.fromText)
#else
objectFromList = HashMap.fromList
#endif

filterObject :: (Value -> Bool) -> Object -> Object
#if MIN_VERSION_aeson(2,0,0)
filterObject = KeyMap.filter
#else
filterObject = HashMap.filter
#endif

#if MIN_VERSION_aeson(2,0,0)
mapToAscList :: KeyMap a -> [(Text, a)]
mapToAscList = map (first Key.toText) . KeyMap.toAscList
#else
mapToAscList :: HashMap Text a -> [(Text, a)]
mapToAscList = List.sortOn fst . HashMap.toList
#endif

lookupObject :: Text -> Object -> Maybe Value
#if MIN_VERSION_aeson(2,0,0)
lookupObject k = KeyMap.lookup (Key.fromText k)
#else
lookupObject = HashMap.lookup
#endif

objectKeys :: Object -> [Text]
#if MIN_VERSION_aeson(2,0,0)
objectKeys = map (Key.toText) . KeyMap.keys
#else
objectKeys = HashMap.keys
#endif

#if MIN_VERSION_aeson(2,0,0)
textToKey :: Text -> Key
textToKey = Key.fromText
#else
textToKey :: Text -> Text
textToKey = id
#endif

#if MIN_VERSION_aeson(2,0,0)
traverseObjectWithKey :: Applicative f => (Key -> v1 -> f v2) -> KeyMap v1 -> f (KeyMap v2)
traverseObjectWithKey = KeyMap.traverseWithKey
#else
traverseObjectWithKey :: Applicative f => (Text -> v1 -> f v2) -> HashMap Text v1 -> f (HashMap Text v2)
traverseObjectWithKey = HashMap.traverseWithKey
#endif
34 changes: 13 additions & 21 deletions dhall-json/src/Dhall/JSONToDhall.hs
Original file line number Diff line number Diff line change
Expand Up @@ -381,17 +381,15 @@ import qualified Data.Aeson as Aeson
import qualified Data.Aeson.Types as Aeson.Types
import qualified Data.ByteString.Lazy.Char8 as BSL8
import qualified Data.Foldable as Foldable
import qualified Data.HashMap.Strict as HM
import qualified Data.List as List
import qualified Data.Map
import qualified Data.Map.Merge.Lazy as Data.Map.Merge
import qualified Data.Ord as Ord
import qualified Data.Sequence as Seq
import qualified Data.String
import qualified Data.Text as Text
import qualified Data.Vector as Vector
import qualified Dhall.Core as D
import qualified Dhall.Import
import qualified Dhall.JSON.Compat as JSON.Compat
import qualified Dhall.Lint as Lint
import qualified Dhall.Map as Map
import qualified Dhall.Optics as Optics
Expand Down Expand Up @@ -519,8 +517,8 @@ typeCheckSchemaExpr compileException expr =

keyValMay :: Value -> Maybe (Text, Value)
keyValMay (Aeson.Object o) = do
Aeson.String k <- HM.lookup "key" o
v <- HM.lookup "value" o
Aeson.String k <- JSON.Compat.lookupObject "key" o
v <- JSON.Compat.lookupObject "value" o
return (k, v)
keyValMay _ = Nothing

Expand All @@ -532,7 +530,7 @@ keyValMay _ = Nothing
-}
inferSchema :: Value -> Schema
inferSchema (Aeson.Object m) =
let convertMap = Data.Map.fromList . HM.toList
let convertMap = Data.Map.fromDistinctAscList . JSON.Compat.mapToAscList

in (Record . RecordSchema . convertMap) (fmap inferSchema m)
inferSchema (Aeson.Array xs) =
Expand Down Expand Up @@ -832,13 +830,13 @@ dhallFromJSON (Conversion {..}) expressionType =

-- object ~> Record
loop jsonPath (D.Record r) v@(Aeson.Object o)
| extraKeys <- HM.keys o \\ Map.keys r
| extraKeys <- JSON.Compat.objectKeys o \\ Map.keys r
, strictRecs && not (null extraKeys)
= Left (UnhandledKeys extraKeys (D.Record r) v jsonPath)
| otherwise
= let f :: Text -> ExprX -> Either CompileError ExprX
f k t | Just value <- HM.lookup k o
= loop (Aeson.Types.Key k : jsonPath) t value
f k t | Just value <- JSON.Compat.lookupObject k o
= loop (Aeson.Types.Key (JSON.Compat.textToKey k) : jsonPath) t value
| App D.Optional t' <- t
= Right (App D.None t')
| App D.List _ <- t
Expand All @@ -853,7 +851,7 @@ dhallFromJSON (Conversion {..}) expressionType =
| not noKeyValArr
, os :: [Value] <- toList a
, Just kvs <- traverse keyValMay os
= loop jsonPath t (Aeson.Object $ HM.fromList kvs)
= loop jsonPath t (Aeson.Object $ JSON.Compat.objectFromList kvs)
| noKeyValArr
= Left (NoKeyValArray t v)
| otherwise
Expand All @@ -866,7 +864,7 @@ dhallFromJSON (Conversion {..}) expressionType =
, Just mapKey <- D.recordFieldValue <$> Map.lookup "mapKey" r
, Just mapValue <- D.recordFieldValue <$> Map.lookup "mapValue" r
= do
keyExprMap <- HM.traverseWithKey (\k child -> loop (Aeson.Types.Key k : jsonPath) mapValue child) o
keyExprMap <- JSON.Compat.traverseObjectWithKey (\k child -> loop (Aeson.Types.Key k : jsonPath) mapValue child) o

toKey <-
case mapKey of
Expand All @@ -881,9 +879,9 @@ dhallFromJSON (Conversion {..}) expressionType =
]

let records =
(fmap f . Seq.fromList . List.sort . HM.toList) keyExprMap
(fmap f . Seq.fromList . JSON.Compat.mapToAscList) keyExprMap

let typeAnn = if HM.null o then Just t else Nothing
let typeAnn = if null o then Just t else Nothing

return (D.ListLit typeAnn records)
| noKeyValMap
Expand Down Expand Up @@ -972,10 +970,7 @@ dhallFromJSON (Conversion {..}) expressionType =
elements =
Seq.fromList
(fmap inner
(List.sortBy
(Ord.comparing fst)
(HM.toList o)
)
(JSON.Compat.mapToAscList o)
)

elementType
Expand Down Expand Up @@ -1061,10 +1056,7 @@ dhallFromJSON (Conversion {..}) expressionType =
elements =
Seq.fromList
(fmap inner
(List.sortBy
(Ord.comparing fst)
(HM.toList o)
)
(JSON.Compat.mapToAscList o)
)

elementType
Expand Down
2 changes: 1 addition & 1 deletion dhall-nixpkgs/dhall-nixpkgs.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Build-Type: Simple
Executable dhall-to-nixpkgs
Main-Is: Main.hs
Build-Depends: base >= 4.11 && < 5
, aeson >= 1.0.0.0 && < 1.6
, aeson >= 1.0.0.0 && < 2.1
, bytestring < 0.12
, data-fix
, dhall >= 1.32.0 && < 1.41
Expand Down
2 changes: 1 addition & 1 deletion dhall-openapi/dhall-openapi.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ Library
Ghc-Options: -Wall
Build-Depends:
base >= 4.11.0.0 && < 5 ,
aeson >= 1.0.0.0 && < 1.6 ,
aeson >= 1.0.0.0 && < 2.1 ,
containers >= 0.5.8.0 && < 0.7 ,
dhall >= 1.38.0 && < 1.41 ,
prettyprinter >= 1.7.0 && < 1.8 ,
Expand Down
2 changes: 1 addition & 1 deletion dhall-yaml/dhall-yaml.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Library
HsYAML >= 0.2 && < 0.3 ,
HsYAML-aeson >= 0.2 && < 0.3 ,
base >= 4.11.0.0 && < 5 ,
aeson >= 1.0.0.0 && < 1.6 ,
aeson >= 1.0.0.0 && < 2.1 ,
bytestring < 0.12,
dhall >= 1.31.0 && < 1.41,
dhall-json >= 1.6.0 && < 1.8 ,
Expand Down
2 changes: 1 addition & 1 deletion dhall/dhall.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ Library
Hs-Source-Dirs: src
Build-Depends:
base >= 4.11.0.0 && < 5 ,
aeson >= 1.0.0.0 && < 1.6 ,
aeson >= 1.0.0.0 && < 2.1 ,
aeson-pretty < 0.9 ,
ansi-terminal >= 0.6.3.1 && < 0.12,
atomic-write >= 0.2.0.7 && < 0.3 ,
Expand Down