schema

package
v0.4.8 Latest Latest
Warning

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

Go to latest
Published: Dec 21, 2020 License: Apache-2.0 Imports: 18 Imported by: 6

README

About

Go library to work with jxs schema from Go. The library basically does two things

  • Implement all the native schema/jsx types (string, numeric, date, etc...)
  • Generate Go types from schema files. The types later can be used to do calls to services that expect schema objects.

Reference

Schema docs is defined here.

How to use

Please check example here

Documentation

Index

Examples

Constants

View Source
const (
	// SchemaQual path to schema package
	SchemaQual = "github.com/threefoldtech/tfexplorer/schema"
)
View Source
const (
	// URL directive
	URL = "url"
)

Variables

This section is empty.

Functions

func GenerateGolang

func GenerateGolang(w io.Writer, pkg string, schema Schema) error

GenerateGolang generate type stubs for Go from schema

Example
const input = `
@url =  jumpscale.digitalme.package
name = "UNKNOWN" (S)    #official name of the package, there can be no overlap (can be dot notation)
enable = true (B)
numerics = (LN)
args = (LO) !jumpscale.digitalme.package.arg
loaders= (LO) !jumpscale.digitalme.package.loader

@url =  jumpscale.digitalme.package.arg
key = "" (S)
val =  "" (S)

@url =  jumpscale.digitalme.package.loader
giturl =  (S)
dest =  (S)
enable = true (B)
creation = (D)
	`

schema, err := New(strings.NewReader(input))
if err != nil {
	panic(err)
}

if err := GenerateGolang(os.Stdout, "test", schema); err != nil {
	panic(err)
}
Output:

package test

import (
	"encoding/json"
	schema "github.com/threefoldtech/tfexplorer/schema"
)

type JumpscaleDigitalmePackage struct {
	ID       schema.ID                         `bson:"_id" json:"id"`
	Name     string                            `bson:"name" json:"name"`
	Enable   bool                              `bson:"enable" json:"enable"`
	Numerics []schema.Numeric                  `bson:"numerics" json:"numerics"`
	Args     []JumpscaleDigitalmePackageArg    `bson:"args" json:"args"`
	Loaders  []JumpscaleDigitalmePackageLoader `bson:"loaders" json:"loaders"`
}

func NewJumpscaleDigitalmePackage() (JumpscaleDigitalmePackage, error) {
	const value = "{\"name\": \"UNKNOWN\", \"enable\": true}"
	var object JumpscaleDigitalmePackage
	if err := json.Unmarshal([]byte(value), &object); err != nil {
		return object, err
	}
	return object, nil
}

type JumpscaleDigitalmePackageArg struct {
	Key string `bson:"key" json:"key"`
	Val string `bson:"val" json:"val"`
}

func NewJumpscaleDigitalmePackageArg() (JumpscaleDigitalmePackageArg, error) {
	const value = "{\"key\": \"\", \"val\": \"\"}"
	var object JumpscaleDigitalmePackageArg
	if err := json.Unmarshal([]byte(value), &object); err != nil {
		return object, err
	}
	return object, nil
}

type JumpscaleDigitalmePackageLoader struct {
	Giturl   string      `bson:"giturl" json:"giturl"`
	Dest     string      `bson:"dest" json:"dest"`
	Enable   bool        `bson:"enable" json:"enable"`
	Creation schema.Date `bson:"creation" json:"creation"`
}

func NewJumpscaleDigitalmePackageLoader() (JumpscaleDigitalmePackageLoader, error) {
	const value = "{\"enable\": true}"
	var object JumpscaleDigitalmePackageLoader
	if err := json.Unmarshal([]byte(value), &object); err != nil {
		return object, err
	}
	return object, nil
}
Example (Dict)
const input = `
@url =  parent
name = (S)    #official name of the package, there can be no overlap (can be dot notation)
data = (dictO) ! child # dict of children
tags = (M) # dict with no defined object type

@url = child
name = (S)
	`

