Skip to content

yitsushi/gelm

Repository files navigation

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

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:

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

About

An easy way to generate Elm type aliases with decoder functions from a Go struct.

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published