Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge upstream changes #1

Closed
wants to merge 25 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
214537e
Add moduleSpec and renderType functions.
mattjbray Jan 24, 2017
66f7ed1
Readme updates.
krisajenkins Feb 10, 2017
2a0e578
Exporting Elm.Common.require, for the new moduleSpec code.
krisajenkins Feb 11, 2017
b300274
Whitespace changes.
krisajenkins Feb 11, 2017
872576a
Use Control.Monad.RWS.
mattjbray Feb 12, 2017
b445ebf
Whitespace changes.
krisajenkins Feb 21, 2017
0ba463e
Replacing some usages `>>` with do-blocks.
krisajenkins Feb 21, 2017
a3df6c7
Replacing the use of Json.Decode.maybe with .nullable.
krisajenkins Feb 21, 2017
a2599f1
Add a contributing section to the Readme.
mattjbray Mar 5, 2017
7ca89b8
Add HasElmComparable instance for Text
tekul Mar 8, 2017
bad9f74
support for algebraic sum types
Feb 26, 2017
5e4971b
Algebraic sum encoders and decoders follow elm-format
Mar 11, 2017
ed8e4e7
Fixed compilation warnings.
hadronized Jun 20, 2017
6f3b891
Merge pull request #1 from phaazon/fix-warnings
FPtje Jun 20, 2017
52d71ac
Fix decoder generation for string maps to use `Json.Decode.dict`
ehamberg Mar 26, 2017
a9459c0
README tweaks.
krisajenkins May 21, 2017
2c23c6a
Updating to LTS-9.1.
krisajenkins Aug 18, 2017
589bcad
Adding a docstring.
krisajenkins Aug 18, 2017
cfde636
Switching from a .cabal file to a hpack-format package.yaml.
krisajenkins May 21, 2017
980c3a5
Hlint suggestion: removing redundant brackets.
krisajenkins Aug 18, 2017
7971532
Add more information on an error if it happens.
hadronized Aug 25, 2017
f7ffcc2
Complete fix for HasDecoder ElmValue.
hadronized Aug 25, 2017
9571a1e
Merge pull request #2 from phaazon/master
FPtje Aug 25, 2017
8030a8b
Merge remote-tracking branch 'krisajenkins/devel'
jwoudenberg Nov 24, 2017
5a71fb1
Merge remote-tracking branch 'fptje/master' into merge-upstream
jwoudenberg Nov 24, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/elm-export.cabal
/.stack-work/
/TAGS
/test/elm-package.json
Expand Down
41 changes: 27 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,11 @@ 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)
]
moduleSpec ["Db", "Types"] $ do
require "Date exposing (Date)"
renderType (Proxy :: Proxy Person)
renderDecoder (Proxy :: Proxy Person)
renderEncoder (Proxy :: Proxy Person)

