Plutonomicon / plutarch-plutus

Typed eDSL for writing UPLC /ˈpluː.tɑːk/
MIT License
123 stars 63 forks source link

`FromJSON` instance missing for `Config` #707

Closed jaredponn closed 1 month ago

jaredponn commented 1 month ago

In the latest staging branch, we can see that we have the ToJSON Config instance implemented

https://github.com/Plutonomicon/plutarch-plutus/blob/staging/Plutarch/Internal.hs#L325-L345

but there is no FromJSON Config implemented.

Indeed, the CHANGELOG.md suggests the FromJSON Config instance was implemented, but this is evidently not the case.

I don't think I have push permission, but this patch should implement the instance (it's missing roundtrip tests though!):

diff --git a/Plutarch/Internal.hs b/Plutarch/Internal.hs
index 54eff22bd..7dfe6a600 100644
--- a/Plutarch/Internal.hs
+++ b/Plutarch/Internal.hs
@@ -53,7 +53,9 @@ import Data.Aeson (
   object,
   pairs,
   withText,
+  withObject,
   (.=),
+  (.:),
  )
 import Data.ByteString qualified as BS
 import Data.Kind (Type)
@@ -344,6 +346,14 @@ instance ToJSON Config where
           <> ("logLevel" .= ll)
           <> ("tracingMode" .= tm)

+-- | @since 1.6.0
+instance FromJSON Config where
+  parseJSON = withObject "Config" $ \v ->
+    v .: "tag" >>= \(tag :: Int) -> case tag of
+        0 -> return NoTracing
+        1 -> Tracing <$> v .: "logLevel" <*> v .: "tracingMode"
+        _ -> fail "Invalid tag"
+
 {- | If the config indicates that we want to trace, get its mode.

 @since 1.6.0