on master help sign in
elm-ps-bridge/README.md
162 lines 4.9 kB history raw
1# Elm PS Bridge
3A very rough tool to generate compatible ELm and PS type definitions from a common YAML definition. I intend to use them to communicate through Elm ports.
5## Example
7```bash
8elm-ps-bridge --input example.yaml --elmOutput Bridge.elm --psOutput Bridge.purs
9```
11### `example.yaml`
13```yaml
14Subscription:
15- DownloadPasswords
16- UpdatePassword:
17 foo: Int
18 id: Int
19Command:
20- Hello
21```
24### `Bridge.purs`
26```purescript
27-- File auto generated by purescript-bridge! --
28module Bridge where
30import Data.Generic.Rep (class Generic)
31import Data.Lens (Iso', Lens', Prism', lens, prism')
32import Data.Lens.Iso.Newtype (_Newtype)
33import Data.Lens.Record (prop)
34import Data.Maybe (Maybe(..))
35import Data.Newtype (class Newtype)
36import Data.Symbol (SProxy(SProxy))
38import Prelude
40data Command =
41 Hello
43derive instance genericCommand :: Generic Command _
44derive instance newtypeCommand :: Newtype Command _
45derive instance eqCommand :: Eq Command
46derive instance ordCommand :: Ord Command
48--------------------------------------------------------------------------------
49_Hello :: Prism' Command Unit
50_Hello = prism' (\_ -> Hello) f
51 where
52 f Hello = Just unit
54--------------------------------------------------------------------------------
55newtype Subscription_UpdatePassword =
56 Subscription_UpdatePassword {
57 foo :: Int
58 , id :: Int
59 }
61derive instance genericSubscription_UpdatePassword :: Generic Subscription_UpdatePassword _
62derive instance newtypeSubscription_UpdatePassword :: Newtype Subscription_UpdatePassword _
63derive instance eqSubscription_UpdatePassword :: Eq Subscription_UpdatePassword
64derive instance ordSubscription_UpdatePassword :: Ord Subscription_UpdatePassword
66--------------------------------------------------------------------------------
67_Subscription_UpdatePassword :: Iso' Subscription_UpdatePassword { foo :: Int, id :: Int}
68_Subscription_UpdatePassword = _Newtype
70--------------------------------------------------------------------------------
71data Subscription =
72 DownloadPasswords
73 | UpdatePassword Subscription_UpdatePassword
75derive instance genericSubscription :: Generic Subscription _
76derive instance newtypeSubscription :: Newtype Subscription _
77derive instance eqSubscription :: Eq Subscription
78derive instance ordSubscription :: Ord Subscription
80--------------------------------------------------------------------------------
81_DownloadPasswords :: Prism' Subscription Unit
82_DownloadPasswords = prism' (\_ -> DownloadPasswords) f
83 where
84 f DownloadPasswords = Just unit
85 f _ = Nothing
87_UpdatePassword :: Prism' Subscription Subscription_UpdatePassword
88_UpdatePassword = prism' UpdatePassword f
89 where
90 f (UpdatePassword a) = Just $ a
91 f _ = Nothing
93--------------------------------------------------------------------------------
94```
96### `Bridge.elm`
98```elm
99module Bridge exposing(..)
101import Json.Decode
102import Json.Encode exposing (Value)
103-- The following module comes from bartavelle/json-helpers
104import Json.Helpers exposing (..)
105import Dict exposing (Dict)
106import Set exposing (Set)
109type Command =
110 Hello
112jsonDecCommand : Json.Decode.Decoder ( Command )
113jsonDecCommand =
114 let jsonDecDictCommand = Dict.fromList [("Hello", Hello)]
115 in decodeSumUnaries "Command" jsonDecDictCommand
117jsonEncCommand : Command -> Value
118jsonEncCommand val =
119 case val of
120 Hello -> Json.Encode.string "Hello"
124type alias Subscription_UpdatePassword =
125 { foo: Int
126 , id: Int
127 }
129jsonDecSubscription_UpdatePassword : Json.Decode.Decoder ( Subscription_UpdatePassword )
130jsonDecSubscription_UpdatePassword =
131 Json.Decode.succeed (\pfoo pid -> {foo = pfoo, id = pid})
132 |> required "foo" (Json.Decode.int)
133 |> required "id" (Json.Decode.int)
135jsonEncSubscription_UpdatePassword : Subscription_UpdatePassword -> Value
136jsonEncSubscription_UpdatePassword val =
137 Json.Encode.object
138 [ ("foo", Json.Encode.int val.foo)
139 , ("id", Json.Encode.int val.id)
140 ]
144type Subscription =
145 DownloadPasswords
146 | UpdatePassword Subscription_UpdatePassword
148jsonDecSubscription : Json.Decode.Decoder ( Subscription )
149jsonDecSubscription =
150 let jsonDecDictSubscription = Dict.fromList
151 [ ("DownloadPasswords", Json.Decode.lazy (\_ -> Json.Decode.succeed DownloadPasswords))
152 , ("UpdatePassword", Json.Decode.lazy (\_ -> Json.Decode.map UpdatePassword (jsonDecSubscription_UpdatePassword)))
153 ]
154 in decodeSumObjectWithSingleField "Subscription" jsonDecDictSubscription
156jsonEncSubscription : Subscription -> Value
157jsonEncSubscription val =
158 let keyval v = case v of
159 DownloadPasswords -> ("DownloadPasswords", encodeValue (Json.Encode.list identity []))
160 UpdatePassword v1 -> ("UpdatePassword", encodeValue (jsonEncSubscription_UpdatePassword v1))
161 in encodeSumObjectWithSingleField keyval val
162```