Skip to content

A peer-to-peer stack for code collaboration

Add Serialisable instance for more types.

Julian Arni12707c836639
diff --git a/src/Radicle.hs b/src/Radicle.hs
index fd7f685..c9f2627 100644
--- a/src/Radicle.hs
+++ b/src/Radicle.hs
@@ -35,6 +35,8 @@ module Radicle
, pattern Ref
, pattern Lambda
, maybeJson
+ , UntaggedValue
+ , untag
-- *** LangError
, LangError(..)
, LangErrorData(..)
diff --git a/src/Radicle/Internal.hs b/src/Radicle/Internal.hs
index aa3ae62..bb3df9b 100644
--- a/src/Radicle/Internal.hs
+++ b/src/Radicle/Internal.hs
@@ -9,6 +9,7 @@ import Prelude (error)
import Protolude
import Language.Haskell.TH.Quote
+import Radicle.Internal.Annotation as X
import Radicle.Internal.CLI as X
import Radicle.Internal.Core as X
import Radicle.Internal.Effects as X
diff --git a/src/Radicle/Internal/Annotation.hs b/src/Radicle/Internal/Annotation.hs
index 38a73eb..03ae057 100644
--- a/src/Radicle/Internal/Annotation.hs
+++ b/src/Radicle/Internal/Annotation.hs
@@ -59,6 +59,8 @@ thisPos = InternalPos (pack (prettyCallStack callStack))
data WithPos a = WithPos SrcPos a
deriving (Read, Show, Generic, Functor, Foldable, Traversable)
+instance Serialise a => Serialise (WithPos a)
+
-- Ignore source locations when comparing.
instance Eq a => Eq (WithPos a) where
WithPos _ x == WithPos _ y = x == y
diff --git a/src/Radicle/Internal/Core.hs b/src/Radicle/Internal/Core.hs
index 3bd0316..0facf11 100644
--- a/src/Radicle/Internal/Core.hs
+++ b/src/Radicle/Internal/Core.hs
@@ -142,6 +142,8 @@ data ValueF r =
| LambdaF [Ident] (NonEmpty r) (Env r)
deriving (Eq, Ord, Read, Show, Generic, Functor)
+instance Serialise r => Serialise (ValueF r)
+
{-# COMPLETE Atom, Keyword, String, Number, Boolean, List, Vec, PrimFn, Dict, Ref, Lambda #-}
type ValueConC t = (HasCallStack, Ann.Annotation t, Copointed t)
diff --git a/test/spec/Radicle/Internal/Arbitrary.hs b/test/spec/Radicle/Internal/Arbitrary.hs
index 675f151..9bea0f6 100644
--- a/test/spec/Radicle/Internal/Arbitrary.hs
+++ b/test/spec/Radicle/Internal/Arbitrary.hs
@@ -45,6 +45,9 @@ instance Arbitrary Value where
isPrimop x = x `elem` Map.keys (getPrimFns prims)
isNum x = isJust (readMaybe (toS $ fromIdent x) :: Maybe Scientific)
+instance Arbitrary UntaggedValue where
+ arbitrary = untag <$> (arbitrary :: Gen Value)
+
instance Arbitrary Ident where
arbitrary = ((:) <$> firstL <*> rest) `suchThatMap` (mkIdent . toS)
where
diff --git a/test/spec/Radicle/Tests.hs b/test/spec/Radicle/Tests.hs
index 86db288..f0ee195 100644
--- a/test/spec/Radicle/Tests.hs
+++ b/test/spec/Radicle/Tests.hs
@@ -3,6 +3,7 @@ module Radicle.Tests where
import Protolude hiding (toList)
+import Codec.Serialise (Serialise, deserialise, serialise)
import Data.List (isInfixOf, isSuffixOf)
import qualified Data.Map.Strict as Map
import Data.Maybe (fromJust)
@@ -685,6 +686,26 @@ test_from_to_radicle =
<> "\nGot\n\t" <> show got
counterexample info $ got == expected
+test_cbor :: [TestTree]
+test_cbor =
+ [ testGroup "UntaggedValue"
+ [ testForType (Proxy :: Proxy UntaggedValue) ]
+ , testGroup "Value"
+ [ testForType (Proxy :: Proxy Value) ]
+ ]
+ where
+ testForType
+ :: forall a. (Arbitrary a, Show a, Serialise a, Eq a)
+ => Proxy a -> TestTree
+ testForType _ =
+ testProperty "deserialise . serialise == id" $ \(v :: a) -> do
+ let expected = v
+ got = deserialise (serialise v)
+ info = "Expected\n\t" <> show expected
+ <> "\nGot\n\t" <> show got
+ counterexample info $ got == expected
+
+
-- Tests all radicle files 'repl' dir. These should use the 'should-be'
-- function to ensure they are in the proper format.
test_source_files :: IO TestTree