main :: IO ()
main = specsToDir [spec] "some/where/output"
Expand All @@ -57,11 +55,6 @@ 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:
Expand All @@ -87,8 +80,28 @@ stack build
stack test --file-watch
```

### Contribution Guide

Development happens on the `devel` branch. Pull requests target this branch.

Generated Elm code adheres to the [`elm-format`][1] style.

JSON encoders and decoders match the default behavior of [Aeson][2].

[1]: https://github.com/avh4/elm-format
[2]: https://hackage.haskell.org/package/aeson

## Change Log

### V0.6.x
Updated to Elm 0.18.

### V0.5.x
???

### V0.4.x
???

### V0.3.0.0
* Renamed `ToElmType` to `ElmType`, for brevity.

Expand All @@ -100,8 +113,8 @@ stack test --file-watch

## Status

Alpha. The author is using it in production, but it is not yet
expected to work for every reasonable case.
Beta. Several people are using it in production, reliably, but it is
not yet expected to work for every reasonable datatype.

There are some Haskell datatypes that cannot be represented in
Elm. Obviously we will not support those. But there are some which are
Expand Down
153 changes: 96 additions & 57 deletions elm-export.cabal
Original file line number Diff line number Diff line change
@@ -1,62 +1,101 @@
name: elm-export
version: 0.6.0.1
synopsis: A library to generate Elm types from Haskell source.
description: Generate Elm source code automatically from Haskell types. Using GHC.Generics, we can automatically derive Elm type declarations, and Aeson-compatible JSON decoders & encoders.
homepage: http://github.com/krisajenkins/elm-export
stability: alpha
license: OtherLicense
license-file: LICENSE.txt
author: Kris Jenkins
maintainer: [email protected]
copyright: 2015-2017 Kris Jenkins
category: Web
build-type: Simple
extra-source-files: test/*.elm
cabal-version: >=1.10
-- This file has been generated from package.yaml by hpack version 0.17.1.
--
-- see: https://github.com/sol/hpack

library
hs-source-dirs: src
exposed-modules: Elm
build-depends: base >= 4.7 && < 5
, bytestring
, containers
, directory
, formatting
, mtl
, servant
, text
, time
, wl-pprint-text
default-language: Haskell2010
ghc-options: -Wall
other-modules: Elm.Type
, Elm.Common
, Elm.Decoder
, Elm.Encoder
, Elm.File
, Elm.Record
name: elm-export
version: 0.6.0.1
synopsis: A library to generate Elm types from Haskell source.
description: Generate Elm source code automatically from Haskell types. Using GHC.Generics, we can automatically derive Elm type declarations, and Aeson-compatible JSON decoders & encoders.
category: Web
stability: alpha
homepage: http://github.com/krisajenkins/elm-export
bug-reports: https://github.com/krisajenkins/elm-export/issues
author: Kris Jenkins
maintainer: [email protected]
copyright: 2015-2017 Kris Jenkins
license: OtherLicense
license-file: LICENSE.txt
build-type: Simple
cabal-version: >= 1.10

test-suite elm-export-test
type: exitcode-stdio-1.0
hs-source-dirs: test
main-is: Spec.hs
other-modules: ExportSpec
, TypesSpec
build-depends: Diff
, HUnit
, QuickCheck
, base
, bytestring
, containers
, elm-export
, hspec
, hspec-core
, quickcheck-instances
, text
, time
ghc-options: -threaded -rtsopts -with-rtsopts=-N -Wall
default-language: Haskell2010
extra-source-files:
test/CommentDecoder.elm
test/CommentDecoderWithOptions.elm
test/CommentEncoder.elm
test/CommentEncoderWithOptions.elm
test/CommentType.elm
test/CommentTypeWithOptions.elm
test/FavoritePlacesType.elm
test/MonstrosityDecoder.elm
test/MonstrosityEncoder.elm
test/MonstrosityType.elm
test/PositionDecoder.elm
test/PositionEncoder.elm
test/PositionType.elm
test/PostDecoder.elm
test/PostDecoderWithOptions.elm
test/PostEncoder.elm
test/PostEncoderWithOptions.elm
test/PostType.elm
test/PostTypeWithOptions.elm
test/TimingDecoder.elm
test/TimingEncoder.elm
test/TimingType.elm
test/UselessDecoder.elm
test/UselessType.elm

source-repository head
type: git
type: git
location: https://github.com/krisajenkins/elm-export

library
hs-source-dirs:
src
ghc-options: -Wall
build-depends:
base
, bytestring
, containers
, servant
, text
, time
, directory
, formatting
, mtl
, wl-pprint-text
exposed-modules:
Elm
other-modules:
Elm.Common
Elm.Decoder
Elm.Encoder
Elm.File
Elm.Record
Elm.Type
Paths_elm_export
default-language: Haskell2010

test-suite elm-export-test
type: exitcode-stdio-1.0
main-is: Spec.hs
hs-source-dirs:
test
ghc-options: -Wall -threaded -rtsopts -with-rtsopts=-N
build-depends:
base
, bytestring
, containers
, servant
, text
, time
, Diff
, HUnit
, QuickCheck
, elm-export
, hspec
, hspec-core
, quickcheck-instances
other-modules:
ExportSpec
TypesSpec
default-language: Haskell2010
53 changes: 53 additions & 0 deletions package.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: elm-export
version: '0.6.0.1'
synopsis: A library to generate Elm types from Haskell source.
description: Generate Elm source code automatically from Haskell types. Using GHC.Generics,
we can automatically derive Elm type declarations, and Aeson-compatible JSON decoders
& encoders.
category: Web
author: Kris Jenkins
maintainer: [email protected]
copyright: 2015-2017 Kris Jenkins
license: OtherLicense
license-file: LICENSE.txt
github: krisajenkins/elm-export
homepage: http://github.com/krisajenkins/elm-export
stability: alpha
extra-source-files:
- test/*.elm
dependencies:
- base
- bytestring
- containers
- servant
- text
- time
ghc-options:
- -Wall

library:
source-dirs: src
exposed-modules:
- Elm
dependencies:
- directory
- formatting
- mtl
- wl-pprint-text

tests:
elm-export-test:
main: Spec.hs
source-dirs: test
ghc-options:
- -threaded
- -rtsopts
- -with-rtsopts=-N
dependencies:
- Diff
- HUnit
- QuickCheck
- elm-export
- hspec
- hspec-core
- quickcheck-instances
4 changes: 3 additions & 1 deletion src/Elm.hs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
{-| Generate Elm types, JSON decoders & JSON encoders from Haskell datatypes.
-}
module Elm
( module X
) where

import Elm.Common as X (Options(..), defaultOptions)
import Elm.Common as X (Options(..), defaultOptions, require)
import Elm.Decoder as X
import Elm.Encoder as X
import Elm.File as X
Expand Down
50 changes: 43 additions & 7 deletions src/Elm/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

module Elm.Common where

import Control.Monad.RWS
import Data.Set (Set)
import qualified Data.Set as S
import Data.Text (Text)
import qualified Data.Text.Lazy as LT
import Formatting hiding (text)
import Text.PrettyPrint.Leijen.Text hiding ((<$>), (<>))
import Data.Monoid
import Data.Text (Text)
import qualified Data.Text.Lazy as LT
import Formatting hiding (text)

data Options = Options
{ fieldLabelModifier :: Text -> Text
Expand All @@ -18,9 +20,7 @@ defaultOptions = Options {fieldLabelModifier = id}
cr :: Format r r
cr = now "\n"

mintercalate
:: Monoid m
=> m -> [m] -> m
mintercalate :: Monoid m => m -> [m] -> m
mintercalate _ [] = mempty
mintercalate _ [x] = x
mintercalate seperator (x:xs) = x <> seperator <> mintercalate seperator xs
Expand All @@ -33,3 +33,39 @@ stext = text . LT.fromStrict

spaceparens :: Doc -> Doc
spaceparens doc = "(" <+> doc <+> ")"

-- | Parentheses of which the right parenthesis exists on a new line
newlineparens :: Doc -> Doc
newlineparens doc = "(" <> doc <$$> ")"

-- | An empty line, regardless of current indentation
emptyline :: Doc
emptyline = nest minBound linebreak

-- | Like <$$>, but with an empty line in between
(<$+$>) :: Doc -> Doc -> Doc
l <$+$> r = l <> emptyline <$$> r

--
type RenderM
= RWS Options ( Set Text -- The set of required imports
, [Text] -- Generated declarations
) ()

{-| Add an import to the set.
-}
require :: Text -> RenderM ()
require dep = tell (S.singleton dep, [])

{-| Take the result of a RenderM computation and put it into the Writer's
declarations.
-}
collectDeclaration :: RenderM Doc -> RenderM ()
collectDeclaration =
mapRWS (\(defn, (), (imports, _)) -> ((), (), (imports, [pprinter defn])))

squarebracks :: Doc -> Doc
squarebracks doc = "[" <+> doc <+> "]"

pair :: Doc -> Doc -> Doc
pair l r = spaceparens $ l <> comma <+> r
Loading