schema

package
Version: v0.0.0-...-d65df50 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2016 License: BSD-2-Clause Imports: 27 Imported by: 8

Documentation

Index

Constants

View Source
const (
	Tag        = "db"
	DefaultTag = "default"
)
View Source
const (
	RedisEngine     = "redis"
	MysqlEngine     = "mysql"
	CassandraEngine = "cassandra"
)
View Source
const (
	OptSubType  = "subtype"
	OptRequired = "required"
	OptMaxLen   = "max_len"
)
View Source
const ListBson = "__MDZL__"
View Source
const NilValue = "{NIL}"
View Source
const SetBson = "__MDZS__"

Variables

This section is empty.

Functions

func DecodeEntity

func DecodeEntity(e Entity, dst interface{}) error

MapEntity takes an entity and a pointer to a mapped object, and maps the entity's properties into the object's fields. Note that dst must be a pointer to a struct, anything else will fail

func InternalType

func InternalType(v interface{}) (interface{}, error)

InternalType takes an arbitrary object and maps it to the internal db type matching it

func IsAllowedType

func IsAllowedType(c ColumnType) bool

IsAllowedType returns true if a given type string is in the our allowed types list

func SetPrimary

func SetPrimary(id Key, dst interface{}) error

SetPrimary puts a primary id into a model object's primary field. dst must be a non nil pointer to a struct for this to work

Types

type Binary

type Binary []byte

type Bool

type Bool bool //

type Column

type Column struct {
	Name       string                 `yaml:"name"`
	ClientName string                 `yaml:"clientName"`
	Type       ColumnType             `yaml:"type"`
	Default    interface{}            `yaml:"default,omitempty"`
	Comment    string                 `yaml:"comment,omitempty"`
	Options    map[string]interface{} `yaml:"options,omitempty"`
	// Admin Options
	AdminOptions struct {
		// If true - we do not show this column in forms
		Hidden bool `yaml:"hidden,omitempty"`
		// if true - we set a readonly property in the admin for this column
		ReadOnly bool `yaml:"readonly,omitempty"`
		// If set - mark the form ordering priority of this column in the admin form
		Priority int `yaml:"priority,omitempty"`
		// If set - specify a custom format for the admin (date, location, markdown, etc)
		// see https://github.com/jdorn/json-editor#format
		Format string `yaml:"format,omitempty"`
	} `yaml:"adminOptions,omitempty"`
}

Column describes a column in a table, if we are talking about a strict schema

func (Column) BoolOption

func (c Column) BoolOption(key string) (b bool, found bool)

func (Column) Equals

func (c Column) Equals(other *Column) bool

Equals checks if two columns are practically identical (between 2 tables)

func (Column) GoName

func (c Column) GoName() string

func (Column) HasDefault

func (c Column) HasDefault() bool

func (Column) IntOption

func (c Column) IntOption(key string) (i int, found bool)

func (Column) StringOption

func (c Column) StringOption(key string) (s string, found bool)

func (*Column) Validate

func (c *Column) Validate() error

type ColumnAddedChange

type ColumnAddedChange struct {
	SchemaChange
	Column *Column
}

type ColumnAlterChange

type ColumnAlterChange struct {
	SchemaChange
	Column *Column
}

type ColumnDeletedChange

type ColumnDeletedChange struct {
	SchemaChange
	Column *Column
}

type ColumnType

type ColumnType string
const (
	IntType       ColumnType = "Int"
	UintType      ColumnType = "Uint"
	FloatType     ColumnType = "Float"
	TextType      ColumnType = "Text"
	BoolType      ColumnType = "Bool"
	TimestampType ColumnType = "Timestamp"
	BinaryType    ColumnType = "Binary"
	SetType       ColumnType = "Set"
	ListType      ColumnType = "List"
	MapType       ColumnType = "Map"
	UnknownType   ColumnType = ""
)

data types

func TypeOf

func TypeOf(v interface{}) ColumnType

TypeOf returns the ColumnType representation of an internal data type variable. If the variable is not of any internal type, we return UnknownType

type Decoder