schema, err := New(strings.NewReader(input))
if err != nil {
	panic(err)
}

if err := GenerateGolang(os.Stdout, "test", schema); err != nil {
	panic(err)
}
Output:

package test

import (
	"encoding/json"
	schema "github.com/threefoldtech/tfexplorer/schema"
)

type Parent struct {
	ID   schema.ID              `bson:"_id" json:"id"`
	Name string                 `bson:"name" json:"name"`
	Data map[string]Child       `bson:"data" json:"data"`
	Tags map[string]interface{} `bson:"tags" json:"tags"`
}

func NewParent() (Parent, error) {
	const value = "{}"
	var object Parent
	if err := json.Unmarshal([]byte(value), &object); err != nil {
		return object, err
	}
	return object, nil
}

type Child struct {
	Name string `bson:"name" json:"name"`
}

func NewChild() (Child, error) {
	const value = "{}"
	var object Child
	if err := json.Unmarshal([]byte(value), &object); err != nil {
		return object, err
	}
	return object, nil
}
Example (Enums)
const input = `
@url =  person
name = "UNKNOWN" (S)    #official name of the package, there can be no overlap (can be dot notation)
gender = "male,female,others" (E)
	`

schema, err := New(strings.NewReader(input))
if err != nil {
	panic(err)
}

if err := GenerateGolang(os.Stdout, "test", schema); err != nil {
	panic(err)
}
Output:

package test

import (
	"encoding/json"
	schema "github.com/threefoldtech/tfexplorer/schema"
)

type Person struct {
	ID     schema.ID        `bson:"_id" json:"id"`
	Name   string           `bson:"name" json:"name"`
	Gender PersonGenderEnum `bson:"gender" json:"gender"`
}

func NewPerson() (Person, error) {
	const value = "{\"name\": \"UNKNOWN\"}"
	var object Person
	if err := json.Unmarshal([]byte(value), &object); err != nil {
		return object, err
	}
	return object, nil
}

type PersonGenderEnum uint8

const (
	PersonGenderMale PersonGenderEnum = iota
	PersonGenderFemale
	PersonGenderOthers
)

func (e PersonGenderEnum) String() string {
	switch e {
	case PersonGenderMale:
		return "male"
	case PersonGenderFemale:
		return "female"
	case PersonGenderOthers:
		return "others"
	}
	return "UNKNOWN"
}
Example (Enums2)
const input = `

@url = tfgrid.node.resource.price.1
cru = (F)

mru = (F)
hru = (F)
sru = (F)
nru = (F)
currency = "EUR,USD,TFT,AED,GBP" (E)
	`

schema, err := New(strings.NewReader(input))
if err != nil {
	panic(err)
}

if err := GenerateGolang(os.Stdout, "test", schema); err != nil {
	panic(err)
}
Output:

package test

import (
	"encoding/json"
	schema "github.com/threefoldtech/tfexplorer/schema"
)

type TfgridNodeResourcePrice1 struct {
	ID       schema.ID                            `bson:"_id" json:"id"`
	Cru      float64                              `bson:"cru" json:"cru"`
	Mru      float64                              `bson:"mru" json:"mru"`
	Hru      float64                              `bson:"hru" json:"hru"`
	Sru      float64                              `bson:"sru" json:"sru"`
	Nru      float64                              `bson:"nru" json:"nru"`
	Currency TfgridNodeResourcePrice1CurrencyEnum `bson:"currency" json:"currency"`
}

func NewTfgridNodeResourcePrice1() (TfgridNodeResourcePrice1, error) {
	const value = "{}"
	var object TfgridNodeResourcePrice1
	if err := json.Unmarshal([]byte(value), &object); err != nil {
		return object, err
	}
	return object, nil
}

type TfgridNodeResourcePrice1CurrencyEnum uint8

