Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ilyakooo0/elm-ps-bridge
🌉 A PureScript-Elm bridge generator
https://github.com/ilyakooo0/elm-ps-bridge
elm json purescript
Last synced: 10 days ago
JSON representation
🌉 A PureScript-Elm bridge generator
- Host: GitHub
- URL: https://github.com/ilyakooo0/elm-ps-bridge
- Owner: ilyakooo0
- License: bsd-3-clause
- Created: 2022-07-22T16:17:29.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2022-09-11T08:20:41.000Z (over 2 years ago)
- Last Synced: 2024-10-12T01:01:21.884Z (2 months ago)
- Topics: elm, json, purescript
- Language: Haskell
- Homepage:
- Size: 23.4 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Elm PS Bridge
A 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.
## Example
```bash
elm-ps-bridge --input example.yaml --elmOutput Bridge.elm --psOutput Bridge.purs
```### `example.yaml`
```yaml
Subscription:
- DownloadPasswords
- UpdatePassword:
foo: Int
id: Int
Command:
- Hello
```### `Bridge.purs`
```purescript
-- File auto generated by purescript-bridge! --
module Bridge whereimport Data.Generic.Rep (class Generic)
import Data.Lens (Iso', Lens', Prism', lens, prism')
import Data.Lens.Iso.Newtype (_Newtype)
import Data.Lens.Record (prop)
import Data.Maybe (Maybe(..))
import Data.Newtype (class Newtype)
import Data.Symbol (SProxy(SProxy))import Prelude
data Command =
Helloderive instance genericCommand :: Generic Command _
derive instance newtypeCommand :: Newtype Command _
derive instance eqCommand :: Eq Command
derive instance ordCommand :: Ord Command--------------------------------------------------------------------------------
_Hello :: Prism' Command Unit
_Hello = prism' (\_ -> Hello) f
where
f Hello = Just unit--------------------------------------------------------------------------------
newtype Subscription_UpdatePassword =
Subscription_UpdatePassword {
foo :: Int
, id :: Int
}derive instance genericSubscription_UpdatePassword :: Generic Subscription_UpdatePassword _
derive instance newtypeSubscription_UpdatePassword :: Newtype Subscription_UpdatePassword _
derive instance eqSubscription_UpdatePassword :: Eq Subscription_UpdatePassword
derive instance ordSubscription_UpdatePassword :: Ord Subscription_UpdatePassword--------------------------------------------------------------------------------
_Subscription_UpdatePassword :: Iso' Subscription_UpdatePassword { foo :: Int, id :: Int}
_Subscription_UpdatePassword = _Newtype--------------------------------------------------------------------------------
data Subscription =
DownloadPasswords
| UpdatePassword Subscription_UpdatePasswordderive instance genericSubscription :: Generic Subscription _
derive instance newtypeSubscription :: Newtype Subscription _
derive instance eqSubscription :: Eq Subscription
derive instance ordSubscription :: Ord Subscription--------------------------------------------------------------------------------
_DownloadPasswords :: Prism' Subscription Unit
_DownloadPasswords = prism' (\_ -> DownloadPasswords) f
where
f DownloadPasswords = Just unit
f _ = Nothing_UpdatePassword :: Prism' Subscription Subscription_UpdatePassword
_UpdatePassword = prism' UpdatePassword f
where
f (UpdatePassword a) = Just $ a
f _ = Nothing--------------------------------------------------------------------------------
```### `Bridge.elm`
```elm
module Bridge exposing(..)import Json.Decode
import Json.Encode exposing (Value)
-- The following module comes from bartavelle/json-helpers
import Json.Helpers exposing (..)
import Dict exposing (Dict)
import Set exposing (Set)type Command =
HellojsonDecCommand : Json.Decode.Decoder ( Command )
jsonDecCommand =
let jsonDecDictCommand = Dict.fromList [("Hello", Hello)]
in decodeSumUnaries "Command" jsonDecDictCommandjsonEncCommand : Command -> Value
jsonEncCommand val =
case val of
Hello -> Json.Encode.string "Hello"type alias Subscription_UpdatePassword =
{ foo: Int
, id: Int
}jsonDecSubscription_UpdatePassword : Json.Decode.Decoder ( Subscription_UpdatePassword )
jsonDecSubscription_UpdatePassword =
Json.Decode.succeed (\pfoo pid -> {foo = pfoo, id = pid})
|> required "foo" (Json.Decode.int)
|> required "id" (Json.Decode.int)jsonEncSubscription_UpdatePassword : Subscription_UpdatePassword -> Value
jsonEncSubscription_UpdatePassword val =
Json.Encode.object
[ ("foo", Json.Encode.int val.foo)
, ("id", Json.Encode.int val.id)
]type Subscription =
DownloadPasswords
| UpdatePassword Subscription_UpdatePasswordjsonDecSubscription : Json.Decode.Decoder ( Subscription )
jsonDecSubscription =
let jsonDecDictSubscription = Dict.fromList
[ ("DownloadPasswords", Json.Decode.lazy (\_ -> Json.Decode.succeed DownloadPasswords))
, ("UpdatePassword", Json.Decode.lazy (\_ -> Json.Decode.map UpdatePassword (jsonDecSubscription_UpdatePassword)))
]
in decodeSumObjectWithSingleField "Subscription" jsonDecDictSubscriptionjsonEncSubscription : Subscription -> Value
jsonEncSubscription val =
let keyval v = case v of
DownloadPasswords -> ("DownloadPasswords", encodeValue (Json.Encode.list identity []))
UpdatePassword v1 -> ("UpdatePassword", encodeValue (jsonEncSubscription_UpdatePassword v1))
in encodeSumObjectWithSingleField keyval val
```