golang

package
v0.0.0-...-273afdd Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2018 License: MIT Imports: 7 Imported by: 2

Documentation

Overview

Package golang generates go types including validations.

Parse a jsonschema.Index into a "main" package including imports, e.g. for saving to a file:

schema := []byte(`...`)
idx, err := jsonschema.Parse(schema)
if err != nil {
	panic(err)
}

src, err := PackageSrc(idx, "main")
if err != nil {
	panic(err)
}

fmt.Printf("%s", src)

A schema definition named user:

schema := []byte(`{
  "definitions": {
    "user": {
      "type": "object",
      "properties": {
        "id": { "type": "string" },
        "name": { "type": "string" },
      },
      "required": ["id"]
    }
}`)

idx, err := jsonschema.Parse(schema)
if err != nil {
	panic(err)
}

src, err := Src(idx, "main")
if err != nil {
	panic(err)
}

Results in a User type with a Validate method:

type User struct {
	ID    *string `json:"id,omitempty"`
	Name  *string `json:"name,omitempty"`
}

func (t *User) Validate() error {
	if t.ID == nil {
		return errors.New("invalid user: missing id")
	}

	return nil
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Imports

func Imports(src []byte) []string

Returns a list of required imports

func PackageSrc

func PackageSrc(idx *jsonschema.Index, pack string) ([]byte, error)

Generates go src for a package including imports and package

Example

Generate a complete go package with all types and validations

schema := `
{
	"definitions": {
		"user": {
			"type": "object",
			"properties": {
				"id": {
					"type": "string"
				},
				"name": {
					"type": "string"
				},
				"roles": {
					"$ref": "#/definitions/roles"
				}
			}
		},
		"roles": {
			"type": "array",
			"items": {
				"$ref": "#/definitions/role"
			}
		},
		"role": {
			"type": "object",
			"properties": {
				"name": {
					"type": "string"
				}
			},
			"required": ["name"]
		}
	}
}
`
// parse into index
idx, err := jsonschema.Parse([]byte(schema))
if err != nil {
	panic(err)
}

// generate package source
src, err := PackageSrc(idx, "main")
if err != nil {
	panic(err)
}

fmt.Printf("%s", src)
Output:

package main

import (
	"errors"
)

type Role struct {
	Name *string `json:"name,omitempty"`
}

type Roles []Role

type User struct {
	ID    *string `json:"id,omitempty"`
	Name  *string `json:"name,omitempty"`
	Roles *Roles  `json:"roles,omitempty"`
}

func (t *Role) Validate() error {
	if t.Name == nil {
		return errors.New("invalid role: missing name")
	}

	return nil
}

func (t *Roles) Validate() error {
	for _, a := range *t {
		err := a.Validate()
		if err != nil {
			return err
		}
	}
	return nil
}

func (t *User) Validate() error {
	err := t.Roles.Validate()
	if err != nil {
		return err
	}
	return nil
}

func newString(s string) *string {
	return &s
}

func newInt(i int) *int {
	return &i
}

func newFloat(f float64) *float64 {
	return &f
}

func newBool(b bool) *bool {
	return &b
}

func Src

func Src(idx *jsonschema.Index) ([]byte, error)

Generates go src from an jsonschema.Index without imports and package

Example

Generate source with all types and validations but no imports and package

schema := `
{
	"definitions": {
		"user": {
			"type": "object",
			"properties": {
				"id": {
					"type": "string"
				},
				"name": {
					"type": "string"
				},
				"roles": {
					"$ref": "#/definitions/roles"
				}
			}
		},
		"roles": {
			"type": "array",
			"items": {
				"$ref": "#/definitions/role"
			}
		},
		"role": {
			"type": "object",
			"properties": {
				"name": {
					"type": "string"
				}
			},
			"required": ["name"]
		}
	}
}
`
// parse into index
idx, err := jsonschema.Parse([]byte(schema))
if err != nil {
	panic(err)
}

// generate source
src, err := Src(idx)
if err != nil {
	panic(err)
}

fmt.Printf("%s", src)
Output:

type Role struct {
	Name *string `json:"name,omitempty"`
}

type Roles []Role

type User struct {
	ID    *string `json:"id,omitempty"`
	Name  *string `json:"name,omitempty"`
	Roles *Roles  `json:"roles,omitempty"`
}

func (t *Role) Validate() error {
	if t.Name == nil {
		return errors.New("invalid role: missing name")
	}

	return nil
}

func (t *Roles) Validate() error {
	for _, a := range *t {
		err := a.Validate()
		if err != nil {
			return err
		}
	}
	return nil
}

func (t *User) Validate() error {
	err := t.Roles.Validate()
	if err != nil {
		return err
	}
	return nil
}

func newString(s string) *string {
	return &s
}

func newInt(i int) *int {
	return &i
}

func newFloat(f float64) *float64 {
	return &f
}

func newBool(b bool) *bool {
	return &b
}

Types

This section is empty.

Jump to

Keyboard shortcuts

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