const (
	TfgridNodeResourcePrice1CurrencyEUR TfgridNodeResourcePrice1CurrencyEnum = iota
	TfgridNodeResourcePrice1CurrencyUSD
	TfgridNodeResourcePrice1CurrencyTFT
	TfgridNodeResourcePrice1CurrencyAED
	TfgridNodeResourcePrice1CurrencyGBP
)

func (e TfgridNodeResourcePrice1CurrencyEnum) String() string {
	switch e {
	case TfgridNodeResourcePrice1CurrencyEUR:
		return "EUR"
	case TfgridNodeResourcePrice1CurrencyUSD:
		return "USD"
	case TfgridNodeResourcePrice1CurrencyTFT:
		return "TFT"
	case TfgridNodeResourcePrice1CurrencyAED:
		return "AED"
	case TfgridNodeResourcePrice1CurrencyGBP:
		return "GBP"
	}
	return "UNKNOWN"
}
Example (Ip)
const input = `
@url =  network
name = "UNKNOWN" (S)    #official name of the package, there can be no overlap (can be dot notation)
ip = "172.0.0.1" (ipaddr)
net = "2001:db8::/32" (iprange)
addresses = (Lipaddr)
	`

schema, err := New(strings.NewReader(input))
if err != nil {
	panic(err)
}

if err := GenerateGolang(os.Stdout, "test", schema); err != nil {
	panic(err)
}
Output:

package test

import (
	"encoding/json"
	schema "github.com/threefoldtech/tfexplorer/schema"
	"net"
)

type Network struct {
	ID        schema.ID      `bson:"_id" json:"id"`
	Name      string         `bson:"name" json:"name"`
	Ip        net.IP         `bson:"ip" json:"ip"`
	Net       schema.IPRange `bson:"net" json:"net"`
	Addresses []net.IP       `bson:"addresses" json:"addresses"`
}

func NewNetwork() (Network, error) {
	const value = "{\"name\": \"UNKNOWN\", \"ip\": \"172.0.0.1\", \"net\": \"2001:db8::/32\"}"
	var object Network
	if err := json.Unmarshal([]byte(value), &object); err != nil {
		return object, err
	}
	return object, nil
}

Types

type Date

type Date struct{ time.Time }

Date a jumpscale date wrapper

func (Date) MarshalJSON

func (d Date) MarshalJSON() ([]byte, error)

MarshalJSON formats a text

func (Date) String

func (d Date) String() ([]byte, error)

String implements stringer interface

func (*Date) UnmarshalJSON

func (d *Date) UnmarshalJSON(bytes []byte) error

UnmarshalJSON method

type Directive

type Directive struct {
	Key   string
	Value string
}

Directive is a piece of information attached to a schema object

func (*Directive) String

func (d *Directive) String() string

type Email

type Email string

Email type

func (*Email) UnmarshalText

func (e *Email) UnmarshalText(text []byte) error

UnmarshalText loads IPRange from string

type ID

type ID int64

ID object id

type IP added in v0.4.8

type IP struct{ net.IP }

IP schema type

func (IP) MarshalBSONValue added in v0.4.8

func (i IP) MarshalBSONValue() (bsontype.Type, []byte, error)

MarshalBSONValue dumps ip as a bson

func (*IP) UnmarshalBSONValue added in v0.4.8

func (i *IP) UnmarshalBSONValue(t bsontype.Type, bytes []byte) error

UnmarshalBSONValue loads IP from bson

type IPCidr added in v0.4.8

type IPCidr struct{ net.IPNet }

IPCidr type is improved version of IPRange which can marshal/unmarshal itself to BSON for database readability

func MustParseIPCidr added in v0.4.8

func MustParseIPCidr(txt string) IPCidr

MustParseIPCidr prases ipcidr, panics if invalid

func ParseIPCidr added in v0.4.8

func ParseIPCidr(txt string) (r IPCidr, err error)

ParseIPCidr parse iprange

