gostruct

command module
v0.0.0-...-12bb77f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 19, 2022 License: MIT Imports: 9 Imported by: 0

README

gostruct: Convert example JSON objects to Go Struct definitions

This is an imperfect but handy little tool to quickly convert JSON objects into Go struct definitions. For example:

{
  "id": "623f6018-6cf0-431e-84a5-43aeb8be4372",
  "extra_data": null,
  "created": 1405637075
}

becomes

type NewObject struct {
        Id              string `json:"id"`
        ExtraData               interface{} `json:"extra_data"`
        Created         float64 `json:"created"`
}

Installation

Haven't done a release yet, so for now just run go install -v github.com/seanph/gostruct@latest.

Usage

gostruct can be used via either piped input or direct input.

Pipe

echo '{"id": 5, "name": "test"}' | gostruct

Direct

gostruct --dump

Then paste in the JSON data and hit Ctrl+D to signal EOF.

Caveats

  • Currently only supports map[string]interface{} shaped JSON objects. Arrays or alternative maps won't work.
  • Currently outputs ints and floats as float64 as this is the default Go behaviour. Worth manually checking back over these!
  • Formatting isn't great, but your editor should fix this.

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL