Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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.

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 main

import (
"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 Dp

type 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
```