Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/yitsushi/gelm
An easy way to generate Elm type aliases with decoder functions from a Go struct.
https://github.com/yitsushi/gelm
elm go
Last synced: about 15 hours ago
JSON representation
An easy way to generate Elm type aliases with decoder functions from a Go struct.
- Host: GitHub
- URL: https://github.com/yitsushi/gelm
- Owner: yitsushi
- License: gpl-3.0
- Created: 2022-03-10T15:53:19.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2022-03-10T15:53:25.000Z (almost 3 years ago)
- Last Synced: 2024-06-21T08:07:37.143Z (7 months ago)
- Topics: elm, go
- Language: Go
- Homepage:
- Size: 19.5 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Gelm
An easy way to generate Elm type aliases with decoder functions from a Go
struct.## Why?
When I want to build a web application, my first thing is to build a very basic
Go project, and then add an Elm project to create the UI. When I change the
model on the Go side, I have to update the Elm side as well, but why would I do
it manually if I can simply generate them. So that's the simple explanation why
this library exists.## Usage
```go
package mainimport (
"fmt""github.com/yitsushi/gelm"
)type MyData struct {
Name string `elm:"name"`
Value int64 `elm:"value"`
Flag bool `elm:"flag,optional=True"`
DataSet []int64 `elm:"dataSet"`
Sub SubData `elm:"sub"`
SubList []SubData `elm:"subs"`
}type SubData struct {
Name string `elm:"name"`
}func main() {
output := gelm.Generate("Models.Fancy", MyData{}, SubData{})fmt.Printf("%s", output)
}
```And the output will be:
```elm
-- This file was generated with github.com/yitsushi/gelm
-- Do not edit this file unless you know what you are doing.module Models.Fancy exposing (..)
import Json.Decode as Decode
import Json.Decode.Pipeline as Dptype alias MyData =
{ name : String
, value : Int
, flag : Bool
, dataSet : List Int
, sub : SubData
, subs : List SubData
}type alias SubData =
{ name : String
}decodeMyData : Decode.Decoder MyData
decodeMyData =
Decode.succeed MyData
|> Dp.required "Name" Decode.string
|> Dp.required "Value" Decode.int
|> Dp.optional "Flag" Decode.bool True
|> Dp.required "DataSet" (Decode.list Decode.int)
|> Dp.required "Sub" (Decode.lazy (\_ -> decodeSubData))
|> Dp.required "SubList" (Decode.list (Decode.lazy (\_ -> decodeSubData)))decodeSubData : Decode.Decoder SubData
decodeSubData =
Decode.succeed SubData
|> Dp.required "Name" Decode.string
```