generate

package module
v0.0.0-...-2a35d6f Latest Latest
Warning

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

Go to latest
Published: Dec 21, 2017 License: MIT Imports: 8 Imported by: 0

README

generate

Generates Go (golang) Structs from JSON schema.

Requirements

  • Go 1.8+

Examples

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "Example",
  "id": "Example",
  "type": "object",
  "description": "example",
  "definitions": {
    "address": {
      "id": "address",
      "type": "object",
      "description": "Address",
      "properties": {
        "houseName": {
          "type": "string",
          "description": "House Name",
          "maxLength": 30
        },
        "houseNumber": {
          "type": "string",
          "description": "House Number",
          "maxLength": 4
        },
        "flatNumber": {
          "type": "string",
          "description": "Flat",
          "maxLength": 15
        },
        "street": {
          "type": "string",
          "description": "Address 1",
          "maxLength": 40
        },
        "district": {
          "type": "string",
          "description": "Address 2",
          "maxLength": 30
        },
        "town": {
          "type": "string",
          "description": "City",
          "maxLength": 20
        },
        "county": {
          "type": "string",
          "description": "County",
          "maxLength": 20
        },
        "postcode": {
          "type": "string",
          "description": "Postcode",
          "maxLength": 8
        }
      }
    },
    "status": {
      "type": "object",
      "properties": {
        "favouritecat": {
          "enum": [
            "A",
            "B",
            "C",
            "D",
            "E",
            "F"
          ],
          "type": "string",
          "description": "The favourite cat.",
          "maxLength": 1
        }
      }
    }
  },
  "properties": {
    "name": {
      "type": "string"
    },
    "address": {
      "$ref": "#/definitions/address"
    },
    "status": {
      "$ref": "#/definitions/status"
    }
  }
}

Run this:

go run main.go exampleschema.json

Get this:

package main

type Address struct {
  County string `json:"county,omitempty"`
  District string `json:"district,omitempty"`
  FlatNumber string `json:"flatNumber,omitempty"`
  HouseName string `json:"houseName,omitempty"`
  HouseNumber string `json:"houseNumber,omitempty"`
  Postcode string `json:"postcode,omitempty"`
  Street string `json:"street,omitempty"`
  Town string `json:"town,omitempty"`
}

type Example struct {
  Address *Address `json:"address,omitempty"`
  Name string `json:"name,omitempty"`
  Status *Status `json:"status,omitempty"`
}

type Status struct {
  Favouritecat string `json:"favouritecat,omitempty"`
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Field

type Field struct {
	// The golang name, e.g. "Address1"
	Name string
	// The JSON name, e.g. "address1"
	JSONName string
	// The golang type of the field, e.g. a built-in type like "string" or the name of a struct generated from the JSON schema.
	Type string
	// Required is set to true when the field is required.
	Required bool
}

Field defines the data required to generate a field in Go.

type Generator

type Generator struct {
	// contains filtered or unexported fields
}

Generator will produce structs from the JSON schema.

func New

func New(schemas ...*jsonschema.Schema) *Generator

New creates an instance of a generator which will produce structs.

func (*Generator) CreateStructs

func (g *Generator) CreateStructs() (structs map[string]Struct, err error)

CreateStructs creates structs from the JSON schemas, keyed by the golang name.

type Struct

type Struct struct {
	// The ID within the JSON schema, e.g. #/definitions/address
	ID string
	// The golang name, e.g. "Address"
	Name string
	// Description of the struct
	Description string
	Fields      map[string]Field
}

Struct defines the data required to generate a struct in Go.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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