forked from haskell-to-elm/haskell-to-elm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUser.hs
47 lines (38 loc) · 1.31 KB
/
User.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
{-# language DeriveAnyClass #-}
{-# language DeriveGeneric #-}
{-# language MultiParamTypeClasses #-}
{-# language OverloadedStrings #-}
{-# language TypeApplications #-}
module User where
import qualified Data.Aeson as Aeson
import Data.Foldable
import qualified Data.HashMap.Lazy as HashMap
import Data.Text (Text)
import qualified Generics.SOP as SOP
import GHC.Generics
import qualified Language.Elm.Pretty as Pretty
import qualified Language.Elm.Simplification as Simplification
import Language.Haskell.To.Elm
data User = User
{ name :: Text
, age :: Int
} deriving (Generic, Aeson.ToJSON, SOP.Generic, SOP.HasDatatypeInfo)
instance HasElmType User where
elmDefinition =
Just $ deriveElmTypeDefinition @User defaultOptions "Api.User.User"
instance HasElmDecoder Aeson.Value User where
elmDecoderDefinition =
Just $ deriveElmJSONDecoder @User defaultOptions Aeson.defaultOptions "Api.User.decoder"
instance HasElmEncoder Aeson.Value User where
elmEncoderDefinition =
Just $ deriveElmJSONEncoder @User defaultOptions Aeson.defaultOptions "Api.User.encoder"
main :: IO ()
main = do
let
definitions =
Simplification.simplifyDefinition <$>
jsonDefinitions @User
modules =
Pretty.modules definitions
forM_ (HashMap.toList modules) $ \(_moduleName, contents) ->
print contents