• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

krisajenkins/elm-export: Create Elm types and JSON decoders from Haskell source.

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

krisajenkins/elm-export

开源软件地址(OpenSource Url):

https://github.com/krisajenkins/elm-export

开源编程语言(OpenSource Language):

Haskell 82.9%

开源软件介绍(OpenSource Introduction):

Elm Export

Build Status

Create Elm classes and JSON decoders from Haskell DataTypes.

Installation

Elm Export is available on Hackage.

Usage

To use this library, you must first make the types you want to export implement ElmType. This is easy. Just derive Generic, and then we can automatically generate the ElmType instance for you. Here's an example with a Person type:

{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveAnyClass #-}

module Db where

import Elm
import GHC.Generics

data Person = Person
  { id :: Int
  , name :: Maybe String
  } deriving (Show, Eq, Generic, ElmType)

That's it for the type. Now you'll want to write a main that generates the Elm source code:

module Main where

import Data.Proxy
import Db
import Elm

spec :: Spec
spec =
  Spec
    ["Db", "Types"]
    [ "import Json.Decode exposing (..)"
    , "import Json.Decode.Pipeline exposing (..)"
    , toElmTypeSource (Proxy :: Proxy Person)
    , toElmDecoderSource (Proxy :: Proxy Person)
    ]

main :: IO ()
main = specsToDir [spec] "some/where/output"

Run this and the directory some/where/output will be created, and under that the Elm source file Db/Types.elm will be found.

All the hard work here is done by toElmTypeSource and toElmDecoderSource. The Spec code is just wrapping to make it easy to create a complete Elm file from the meat that ElmType gives you.

Required Elm Packages

The decoders we produce require these extra Elm packages installed:

elm package install NoRedInk/elm-decode-pipeline
elm package install krisajenkins/elm-exts

Development

You will need Stack.

Building

stack build

Testing

stack test --file-watch

Change Log

V0.3.0.0

  • Renamed ToElmType to ElmType, for brevity.

V0.2.0.0

V0.1.0.0

  • Initial release.

Status

Alpha. The author is using it in production, but it is not yet expected to work for every reasonable case.

There are some Haskell datatypes that cannot be represented in Elm. Obviously we will not support those. But there are some which are legal Haskell and legal Elm, but we do not yet generate. Please send examples, PRs and code-suggestions!

Contributors

License

Copyright © 2015-2017 Kris Jenkins

Distributed under the Eclipse Public License.

See Also

Elm Bridge is a different implementation of the same goal. That project uses Template Haskell, this one uses GHC Generics.




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
psibi/tldr-hs: Haskell tldr client发布时间:2022-06-24
下一篇:
VerbalExpressions/HaskellVerbalExpressions发布时间:2022-06-24
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap