GitXplorerGitXplorer
j

go-to-elm-json

public
5 stars
0 forks
4 issues

Commits

List of commits on branch main.
Verified
d73de098b748fd7410577617e7abe2510e5cdcc3

rename structFromPackage, no logger timestamps (#12)

jjhillyerd committed a year ago
Verified
1ae19d9f94cb8d14438b05e0b5e0160b95eac611

Switch from deprecated x/tools/go/loader to x/tools/go/packages (#10)

jjhillyerd committed a year ago
Verified
459e190c08382af859a50571ff335a1570ab594b

ci: update lint workflow (#11)

jjhillyerd committed a year ago
Verified
ee3ee862ee54800fc7f8619b9382af1949437ec5

go: update deps (#8)

jjhillyerd committed a year ago
Verified
f3bb97225815dfc80b4dbe08a5d3a038d41a5176

README: update install command (#9)

jjhillyerd committed a year ago
Verified
189c456adb275a5327d2d11fed645e1198c8fbb0

README: update badges (#7)

jjhillyerd committed a year ago

README

The README file for this repository.

go-to-elm-json

Build and Test Coverage Status

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

Status

Useful, but not feature complete.

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

Install

go install 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.