From a288587a18cffcd210376f66e3349e743da51e38 Mon Sep 17 00:00:00 2001 From: rsoeldner Date: Thu, 16 Nov 2023 11:17:37 +0100 Subject: [PATCH] wip --- pact-core/Pact/Core/Guards.hs | 12 ++--- pact-core/Pact/Core/Serialise/LegacyPact.hs | 50 ++++++++++++++++++--- 2 files changed, 49 insertions(+), 13 deletions(-) diff --git a/pact-core/Pact/Core/Guards.hs b/pact-core/Pact/Core/Guards.hs index 1a155fa8a..5b8ae8560 100644 --- a/pact-core/Pact/Core/Guards.hs +++ b/pact-core/Pact/Core/Guards.hs @@ -155,12 +155,12 @@ instance (Pretty name, Pretty term) => Pretty (Guard name term) where GModuleGuard g -> pretty g -data Namespace name term - = Namespace - { _nsName :: !NamespaceName - , _nsUser :: !(Guard name term) - , _nsAdmin :: !(Guard name term) - } deriving (Eq, Show) +-- data Namespace name term +-- = Namespace +-- { _nsName :: !NamespaceName +-- , _nsUser :: !(Guard name term) +-- , _nsAdmin :: !(Guard name term) +-- } deriving (Eq, Show) instance (Pretty name, Pretty term) => Pretty (CapabilityGuard name term) where pretty (CapabilityGuard cg args pid) = "CapabilityGuard" <+> commaBraces diff --git a/pact-core/Pact/Core/Serialise/LegacyPact.hs b/pact-core/Pact/Core/Serialise/LegacyPact.hs index d76c22288..b506d7d49 100644 --- a/pact-core/Pact/Core/Serialise/LegacyPact.hs +++ b/pact-core/Pact/Core/Serialise/LegacyPact.hs @@ -1,5 +1,5 @@ -- | - +{-# OPTIONS_GHC -fno-warn-orphans #-} module Pact.Core.Serialise.LegacyPact ( decodeModuleData , decodeKeySet @@ -16,21 +16,57 @@ import Pact.Core.DefPacts.Types import Pact.Core.Namespace import Pact.Core.PactValue import Data.ByteString (ByteString) +import Control.Applicative ((<|>)) +import Data.Maybe (fromMaybe) import qualified Pact.JSON.Decode as JD decodeModuleData :: ByteString -> Maybe (ModuleData RawBuiltin ()) -decodeModuleData = undefined - +decodeModuleData = JD.decodeStrict' decodeKeySet :: ByteString -> Maybe (KeySet FullyQualifiedName) -decodeKeySet = undefined +decodeKeySet = JD.decodeStrict' decodeDefPactExec :: ByteString -> Maybe (Maybe DefPactExec) -decodeDefPactExec = undefined +decodeDefPactExec = JD.decodeStrict' decodeNamespace :: ByteString -> Maybe Namespace -decodeNamespace = undefined +decodeNamespace = JD.decodeStrict' decodeRowData :: ByteString -> Maybe RowData -decodeRowData = undefined +decodeRowData = JD.decodeStrict' + +instance JD.FromJSON NamespaceName where + parseJSON = JD.withText "NamespaceName" (pure . NamespaceName) + + +instance JD.FromJSON (KeySet FullyQualifiedName) where + parseJSON v = JD.withObject "KeySet" keyListPred v <|> keyListOnly + where + defPred = KeysAll + + keyListPred o = KeySet + <$> o JD..: "keys" + <*> (fromMaybe defPred <$> o JD..:? "pred") + + keyListOnly = KeySet <$> JD.parseJSON v <*> pure defPred + +instance JD.FromJSON (KSPredicate FullyQualifiedName) where + parseJSON = JD.withText "KSPredicate" $ \case + "keys-all" -> pure KeysAll + "keys2" -> pure Keys2 + "KeysAny" -> pure KeysAny + _ -> fail "unexpected parsing" + +instance JD.FromJSON PublicKeyText where + parseJSON = JD.withText "PublicKeyText" (pure . PublicKeyText) + + +instance JD.FromJSON Namespace where + parseJSON = JD.withObject "Namespace" $ \v -> Namespace + <$> v JD..: "name" + <*> v JD..: "user" + <*> v JD..: "admin" + +instance JD.FromJSON (Guard FullyQualifiedName PactValue) where + parseJSON = undefined