type Decoder interface {
	Decode(value []byte, tp ColumnType) (interface{}, error)
}

type DefaultTextNormalizer

type DefaultTextNormalizer struct {
	Locale language.Tag
	// contains filtered or unexported fields
}

func NewNormalizer

func NewNormalizer(locale language.Tag, removeAccents, removePunct bool) *DefaultTextNormalizer

func (*DefaultTextNormalizer) Normalize

func (d *DefaultTextNormalizer) Normalize(input []byte) (ret string, err error)

func (*DefaultTextNormalizer) NormalizeString

func (d *DefaultTextNormalizer) NormalizeString(input string) (string, error)

type Deployer

type Deployer interface {
	Deploy(r io.Reader) error
	DeployUri(uri string) error
}

type Encoder

type Encoder interface {
	Encode(value interface{}) ([]byte, error)
}

type Entity

type Entity struct {
	Id         Key           `bson:"id"`
	Properties PropertyMap   `bson:"properties"`
	TTL        time.Duration `bson:"ttl"`
}

func EncodeStruct

func EncodeStruct(src interface{}) (*Entity, error)

EncodeStruct takes a model struct and encodes it into an entity. The struct must have a primary field, and cannot be nil

func NewEntity

func NewEntity(id Key, properties ...Property) *Entity

func (*Entity) Expire

func (e *Entity) Expire(ttl time.Duration) *Entity

func (*Entity) Get

func (e *Entity) Get(property string) (interface{}, bool)

func (*Entity) Set

func (e *Entity) Set(name string, value interface{}) *Entity

func (*Entity) SetId

func (e *Entity) SetId(id Key)

func (Entity) Validate

func (e Entity) Validate() error

type FilesDeployer

type FilesDeployer struct {
	// contains filtered or unexported fields
}

func NewFileDeployer

func NewFileDeployer(rootDir string) FilesDeployer

func (FilesDeployer) Deploy

func (p FilesDeployer) Deploy(r io.Reader, name string) error

func (FilesDeployer) DeployUri

func (p FilesDeployer) DeployUri(uri string, name string) error

type FilesProvider

type FilesProvider struct {
	// contains filtered or unexported fields
}

FilesProvider is a simple schema provider that reads static files in a directory, and monitors this directory for changes in files. If it finds a changed file, it re-reads it and issues an update

func NewFilesProvider

func NewFilesProvider(root string) *FilesProvider

NewFilesProvider creates a files based provider listening on schema files inside root

func (*FilesProvider) Init

func (p *FilesProvider) Init() error

Init reads all the schema files in the root directory.

NOTE: It raises an error only if reading the directory is empty. It can behave as if everything is normal even if ALL the schema files in the directory are bad

func (*FilesProvider) Schemas

func (p *FilesProvider) Schemas() []*Schema

Schemas returns a list of all the schemas in the root directory

func (*FilesProvider) Stop

func (p *FilesProvider) Stop()

Stop stops litening for changes

func (*FilesProvider) Updates

func (p *FilesProvider) Updates() (<-chan *Schema, error)

Updates starts monitoring the root dir for changes. Any changed or new schema is returned through the updates channel. Drivers can diff it by name with its older version to tell what changes had been made to it

type Float

type Float float64 //

type Index

type Index struct {
	Name        string                 `yaml:"name"`
	Columns     []string               `yaml:"columns,omitempty"`
	Type        IndexType              `yaml:"type"`
	State       IndexState             `yaml:"-"`
	ExtraParams map[string]interface{} `yaml:"options,omitempty"`
}

Index provides a description of an index, to be used by the specific driver

func (Index) Equals

func (i Index) Equals(other *Index) bool

func (*Index) SetName

func (i *Index) SetName(table string)

func (*Index) Validate

func (i *Index) Validate(t *Table) error

Validate makes sure the that the index configuration is sane in the context of the table and the storage engine

type IndexAddedChange

type IndexAddedChange struct {
	SchemaChange
	Index *Index
}

type IndexRemovedChange

type IndexRemovedChange struct {
	SchemaChange
	Index *Index
}

type IndexState

type IndexState int8
const (
	IndexReady   IndexState = 1
	IndexPending IndexState = 2
	IndexGarbage IndexState = 3
)

type IndexType

type IndexType string
const (
	IdKey = "id"

	// Index type specds
	SimpleIndex   IndexType = "simple"
	CompoundIndex IndexType = "compound"
	SortedIndex   IndexType = "sorted"
	GeoIndex      IndexType = "geo"
	FullTextIndex IndexType = "fulltext"

	// primary index types
	PrimaryRandom   IndexType = "random"
	PrimaryCompound IndexType = "compound"
)

type Int

type Int int64 //

type Key

type Key string

func (Key) IsNull

func (k Key) IsNull() bool

type List

type List []interface{} //

func NewList

func NewList(elements ...interface{}) List

func (List) MarshalBson

func (l List) MarshalBson(buf *bytes2.ChunkedWriter, key string)

type Map

type Map map[string]interface{} //

func NewMap

func NewMap() Map

func (Map) Set

func (m Map) Set(k string, val interface{}) Map

func (Map) UnmarshalBson

func (m Map) UnmarshalBson(buf *bytes.Buffer, kind byte)

UnmarshalBson overrieds the default unmarshaling of a map, to allow us to extract our internal types

type Property

type Property struct {
	Name  string      `bson:"name"`
	Value interface{} `bson:"value"`
}

func NewInt

func NewInt(k string, v int64) Property

func NewText

func NewText(k, v string) Property

func (Property) String

func (p Property) String() string

func (*Property) UnmarshalBson

func (p *Property) UnmarshalBson(buf *bytes.Buffer, kind byte)

type PropertyMap

type PropertyMap map[string]interface{}

func (PropertyMap) UnmarshalBson

func (p PropertyMap) UnmarshalBson(buf *bytes.Buffer, kind byte)

UnmarshalBson overrieds the default unmarshaling of a map, to allow us to extract our internal types

type RawDecoder

type RawDecoder struct {
}

RawDecoder taekes a raw byte array and type information and decodes them

func (RawDecoder) Decode

func (d RawDecoder) Decode(value []byte, tp ColumnType) (interface{}, error)

type RawEncoder

type RawEncoder struct{}

func (RawEncoder) Encode

func (c RawEncoder) Encode(v interface{}) ([]byte, error)

type Schema

type Schema struct {
	Name   string            `yaml:"schema"`
	Tables map[string]*Table `yaml:"tables"`
}

Schema describes a database and all its tables

func Load

func Load(reader io.Reader) (*Schema, error)

Load reads a schema from a reader and validates it. Returns the loaded schema if it was okay or an error if it wasn't

func LoadFile

func LoadFile(pth string) (*Schema, error)

LoadFile loads a schema from a file path. This is basically a wrapper for Load

func NewSchema

func NewSchema(name string) *Schema

func (*Schema) AddTable

func (s *Schema) AddTable(t *Table)

func (*Schema) Diff

func (sc *Schema) Diff(other *Schema) ([]interface{}, error)

func (*Schema) Validate

func (s *Schema) Validate() error

Validate validates each table of the schema for sane values, and makes sure the internal name is the same as the external name in the schema's dictionary

type SchemaChange

type SchemaChange struct {
	Table *Table
}

type SchemaProvider

type SchemaProvider interface {
	Init() error
	Schemas() []*Schema
	Updates() (<-chan *Schema, error)
	Stop()
}

SchemaProvider is an interface for drivers, letting them access schema definitions, and notifying them about schema changes in real time

type Set

type Set map[interface{}]struct{}

func NewSet

func NewSet(elements ...interface{}) Set

NewSet creates a new set from a list of elements

func NewSetFromMap

func NewSetFromMap(m map[interface{}]struct{}) Set

func (Set) Add

func (s Set) Add(e interface{})

func (Set) MarshalBson

func (s Set) MarshalBson(buf *bytes2.ChunkedWriter, key string)

MarshalBson overrides the vitess library's marshaling for set objects

func (Set) MarshalJSON

func (s Set) MarshalJSON() ([]byte, error)

MarshalBson encodes a timestamp property as a Bson time

