Skip to content

Generates Go (golang) Structs from JSON schema.

License

Notifications You must be signed in to change notification settings

azarc-io/json-schema-to-go-struct-generator

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

97 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

json-schema-to-go-struct-generator

build-test golangci-lint

A library to generate go models from given json files

A test image

Requirements

  • Go 1.17+

Usage

Install

$ go get -u github.com/azarc-io/json-schema-to-go-struct-generator

or

Example

This schema

{
  "$schema": "http:https://json-schema.org/draft-04/schema#",
  "title": "Example",
  "id": "http:https://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.

Running Tests

In order to run the tests, you must first generate the sample outputs which will produce the code required to compile and run the tests.

go generate github.com/azarc-io/json-schema-to-go-struct-generator/test
go test ./test/...

About

Generates Go (golang) Structs from JSON schema.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Languages

  • Go 98.8%
  • Makefile 1.2%