Giter Club home page Giter Club logo

generate's Introduction

Build Status

generate

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

Differences between original lib

Rendering enums

This

{
  "type": "object",
  "required": [
    "currencyCode"
  ],
  "properties": {
    "currencyCode": {
      "type": "string",
      "enum": [
        "PLN",
        "USD",
        "EUR"
      ]
    }
  },
  "$schema": "http://json-schema.org/draft-07/schema#"
}

Will generate code

// EnumCurrencyCode
type EnumCurrencyCode string
const (
    EnumCurrencyCodeEUR EnumCurrencyCode = "EUR"
    EnumCurrencyCodePLN EnumCurrencyCode = "PLN"
    EnumCurrencyCodeUSD EnumCurrencyCode = "USD"
)

// Root 
type Root struct {
    CurrencyCode EnumCurrencyCode `json:"currencyCode" valid:"required"`
}

Required and not required field rendering

All fields that are not required are treated as nullable, since its value can be undefined.

If given field is an array item or map value then its value is always required (unless it is nullable). Since value in map or array cannot be undefined.

Objects rendering

Objects are rendered as value unless they are not required or nullable.

OneOf rendering

If oneOf consists of two values and one of them is null then type is rendered with pointer.

If oneOf consists of more than two values then it is rendered as interface{}, but all types if not primitive will be created.

Requirements

  • Go 1.13+

Usage

Install

$ go get -u github.com/michalq/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.

generate's People

Contributors

a-h avatar mwlazlo avatar antoineco avatar gidsi avatar andy-miracl avatar sqs avatar merlincox avatar nicovogelaar avatar yvesf avatar

Watchers

James Cloos avatar

Forkers

kernle32dll

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.