Haskell: Derive Elm types from Haskell types
Go to file
2019-02-28 14:17:49 +01:00
examples Sync the README and example 2016-01-13 11:08:01 +01:00
src/Elm rename parameter variable for tuples to 't' 2019-02-28 06:54:32 +01:00
test rename parameter variable for tuples to 't' 2019-02-28 06:54:32 +01:00
.gitignore small improvements, README and example 2015-08-09 22:46:02 +02:00
.travis.yml update travis job 2019-02-28 14:17:49 +01:00
CHANGELOG.md Use a descriptive sum type for sum types variables 2018-07-09 16:50:04 +02:00
elm-bridge.cabal Use a descriptive sum type for sum types variables 2018-07-09 16:50:04 +02:00
LICENSE some meta fixes and version management 2016-04-28 13:46:54 +02:00
README.md v0.4.0 2016-11-25 10:21:52 +01:00
Setup.hs first working prototype 2015-08-09 18:00:06 +02:00
stack.yaml update stack 2018-05-28 09:19:01 +02:00

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!

This version of the package only supports Elm 0.18. Version 0.3.0.2 supports Elm 0.16 and Elm 0.17.

Note that the bartavelle/json-helpers package, with version >= 1.2.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

Haskell

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

Elm

  • elm package install bartavelle/json-helpers

Contribute

Pull requests are welcome! Please consider creating an issue beforehand, so we can discuss what you would like to do. Code should be written in a consistent style throughout the project. Avoid whitespace that is sensible to conflicts. (E.g. alignment of = signs in functions definitions) Note that by sending a pull request you agree that your contribution can be released under the BSD3 License as part of the elm-bridge package or related packages.