Skip to content

eskimor/purescript-bridge

Repository files navigation

purescript-bridge

Haskell library and example PureScript example Nix Flake

Translate your Haskell types to PureScript types. It should in theory work for almost all Haskell types, including type constructors! You just have to instantiate it with dummy parameters from e.g. "Language.PureScript.Bridge.TypeParameters".

Data type translation is fully and easily customizable by providing your own BridgePart instances!

The latest version of this project requires PureScript 0.15.

JSON encoding / decoding

Haskell

Use aeson's generic encoding/decoding with default options

PureScript

There are three PureScript libraries which can interface with Aeson through PureScript bridge. The second, purescript-argonaut-aeson-generic, has issues.

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

  • see ./test/RoundTripJsonHelpers for example
  • sample Dhall config (for spago-legacy):
, json-helpers =
  { dependencies =
    [ "aff"
    , "argonaut-codecs"
    , "argonaut-core"
    , "arrays"
    , "bifunctors"
    , "contravariant"
    , "control"
    , "effect"
    , "either"
    , "enums"
    , "foldable-traversable"
    , "foreign-object"
    , "maybe"
    , "newtype"
    , "ordered-collections"
    , "prelude"
    , "profunctor"
    , "psci-support"
    , "quickcheck"
    , "record"
    , "spec"
    , "spec-quickcheck"
    , "transformers"
    , "tuples"
    , "typelevel-prelude"
    ]
  , repo =
      "https://github.com/input-output-hk/purescript-bridge-json-helpers.git"
  , version = "486db9ee62882baa42cca24f556848c5f6bec565"
  }

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

Additional requirement: peterbecich/purescript-argonaut-codecs

  • commit 04abb3eb24a4deafe125be0eb23e2786c642e66b
  • see ./test/RoundTripArgonautAesonGeneric for 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
  • sample Dhall config:
      , argonaut-codecs =
        { dependencies = [ "console" ]
        , repo = "https://github.com/peterbecich/purescript-argonaut-codecs.git"
        , version = "04abb3eb24a4deafe125be0eb23e2786c642e66b"
        }

See ForeignObject in Language.PureScript.Bridge.SumType.

This may need to be fixed.

The test coverage is less than the other two libraries.

Documentation

Usage of this library is documented in Language.Purescript.Bridge, with writePSTypes you should have everything to get started. Documentation can be found here.

There is an example; see ./example/readme.md.

Status

It works for my use case and is used in production. PRs for more PSTypes definitions and bridges are very welcome!

Contributing

Formatting the source code

This repo uses stylish-haskell. .stylish-haskell.yaml is provided.

find . -name '*.hs' | xargs stylish-haskell -i