type StringProvider

type StringProvider struct {
	// contains filtered or unexported fields
}

StringProvider wraps a schema string (mainly used for testing) in a provider. It does NOT provide any updates and does not support multiple schemata

func NewStringProvider

func NewStringProvider(sc string) *StringProvider

NewStringProvider wraps the schema in sc in a provider

func (*StringProvider) Init

func (p *StringProvider) Init() error

Init loads the schema string into the provider, reporting an error if it is a bad one

func (StringProvider) Schemas

func (p StringProvider) Schemas() []*Schema

Schemas returns a one-sized slice of schemas containing our parsed string schema

func (StringProvider) Stop

func (StringProvider) Stop()

Stop does nothing in this provider

func (StringProvider) Updates

func (StringProvider) Updates() (<-chan *Schema, error)

Updates does nothing in this provider and returns nil

type Table

type Table struct {
	Name     string   `yaml:"-"`
	BaseName string   `yaml:"name"`
	Comment  string   `yaml:"comment,omitempty"`
	Class    string   `yaml:"class,omitempty"` // the generated class name, leave empty for the same as Name
	Engines  []string `yaml:"engines,omitempty"`

	// If the table is not strict, we do not enforce a schema, and are just aware
	// of indexed columns.
	// You could use the schema for reference only
	Strict bool

	Columns map[string]*Column `yaml:"columns,omitempty"`

	Indexes []*Index `yaml:"indexes,omitempty"`

	Primary *Index `yaml:"primary"`

	AdminOptions struct {
		ListColumns []string `yaml:"listColumns,omitempty"`
		SearchBy    []string `yaml:"searchBy,omitempty"`
	} `yaml:"adminOptions,omitempty"`
}

Table describes a table in the database, with its columns (optional) and indexes

func NewTable

func NewTable(name string, strict bool) *Table

func (*Table) AddColumn

func (t *Table) AddColumn(name string, tp ColumnType, def interface{}, comment string) error

AddColumn adds a column to the table, or returns an error if this column already exists

func (*Table) AddColumnQuick

func (t *Table) AddColumnQuick(name string, tp ColumnType) error

AddColumn adds a column to the table, or returns an error if this column already exists

func (*Table) AddIndex

func (t *Table) AddIndex(tp IndexType, columns ...string) error

func (*Table) Validate

func (t *Table) Validate() error

Validates makes sure that the table data is sane

type TableAddedChange

type TableAddedChange struct {
	SchemaChange
}

type TableDeletedChange

type TableDeletedChange struct {
	SchemaChange
}

type Text

type Text string //

func (Text) MarshalBson

func (t Text) MarshalBson(buf *bytes2.ChunkedWriter, key string)

MarshalBson overrides the vitess library's marshaling of strings that encodes strings as binary

type TextNormalizer

type TextNormalizer interface {
	Normalize([]byte) (string, error)
	NormalizeString(string) (string, error)
}

type Timestamp

type Timestamp time.Time

func (*Timestamp) GobDecode

func (t *Timestamp) GobDecode(data []byte) error

GobDecode implements the gob.GobDecoder interface.

func (Timestamp) GobEncode

func (t Timestamp) GobEncode() ([]byte, error)

GobEncode implements the gob.GobEncoder interface.

func (Timestamp) MarshalBson

func (t Timestamp) MarshalBson(buf *bytes2.ChunkedWriter, key string)

MarshalBson encodes a timestamp property as a Bson time

func (Timestamp) MarshalJSON

func (t Timestamp) MarshalJSON() ([]byte, error)

MarshalBson encodes a timestamp property as a Bson time

type Tokenizer

type Tokenizer interface {
	Tokenize(string) error
	HasNext()
	NextToken() string
}

type Transcoder

type Transcoder interface {
	Transcode([]byte, ColumnType) ([]byte, error)
}

type Uint

type Uint uint64

type Value

type Value interface {
}

Directories

Path Synopsis
provider
redis
Package redis provides a redis based schema provider and deployer
Package redis provides a redis based schema provider and deployer

Jump to

Keyboard shortcuts

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