elm-bridge/README.md

95 lines
2.7 KiB
Markdown
Raw Normal View History

2015-08-09 19:43:46 +03:00
Elm Bridge
=====
[![Build Status](https://travis-ci.org/agrafix/elm-bridge.svg)](https://travis-ci.org/agrafix/elm-bridge)
[![Hackage Deps](https://img.shields.io/hackage-deps/v/elm-bridge.svg)](http://packdeps.haskellers.com/reverse/elm-bridge)
## Intro
Hackage: [elm-bridge](http://hackage.haskell.org/package/elm-bridge)
Building the bridge from [Haskell](http://haskell.org) to [Elm](http://elm-lang.org) and back. Define types once, use on both sides and enjoy easy (de)serialisation. Cheers!
2019-10-21 19:31:19 +03:00
This version of the package only supports Elm 0.19. Version 0.5.2 supports Elm 0.18, and Version 0.3.0.2 supports Elm 0.16 and Elm 0.17.
2016-11-25 12:21:52 +03:00
Note that the [bartavelle/json-helpers](http://package.elm-lang.org/packages/bartavelle/json-helpers/latest/) package, with version >= 1.2.0, is expected by the generated Elm modules.
2016-01-13 13:07:40 +03:00
2015-08-09 19:43:46 +03:00
## Usage
```haskell
{-# LANGUAGE TemplateHaskell #-}
import Elm.Derive
2015-08-09 23:46:02 +03:00
import Elm.Module
2015-08-09 19:43:46 +03:00
import Data.Proxy
data Foo
= Foo
{ f_name :: String
, f_blablub :: Int
} deriving (Show, Eq)
2016-01-13 13:08:01 +03:00
deriveBoth defaultOptions ''Foo
2015-08-09 23:46:02 +03:00
2015-08-09 19:43:46 +03:00
main :: IO ()
main =
2015-08-09 23:46:02 +03:00
putStrLn $ makeElmModule "Foo"
[ DefineElm (Proxy :: Proxy Foo)
]
2015-08-09 19:43:46 +03:00
```
Output will be:
```elm
2015-08-09 23:46:02 +03:00
module Foo where
import Json.Decode
import Json.Decode exposing ((:=))
import Json.Encode
2016-01-13 13:08:01 +03:00
import Json.Helpers exposing (..)
2015-08-09 23:46:02 +03:00
type alias Foo =
{ f_name: String
, f_blablub: Int
}
2016-01-13 13:08:01 +03:00
jsonDecFoo : Json.Decode.Decoder ( Foo )
jsonDecFoo =
2015-08-09 23:46:02 +03:00
("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}
2016-01-13 13:08:01 +03:00
jsonEncFoo : Foo -> Value
2015-08-09 23:46:02 +03:00
jsonEncFoo val =
Json.Encode.object
[ ("f_name", Json.Encode.string val.f_name)
, ("f_blablub", Json.Encode.int val.f_blablub)
]
2015-08-09 19:43:46 +03:00
```
2020-04-15 17:12:48 +03:00
Also, there are functions `Elm.Json.stringSerForSimpleAdt` and `Elm.Json.stringParserForSimpleAdt` to generate functions for your non-JSON ADT types.
2015-08-09 23:47:54 +03:00
For more usage examples check the tests or the examples dir.
2015-08-09 19:43:46 +03:00
## Install
2016-04-28 14:46:54 +03:00
### Haskell
2015-08-09 19:43:46 +03:00
* Using cabal: `cabal install elm-bridge`
* From Source: `git clone https://github.com/agrafix/elm-bridge.git && cd elm-bridge && cabal install`
2016-04-28 14:46:54 +03:00
### Elm
* `elm package install bartavelle/json-helpers`
or, for Elm 0.19:
* `elm install bartavelle/json-helpers`
2016-04-28 14:46:54 +03:00
## 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.