model

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2016 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package model implements a basic model abstraction for structs.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Base

type Base struct {
	DocID bson.ObjectId `json:"-" bson:"_id,omitempty"`
	// contains filtered or unexported fields
}

Base is the base for every fire model.

func (*Base) ID

func (b *Base) ID() bson.ObjectId

ID returns the models id.

func (*Base) Meta

func (b *Base) Meta() *Meta

Meta returns the models Meta structure.

func (*Base) MustGet

func (b *Base) MustGet(name string) interface{}

MustGet returns the value of the given field.

Note: MustGet will return the value of the first field that has a matching Name, JSONName, or BSONName and will panic if no field can be found.

func (*Base) MustSet

func (b *Base) MustSet(name string, value interface{})

MustSet will set the given field to the the passed valued.

Note: MustSet will set the value of the first field that has a matching Name, JSONName, or BSONName and will panic if no field can been found. The method will also panic if the type of the field and the passed value do not match.

type Field

type Field struct {
	Name       string
	Type       reflect.Type
	Kind       reflect.Kind
	JSONName   string
	BSONName   string
	Optional   bool
	ToOne      bool
	ToMany     bool
	HasMany    bool
	RelName    string
	RelType    string
	RelInverse string
	// contains filtered or unexported fields
}

A Field contains the meta information about a single field of a model.

type HasMany

type HasMany struct{}

The HasMany type denotes a has many relationship in a model declaration.

type Meta

type Meta struct {
	Name       string
	PluralName string
	Collection string
	Fields     []Field
	// contains filtered or unexported fields
}

Meta stores extracted meta data from a model.

func NewMeta

func NewMeta(model Model) *Meta

NewMeta returns the Meta structure for the passed Model.

Note: This method panics if the passed Model has invalid fields and tags.

func (*Meta) FindField

func (m *Meta) FindField(name string) *Field

FindField returns the first field that has a matching Name, JSONName, or BSONName. FindField will return nil if no field has been found.

func (*Meta) Make

func (m *Meta) Make() Model

Make returns a pointer to a new zero initialized model e.g. *Post.

Note: Other libraries like mgo might replace the pointer content with a new structure, therefore the model eventually needs to be initialized again using Init().

func (*Meta) MakeSlice

func (m *Meta) MakeSlice() interface{}

MakeSlice returns a pointer to a zero length slice of the model e.g. *[]*Post.

Note: Don't forget to initialize the slice using InitSlice() after adding elements with libraries like mgo.

func (*Meta) MustFindField

func (m *Meta) MustFindField(name string) *Field

MustFindField returns the first field that has a matching Name, JSONName, or BSONName. MustFindField will panic if no field has been found.

type Model

type Model interface {
	ID() bson.ObjectId
	MustGet(string) interface{}
	MustSet(string, interface{})
	Meta() *Meta
	// contains filtered or unexported methods
}

Model is the main interface implemented by every fire model embedding Base.

func Init

func Init(model Model) Model

Init initializes the internals of a model and should be called before using a newly created Model.

func InitSlice

func InitSlice(ptr interface{}) []Model

InitSlice initializes all models in a slice of the form *[]*Post and returns a new slice that contains all initialized models.

type Store

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

A Store manages the usage of database connections.

func CreateStore

func CreateStore(uri string) (*Store, error)

CreateStore will dial the passed database and return a new store. It will return an error if the initial connection failed

func MustCreateStore

func MustCreateStore(uri string) *Store

MustCreateStore will dial the passed database and return a new store. It will panic if the initial connection failed.

func NewStore

func NewStore(session *mgo.Session) *Store

NewStore returns a Store that uses the passed session and its default database.

func (*Store) C

func (s *Store) C(model Model) *mgo.Collection

C will return the collection associated to the passed model.

func (*Store) Close

func (s *Store) Close()

Close will close the store and its associated session.

func (*Store) Copy

func (s *Store) Copy() *Store

Copy will make a copy of the store and the underlying session. Copied stores that are not used anymore must be closed.

func (*Store) DB

func (s *Store) DB() *mgo.Database

DB returns the database used by this store.

Jump to

Keyboard shortcuts

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