store

package
v1.0.1-0...-8e9dff6 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2018 License: MIT Imports: 7 Imported by: 2

Documentation

Index

Constants

View Source
const (
	And = iota
	Or
)
View Source
const (
	Asc = iota
	Desc
)
View Source
const (
	DefaultSrc keys
)

context keys

Variables

View Source
var ErrorForbidden = Error(http.StatusForbidden, "Permission Denied")

ErrorForbidden singleton status in case permission denied

View Source
var ErrorInternal = Error(http.StatusInternalServerError, "Internal Server Error")

ErrorInternal singleton status in case internal server error

View Source
var ErrorMethodNotAllowed = Error(http.StatusMethodNotAllowed, "Method Not Allowed")

ErrorMethodNotAllowed singleton status in case HTTP method is not allowed to use

View Source
var ErrorNotFound = Error(http.StatusNotFound, "Not Found")

ErrorNotFound singleton status in case not found

View Source
var StatusFound = Error(http.StatusFound, "Success")

StatusFound singleton status in case entity found

Functions

func ClearFactory

func ClearFactory(inner endpoint.Endpoint) endpoint.Endpoint

ClearFactory cleans up after RequestFunc. If you called RequestFunc in your http transport, you should use this

func CloseAllIn

func CloseAllIn(ctx context.Context)

CloseAllIn close all Store connections in the context

func Middleware

func Middleware(factory Factory) endpoint.Middleware

Middleware takes a Factory and create a middleware that does WithStores and CloseAllIn

func ParseError

func ParseError(err error) (code int, msg string)

ParseError reads and parse a given status message. It reads Error type and unpack the status code and message. If it is not of Error type, it will return 400 internal server error. If it is nil, it will return 302 found

func RequestFuncWithFactory

func RequestFuncWithFactory(factory Factory) httptransport.RequestFunc

RequestFuncWithFactory returns a go-kit/kit/transpoirt/http.RequestFunc that will add a given factory to the context

func WithFactory

func WithFactory(parent context.Context, factory Factory) context.Context

WithFactory attachs a factory to the context

Types

type BasicConds

type BasicConds struct {
	Conds []Cond
	Rel   int
}

BasicConds implements of Conds

func (*BasicConds) Add

func (c *BasicConds) Add(prop string, value interface{}) Conds

Add adds a condition

func (*BasicConds) GetAll

func (c *BasicConds) GetAll() []Cond

GetAll gets the list of conditions

func (*BasicConds) GetMap

func (c *BasicConds) GetMap() (m map[string]interface{}, err error)

GetMap gets the list of conditions in the form of map[string]interface{}

func (*BasicConds) GetRel

func (c *BasicConds) GetRel() int

GetRel is getter of relation flag

func (*BasicConds) SetRel

func (c *BasicConds) SetRel(r int) Conds

SetRel is setter of relation flag

type BasicQuery

type BasicQuery struct {
	Conds  Conds
	Sorts  Sorts
	Limit  uint64
	Offset uint64
}

BasicQuery implements Query interface

func (*BasicQuery) AddCond

func (q *BasicQuery) AddCond(p string, v interface{}) Query

AddCond add a Cond to Conds

func (*BasicQuery) GetConds

func (q *BasicQuery) GetConds() Conds

GetConds get the conds interface within

func (*BasicQuery) GetLimit

func (q *BasicQuery) GetLimit() uint64

GetLimit is getter of limit

func (*BasicQuery) GetOffset

func (q *BasicQuery) GetOffset() uint64

GetOffset is getter of offset

func (*BasicQuery) GetSorts

func (q *BasicQuery) GetSorts() Sorts

GetSorts get the Sorts interface within

func (*BasicQuery) SetConds

func (q *BasicQuery) SetConds(cs Conds) Query

SetConds set the conds interface within

func (*BasicQuery) SetLimit

func (q *BasicQuery) SetLimit(n uint64) Query

SetLimit is setter of limit

func (*BasicQuery) SetOffset

func (q *BasicQuery) SetOffset(n uint64) Query

SetOffset is setter of offset

func (*BasicQuery) SetSorts

func (q *BasicQuery) SetSorts(cs Sorts) Query

SetSorts set the Sorts interface within

func (*BasicQuery) Sort

func (q *BasicQuery) Sort(sstr string) Query

Sort add a Sort to Sorts

type BasicSorts

type BasicSorts []*Sort

BasicSorts implements Sorts

func (*BasicSorts) Add

func (ss *BasicSorts) Add(sstr string) Sorts

Add adds a Sort to the BasicSort collection

func (*BasicSorts) GetAll

