Skip to content

Commit

Permalink
demonstrate json-helpers in the example
Browse files Browse the repository at this point in the history
  • Loading branch information
peterbecich committed Jan 28, 2024
1 parent 7d7c1d8 commit de32d3f
Show file tree
Hide file tree
Showing 12 changed files with 399 additions and 78 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ There are three PureScript libraries which can interface with Aeson through Pure

Enable this on the Haskell side with `Language.PureScript.Bridge.SumType.jsonHelpers`.

* see `./test/RoundTripJsonHelpers` for example

* this library is demonstrated by the `example`; see `./example/readme.md`.
* see `./test/RoundTripJsonHelpers` for an additional example
* sample Dhall config (for [spago-legacy](https://github.com/purescript/spago-legacy)):
```
, json-helpers =
Expand Down Expand Up @@ -55,18 +57,16 @@ Enable this on the Haskell side with `Language.PureScript.Bridge.SumType.jsonHel
, version = "486db9ee62882baa42cca24f556848c5f6bec565"
}
```

#### [`purescript-argonaut-aeson-generic >=0.4.1`](https://pursuit.purescript.org/packages/purescript-argonaut-aeson-generic/0.4.1) ([GitHub](https://github.com/coot/purescript-argonaut-aeson-generic))

Enable this on the Haskell side with `Language.PureScript.Bridge.SumType.argonautAesonGeneric`.

This library is demonstrated by the `example`; see `./example/readme.md`.

**TODO**: [resolve incompatibility between Argonaut and Aeson](https://github.com/purescript-contrib/purescript-argonaut-codecs/issues/115)

**Additional requirement**: [`peterbecich/purescript-argonaut-codecs`](https://github.com/peterbecich/purescript-argonaut-codecs.git)
* commit `04abb3eb24a4deafe125be0eb23e2786c642e66b`
* see `./test/RoundTripArgonautAesonGeneric` for example
* this library is demonstrated by the `example`; see `./example/readme.md`.
* see `./test/RoundTripArgonautAesonGeneric` for additional example
* note that some types have been disabled from the `RoundTripArgonautAesonGeneric` test
* `RoundTripJsonHelpers` tests more types
* the types tested can be expanded when the incompatibility issue is resolved
Expand Down
15 changes: 13 additions & 2 deletions example/app/GeneratePurescript.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,21 @@ import Control.Lens
import Data.Text (pack)
import Language.PureScript.Bridge

import ArgonautTypes
import JsonHelpersTypes
import qualified MyLib
import Types
import Types (myBridge)

-- https://discourse.purescript.org/t/latest-and-greatest-haskell-purescript-serialization/1640/6
main :: IO ()
main = do
writePSTypesWith "src" (buildBridge myBridge) myTypes
writePSTypesWithNamespace
(Just . PackageName $ pack "Argonaut")
"src"
(buildBridge myBridge)
myArgonautTypes
writePSTypesWithNamespace
(Just . PackageName $ pack "JsonHelpers")
"src"
(buildBridge myBridge)
myJsonHelpersTypes
4 changes: 1 addition & 3 deletions example/app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,4 @@ import qualified MyLib (main)
import Types

main :: IO ()
main = do
writePSTypesWith "src" (buildBridge myBridge) myTypes
MyLib.main
main = MyLib.main
2 changes: 1 addition & 1 deletion example/example.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ build-type: Simple
extra-source-files: CHANGELOG.md

library
exposed-modules: MyLib, Types
exposed-modules: MyLib, Types, ArgonautTypes, JsonHelpersTypes
hs-source-dirs: src
build-depends: base >=4.8 && <5
, aeson >= 1.5.5.0
Expand Down
19 changes: 13 additions & 6 deletions example/readme.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
# PureScript Bridge example

This project demonstrates the libraries PureScript Bridge and [`purescript-argonaut-aeson-generic`](https://pursuit.purescript.org/packages/purescript-argonaut-aeson-generic) ([GitHub](https://github.com/coot/purescript-argonaut-aeson-generic))

It does not use [`input-output-hk/purescript-bridge-json-helpers`](https://github.com/input-output-hk/purescript-bridge-json-helpers.git).
To demonstrate this library in the example, more work is needed in `Main.purs`.

The Haskell type `Foo`, in `src/Types.hs`, is generated for PureScript by PureScript Bridge. Some of values in `Foo` are randomly generated every time the page is loaded. `purescript-argonaut-aeson-generic` is used to decode and encode this payload, client-side. The client modifies some of the payload's values and sends it back to the server.
This project demonstrates:
- PureScript Bridge
- [`purescript-argonaut-aeson-generic`](https://pursuit.purescript.org/packages/purescript-argonaut-aeson-generic) ([GitHub](https://github.com/coot/purescript-argonaut-aeson-generic))
- [`input-output-hk/purescript-bridge-json-helpers`](https://github.com/input-output-hk/purescript-bridge-json-helpers.git)

The Haskell type `Foo`, in `src/Types.hs`, is generated for PureScript by PureScript Bridge. Some of values in `Foo` are randomly generated every time the page is loaded.

On page load, the client:
- requests a `Foo` from the server
- decodes this value using `purescript-argonaut-aeson-generic`
- modifies some of the values
- encodes this with `purescript-argonaut-aeson-generic`, and sends it back to the server
- repeats these steps, encoding and decoding instead with `json-helpers`

# Dependencies
## Nix
Expand Down
2 changes: 2 additions & 0 deletions example/spago.dhall
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
, "control"
, "effect"
, "either"
, "tuples"
, "json-helpers"
, "foldable-traversable"
, "foreign-generic"
, "foreign-object"
Expand Down
52 changes: 26 additions & 26 deletions example/src/Types.purs → example/src/Argonaut/Types.purs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-- File auto generated by purescript-bridge! --
module Types where
module Argonaut.Types where

import Prelude

Expand All @@ -26,17 +26,17 @@ import Type.Proxy (Proxy(Proxy))

newtype Baz = Baz { _bazMessage :: String }



instance Show Baz where
show a = genericShow a

instance EncodeJson Baz where
encodeJson = defer \_ -> genericEncodeAeson Argonaut.defaultOptions

instance DecodeJson Baz where
decodeJson = defer \_ -> genericDecodeAeson Argonaut.defaultOptions



instance Show Baz where
show a = genericShow a

derive instance Generic Baz _

derive instance Newtype Baz _
Expand All @@ -62,17 +62,17 @@ newtype Foo = Foo
, _fooTestData :: TestData
}



instance Show Foo where
show a = genericShow a

instance EncodeJson Foo where
encodeJson = defer \_ -> genericEncodeAeson Argonaut.defaultOptions

instance DecodeJson Foo where
decodeJson = defer \_ -> genericDecodeAeson Argonaut.defaultOptions



instance Show Foo where
show a = genericShow a

derive instance Generic Foo _

derive instance Newtype Foo _
Expand Down Expand Up @@ -110,17 +110,17 @@ fooTestData = _Newtype <<< prop (Proxy :: _"_fooTestData")

newtype Bar a = Bar a



instance (Show a) => Show (Bar a) where
show a = genericShow a

instance (EncodeJson a) => EncodeJson (Bar a) where
encodeJson = defer \_ -> genericEncodeAeson Argonaut.defaultOptions

instance (DecodeJson a, DecodeJsonField a) => DecodeJson (Bar a) where
decodeJson = defer \_ -> genericDecodeAeson Argonaut.defaultOptions



instance (Show a) => Show (Bar a) where
show a = genericShow a

derive instance Generic (Bar a) _

derive instance Newtype (Bar a) _
Expand All @@ -138,17 +138,17 @@ data TestSum
| Int Int
| Number Number



instance Show TestSum where
show a = genericShow a

instance EncodeJson TestSum where
encodeJson = defer \_ -> genericEncodeAeson Argonaut.defaultOptions

instance DecodeJson TestSum where
decodeJson = defer \_ -> genericDecodeAeson Argonaut.defaultOptions



instance Show TestSum where
show a = genericShow a

derive instance Generic TestSum _

--------------------------------------------------------------------------------
Expand Down Expand Up @@ -179,17 +179,17 @@ data TestData
= TMaybe (Maybe TestSum)
| TEither String



instance Show TestData where
show a = genericShow a

instance EncodeJson TestData where
encodeJson = defer \_ -> genericEncodeAeson Argonaut.defaultOptions

instance DecodeJson TestData where
decodeJson = defer \_ -> genericDecodeAeson Argonaut.defaultOptions



instance Show TestData where
show a = genericShow a

derive instance Generic TestData _

--------------------------------------------------------------------------------
Expand Down
21 changes: 21 additions & 0 deletions example/src/ArgonautTypes.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE TypeApplications #-}

module ArgonautTypes where

import Language.PureScript.Bridge (Language (Haskell),
argonautAesonGeneric, mkSumType)
import Language.PureScript.Bridge.SumType (SumType)
import Language.PureScript.Bridge.TypeParameters (A)
import Types

myArgonautTypes :: [SumType 'Haskell]
myArgonautTypes =
[ argonautAesonGeneric . additionalInstances $ mkSumType @Baz
, argonautAesonGeneric . additionalInstances $ mkSumType @Foo
, argonautAesonGeneric . additionalInstances $ mkSumType @(Bar A)
, argonautAesonGeneric . additionalInstances $ mkSumType @TestSum
, argonautAesonGeneric . additionalInstances $ mkSumType @TestData
]
Loading

0 comments on commit de32d3f

Please sign in to comment.