Convert a Go struct to an Elm record via JSON decoder pipeline.
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
testdata
.gitignore
.travis.yml
LICENSE
Makefile
README.md
abbrev.go
abbrev_test.go
elm_util.go
elm_util_test.go
go.mod
go.sum
main.go
main_test.go
record.go
record_test.go
tags.go
tags_test.go
template.go
types.go

README.md

go-to-elm-json

Build Status Coverage Status

A tool to create Elm JSON decoder pipelines from Go struct type definitions.

Status

Useful, but not feature complete.

  • Support basic types: string, int, float, bool
  • CamelCase common acronyms
  • Generate Elm record
  • Generate decoder pipeline
  • Generate encoder
  • Usage example in README
  • Support slice form of basic types
  • Support for optional fields
  • Support nested structs
  • Support nullable pointer types
  • Allow records to be renamed
  • Handle json:"-" correctly
  • Specify module name
  • Support for string-keyed basic type maps

Install

go get github.com/jhillyerd/go-to-elm-json

Usage

go-to-elm-json <go source files> -- <package> <go type:elm name>

Example

Given the file foo/bar.go containing:

package foo

type UserJSON struct {
	Name    string   `json:"name"`
	UserID  int      `json:"userID"`
	Friends []string `json:"friends"`
	Enabled bool     `json:"enabled"`
}

Running: go-to-elm-json foo/*.go -- foo UserJSON:User will output:

module User exposing (User, decoder, encode)

import Json.Decode as D
import Json.Decode.Pipeline as P
import Json.Encode as E


type alias User =
    { name : String
    , userId : Int
    , friends : List String
    , enabled : Bool
    }


decoder : D.Decoder User
decoder =
    D.succeed User
        |> P.required "name" D.string
        |> P.required "userID" D.int
        |> P.required "friends" (D.list D.string)
        |> P.required "enabled" D.bool


encode : User -> E.Value
encode r =
    E.object
        [ ("name", E.string r.name)
        , ("userID", E.int r.userId)
        , ("friends", (E.list E.string) r.friends)
        , ("enabled", E.bool r.enabled)
        ]

Contributing

PRs welcome, please:

  • Base your work off of the development branch, and target pull requests to the same.
  • Run the unit tests before filing a PR. make will run tests and lint.
  • Include unit tests for your changes.