eskimor / purescript-bridge

Create PureScript datatypes from Haskell datatypes
BSD 3-Clause "New" or "Revised" License
110 stars 49 forks source link

Recommended client side library broken? #87

Open flip111 opened 1 year ago

flip111 commented 1 year ago

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:

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

yaitskov commented 1 year ago

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.