purescript-bridge icon indicating copy to clipboard operation
purescript-bridge copied to clipboard

Recommended client side library broken?

Open flip111 opened this issue 2 years ago • 1 comments

Given this purescript code

module Foo where

import Data.Generic.Rep (class Generic)
import Data.Argonaut.Encode.Class (class EncodeJson, encodeJson)
import Data.Argonaut.Aeson.Encode.Generic (genericEncodeAeson)
import Data.Argonaut.Aeson.Options as Argonaut
import Data.Argonaut.Core (stringify)

data Bla = Bla
  { a :: Boolean
  , b :: Boolean
  }
  | Foo
instance encodeJsonBla :: EncodeJson Bla where
  encodeJson = genericEncodeAeson Argonaut.defaultOptions
derive instance genericBla :: Generic Bla _

bla :: Bla
bla = Bla {a: true, b: false}

json :: String
json = stringify (encodeJson bla)

The code uses this purescript library as per recommendation of this repositories readme https://pursuit.purescript.org/packages/purescript-argonaut-aeson-generic/0.4.1

Output of the code is

{"tag":"Bla","contents":{"b":false,"a":true}}

Then with this haskell program

{-# LANGUAGE OverloadedStrings #-}

import Data.Aeson (FromJSON, eitherDecode)
import GHC.Generics (Generic)

data Bla = Bla
  { a :: Bool
  , b :: Bool
  }
  | Foo
  deriving (Show, Generic)
instance FromJSON Bla

main :: IO ()
main = do
  putStrLn $ show (eitherDecode "{\"tag\":\"Bla\",\"contents\":{\"a\":true,\"b\":false}}" :: Either String Bla)
  putStrLn $ show (eitherDecode "{\"tag\":\"Bla\",\"a\":true,\"b\":false}" :: Either String Bla)

https://play.haskell.org/saved/iSQjGLvY

Output is

Left "Error in $: parsing Main.Bla(Bla) failed, key \"a\" not found"
Right (Bla {a = True, b = False})

This old library had an option which i think get rid of the contents field for Aeson https://github.com/eskimor/purescript-argonaut-generic-codecs/blob/master/src/Data/Argonaut/Generic/Aeson.purs#L37 But i didn't test this.

When i did a search for client side libraries i found these:

  • https://github.com/eskimor/purescript-argonaut-generic-codecs - old library maybe with working code. 5 deps great
  • https://github.com/coot/purescript-argonaut-aeson-generic - recommended library doesn't work. 17 deps all purescript seems to be cleanest
  • https://github.com/paf31/purescript-foreign-generic - recommended library unmaintained, lot's of open pull requests. The recommended branch has 23 deps, all purescript
  • https://github.com/mlabs-haskell/purescript-aeson - didn't test it but looks interesting because it's company backed https://mlabs.city/ 33 dependencies comes with 4 dependencies to handle big numbers 1 2 3 4. Doesn't look like they are interested for it to be picked up by the community because there is no readme, but can ask i suppose.

Does someone have a recommendation of how to get client side encoding compatible with purescript-bridge?

Examples with purescript version 0.15.10 GHC version 9.4.6 (stackage LTS 21.9) github: eskimor/purescript-bridge 5ee8778ff141ae6ad9a6057921ec4d49e067b6f0 package set https://github.com/purescript/package-sets/releases/download/psc-0.15.10-20230828/packages.dhall

flip111 avatar Aug 28 '23 21:08 flip111

I also had an issue with PureScript code generated by the library.

Generated code depends on obsolete purescript libraries (purescript-foreign-generic) and PureScript translators fails with generic errors I cannot solve.

yaitskov avatar Sep 08 '23 16:09 yaitskov