func (IPCidr) MarshalBSONValue added in v0.4.8

func (i IPCidr) MarshalBSONValue() (bsontype.Type, []byte, error)

MarshalBSONValue dumps ip as a bson

func (IPCidr) MarshalJSON added in v0.4.8

func (i IPCidr) MarshalJSON() ([]byte, error)

MarshalJSON dumps iprange as a string

func (IPCidr) String added in v0.4.8

func (i IPCidr) String() string

func (*IPCidr) UnmarshalBSONValue added in v0.4.8

func (i *IPCidr) UnmarshalBSONValue(t bsontype.Type, bytes []byte) error

UnmarshalBSONValue loads IP from bson

func (*IPCidr) UnmarshalText added in v0.4.8

func (i *IPCidr) UnmarshalText(text []byte) error

UnmarshalText loads IPRange from string

func (*IPCidr) Validate added in v0.4.8

func (i *IPCidr) Validate() (IPCidr, error)

Validate validates the IPCidr range and returns a corrected copy, or error

type IPRange

type IPRange struct{ net.IPNet }

IPRange type (deprecated, please check IPCidr type) this is kept for backward compatibility for objects that are already in the database

func MustParseIPRange

func MustParseIPRange(txt string) IPRange

MustParseIPRange prases iprange, panics if invalid

func ParseIPRange

func ParseIPRange(txt string) (r IPRange, err error)

ParseIPRange parse iprange

func (IPRange) MarshalJSON

func (i IPRange) MarshalJSON() ([]byte, error)

MarshalJSON dumps iprange as a string

func (IPRange) String

func (i IPRange) String() string

func (*IPRange) UnmarshalText

func (i *IPRange) UnmarshalText(text []byte) error

UnmarshalText loads IPRange from string

type Kind

type Kind int

Kind defines the kind of a type

const (
	UnknownKind Kind = iota
	IntegerKind
	FloatKind
	BoolKind
	StringKind
	MobileKind
	EmailKind
	IPPortKind
	IPAddressKind
	IPRangeKind
	DateKind
	DateTimeKind
	NumericKind
	GUIDKind
	DictKind
	ListKind
	ObjectKind
	YamlKind
	MultilineKind
	HashKind
	BytesKind
	PercentKind
	URLKind
	EnumKind
)

List of all the supported JSX types

func (Kind) String

func (k Kind) String() string

type MacAddress

type MacAddress struct{ net.HardwareAddr }

MacAddress type

func (MacAddress) MarshalText

func (mac MacAddress) MarshalText() ([]byte, error)

MarshalText marshals MacAddress type to a string

func (*MacAddress) UnmarshalText

func (mac *MacAddress) UnmarshalText(addr []byte) error

UnmarshalText loads a macaddress from a string

type Numeric

type Numeric string

Numeric type. this type is tricky so we just going to handle it as string for now.

func (Numeric) BigInt

func (n Numeric) BigInt() (*big.Int, error)

BigInt returns parsed big.Int value

func (Numeric) Float64

func (n Numeric) Float64() (float64, error)

Float64 returns parsed float64 value

func (*Numeric) UnmarshalJSON

func (n *Numeric) UnmarshalJSON(in []byte) error

UnmarshalJSON method

type Object

type Object struct {
	URL        string
	IsRoot     bool
	Directives []Directive
	Properties []Property
}

Object defines a schema object

func (*Object) Default

func (o *Object) Default() string

Default generates a json string that holds the default value for the object

type Property

type Property struct {
	Name    string
	Indexed bool
	Type    Type
}

Property defines a schema property

type Schema

type Schema []*Object

Schema is a container for all objects defined in the source

func New

func New(r io.Reader) (schema Schema, err error)

New reads the schema and return schema description objects

type Type

type Type struct {
	Kind      Kind
	Default   string
	Reference string
	Element   *Type
}

Type holds type information for a property

Jump to

Keyboard shortcuts

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