Skip to content

Commit

Permalink
[wip] work on FromJSON instances
Browse files Browse the repository at this point in the history
  • Loading branch information
byorgey committed Nov 17, 2024
1 parent 2a3b795 commit 422f389
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 8 deletions.
51 changes: 43 additions & 8 deletions src/swarm-lang/Swarm/Language/JSON.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ViewPatterns #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}

-- |
Expand All @@ -8,13 +9,18 @@
-- to put them all here to avoid circular module dependencies.
module Swarm.Language.JSON where

import Data.Aeson (FromJSON (..), ToJSON (..), withText)
import Data.Aeson (FromJSON (..), ToJSON (..), genericParseJSON, genericToJSON, withText)

Check warning on line 12 in src/swarm-lang/Swarm/Language/JSON.hs

View workflow job for this annotation

GitHub Actions / Haskell-CI - windows-latest - ghc-9.8.2

The import of ‘genericParseJSON’
import Data.Aeson qualified as Ae
import Data.Aeson.KeyMap qualified as Ae
import Data.Vector qualified as V
import Swarm.Language.Context (CtxMap, CtxTree)
import Swarm.Language.Pipeline (processTermEither)
import Swarm.Language.Syntax (Term)
import Swarm.Language.Syntax.Pattern (Syntax, TSyntax)
import Swarm.Language.Value (Env, Value)
import Swarm.Language.Value (Env, Value (..))
import Swarm.Pretty (prettyText)
import Swarm.Util.JSON (optionsMinimize)
import Swarm.Util.Yaml (FromJSONE, liftE, parseJSONE, withObjectE)
import Witch (into)

instance FromJSON TSyntax where
Expand All @@ -29,13 +35,42 @@ instance ToJSON Term
instance ToJSON Syntax

instance ToJSON Value where
toJSON = undefined
toJSON = genericToJSON optionsMinimize

instance FromJSON Value where
parseJSON = undefined
instance FromJSONE (CtxMap CtxTree t) Value where
parseJSONE = withObjectE "Value" $ \v -> case Ae.toList v of

Check warning on line 41 in src/swarm-lang/Swarm/Language/JSON.hs

View workflow job for this annotation

GitHub Actions / Haskell-CI - windows-latest - ghc-9.8.2

Pattern match(es) are non-exhaustive

Check warning on line 41 in src/swarm-lang/Swarm/Language/JSON.hs

View workflow job for this annotation

GitHub Actions / Haskell-CI - windows-latest - ghc-9.8.2

Pattern match checker ran into -fmax-pmcheck-models=30 limit, so
[("VUnit", _)] -> pure VUnit
[("VInt", n)] -> VInt <$> liftE (parseJSON n)
[("VText", t)] -> VText <$> liftE (parseJSON t)
[("VInj", Ae.Array (V.toList -> [i, x]))] -> VInj <$> liftE (parseJSON i) <*> parseJSONE x
[("VPair", Ae.Array (V.toList -> [v1,v2]))] -> VPair <$> parseJSONE v1 <*> parseJSONE v2
[("VClo", Ae.Array (V.toList -> [x,t,e]))] ->
VClo <$> liftE (parseJSON x) <*> liftE (parseJSON t) <*> parseJSONE e
[("VCApp", Ae.Array (V.toList -> [c, vs]))] ->
VCApp <$> liftE (parseJSON c) <*> parseJSONE vs
[("VBind", Ae.Array (V.toList -> [x,ty,r,t1,t2,e]))] ->
VBind
<$> liftE (parseJSON x)
<*> liftE (parseJSON ty)
<*> liftE (parseJSON r)
<*> liftE (parseJSON t1)
<*> liftE (parseJSON t2)
<*> parseJSONE e
[("VDelay", Ae.Array (V.toList -> [t, e]))] ->
VDelay <$> liftE (parseJSON t) <*> parseJSONE e
[("VRef", n)] -> VRef <$> liftE (parseJSON n)
[("VIndir", n)] -> VIndir <$> liftE (parseJSON n)
[("VRcd", m)] -> VRcd <$> parseJSONE m
[("VKey", k)] -> VKey <$> liftE (parseJSON k)
[("VRequirements", Ae.Array (V.toList -> [txt, t, e]))] ->
VRequirements <$> liftE (parseJSON txt) <*> liftE (parseJSON t) <*> parseJSONE e
[("VSuspend", Ae.Array (V.toList -> [t, e]))] ->
VSuspend <$> liftE (parseJSON t) <*> parseJSONE e
[("VExc",_)] -> pure VExc
[("VBlackhole",_)] -> pure VBlackhole

instance ToJSON Env where
toJSON = undefined
toJSON = genericToJSON optionsMinimize

instance FromJSON Env where
parseJSON = undefined
instance FromJSONE (CtxMap CtxTree t) Env where
parseJSONE = undefined

Check warning on line 76 in src/swarm-lang/Swarm/Language/JSON.hs

View workflow job for this annotation

GitHub Actions / HLint

Warning in module Swarm.Language.JSON: Avoid restricted function ▫︎ Found: "undefined" ▫︎ Note: may break the code
4 changes: 4 additions & 0 deletions src/swarm-util/Swarm/Util/Yaml.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import Control.Monad.Reader
import Data.Aeson.Key (fromText)
import Data.Aeson.Types (explicitParseField, explicitParseFieldMaybe)
import Data.Bifunctor (first)
import Data.Map (Map)
import Data.Maybe (fromMaybe)
import Data.Text (Text)
import Data.Vector qualified as V
Expand Down Expand Up @@ -101,6 +102,9 @@ instance (FromJSONE e a, FromJSONE e b) => FromJSONE e (a, b) where
<*> parseJSONE (V.unsafeIndex t 1)
else failT ["cannot unpack array of length", showT n, "into a tuple of length 2"]

instance (FromJSONE e a) => FromJSONE e (Map k a) where
parseJSONE = undefined

Check warning on line 106 in src/swarm-util/Swarm/Util/Yaml.hs

View workflow job for this annotation

GitHub Actions / HLint

Warning in module Swarm.Util.Yaml: Avoid restricted function ▫︎ Found: "undefined" ▫︎ Note: may break the code

------------------------------------------------------------
-- Decoding
------------------------------------------------------------
Expand Down

0 comments on commit 422f389

Please sign in to comment.