elm-bridge

Derive Elm types from Haskell types

http://github.com/agrafix/elm-bridge

Version on this page:0.2.1.1
LTS Haskell 22.17:0.8.3
Stackage Nightly 2023-12-26:0.8.2
Latest on Hackage:0.8.3

See all snapshots elm-bridge appears in

BSD-3-Clause licensed by Alexander Thiemann, Simon Marechal
Maintained by Alexander Thiemann
This version can be pinned in stack with:elm-bridge-0.2.1.1@sha256:244ff940a4a514ccdc510413c8ef42fcfbba4b49d31d5181c6a9c58305c0709f,2193

Module documentation for 0.2.1.1

Elm Bridge

Build Status

Hackage Deps

Intro

Hackage: elm-bridge

Building the bridge from Haskell to Elm and back. Define types once, use on both sides and enjoy easy (de)serialisation. Cheers!

Note that the bartavelle/json-helpers package, with version >= 1.1.0, is expected by the generated Elm modules.

Usage

{-# LANGUAGE TemplateHaskell #-}
import Elm.Derive
import Elm.Module

import Data.Proxy

data Foo
   = Foo
   { f_name :: String
   , f_blablub :: Int
   } deriving (Show, Eq)

deriveBoth defaultOptions ''Foo

main :: IO ()
main =
    putStrLn $ makeElmModule "Foo"
    [ DefineElm (Proxy :: Proxy Foo)
    ]

Output will be:

module Foo where

import Json.Decode
import Json.Decode exposing ((:=))
import Json.Encode
import Json.Helpers exposing (..)


type alias Foo  =
   { f_name: String
   , f_blablub: Int
   }

jsonDecFoo : Json.Decode.Decoder ( Foo )
jsonDecFoo =
   ("f_name" := Json.Decode.string) `Json.Decode.andThen` \pf_name ->
   ("f_blablub" := Json.Decode.int) `Json.Decode.andThen` \pf_blablub ->
   Json.Decode.succeed {f_name = pf_name, f_blablub = pf_blablub}

jsonEncFoo : Foo -> Value
jsonEncFoo  val =
   Json.Encode.object
   [ ("f_name", Json.Encode.string val.f_name)
   , ("f_blablub", Json.Encode.int val.f_blablub)
   ]

For more usage examples check the tests or the examples dir.

Install

  • Using cabal: cabal install elm-bridge
  • From Source: git clone https://github.com/agrafix/elm-bridge.git && cd elm-bridge && cabal install

Changes

v0.2.1

New features

  • The template Haskell derivation functions now take aeson Option type instead of a custom type. This change makes it easier to synchronize the Haskell and Elm code.
  • The generated Elm code can be personalized. Helpers functions assist in converting type names, and defining which type will be newtyped.

Notes