Categorygithub.com/orus-io/json-schema-generate
modulepackage
0.0.0-20220509203042-a948615b5683
Repository: https://github.com/orus-io/json-schema-generate.git
Documentation: pkg.go.dev

# README

generate

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

Requirements

  • Go 1.8+

Usage

Install

$ go get -u github.com/orus-io/json-schema-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.

# Packages

No description provided by the author

# Functions

New creates an instance of a generator which will produce structs.
NewRefResolver creates a reference resolver.
Output generates code and writes to w.
Parse parses a JSON schema from a string.
ParseWithSchemaKeyRequired parses a JSON schema from a string with a flag to set whether the schema key is required.
ReadInputFiles from disk and convert to JSON schema.

# Structs

Field defines the data required to generate a field in Go.
Generator will produce structs from the JSON schema.
OneOf is a generated type for holding a oneOf field data.
OneOfType is a type in a OneOf.
OutputData contains all the data necessary for the template.
RefResolver allows references to be resolved.
Schema represents JSON schema.
Struct defines the data required to generate a struct in Go.

# Type aliases

AdditionalProperties handles additional properties present in the JSON schema.