func (ss *BasicSorts) GetAll() []*Sort

GetAll returns the whole collection of Sort as slice

type Cond

type Cond struct {
	Prop  string
	Value interface{}
}

Cond is the generic condition statement aims to be interatable with upper.io and google datastore

type Conds

type Conds interface {

	// SetRel is setter of relation flag
	SetRel(int) Conds

	// GetRel is getter of relation flag
	GetRel() int

	// Add adds a condition
	Add(string, interface{}) Conds

	// GetAll gets the list of conditions
	GetAll() []Cond

	// GetMap gets the list of conditions in the
	// form of map[string]interface{}
	//
	// Compatible layer with upper.io
	// the result can be used as db.Cond
	GetMap() (map[string]interface{}, error)
}

Conds is the general interface represents conditions all setters return itself so the operation can cascade

func NewConds

func NewConds() Conds

NewConds creates Conds with a BasicConds

type Conn

type Conn interface {

	// Raw returns the raw session object
	Raw() interface{}

	// Close disconnect the session to the data source
	Close()
}

Conn is the interface to handle database connections session to Source

type EntityListPtr

type EntityListPtr interface{}

EntityListPtr holds ointer to list of entiity It is basically empty interface. Only named as a hint.

type EntityPtr

type EntityPtr interface{}

EntityPtr holds pointer to entiity. It is basically empty interface. Only named as a hint.

type Factory

type Factory interface {

	// SetSource sets a Source to the key
	SetSource(srcKey interface{}, src Source)

	// GetSource gets a Source with the given key
	GetSource(srcKey interface{}) Source

	// Set associates a source and a store provider to a key (store key)
	Set(key, srcKey interface{}, provider Provider)

	// Get retrieve a source and a store provider
	// associated with the given key (store key)
	Get(key interface{}) (srcKey interface{}, provider Provider)
}

Factory is the interface to manufacture Stores. It contains definitions of Source and Store

Factory produces and manage Store instances

func NewFactory

func NewFactory() Factory

NewFactory returns the default Factory implementation

type Pager

type Pager interface {

	// MarshalJSON implements json Marshaler interface
	MarshalJSON() ([]byte, error)

	// UnmarshalJSON implements json Unarshaler interface
	UnmarshalJSON([]byte) error

	// SetTotal sets the total in the pager descriptor
	SetTotal(total int) Pager

	// GetTotal gets the total in the pager descriptor
	GetTotal() (total int)

	// SetLimit sets the limit and offset in the pager descriptor
	SetLimit(limit, offset int) Pager

	// GetLimit gets the limit and offset in the pager descriptor
	GetLimit() (limit, offset int)
}

Pager is an implementation of a pager descriptor

func NewPager

func NewPager() Pager

NewPager creates a new pager descriptor

type PoolConn

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

PoolConn is a wrapper of Conn that also implments Conn

func (*PoolConn) Close

func (conn *PoolConn) Close()

Close implements store.Conn.Close()

func (*PoolConn) Err

func (conn *PoolConn) Err() error

Err returns the connection error of the wrapped source

func (*PoolConn) Expired

func (conn *PoolConn) Expired() bool

Expired return wether the connection has expired or not

func (*PoolConn) Raw

func (conn *PoolConn) Raw() interface{}

Raw implements store.Conn.Raw()

type Provider

type Provider func(sess interface{}) (Store, error)

Provider takes a connection and return Store for that session

type Query

type Query interface {

	// SetLimit is setter of limit
	SetLimit(uint64) Query

	// GetLimit is getter of limit
	GetLimit() uint64

	// SetOffset is setter of offset
	SetOffset(uint64) Query

	// GetOffset is getter of offset
	GetOffset() uint64

	// SetConds sets the conds interface withing
	SetConds(Conds) Query

	// GetConds gets the conds interface within
	GetConds() Conds

	// AddCond add a Cond to Conds
	AddCond(prop string, val interface{}) Query

	// SetSorts sets the Sorts interface withing
	SetSorts(Sorts) Query

	// GetSorts gets the Sorts interface within
	GetSorts() Sorts

	// AddSort add a Sort to Sorts
	Sort(sstr string) Query
}

func NewQuery

func NewQuery() Query

NewQuery constructs a *BasicQuery and return as Query

type Response

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

Response helps to wrap a

func ExpandResponse

func ExpandResponse(vmap map[string]interface{}) *Response

ExpandResponse converts a map[string]interface{} into proper JSON-marshalable response

func NewResponse

func NewResponse(key string, v interface{}) *Response

NewResponse return a service response pointer

func (*Response) Get

