rdeusser / slipscheme

Json Schema to Go Struct conversion tool

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Build Status

slipscheme

Simple tool to convert JSON schemas to Go types

Download

Download the binaries from the latest release.

You can also run it from super-minimal docker image as well (only 6M). It would be run like:

docker run -i --rm -v $(pwd):/work rdeusser/slipscheme:latest

Usage

Usage of slipscheme:
  -dir string
        output directory for go files (default ".")
  -fmt
        pass code through gofmt (default true)
  -overwrite
        force overwriting existing go files
  -pkg string
        package namespace for go files (default "main")
  -stdout
        print go code to stdout rather than files

By default slipscheme will print go code to a file, one per type.

Example

See the sample.json file checked into git and this is what slipscheme will generate:

$ ./slipscheme -stdout sample.json
type Address struct {
        City    string `json:"city,omitempty" yaml:"city,omitempty"`
        State   string `json:"state,omitempty" yaml:"state,omitempty"`
        Street  string `json:"street,omitempty" yaml:"street,omitempty"`
        Zipcode int    `json:"zipcode,omitempty" yaml:"zipcode,omitempty"`
}

type Phone struct {
        AreaCode    string `json:"area-code,omitempty" yaml:"area-code,omitempty"`
        CountryCode string `json:"country-code,omitempty" yaml:"country-code,omitempty"`
        Number      string `json:"number,omitempty" yaml:"number,omitempty"`
}

type Phones []*Phone

type Contact struct {
        EmailAddress []string `json:"email-address,omitempty" yaml:"email-address,omitempty"`
        HomeAddress  *Address `json:"home-address,omitempty" yaml:"home-address,omitempty"`
        Name         string   `json:"name,omitempty" yaml:"name,omitempty"`
        Phone        Phones   `json:"phone,omitempty" yaml:"phone,omitempty"`
        WorkAddress  *Address `json:"work-address,omitempty" yaml:"work-address,omitempty"`
}

Otherwise, you can run slipscheme to generate the type files:

$ ./slipscheme -dir out sample.json

$ ls -1 out
Address.go
Contact.go
Phone.go
Phones.go

$ cat out/Phone.go
package test

/////////////////////////////////////////////////////////////////////////
// This Code is Generated by SlipScheme Project:
// https://github.com/rdeusser/slipscheme
//
// Generated with command: ./slipscheme -dir out -pkg test sample.json
/////////////////////////////////////////////////////////////////////////
//                            DO NOT EDIT                              //
/////////////////////////////////////////////////////////////////////////

type Phone struct {
        AreaCode    string `json:"area-code,omitempty" yaml:"area-code,omitempty"`
        CountryCode string `json:"country-code,omitempty" yaml:"country-code,omitempty"`
        Number      string `json:"number,omitempty" yaml:"number,omitempty"`
}

About

Json Schema to Go Struct conversion tool

License:Apache License 2.0


Languages

Language:Go 90.5%Language:Makefile 8.1%Language:Dockerfile 1.5%