Need help with generate?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

a-h
295 Stars 94 Forks MIT License 85 Commits 26 Opened issues

Description

Generates Go (golang) Structs from JSON schema.

Services available

!
?

Need anything else?

Contributors list

# 106,257
golang
rabbitm...
c-sharp
gemini
26 commits
# 194,502
Go
golang
codegen
Shell
12 commits
# 767
Go
elk
kibana
logstas...
8 commits
# 222,701
Shell
i3statu...
i3bar
Go
5 commits
# 27,520
Go
vscode
openapi
GraphQL
4 commits
# 400,505
Go
Shell
HTML
saml2
4 commits
# 368,711
Shell
C
jsonsch...
JavaScr...
1 commit
# 221,857
codegen
dataloa...
Shell
GraphQL
1 commit
# 633,443
Go
1 commit

generate

Generates Go (golang) Structs and Validation code from JSON schema.

Requirements

  • Go 1.8+

Usage

Install

$ go get -u github.com/a-h/generate/...

or

Build

$ make

Run

$ schema-generate exampleschema.json

Example

This schema

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "Example",
  "id": "http://example.com/exampleschema.json",
  "type": "object",
  "description": "An example JSON Schema",
  "properties": {
    "name": {
      "type": "string"
    },
    "address": {
      "$ref": "#/definitions/address"
    },
    "status": {
      "$ref": "#/definitions/status"
    }
  },
  "definitions": {
    "address": {
      "id": "address",
      "type": "object",
      "description": "Address",
      "properties": {
        "street": {
          "type": "string",
          "description": "Address 1",
          "maxLength": 40
        },
        "houseNumber": {
          "type": "integer",
          "description": "House Number"
        }
      }
    },
    "status": {
      "type": "object",
      "properties": {
        "favouritecat": {
          "enum": [
            "A",
            "B",
            "C"
          ],
          "type": "string",
          "description": "The favourite cat.",
          "maxLength": 1
        }
      }
    }
  }
}

generates

package main

type Address struct { HouseNumber int json:"houseNumber,omitempty" Street string json:"street,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" }

See the test/ directory for more examples.

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.