func (res *Response) Get(key string) interface{}

Get a value in the repsonse. If there are no values associated with the

func (Response) MarshalJSON

func (res Response) MarshalJSON() ([]byte, error)

MarshalJSON implements json Marshaler interface

func (*Response) Set

func (res *Response) Set(key string, v interface{})

Set a value in the response

type Result

type Result interface {

	// All fetches all results within the result set and dumps them into the
	// given pointer to slice of maps or structs
	All(el interface{}) (err error)

	// Raw returns the underlying database result variable
	Raw() (interface{}, error)

	// Count returns the number of items matches match the given query
	Count() (count uint64, err error)

	// Close closes the result set
	Close() error
}

Result defines interface of an entity servie search result

type Sort

type Sort struct {
	Name  string
	Order int
}

Sort is the generic description of a sorting aims to be interatable with upper.io and google datastore

func SortBy

func SortBy(n string) *Sort

SortBy returns an Sort (ascending) of the given property name

func SortStr

func SortStr(str string) *Sort

SortStr return *Sort described by a given string

func (*Sort) Asc

func (s *Sort) Asc() *Sort

Asc sets the Sort to order by asc order

func (*Sort) Desc

func (s *Sort) Desc() *Sort

Desc sets the Sort to order by desc order

func (*Sort) String

func (s *Sort) String() string

String returns a string represetation to sorting which is compatible with upperio and Google Datastore

type Sorts

type Sorts interface {
	Add(string) Sorts
	GetAll() []*Sort
}

Sorts describes an interface to a collection of Sort

type Source

type Source interface {
	Open() (Conn, error)
}

Source provides connection and, if any, connection error

type SourceFunc

type SourceFunc func() (Conn, error)

SourceFunc implements Source for functions

func (SourceFunc) Open

func (src SourceFunc) Open() (Conn, error)

Open implements Source

type SourcePool

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

SourcePool helps to pool connection of any given source

func Pool

func Pool(src Source, size uint, expires time.Duration) *SourcePool

Pool wraps a source into a SourcePool

func (*SourcePool) AddNewConn

func (pool *SourcePool) AddNewConn()

AddNewConn adds a new connection to the pool

func (*SourcePool) Open

func (pool *SourcePool) Open() (conn Conn, err error)

Open implements Source.Open()

type Store

type Store interface {
	// Basic entity operations
	Create(Conds, EntityPtr) error
	Search(Query) Result
	One(Conds, EntityPtr) error
	Update(Conds, EntityPtr) error
	Delete(Conds) error

	// Memory allocation
	AllocEntity() EntityPtr
	AllocEntityList() EntityListPtr

	// Helper
	Len(EntityListPtr) int64

	// Close the database session this
	// service is using
	Close() error
}

Store defines interface of an entity service

func Get

func Get(ctx context.Context,
	key interface{}) (s Store, err error)

Get try to connect to a store with provided source and provider definition. If fail, return nil and error

type StoreError

type StoreError struct {

	// Status is the http status code
	Status int `json:"status"`

	// Code is service specific status code
	Code int `json:"code"`

	// ServerMsg is the server side log message
	ServerMsg string `json:"-"`

	// ClientMsg is the client side message which could
	// be displayed to their user directly
	ClientMsg string `json:"message"`

	// DeveloperMsg is the client side message which
	// should be of help to developer. Omit if empty
	DeveloperMsg string `json:"developer_message,omitempty"`
}

StoreError is for common service error message

func Error

func Error(code int, msg string, v ...interface{}) *StoreError

Error creates formated error message with entityError type

func ExpandError

func ExpandError(err error) *StoreError

ExpandError trys to cast the error into *StoreError or generate a new *ServicError based on it

func (StoreError) Error

func (err StoreError) Error() string

Error implements the standard error type returns the client message

func (StoreError) String

func (err StoreError) String() string

String implements Stringer type which returns the client message

func (*StoreError) TellClient

func (err *StoreError) TellClient(msg string, v ...interface{}) *StoreError

TellClient sets server message

func (*StoreError) TellDeveloper

func (err *StoreError) TellDeveloper(msg string, v ...interface{}) *StoreError

TellDeveloper sets server message

func (*StoreError) TellServer

func (err *StoreError) TellServer(msg string, v ...interface{}) *StoreError

TellServer sets server message

type Stores

type Stores interface {

	// Connect connects a provider at a source
	// and return connection and, if any, connection error
	Get(key interface{}) (s Store, err error)

	// Close close all Conn in the set
	Close()
}

Stores is an interface for store with connection pool management.

Each HTTP request should have its own Stores instance in the context

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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