A peer-to-peer stack for code collaboration
Json (#93)
James Haydon381069b30cd8
diff --git a/exe/Server/API.hs b/exe/Server/API.hsindex 7650171..824ca84 100644--- a/exe/Server/API.hs+++ b/exe/Server/API.hs@@ -6,6 +6,7 @@ import Radicleimport Servant.APIimport qualified Data.ByteString.Lazy as LBSimport qualified Data.Text as T+import qualified Data.Aeson as Ainstance Show a => MimeRender PlainText a wheremimeRender _ x = show x@@ -16,7 +17,7 @@ instance Read a => MimeUnrender PlainText a wheretype API= "submit" :> ReqBody '[PlainText] Value :> Post '[PlainText] ():<|> "since" :> Capture "chain" Text :> Capture "index" Int :> Get '[PlainText] [Value]- :<|> "outputs" :> Capture "chain" Text :> Get '[JSON, PlainText] [Maybe DataValue]+ :<|> "outputs" :> Capture "chain" Text :> Get '[JSON, PlainText] [Maybe A.Value]:<|> Rawapi :: Proxy APIdiff --git a/exe/Server/Server.hs b/exe/Server/Server.hsindex 31eb793..69e33db 100644--- a/exe/Server/Server.hs+++ b/exe/Server/Server.hs@@ -10,6 +10,7 @@ import Protolude hiding (fromStrict)import Radicleimport Servantimport qualified STMContainers.Map as STMMap+import qualified Data.Aeson as A-- * Main@@ -58,10 +59,10 @@ since :: Chains -> Text -> Int -> Handler [Value]since st name index =fmap fst <$> getActivitySince st name index-jsonOutputs :: Chains -> Text -> Handler [Maybe DataValue]+jsonOutputs :: Chains -> Text -> Handler [Maybe A.Value]jsonOutputs st name = dovs <- fmap snd <$> getActivitySince st name 0- pure (isData <$> vs)+ pure (maybeJson <$> vs)static :: Server Rawstatic = serveDirectoryFileServer "static/"diff --git a/src/Radicle.hs b/src/Radicle.hsindex d57a77a..25bed53 100644--- a/src/Radicle.hs+++ b/src/Radicle.hs@@ -13,8 +13,7 @@ module Radicle---- | The definition of the core language.Value(..)- , DataValue(..)- , isData+ , maybeJson, interpret, interpretMany, LangError(..)diff --git a/src/Radicle/Internal/Core.hs b/src/Radicle/Internal/Core.hsindex ae4089c..59c87f1 100644--- a/src/Radicle/Internal/Core.hs+++ b/src/Radicle/Internal/Core.hs@@ -119,63 +119,33 @@ isAtom :: Value -> Maybe IdentisAtom (Atom i) = pure iisAtom _ = Nothing--- | The data portion of values.-data DataValue =- DAtom Ident- | DKeyword Ident- | DString Text- | DNumber Scientific- | DBoolean Bool- | DList [DataValue]- | DDict (Map.Map DataValue DataValue)- deriving (Eq, Show, Ord, Read, Generic)--instance A.ToJSON Ident-instance A.FromJSON Ident-instance A.ToJSONKey DataValue-instance A.FromJSONKey DataValue--instance A.ToJSON DataValue where- toJSON = \case- DNumber n -> toJSON n- DList ls -> toJSON ls- DBoolean b -> toJSON b- DString s -> toJSON s- DDict m ->- let kvs = Map.toList m- in case traverse isString (fst <$> kvs) of- Just ss -> A.Object (HashMap.fromList (zip ss (toJSON . snd <$> kvs)))- Nothing -> toJSON m- x -> A.genericToJSON A.defaultOptions x- where- isString (DString s) = Just s- isString _ = Nothing--instance A.FromJSON DataValue where+instance A.FromJSON Value whereparseJSON = \case- A.Number n -> pure $ DNumber n- A.String s -> pure $ DString s- A.Array ls -> DList . toList <$> traverse parseJSON ls- A.Bool b -> pure $ DBoolean b- A.Null -> pure $ DKeyword (toIdent "null")- o@(A.Object hm) ->- A.genericParseJSON A.defaultOptions o- <|> do let kvs = HashMap.toList hm- vs <- traverse parseJSON (snd <$> kvs)- pure . DDict . Map.fromList $ zip (DString . fst <$> kvs) vs--isData :: Value -> Maybe DataValue-isData = \case- Atom i -> pure $ DAtom i- Keyword i -> pure $ DKeyword i- String s -> pure $ DString s- Number n -> pure $ DNumber n- Boolean b -> pure $ DBoolean b- List ls -> DList <$> traverse isData ls- Dict m -> DDict . Map.fromList <$> traverse kv (Map.toList m)+ A.Number n -> pure $ Number n+ A.String s -> pure $ String s+ A.Array ls -> List . toList <$> traverse parseJSON ls+ A.Bool b -> pure $ Boolean b+ A.Null -> pure $ Keyword (toIdent "null")+ A.Object hm -> do+ let kvs = HashMap.toList hm+ vs <- traverse parseJSON (snd <$> kvs)+ pure . Dict . Map.fromList $ zip (String . fst <$> kvs) vs++maybeJson :: Value -> Maybe A.Value+maybeJson = \case+ Number n -> pure $ A.Number n+ String s -> pure $ A.String s+ Boolean b -> pure $ A.Bool b+ List ls -> toJSON <$> traverse maybeJson ls+ Dict m -> do+ let kvs = Map.toList m+ ks <- traverse isStr (fst <$> kvs)+ vs <- traverse maybeJson (snd <$> kvs)+ pure $ A.Object (HashMap.fromList (zip ks vs))_ -> Nothingwhere- kv (k,v) = (,) <$> isData k <*> isData v+ isStr (String s) = pure s+ isStr _ = Nothing-- | An identifier in the language.--diff --git a/src/Radicle/Internal/Primops.hs b/src/Radicle/Internal/Primops.hsindex c629b79..bd3f47e 100644--- a/src/Radicle/Internal/Primops.hs+++ b/src/Radicle/Internal/Primops.hs@@ -16,6 +16,7 @@ import qualified Data.IntMap as IntMapimport qualified Data.Map as Mapimport Data.Scientific (Scientific, floatingOrInteger)import GHC.Exts (IsList(..))+import qualified Data.Aeson as Aesonimport Radicle.Internal.Coreimport Radicle.Internal.Parse@@ -245,6 +246,10 @@ purePrimops = fromList $ first Ident <$>Dict kvs -> pure $ List [List [k, v] | (k,v) <- Map.toList kvs ]_ -> throwError $ TypeError "seq: can only create a list from a list or a dict")+ , ( "to-json"+ , evalOneArg "to-json" $ \v -> String . toS . Aeson.encode <$>+ maybeJson v ?? "Could not serialise value to JSON"+ )]wherediff --git a/test/spec/Radicle/Tests.hs b/test/spec/Radicle/Tests.hsindex cb7f8cf..8acaa0e 100644--- a/test/spec/Radicle/Tests.hs+++ b/test/spec/Radicle/Tests.hs@@ -401,6 +401,11 @@ test_eval =, testCase "'read' works" $runTest' "(read \"(:hello 42)\")" @?= Right (List [Keyword (toIdent "hello"), Number 42])++ , testCase "'to-json' works" $ do+ runTest' "(to-json (dict \"foo\" #t))" @?= Right (String "{\"foo\":true}")+ runTest' "(to-json (dict \"key\" (list 1 \"value\")))" @?= Right (String "{\"key\":[1,\"value\"]}")+ runTest' "(to-json (dict 1 2))" @?= Left (OtherError "Could not serialise value to JSON")]wherefailsWith src err = runTest' src @?= Left err