Documentation
¶
Overview ¶
Package cast provides easy and safe casting in Go.
Index ¶
- Variables
- func StringToDate(s string) (time.Time, error)
- func StringToDateInDefaultLocation(s string, location *time.Location) (time.Time, error)
- func ToBool(i interface{}) bool
- func ToBoolE(i interface{}) (bool, error)
- func ToBoolSlice(i interface{}) []bool
- func ToBoolSliceE(i interface{}) ([]bool, error)
- func ToDuration(i interface{}) time.Duration
- func ToDurationE(i interface{}) (d time.Duration, err error)
- func ToDurationSlice(i interface{}) []time.Duration
- func ToDurationSliceE(i interface{}) ([]time.Duration, error)
- func ToFloat32(i interface{}) float32
- func ToFloat32E(i interface{}) (float32, error)
- func ToFloat64(i interface{}) float64
- func ToFloat64E(i interface{}) (float64, error)
- func ToInt(i interface{}) int
- func ToInt16(i interface{}) int16
- func ToInt16E(i interface{}) (int16, error)
- func ToInt32(i interface{}) int32
- func ToInt32E(i interface{}) (int32, error)
- func ToInt64(i interface{}) int64
- func ToInt64E(i interface{}) (int64, error)
- func ToInt8(i interface{}) int8
- func ToInt8E(i interface{}) (int8, error)
- func ToIntE(i interface{}) (int, error)
- func ToIntSlice(i interface{}) []int
- func ToIntSliceE(i interface{}) ([]int, error)
- func ToSlice(i interface{}) []interface{}
- func ToSliceE(i interface{}) ([]interface{}, error)
- func ToString(i interface{}) string
- func ToStringE(i interface{}) (string, error)
- func ToStringMap(i interface{}) map[string]interface{}
- func ToStringMapBool(i interface{}) map[string]bool
- func ToStringMapBoolE(i interface{}) (map[string]bool, error)
- func ToStringMapE(i interface{}) (map[string]interface{}, error)
- func ToStringMapInt(i interface{}) map[string]int
- func ToStringMapInt64(i interface{}) map[string]int64
- func ToStringMapInt64E(i interface{}) (map[string]int64, error)
- func ToStringMapIntE(i interface{}) (map[string]int, error)
- func ToStringMapString(i interface{}) map[string]string
- func ToStringMapStringE(i interface{}) (map[string]string, error)
- func ToStringMapStringSlice(i interface{}) map[string][]string
- func ToStringMapStringSliceE(i interface{}) (map[string][]string, error)
- func ToStringSlice(i interface{}) []string
- func ToStringSliceE(i interface{}) ([]string, error)
- func ToTime(i interface{}) time.Time
- func ToTimeE(i interface{}) (tim time.Time, err error)
- func ToTimeInDefaultLocation(i interface{}, location *time.Location) time.Time
- func ToTimeInDefaultLocationE(i interface{}, location *time.Location) (tim time.Time, err error)
- func ToUint(i interface{}) uint
- func ToUint16(i interface{}) uint16
- func ToUint16E(i interface{}) (uint16, error)
- func ToUint32(i interface{}) uint32
- func ToUint32E(i interface{}) (uint32, error)
- func ToUint64(i interface{}) uint64
- func ToUint64E(i interface{}) (uint64, error)
- func ToUint8(i interface{}) uint8
- func ToUint8E(i interface{}) (uint8, error)
- func ToUintE(i interface{}) (uint, error)
- type Client
- type ClientOption
- type Context
- func (c *Context) Bind(v interface{}) error
- func (c *Context) Deadline() (deadline time.Time, ok bool)
- func (c *Context) DecodeTo(data []byte, v interface{}) error
- func (c *Context) Done() <-chan struct{}
- func (c *Context) Err() error
- func (c *Context) Get(key string) (value interface{}, exists bool)
- func (c *Context) Message() *Entry
- func (c *Context) MustBind(v interface{})
- func (c *Context) MustDecodeTo(data []byte, v interface{})
- func (c *Context) MustGet(key string) interface{}
- func (c *Context) Response(id interface{}, data interface{}) (*Entry, error)
- func (c *Context) Session() *Session
- func (c *Context) Set(key string, value interface{})
- func (c *Context) Value(key interface{}) interface{}
- type CustomPacker
- type DefaultPacker
- type Entry
- type Error
- type HandlerFunc
- type MiddlewareFunc
- type Packer
- type ReceiveHandle
- type Router
- type Server
- type ServerOption
- type Session
- type SessionManager
- type SessionOption
- type UnpackError
Constants ¶
This section is empty.
Variables ¶
var ErrServerStopped = fmt.Errorf("server stopped")
ErrServerStopped is used when server stopped.
Functions ¶
func StringToDate ¶
StringToDate attempts to parse a string into a time.Time type using a predefined list of formats. If no suitable format is found, an error is returned.
func StringToDateInDefaultLocation ¶
StringToDateInDefaultLocation casts an empty interface to a time.Time, interpreting inputs without a timezone to be in the given location, or the local timezone if nil.
func ToBoolSlice ¶
func ToBoolSlice(i interface{}) []bool
ToBoolSlice casts an interface to a []bool type.
func ToBoolSliceE ¶
ToBoolSliceE casts an interface to a []bool type.
func ToDuration ¶
ToDuration casts an interface to a time.Duration type.
func ToDurationE ¶
ToDurationE casts an interface to a time.Duration type.
func ToDurationSlice ¶
ToDurationSlice casts an interface to a []time.Duration type.
func ToDurationSliceE ¶
ToDurationSliceE casts an interface to a []time.Duration type.
func ToFloat32 ¶
func ToFloat32(i interface{}) float32
ToFloat32 casts an interface to a float32 type.
func ToFloat32E ¶
ToFloat32E casts an interface to a float32 type.
func ToFloat64 ¶
func ToFloat64(i interface{}) float64
ToFloat64 casts an interface to a float64 type.
func ToFloat64E ¶
ToFloat64E casts an interface to a float64 type.
func ToIntSlice ¶
func ToIntSlice(i interface{}) []int
ToIntSlice casts an interface to a []int type.
func ToIntSliceE ¶
ToIntSliceE casts an interface to a []int type.
func ToSlice ¶
func ToSlice(i interface{}) []interface{}
ToSlice casts an interface to a []interface{} type.
func ToSliceE ¶
func ToSliceE(i interface{}) ([]interface{}, error)
ToSliceE casts an interface to a []interface{} type.
func ToStringMap ¶
func ToStringMap(i interface{}) map[string]interface{}
ToStringMap casts an interface to a map[string]interface{} type.
func ToStringMapBool ¶
ToStringMapBool casts an interface to a map[string]bool type.
func ToStringMapBoolE ¶
ToStringMapBoolE casts an interface to a map[string]bool type.
func ToStringMapE ¶
ToStringMapE casts an interface to a map[string]interface{} type.
func ToStringMapInt ¶
ToStringMapInt casts an interface to a map[string]int type.
func ToStringMapInt64 ¶
ToStringMapInt64 casts an interface to a map[string]int64 type.
func ToStringMapInt64E ¶
ToStringMapInt64E casts an interface to a map[string]int64{} type.
func ToStringMapIntE ¶
ToStringMapIntE casts an interface to a map[string]int{} type.
func ToStringMapString ¶
ToStringMapString casts an interface to a map[string]string type.
func ToStringMapStringE ¶
ToStringMapStringE casts an interface to a map[string]string type.
func ToStringMapStringSlice ¶
ToStringMapStringSlice casts an interface to a map[string][]string type.
func ToStringMapStringSliceE ¶
ToStringMapStringSliceE casts an interface to a map[string][]string type.
func ToStringSlice ¶
func ToStringSlice(i interface{}) []string
ToStringSlice casts an interface to a []string type.
func ToStringSliceE ¶
ToStringSliceE casts an interface to a []string type.
func ToTimeInDefaultLocation ¶
func ToTimeInDefaultLocationE ¶
ToTimeInDefaultLocationE casts an empty interface to time.Time, interpreting inputs without a timezone to be in the given location, or the local timezone if nil.
Types ¶
type Client ¶
type Client struct { Packer Packer DiaAddress string ReaderNum int ReceiveHandle ReceiveHandle // contains filtered or unexported fields }
func NewClient ¶
func NewClient(opt *ClientOption) *Client
type ClientOption ¶
type Context ¶
type Context struct {
// contains filtered or unexported fields
}
Context is a generic context in a message routing. It allows us to pass variables between handler and middlewares. Context implements the context.Context interface.
func (*Context) Done ¶
func (c *Context) Done() <-chan struct{}
Done implements the context.Context Done method.
func (*Context) MustBind ¶
func (c *Context) MustBind(v interface{})
MustBind binds the request message's raw data to v. Panics if any error occurred.
func (*Context) MustDecodeTo ¶
MustDecodeTo decodes data to v via codec. Panics if any error occurred.
func (*Context) MustGet ¶
MustGet returns the value from c.storage by key. Panics if key does not exist.
type CustomPacker ¶
type CustomPacker struct{}
type DefaultPacker ¶
type DefaultPacker struct {
MaxSize int
}
DefaultPacker is the default Packer used in session. DefaultPacker treats the packet with the format:
(size)(id)(data): size: uint32 | took 4 bytes, only the size of `data` id: uint32 | took 4 bytes data: []byte | took `size` bytes
func NewDefaultPacker ¶
func NewDefaultPacker() *DefaultPacker
NewDefaultPacker create a *DefaultPacker with initial field value.
type Entry ¶
type Entry struct { ID interface{} Data []byte // contains filtered or unexported fields }
Entry is the unpacked message object.
type Error ¶
type Error interface { error Fatal() bool // should return true if the error is fatal, otherwise false. }
Error is a generic interface for error handling.
type HandlerFunc ¶
HandlerFunc is the function type for handlers.
type MiddlewareFunc ¶
type MiddlewareFunc func(next HandlerFunc) HandlerFunc
MiddlewareFunc is the function type for middlewares. A common pattern is like:
var md MiddlewareFunc = func(next HandlerFunc) HandlerFunc { return func(ctx *Context) (message.Entry, error) { return next(ctx) } }
func RecoverMiddleware ¶
func RecoverMiddleware(log *logger.Logger) MiddlewareFunc
type Packer ¶
type Packer interface { // Pack packs Message into the packet to be written. // Pack(msg Message) ([]byte, error) Pack(entry *Entry) ([]byte, error) // Unpack unpacks the message packet from reader, // returns the Message interface, and error if error occurred. Unpack(reader io.Reader) (*Entry, error) }
Packer is a generic interface to pack and unpack message packet.
type ReceiveHandle ¶
type Router ¶
type Router struct {
// contains filtered or unexported fields
}
Router is a router for incoming message. Router routes the message to its handler and middlewares.
type Server ¶
type Server struct { Listener net.Listener // Packer is the message packer, will be passed to session. Packer Packer // Codec is the message codec, will be passed to session. Codec encoding.Codec // OnSessionCreate is an event hook, will be invoked when session's created. OnSessionCreate func(sess *Session) // OnSessionClose is an event hook, will be invoked when session's closed. OnSessionClose func(sess *Session) ServerAddress string Logger *logger.Logger // contains filtered or unexported fields }
Server is a server for TCP connections.
func NewServer ¶
func NewServer(opt *ServerOption) *Server
NewServer creates a Server according to opt.
func (*Server) AddRoute ¶
func (s *Server) AddRoute(msgID interface{}, handler HandlerFunc, middlewares ...MiddlewareFunc)
AddRoute registers message handler and middlewares to the router.
func (*Server) NotFoundHandler ¶
func (s *Server) NotFoundHandler(handler HandlerFunc)
NotFoundHandler sets the not-found handler for router.
func (*Server) Serve ¶
Serve starts to listen TCP and keeps accepting TCP connection in a loop. The loop breaks when error occurred, and the error will be returned.
func (*Server) Use ¶
func (s *Server) Use(middlewares ...MiddlewareFunc)
Use registers global middlewares to the router.
type ServerOption ¶
type ServerOption struct { SocketReadBufferSize int // sets the socket read buffer size. SocketWriteBufferSize int // sets the socket write buffer size. SocketSendDelay bool // sets the socket delay or not. ReadTimeout time.Duration // sets the timeout for connection read. WriteTimeout time.Duration // sets the timeout for connection write. Packer Packer // packs and unpacks packet payload, default packer is the packet.DefaultPacker. Codec encoding.Codec // encodes and decodes the message data, can be nil. RespQueueSize int // sets the response channel size of session, 1024 will be used if < 0. ReqQueueSize int // sets the request channel size of router, 1024 will be used if < 0. DoNotPrintRoutes bool // whether to print registered route handlers to the console. ServerAddress string ReqCtxQueue chan *Context Logger *logger.Logger }
ServerOption is the option for Server.
type Session ¶
type Session struct {
// contains filtered or unexported fields
}
Session represents a TCP session.
type SessionManager ¶
type SessionManager struct {
// contains filtered or unexported fields
}
SessionManager manages all the sessions in application runtime.
func Sessions ¶
func Sessions() *SessionManager
Sessions returns a SessionManager pointer in a singleton way.
func (*SessionManager) Add ¶
func (m *SessionManager) Add(s *Session)
Add adds a session to Sessions. If the ID of s already existed in Sessions, it replaces the value with the s.
func (*SessionManager) Get ¶
func (m *SessionManager) Get(id string) *Session
Get returns a Session when found by the id, returns nil otherwise.
func (*SessionManager) Range ¶
func (m *SessionManager) Range(fn func(id string, sess *Session) (next bool))
Range calls fn sequentially for each id and sess present in the Sessions. If fn returns false, range stops the iteration.
func (*SessionManager) Remove ¶
func (m *SessionManager) Remove(id string)
Remove removes a session from Sessions. Parameter id should be the session's id.
type SessionOption ¶
type SessionOption struct { Packer Packer Codec encoding.Codec // contains filtered or unexported fields }
SessionOption is the extra options for Session.
type UnpackError ¶
type UnpackError struct {
Err error
}
UnpackError is the error returned in packer.Unpack.
func (*UnpackError) Error ¶
func (pe *UnpackError) Error() string
func (*UnpackError) Fatal ¶
func (pe *UnpackError) Fatal() bool