A peer-to-peer stack for code collaboration
Add abstract interface for storage backend
Thomas Scholtesbc71f032fc90
diff --git a/.weeder.yaml b/.weeder.yamlindex 299b19a..a13393d 100644--- a/.weeder.yaml+++ b/.weeder.yaml@@ -34,8 +34,8 @@- name: Radicle.Internal.TestCapabilities- identifier:- TestLang- - clientPrimFns- runTestWith'+ - storagePrimFns- module:- name: Radicle.Tests- identifier:diff --git a/src/Radicle/Internal/HttpStorage.hs b/src/Radicle/Internal/HttpStorage.hsindex 3c0b218..f29e3f0 100644--- a/src/Radicle/Internal/HttpStorage.hs+++ b/src/Radicle/Internal/HttpStorage.hs@@ -21,8 +21,7 @@ import Servant.Clientimport Radicleimport Radicle.Internal.Annotation (thisPos)-import qualified Radicle.Internal.Number as Num-import qualified Radicle.Internal.PrimFns as PrimFns+import Radicle.Internal.Storage--@@ -87,40 +86,27 @@ createHttpStoragePrimFns = dopure $ httpStoragePrimFns' httpMgrhttpStoragePrimFns' :: MonadIO m => HttpClient.Manager -> PrimFns m-httpStoragePrimFns' mgr = fromList . PrimFns.allDocs $ [sendPrimop, receivePrimop]- where- sendPrimop =- ( "send!"- , "Given a URL (string) and a value, sends the value `v` to the remote\- \ chain located at the URL for evaluation."- , PrimFns.twoArg "send!" $ \case- (String url, Vec v) -> do- res <- liftIO $ runClientM' url mgr (submit $ toList v)- case res of- Left e -> throwErrorHere . OtherError- $ "send!: failed:" <> show e- Right r -> pure r- (String _, v) -> throwErrorHere $ TypeError "send!" 1 TVec v- (v, _) -> throwErrorHere $ TypeError "send!" 0 TString v- )- receivePrimop =- ( "receive!"- , "Given a URL (string) and a integral number `n`, queries the remote chain\- \ for the last `n` inputs that have been evaluated."- , PrimFns.twoArg "receive!" $ \case- (String url, Number n) -> do- case Num.isInt n of- Left _ -> throwErrorHere . OtherError- $ "receive!: expecting int argument"- Right r -> do- res <- runClientM' url mgr (since r)- case res of- Left err -> throwErrorHere . OtherError- $ "receive!: request failed:" <> show err- Right v' -> pure v'- (String _, v) -> throwErrorHere $ TypeError "receive!" 1 TNumber v- (v, _) -> throwErrorHere $ TypeError "receive!" 0 TString v- )+httpStoragePrimFns' mgr =+ buildStoragePrimFns StorageBackend+ { storageSend =+ ( "send!"+ , "Given a URL (string) and a value, sends the value `v` to the remote\+ \ chain located at the URL for evaluation."+ , \url values -> do+ res <- liftIO $ runClientM' url mgr (submit $ toList values)+ pure $ case res of+ Left servantError -> Left $ show servantError+ Right _ -> Right ()+ )+ , storageReceive =+ ( "receive!"+ , "Given a URL (string) and a integral number `n`, queries the remote chain\+ \ for the last `n` inputs that have been evaluated."+ , \url index -> do+ res <- liftIO $ runClientM' url mgr (since index)+ pure $ first show res+ )+ }submit :: [Value] -> ClientM Valuesubmit = client chainSubmitEndpoint . Valuesdiff --git a/src/Radicle/Internal/Storage.hs b/src/Radicle/Internal/Storage.hsnew file mode 100644index 0000000..07f746c--- /dev/null+++ b/src/Radicle/Internal/Storage.hs@@ -0,0 +1,79 @@+-- | Defines and abstract interface 'StorageBackend' for Radicle+-- chains and 'buildStoragePrimFns' to add the storage backend as+-- primitive functions.+--+-- A storage backend defines two functions 'StorageSend' and+-- 'StorageReceive' explained below.+--+-- For usage examples see "Radicle.Internal.HttpStorage" and+-- "Radicle.Internal.TestCapabilities".+module Radicle.Internal.Storage+ ( StorageBackend(..)+ , buildStoragePrimFns+ ) where++import Protolude hiding (TypeError)++import GHC.Exts (fromList)++import Radicle.Internal.Core+import qualified Radicle.Internal.Number as Num+import qualified Radicle.Internal.PrimFns as PrimFns+import Radicle.Internal.Type++-- | Definitions for @send@ and @receive@ functions of a storage+-- backend.+--+-- The first tuple item is the Radicle identifier the function will be+-- exposed as. The second tuple item is documentation.+data StorageBackend m = StorageBackend+ { storageSend :: (Text, Text, StorageSend m)+ , storageReceive :: (Text, Text, StorageReceive m)+ }++-- | Send a list of expressions to a chain identified by the first+-- argument.+type StorageSend m = Text -> Seq Value -> m (Either Text ())++-- | Receive a list of expressions from a chain. The chain is identified by the first+-- argument. The second argument is the index from which to start.+-- Morally this is @'Data.List.drop' index chain@.+type StorageReceive m = Text -> Int -> m (Either Text Value)++buildStoragePrimFns :: Monad m => StorageBackend m -> PrimFns m+buildStoragePrimFns backend =+ fromList . PrimFns.allDocs $ [sendPrimop, receivePrimop]+ where+ (sendName, sendDoc, send) = storageSend backend+ sendPrimop =+ ( sendName+ , sendDoc+ , PrimFns.twoArg sendName $ \case+ (String id, Vec v) -> do+ res <- lift $ send id v+ case res of+ Left e -> throwErrorHere . OtherError+ $ sendName <> ": failed:" <> show e+ Right _ -> pure $ Keyword $ unsafeToIdent "ok"+ (String _, v) -> throwErrorHere $ TypeError sendName 1 TVec v+ (v, _) -> throwErrorHere $ TypeError sendName 0 TString v+ )++ (receiveName, receiveDoc, receive) = storageReceive backend+ receivePrimop =+ ( receiveName+ , receiveDoc+ , PrimFns.twoArg receiveName $ \case+ (String id, Number q) -> do+ case Num.isInt q of+ Left _ -> throwErrorHere . OtherError+ $ receiveName <> ": expecting int argument"+ Right n -> do+ res <- lift $ receive id n+ case res of+ Left err -> throwErrorHere . OtherError+ $ receiveName <> ": request failed: " <> show err+ Right v' -> pure v'+ (String _, v) -> throwErrorHere $ TypeError receiveName 1 TNumber v+ (v, _) -> throwErrorHere $ TypeError receiveName 0 TString v+ )diff --git a/test/spec/Radicle/Internal/TestCapabilities.hs b/test/spec/Radicle/Internal/TestCapabilities.hsindex e186c5f..0ef7289 100644--- a/test/spec/Radicle/Internal/TestCapabilities.hs+++ b/test/spec/Radicle/Internal/TestCapabilities.hs@@ -8,7 +8,6 @@ import qualified Crypto.Random as CryptoRandimport qualified Data.Map.Strict as Mapimport qualified Data.Sequence as Seqimport qualified Data.Time as Time-import GHC.Exts (fromList)import qualified System.FilePath.Find as FPimport System.IO.Unsafe (unsafePerformIO)@@ -16,9 +15,7 @@ import Radicleimport Radicle.Internal.Core (addBinding)import Radicle.Internal.Cryptoimport Radicle.Internal.Effects.Capabilities-import qualified Radicle.Internal.Number as Num-import Radicle.Internal.PrimFns (allDocs, twoArg)-import Radicle.Internal.Type (Type(..))+import Radicle.Internal.Storageimport qualified Radicle.Internal.UUID as UUIDimport Paths_radicle@@ -97,40 +94,34 @@ sourceFiles = dotestBindings :: Bindings (PrimFns (StateT WorldState IO))testBindings= addBinding (unsafeToIdent "test-env__") Nothing (Boolean True)- $ addPrimFns clientPrimFns replBindings---- | Mocked versions of 'send!' and 'receive!'-clientPrimFns :: PrimFns (StateT WorldState IO)-clientPrimFns = fromList . allDocs $ [sendPrimop, receivePrimop]- where- sendPrimop =- ( "send!"- , "Mocked version of `send!` that stores sent values in a map with chains as keys."- , twoArg "send!" $ \case- (String url, Vec v) -> do- lift . modify $ \s ->- s { worldStateRemoteChains- = Map.insertWith (flip (Seq.><)) url v $ worldStateRemoteChains s }- pure $ List []- (String _, v) -> throwErrorHere $ TypeError "send!" 1 TVec v- (v, _) -> throwErrorHere $ TypeError "send!" 0 TString v- )- receivePrimop =- ( "receive!"- , "Mocked version of `receive!` that retrieves values from a map with chains as keys."- , twoArg "receive!" $ \case- (String url, Number n) -> do- case Num.isInt n of- Left _ -> throwErrorHere . OtherError- $ "receive!: expecting int argument"- Right r -> do- chains <- lift $ gets worldStateRemoteChains- pure . List $ case Map.lookup url chains of- Nothing -> []- Just res -> toList $ Seq.drop r res- (String _, v) -> throwErrorHere $ TypeError "receive!" 1 TNumber v- (v, _) -> throwErrorHere $ TypeError "receive!" 0 TString v- )+ $ addPrimFns storagePrimFns replBindings+++-- | Provides @send!@ and @receive!@ functions that store chains in a+-- 'Map' in the 'WorldState'.+storagePrimFns :: PrimFns (StateT WorldState IO)+storagePrimFns =+ buildStoragePrimFns StorageBackend+ { storageSend =+ ( "send!"+ , "Mocked version of `send!` that stores sent values in a map with chains as keys."+ , \id values -> do+ modify $ \s ->+ s { worldStateRemoteChains+ = Map.insertWith (flip (Seq.><)) id values $ worldStateRemoteChains s }+ pure $ Right ()+ )+ , storageReceive =+ ( "receive!"+ , "Mocked version of `receive!` that retrieves values from a map with chains as keys."+ , \id index -> do+ chains <- gets worldStateRemoteChains+ let exprs = case Map.lookup id chains of+ Nothing -> []+ Just res -> toList $ Seq.drop index res+ pure $ Right $ List exprs+ )+ }instance {-# OVERLAPPING #-} Stdin TestLang wheregetLineS = do