mirror of
https://github.com/input-output-hk/foliage.git
synced 2024-09-11 20:27:09 +03:00
Move json stuff to its own module
This commit is contained in:
parent
678943d4b6
commit
9cc19c70ea
@ -8,13 +8,18 @@ import Distribution.Types.PackageName (PackageName)
|
||||
import Distribution.Types.Version (Version)
|
||||
import Distribution.Types.VersionRange (VersionRange)
|
||||
import Distribution.Utils.ShortText (ShortText)
|
||||
import Data.Aeson (ToJSON)
|
||||
|
||||
instance Hashable ShortText
|
||||
instance Hashable PackageIdentifier
|
||||
|
||||
instance Hashable PackageName
|
||||
|
||||
instance Hashable PackageIdentifier
|
||||
instance Hashable ShortText
|
||||
|
||||
instance Hashable Version
|
||||
|
||||
instance Hashable VersionRange
|
||||
|
||||
instance ToJSON Version
|
||||
|
||||
instance ToJSON VersionRange
|
||||
|
@ -3,10 +3,13 @@
|
||||
{-# LANGUAGE DerivingVia #-}
|
||||
{-# LANGUAGE OverloadedStrings #-}
|
||||
{-# LANGUAGE PatternSynonyms #-}
|
||||
{-# LANGUAGE StandaloneDeriving #-}
|
||||
|
||||
module Foliage.Meta
|
||||
( PackageMeta (PackageMeta),
|
||||
PackageMetaEntry (PackageMetaEntry),
|
||||
packageMetaEntryDeprecated,
|
||||
packageMetaEntryPreferred,
|
||||
packageMetaEntryTimestamp,
|
||||
readPackageMeta,
|
||||
writePackageMeta,
|
||||
PackageVersionMeta (PackageVersionMeta),
|
||||
@ -32,7 +35,6 @@ where
|
||||
|
||||
import Control.Applicative ((<|>))
|
||||
import Control.Monad (void)
|
||||
import Data.Aeson qualified as Aeson
|
||||
import Data.List (sortOn)
|
||||
import Data.Maybe (fromMaybe)
|
||||
import Data.Ord
|
||||
@ -80,14 +82,6 @@ data PackageMetaEntry = PackageMetaEntry
|
||||
deriving (Show, Eq, Generic)
|
||||
deriving anyclass (Binary, Hashable, NFData)
|
||||
|
||||
instance Aeson.ToJSON Version
|
||||
|
||||
instance Aeson.ToJSON VersionRange
|
||||
|
||||
instance Aeson.ToJSON PackageMetaEntry
|
||||
|
||||
instance Aeson.ToJSON PackageMeta
|
||||
|
||||
readPackageMeta :: FilePath -> IO PackageMeta
|
||||
readPackageMeta = Toml.decodeFile packageMetaCodec
|
||||
|
||||
@ -125,13 +119,9 @@ _VersionRange = Toml._TextBy showVersion parseVersion
|
||||
newtype GitHubRepo = GitHubRepo {unGitHubRepo :: Text}
|
||||
deriving (Show, Eq, Binary, Hashable, NFData) via Text
|
||||
|
||||
deriving via Text instance Aeson.ToJSON GitHubRepo
|
||||
|
||||
newtype GitHubRev = GitHubRev {unGitHubRev :: Text}
|
||||
deriving (Show, Eq, Binary, Hashable, NFData) via Text
|
||||
|
||||
deriving via Text instance Aeson.ToJSON GitHubRev
|
||||
|
||||
data PackageVersionSource
|
||||
= TarballSource
|
||||
{ tarballSourceURI :: URI,
|
||||
@ -145,16 +135,6 @@ data PackageVersionSource
|
||||
deriving (Show, Eq, Generic)
|
||||
deriving anyclass (Binary, Hashable, NFData)
|
||||
|
||||
instance Aeson.ToJSON PackageVersionSource where
|
||||
toJSON =
|
||||
Aeson.genericToJSON
|
||||
Aeson.defaultOptions
|
||||
{ Aeson.sumEncoding = Aeson.ObjectWithSingleField
|
||||
}
|
||||
|
||||
instance Aeson.ToJSON URI where
|
||||
toJSON = Aeson.toJSON . show
|
||||
|
||||
packageSourceCodec :: TomlCodec PackageVersionSource
|
||||
packageSourceCodec =
|
||||
Toml.dimatch matchTarballSource (uncurry TarballSource) tarballSourceCodec
|
||||
@ -207,8 +187,6 @@ data PackageVersionMeta = PackageVersionMeta
|
||||
deriving (Show, Eq, Generic)
|
||||
deriving anyclass (Binary, Hashable, NFData)
|
||||
|
||||
instance Aeson.ToJSON PackageVersionMeta
|
||||
|
||||
sourceMetaCodec :: TomlCodec PackageVersionMeta
|
||||
sourceMetaCodec =
|
||||
PackageVersionMeta
|
||||
@ -230,8 +208,6 @@ data RevisionMeta = RevisionMeta
|
||||
deriving (Show, Eq, Generic)
|
||||
deriving anyclass (Binary, Hashable, NFData)
|
||||
|
||||
instance Aeson.ToJSON RevisionMeta
|
||||
|
||||
revisionMetaCodec :: TomlCodec RevisionMeta
|
||||
revisionMetaCodec =
|
||||
RevisionMeta
|
||||
|
33
app/Foliage/Meta/Aeson.hs
Normal file
33
app/Foliage/Meta/Aeson.hs
Normal file
@ -0,0 +1,33 @@
|
||||
{-# LANGUAGE DerivingVia #-}
|
||||
{-# LANGUAGE StandaloneDeriving #-}
|
||||
{-# OPTIONS_GHC -Wno-orphans #-}
|
||||
|
||||
module Foliage.Meta.Aeson where
|
||||
|
||||
import Data.Aeson
|
||||
import Data.Text
|
||||
import Foliage.Meta
|
||||
import Foliage.Utils.Aeson
|
||||
import Network.URI (URI)
|
||||
|
||||
deriving via MyAesonEncoding PackageMeta instance ToJSON PackageMeta
|
||||
|
||||
deriving via MyAesonEncoding PackageMetaEntry instance ToJSON PackageMetaEntry
|
||||
|
||||
deriving via MyAesonEncoding RevisionMeta instance ToJSON RevisionMeta
|
||||
|
||||
deriving via MyAesonEncoding PackageVersionMeta instance ToJSON PackageVersionMeta
|
||||
|
||||
deriving via Text instance ToJSON GitHubRepo
|
||||
|
||||
deriving via Text instance ToJSON GitHubRev
|
||||
|
||||
instance ToJSON PackageVersionSource where
|
||||
toJSON =
|
||||
genericToJSON
|
||||
defaultOptions
|
||||
{ sumEncoding = ObjectWithSingleField
|
||||
}
|
||||
|
||||
instance ToJSON URI where
|
||||
toJSON = toJSON . show
|
22
app/Foliage/Utils/Aeson.hs
Normal file
22
app/Foliage/Utils/Aeson.hs
Normal file
@ -0,0 +1,22 @@
|
||||
{-# LANGUAGE DeriveGeneric #-}
|
||||
{-# LANGUAGE FlexibleContexts #-}
|
||||
{-# LANGUAGE ScopedTypeVariables #-}
|
||||
{-# LANGUAGE UndecidableInstances #-}
|
||||
module Foliage.Utils.Aeson where
|
||||
|
||||
import Data.Aeson
|
||||
import GHC.Generics
|
||||
import Data.Coerce
|
||||
|
||||
newtype MyAesonEncoding a = MyAesonEncoding a
|
||||
deriving Generic
|
||||
|
||||
myOptions :: Options
|
||||
myOptions =
|
||||
defaultOptions
|
||||
{ sumEncoding = ObjectWithSingleField
|
||||
}
|
||||
|
||||
instance (Generic a, GToJSON' Value Zero (Rep a), GToJSON' Encoding Zero (Rep a)) => ToJSON (MyAesonEncoding a) where
|
||||
toJSON = coerce (genericToJSON defaultOptions :: a -> Value)
|
||||
toEncoding = coerce (genericToEncoding defaultOptions :: a -> Encoding)
|
@ -21,6 +21,7 @@ executable foliage
|
||||
Foliage.CmdImportIndex
|
||||
Foliage.HackageSecurity
|
||||
Foliage.Meta
|
||||
Foliage.Meta.Aeson
|
||||
Foliage.Options
|
||||
Foliage.PrepareSource
|
||||
Foliage.PrepareSdist
|
||||
@ -28,6 +29,7 @@ executable foliage
|
||||
Foliage.Shake
|
||||
Foliage.Time
|
||||
Foliage.UpdateCabalFile
|
||||
Foliage.Utils.Aeson
|
||||
Network.URI.Orphans
|
||||
|
||||
default-language: Haskell2010
|
||||
|
Loading…
Reference in New Issue
Block a user