{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE TypeFamilies #-}
module Cardano.Api.Governance.Metadata.GovAction
(
CIP108 (..)
)
where
import Cardano.Api.Governance.Metadata.Parsers (textWithMaxLength)
import Cardano.Api.Governance.Metadata.Validation (Authors, Body, GovActionMetadata (..),
HashAlgorithm)
import Data.Aeson (FromJSON, withArray, withObject, withText, (.:), (.:?))
import qualified Data.Aeson as Aeson
import Data.Aeson.Types (Parser, Value (..))
import Data.Text (Text)
import GHC.Generics (Generic)
data CIP108 = BaseGovActionMetadata
instance FromJSON (GovActionMetadata CIP108) where
parseJSON :: Value -> Parser (GovActionMetadata CIP108)
parseJSON :: Value -> Parser (GovActionMetadata CIP108)
parseJSON = String
-> (Object -> Parser (GovActionMetadata CIP108))
-> Value
-> Parser (GovActionMetadata CIP108)
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"CIP108Common" ((Object -> Parser (GovActionMetadata CIP108))
-> Value -> Parser (GovActionMetadata CIP108))
-> (Object -> Parser (GovActionMetadata CIP108))
-> Value
-> Parser (GovActionMetadata CIP108)
forall a b. (a -> b) -> a -> b
$ \Object
v ->
HashAlgorithm CIP108
-> Authors CIP108 -> Body CIP108 -> GovActionMetadata CIP108
forall cip.
HashAlgorithm cip
-> Authors cip -> Body cip -> GovActionMetadata cip
GovActionMetadata
(HashAlgorithm CIP108
-> Authors CIP108 -> Body CIP108 -> GovActionMetadata CIP108)
-> Parser (HashAlgorithm CIP108)
-> Parser
(Authors CIP108 -> Body CIP108 -> GovActionMetadata CIP108)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
v Object -> Key -> Parser (HashAlgorithm CIP108)
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"hashAlgorithm"
Parser (Authors CIP108 -> Body CIP108 -> GovActionMetadata CIP108)
-> Parser (Authors CIP108)
-> Parser (Body CIP108 -> GovActionMetadata CIP108)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v Object -> Key -> Parser (Authors CIP108)
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"authors"
Parser (Body CIP108 -> GovActionMetadata CIP108)
-> Parser (Body CIP108) -> Parser (GovActionMetadata CIP108)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v Object -> Key -> Parser (Body CIP108)
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"body"
data instance HashAlgorithm CIP108 = Blake2b256
deriving (Int -> HashAlgorithm CIP108 -> ShowS
[HashAlgorithm CIP108] -> ShowS
HashAlgorithm CIP108 -> String
(Int -> HashAlgorithm CIP108 -> ShowS)
-> (HashAlgorithm CIP108 -> String)
-> ([HashAlgorithm CIP108] -> ShowS)
-> Show (HashAlgorithm CIP108)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> HashAlgorithm CIP108 -> ShowS
showsPrec :: Int -> HashAlgorithm CIP108 -> ShowS
$cshow :: HashAlgorithm CIP108 -> String
show :: HashAlgorithm CIP108 -> String
$cshowList :: [HashAlgorithm CIP108] -> ShowS
showList :: [HashAlgorithm CIP108] -> ShowS
Show, (forall x. HashAlgorithm CIP108 -> Rep (HashAlgorithm CIP108) x)
-> (forall x. Rep (HashAlgorithm CIP108) x -> HashAlgorithm CIP108)
-> Generic (HashAlgorithm CIP108)
forall x. Rep (HashAlgorithm CIP108) x -> HashAlgorithm CIP108
forall x. HashAlgorithm CIP108 -> Rep (HashAlgorithm CIP108) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. HashAlgorithm CIP108 -> Rep (HashAlgorithm CIP108) x
from :: forall x. HashAlgorithm CIP108 -> Rep (HashAlgorithm CIP108) x
$cto :: forall x. Rep (HashAlgorithm CIP108) x -> HashAlgorithm CIP108
to :: forall x. Rep (HashAlgorithm CIP108) x -> HashAlgorithm CIP108
Generic)
instance FromJSON (HashAlgorithm CIP108) where
parseJSON :: Value -> Parser (HashAlgorithm CIP108)
parseJSON :: Value -> Parser (HashAlgorithm CIP108)
parseJSON = String
-> (Text -> Parser (HashAlgorithm CIP108))
-> Value
-> Parser (HashAlgorithm CIP108)
forall a. String -> (Text -> Parser a) -> Value -> Parser a
withText String
"HashAlgorithm" ((Text -> Parser (HashAlgorithm CIP108))
-> Value -> Parser (HashAlgorithm CIP108))
-> (Text -> Parser (HashAlgorithm CIP108))
-> Value
-> Parser (HashAlgorithm CIP108)
forall a b. (a -> b) -> a -> b
$
\case
Text
"blake2b-256" -> HashAlgorithm CIP108 -> Parser (HashAlgorithm CIP108)
forall a. a -> Parser a
forall (m :: * -> *) a. Monad m => a -> m a
return HashAlgorithm CIP108
Blake2b256
Text
_ -> String -> Parser (HashAlgorithm CIP108)
forall a. String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Invalid hashAlgorithm value, must be: blake2b-256"
newtype instance Authors CIP108 = Authors [Author]
deriving (Int -> Authors CIP108 -> ShowS
[Authors CIP108] -> ShowS
Authors CIP108 -> String
(Int -> Authors CIP108 -> ShowS)
-> (Authors CIP108 -> String)
-> ([Authors CIP108] -> ShowS)
-> Show (Authors CIP108)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Authors CIP108 -> ShowS
showsPrec :: Int -> Authors CIP108 -> ShowS
$cshow :: Authors CIP108 -> String
show :: Authors CIP108 -> String
$cshowList :: [Authors CIP108] -> ShowS
showList :: [Authors CIP108] -> ShowS
Show, (forall x. Authors CIP108 -> Rep (Authors CIP108) x)
-> (forall x. Rep (Authors CIP108) x -> Authors CIP108)
-> Generic (Authors CIP108)
forall x. Rep (Authors CIP108) x -> Authors CIP108
forall x. Authors CIP108 -> Rep (Authors CIP108) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. Authors CIP108 -> Rep (Authors CIP108) x
from :: forall x. Authors CIP108 -> Rep (Authors CIP108) x
$cto :: forall x. Rep (Authors CIP108) x -> Authors CIP108
to :: forall x. Rep (Authors CIP108) x -> Authors CIP108
Generic)
instance FromJSON (Authors CIP108) where
parseJSON :: Value -> Parser (Authors CIP108)
parseJSON :: Value -> Parser (Authors CIP108)
parseJSON = String
-> (Array -> Parser (Authors CIP108))
-> Value
-> Parser (Authors CIP108)
forall a. String -> (Array -> Parser a) -> Value -> Parser a
withArray String
"Authors" ((Array -> Parser (Authors CIP108))
-> Value -> Parser (Authors CIP108))
-> (Array -> Parser (Authors CIP108))
-> Value
-> Parser (Authors CIP108)
forall a b. (a -> b) -> a -> b
$ \Array
arr ->
[Author] -> Authors CIP108
Authors ([Author] -> Authors CIP108)
-> Parser [Author] -> Parser (Authors CIP108)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Value -> Parser [Author]
forall a. FromJSON a => Value -> Parser a
Aeson.parseJSON (Array -> Value
Array Array
arr)
data Author = Author
{ Author -> Maybe Text
name :: Maybe Text
, Author -> Witness
witness :: Witness
}
deriving (Int -> Author -> ShowS
[Author] -> ShowS
Author -> String
(Int -> Author -> ShowS)
-> (Author -> String) -> ([Author] -> ShowS) -> Show Author
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Author -> ShowS
showsPrec :: Int -> Author -> ShowS
$cshow :: Author -> String
show :: Author -> String
$cshowList :: [Author] -> ShowS
showList :: [Author] -> ShowS
Show, (forall x. Author -> Rep Author x)
-> (forall x. Rep Author x -> Author) -> Generic Author
forall x. Rep Author x -> Author
forall x. Author -> Rep Author x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. Author -> Rep Author x
from :: forall x. Author -> Rep Author x
$cto :: forall x. Rep Author x -> Author
to :: forall x. Rep Author x -> Author
Generic)
instance FromJSON Author where
parseJSON :: Value -> Parser Author
parseJSON :: Value -> Parser Author
parseJSON = String -> (Object -> Parser Author) -> Value -> Parser Author
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"Author" ((Object -> Parser Author) -> Value -> Parser Author)
-> (Object -> Parser Author) -> Value -> Parser Author
forall a b. (a -> b) -> a -> b
$ \Object
v ->
Maybe Text -> Witness -> Author
Author
(Maybe Text -> Witness -> Author)
-> Parser (Maybe Text) -> Parser (Witness -> Author)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
v Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"name"
Parser (Witness -> Author) -> Parser Witness -> Parser Author
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v Object -> Key -> Parser Witness
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"witness"
data Witness = Witness
{ Witness -> Maybe WitnessAlgorithm
witnessAlgorithm :: Maybe WitnessAlgorithm
, Witness -> Maybe Text
publicKey :: Maybe Text
, Witness -> Maybe Text
signature :: Maybe Text
}
deriving (Int -> Witness -> ShowS
[Witness] -> ShowS
Witness -> String
(Int -> Witness -> ShowS)
-> (Witness -> String) -> ([Witness] -> ShowS) -> Show Witness
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Witness -> ShowS
showsPrec :: Int -> Witness -> ShowS
$cshow :: Witness -> String
show :: Witness -> String
$cshowList :: [Witness] -> ShowS
showList :: [Witness] -> ShowS
Show, (forall x. Witness -> Rep Witness x)
-> (forall x. Rep Witness x -> Witness) -> Generic Witness
forall x. Rep Witness x -> Witness
forall x. Witness -> Rep Witness x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. Witness -> Rep Witness x
from :: forall x. Witness -> Rep Witness x
$cto :: forall x. Rep Witness x -> Witness
to :: forall x. Rep Witness x -> Witness
Generic)
instance FromJSON Witness where
parseJSON :: Value -> Parser Witness
parseJSON :: Value -> Parser Witness
parseJSON = String -> (Object -> Parser Witness) -> Value -> Parser Witness
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"Witness" ((Object -> Parser Witness) -> Value -> Parser Witness)
-> (Object -> Parser Witness) -> Value -> Parser Witness
forall a b. (a -> b) -> a -> b
$ \Object
v ->
Maybe WitnessAlgorithm -> Maybe Text -> Maybe Text -> Witness
Witness
(Maybe WitnessAlgorithm -> Maybe Text -> Maybe Text -> Witness)
-> Parser (Maybe WitnessAlgorithm)
-> Parser (Maybe Text -> Maybe Text -> Witness)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
v Object -> Key -> Parser (Maybe WitnessAlgorithm)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"witnessAlgorithm"
Parser (Maybe Text -> Maybe Text -> Witness)
-> Parser (Maybe Text) -> Parser (Maybe Text -> Witness)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"publicKey"
Parser (Maybe Text -> Witness)
-> Parser (Maybe Text) -> Parser Witness
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"signature"
data WitnessAlgorithm = Ed25519 | CIP0008
deriving (Int -> WitnessAlgorithm -> ShowS
[WitnessAlgorithm] -> ShowS
WitnessAlgorithm -> String
(Int -> WitnessAlgorithm -> ShowS)
-> (WitnessAlgorithm -> String)
-> ([WitnessAlgorithm] -> ShowS)
-> Show WitnessAlgorithm
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> WitnessAlgorithm -> ShowS
showsPrec :: Int -> WitnessAlgorithm -> ShowS
$cshow :: WitnessAlgorithm -> String
show :: WitnessAlgorithm -> String
$cshowList :: [WitnessAlgorithm] -> ShowS
showList :: [WitnessAlgorithm] -> ShowS
Show, (forall x. WitnessAlgorithm -> Rep WitnessAlgorithm x)
-> (forall x. Rep WitnessAlgorithm x -> WitnessAlgorithm)
-> Generic WitnessAlgorithm
forall x. Rep WitnessAlgorithm x -> WitnessAlgorithm
forall x. WitnessAlgorithm -> Rep WitnessAlgorithm x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. WitnessAlgorithm -> Rep WitnessAlgorithm x
from :: forall x. WitnessAlgorithm -> Rep WitnessAlgorithm x
$cto :: forall x. Rep WitnessAlgorithm x -> WitnessAlgorithm
to :: forall x. Rep WitnessAlgorithm x -> WitnessAlgorithm
Generic)
instance FromJSON WitnessAlgorithm where
parseJSON :: Value -> Parser WitnessAlgorithm
parseJSON :: Value -> Parser WitnessAlgorithm
parseJSON = String
-> (Text -> Parser WitnessAlgorithm)
-> Value
-> Parser WitnessAlgorithm
forall a. String -> (Text -> Parser a) -> Value -> Parser a
withText String
"WitnessAlgorithm" ((Text -> Parser WitnessAlgorithm)
-> Value -> Parser WitnessAlgorithm)
-> (Text -> Parser WitnessAlgorithm)
-> Value
-> Parser WitnessAlgorithm
forall a b. (a -> b) -> a -> b
$
\case
Text
"ed25519" -> WitnessAlgorithm -> Parser WitnessAlgorithm
forall a. a -> Parser a
forall (m :: * -> *) a. Monad m => a -> m a
return WitnessAlgorithm
Ed25519
Text
"CIP-0008" -> WitnessAlgorithm -> Parser WitnessAlgorithm
forall a. a -> Parser a
forall (m :: * -> *) a. Monad m => a -> m a
return WitnessAlgorithm
CIP0008
Text
_ -> String -> Parser WitnessAlgorithm
forall a. String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Invalid witnessAlgorithm value, must be: ed25519 or CIP-0008"
data instance Body CIP108 = Body
{ Body CIP108 -> Text
title :: Text
, Body CIP108 -> Text
abstract :: Text
, Body CIP108 -> Text
motivation :: Text
, Body CIP108 -> Text
rationale :: Text
, Body CIP108 -> Maybe [Reference]
references :: Maybe [Reference]
}
deriving (Int -> Body CIP108 -> ShowS
[Body CIP108] -> ShowS
Body CIP108 -> String
(Int -> Body CIP108 -> ShowS)
-> (Body CIP108 -> String)
-> ([Body CIP108] -> ShowS)
-> Show (Body CIP108)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Body CIP108 -> ShowS
showsPrec :: Int -> Body CIP108 -> ShowS
$cshow :: Body CIP108 -> String
show :: Body CIP108 -> String
$cshowList :: [Body CIP108] -> ShowS
showList :: [Body CIP108] -> ShowS
Show, (forall x. Body CIP108 -> Rep (Body CIP108) x)
-> (forall x. Rep (Body CIP108) x -> Body CIP108)
-> Generic (Body CIP108)
forall x. Rep (Body CIP108) x -> Body CIP108
forall x. Body CIP108 -> Rep (Body CIP108) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. Body CIP108 -> Rep (Body CIP108) x
from :: forall x. Body CIP108 -> Rep (Body CIP108) x
$cto :: forall x. Rep (Body CIP108) x -> Body CIP108
to :: forall x. Rep (Body CIP108) x -> Body CIP108
Generic)
instance FromJSON (Body CIP108) where
parseJSON :: Value -> Parser (Body CIP108)
parseJSON :: Value -> Parser (Body CIP108)
parseJSON = String
-> (Object -> Parser (Body CIP108))
-> Value
-> Parser (Body CIP108)
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"Body" ((Object -> Parser (Body CIP108)) -> Value -> Parser (Body CIP108))
-> (Object -> Parser (Body CIP108))
-> Value
-> Parser (Body CIP108)
forall a b. (a -> b) -> a -> b
$ \Object
v ->
Text -> Text -> Text -> Text -> Maybe [Reference] -> Body CIP108
Body
(Text -> Text -> Text -> Text -> Maybe [Reference] -> Body CIP108)
-> Parser Text
-> Parser
(Text -> Text -> Text -> Maybe [Reference] -> Body CIP108)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Object
v Object -> Key -> Parser Value
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"title" Parser Value -> (Value -> Parser Text) -> Parser Text
forall a b. Parser a -> (a -> Parser b) -> Parser b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= String -> Int -> Value -> Parser Text
textWithMaxLength String
"title" Int
80)
Parser (Text -> Text -> Text -> Maybe [Reference] -> Body CIP108)
-> Parser Text
-> Parser (Text -> Text -> Maybe [Reference] -> Body CIP108)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Object
v Object -> Key -> Parser Value
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"abstract" Parser Value -> (Value -> Parser Text) -> Parser Text
forall a b. Parser a -> (a -> Parser b) -> Parser b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= String -> Int -> Value -> Parser Text
textWithMaxLength String
"abstract" Int
2500)
Parser (Text -> Text -> Maybe [Reference] -> Body CIP108)
-> Parser Text -> Parser (Text -> Maybe [Reference] -> Body CIP108)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v Object -> Key -> Parser Text
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"motivation"
Parser (Text -> Maybe [Reference] -> Body CIP108)
-> Parser Text -> Parser (Maybe [Reference] -> Body CIP108)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v Object -> Key -> Parser Text
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"rationale"
Parser (Maybe [Reference] -> Body CIP108)
-> Parser (Maybe [Reference]) -> Parser (Body CIP108)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v Object -> Key -> Parser (Maybe [Reference])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"references"
data Reference = Reference
{ Reference -> ReferenceType
refType :: ReferenceType
, Reference -> Text
label :: Text
, Reference -> Text
uri :: Text
, Reference -> Maybe ReferenceHash
referenceHash :: Maybe ReferenceHash
}
deriving (Int -> Reference -> ShowS
[Reference] -> ShowS
Reference -> String
(Int -> Reference -> ShowS)
-> (Reference -> String)
-> ([Reference] -> ShowS)
-> Show Reference
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Reference -> ShowS
showsPrec :: Int -> Reference -> ShowS
$cshow :: Reference -> String
show :: Reference -> String
$cshowList :: [Reference] -> ShowS
showList :: [Reference] -> ShowS
Show, (forall x. Reference -> Rep Reference x)
-> (forall x. Rep Reference x -> Reference) -> Generic Reference
forall x. Rep Reference x -> Reference
forall x. Reference -> Rep Reference x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. Reference -> Rep Reference x
from :: forall x. Reference -> Rep Reference x
$cto :: forall x. Rep Reference x -> Reference
to :: forall x. Rep Reference x -> Reference
Generic)
instance FromJSON Reference where
parseJSON :: Value -> Parser Reference
parseJSON :: Value -> Parser Reference
parseJSON = String -> (Object -> Parser Reference) -> Value -> Parser Reference
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"Reference" ((Object -> Parser Reference) -> Value -> Parser Reference)
-> (Object -> Parser Reference) -> Value -> Parser Reference
forall a b. (a -> b) -> a -> b
$ \Object
v ->
ReferenceType -> Text -> Text -> Maybe ReferenceHash -> Reference
Reference
(ReferenceType -> Text -> Text -> Maybe ReferenceHash -> Reference)
-> Parser ReferenceType
-> Parser (Text -> Text -> Maybe ReferenceHash -> Reference)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
v Object -> Key -> Parser ReferenceType
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"@type"
Parser (Text -> Text -> Maybe ReferenceHash -> Reference)
-> Parser Text -> Parser (Text -> Maybe ReferenceHash -> Reference)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v Object -> Key -> Parser Text
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"label"
Parser (Text -> Maybe ReferenceHash -> Reference)
-> Parser Text -> Parser (Maybe ReferenceHash -> Reference)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v Object -> Key -> Parser Text
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"uri"
Parser (Maybe ReferenceHash -> Reference)
-> Parser (Maybe ReferenceHash) -> Parser Reference
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v Object -> Key -> Parser (Maybe ReferenceHash)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"referenceHash"
data ReferenceType = GovernanceMetadata | Other
deriving (Int -> ReferenceType -> ShowS
[ReferenceType] -> ShowS
ReferenceType -> String
(Int -> ReferenceType -> ShowS)
-> (ReferenceType -> String)
-> ([ReferenceType] -> ShowS)
-> Show ReferenceType
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ReferenceType -> ShowS
showsPrec :: Int -> ReferenceType -> ShowS
$cshow :: ReferenceType -> String
show :: ReferenceType -> String
$cshowList :: [ReferenceType] -> ShowS
showList :: [ReferenceType] -> ShowS
Show, (forall x. ReferenceType -> Rep ReferenceType x)
-> (forall x. Rep ReferenceType x -> ReferenceType)
-> Generic ReferenceType
forall x. Rep ReferenceType x -> ReferenceType
forall x. ReferenceType -> Rep ReferenceType x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. ReferenceType -> Rep ReferenceType x
from :: forall x. ReferenceType -> Rep ReferenceType x
$cto :: forall x. Rep ReferenceType x -> ReferenceType
to :: forall x. Rep ReferenceType x -> ReferenceType
Generic)
instance FromJSON ReferenceType where
parseJSON :: Value -> Parser ReferenceType
parseJSON :: Value -> Parser ReferenceType
parseJSON = String
-> (Text -> Parser ReferenceType) -> Value -> Parser ReferenceType
forall a. String -> (Text -> Parser a) -> Value -> Parser a
withText String
"ReferenceType" ((Text -> Parser ReferenceType) -> Value -> Parser ReferenceType)
-> (Text -> Parser ReferenceType) -> Value -> Parser ReferenceType
forall a b. (a -> b) -> a -> b
$
\case
Text
"GovernanceMetadata" -> ReferenceType -> Parser ReferenceType
forall a. a -> Parser a
forall (m :: * -> *) a. Monad m => a -> m a
return ReferenceType
GovernanceMetadata
Text
"Other" -> ReferenceType -> Parser ReferenceType
forall a. a -> Parser a
forall (m :: * -> *) a. Monad m => a -> m a
return ReferenceType
Other
Text
_ -> String -> Parser ReferenceType
forall a. String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Invalid reference type, must be one of: GovernanceMetadata, Other"
data ReferenceHash = ReferenceHash
{ ReferenceHash -> Text
referenceHashDigest :: Text
, ReferenceHash -> HashAlgorithm CIP108
referenceHashAlgorithm :: HashAlgorithm CIP108
}
deriving (Int -> ReferenceHash -> ShowS
[ReferenceHash] -> ShowS
ReferenceHash -> String
(Int -> ReferenceHash -> ShowS)
-> (ReferenceHash -> String)
-> ([ReferenceHash] -> ShowS)
-> Show ReferenceHash
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ReferenceHash -> ShowS
showsPrec :: Int -> ReferenceHash -> ShowS
$cshow :: ReferenceHash -> String
show :: ReferenceHash -> String
$cshowList :: [ReferenceHash] -> ShowS
showList :: [ReferenceHash] -> ShowS
Show, (forall x. ReferenceHash -> Rep ReferenceHash x)
-> (forall x. Rep ReferenceHash x -> ReferenceHash)
-> Generic ReferenceHash
forall x. Rep ReferenceHash x -> ReferenceHash
forall x. ReferenceHash -> Rep ReferenceHash x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. ReferenceHash -> Rep ReferenceHash x
from :: forall x. ReferenceHash -> Rep ReferenceHash x
$cto :: forall x. Rep ReferenceHash x -> ReferenceHash
to :: forall x. Rep ReferenceHash x -> ReferenceHash
Generic)
instance FromJSON ReferenceHash where
parseJSON :: Value -> Parser ReferenceHash
parseJSON :: Value -> Parser ReferenceHash
parseJSON = String
-> (Object -> Parser ReferenceHash)
-> Value
-> Parser ReferenceHash
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"ReferenceHash" ((Object -> Parser ReferenceHash) -> Value -> Parser ReferenceHash)
-> (Object -> Parser ReferenceHash)
-> Value
-> Parser ReferenceHash
forall a b. (a -> b) -> a -> b
$ \Object
v ->
Text -> HashAlgorithm CIP108 -> ReferenceHash
ReferenceHash
(Text -> HashAlgorithm CIP108 -> ReferenceHash)
-> Parser Text -> Parser (HashAlgorithm CIP108 -> ReferenceHash)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
v Object -> Key -> Parser Text
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"hashDigest"
Parser (HashAlgorithm CIP108 -> ReferenceHash)
-> Parser (HashAlgorithm CIP108) -> Parser ReferenceHash
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
v Object -> Key -> Parser (HashAlgorithm CIP108)
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"hashAlgorithm"