j2rpc

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2023 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	StartTimeKey = "__j2rpc_startTimeKey"
	EndTimeKey   = "__j2rpc_endTimeKey"
	TimeUsedKey  = "__j2rpc_timeUsedKey"
)

Variables

View Source
var SnakeOption = &Option{SnakeNamespace: true}

SnakeOption ...

Functions

func BytesToString

func BytesToString(b []byte) string

BytesToString convert []byte type to string type.

func CamelString

func CamelString(s string) string

CamelString converts the accepted string to a camel string (xx_yy to XxYy)

func FuncName

func FuncName(fn interface{}) string

FuncName ...

func FuncNameFull

func FuncNameFull(fn interface{}) string

func HasWriteHeader

func HasWriteHeader(w http.ResponseWriter) bool

func JSONDump

func JSONDump(val interface{}, args ...interface{}) string

JSONDump ...

func JSONExchange

func JSONExchange(dst interface{}, src interface{}) error

JSONExchange ...使用json copy对象

func JSONMarshalToBase64

func JSONMarshalToBase64(val interface{}) ([]byte, error)

JSONMarshalToBase64 ...

func JSONUnmarshalFromBase64

func JSONUnmarshalFromBase64(data []byte, val interface{}) error

JSONUnmarshalFromBase64 ...

func JsMarshal

func JsMarshal(val interface{}) (bts []byte)

JsMarshal ...

func JsMarshalIndent

func JsMarshalIndent(val interface{}) (bts []byte)

JsMarshalIndent ...

func NewValue

func NewValue(bean interface{}) (val interface{})

NewValue ...

func ObjectTagInstances

func ObjectTagInstances(obj interface{}, tagName string) []interface{}

ObjectTagInstances ... *

  • @Description:根据标签获取字段实例集合
  • @param obj
  • @param tagName
  • @return []interface{}

func ParsePositionalArguments

func ParsePositionalArguments(rawArgs json.RawMessage, types []reflect.Type) ([]reflect.Value, error)

ParsePositionalArguments tries to parse the given args to an array of values with the given types. It returns the parsed values or an error when the args could not be parsed. Missing optional arguments are returned as reflect.Zero|reflect.New values.

func PopulateConstructor

func PopulateConstructor(value interface{}, args ...interface{})

PopulateConstructor ...

func ReflectValue

func ReflectValue(bean interface{}, fn func(v reflect.Value))

ReflectValue ...回调函数

func ReflectZeroFields

func ReflectZeroFields(v interface{}, fields []string)

ReflectZeroFields 将多个字段设置为零值

func SetWriteHeader

func SetWriteHeader(w http.ResponseWriter)

SetWriteHeader ...

func SlicePointerValue

func SlicePointerValue(bean interface{}) reflect.Value

SlicePointerValue ...构建类型切片

func SliceStringContainsPrefix added in v1.0.0

func SliceStringContainsPrefix(slice []string, val string) bool

func SnakeString

func SnakeString(s string) string

SnakeString converts the accepted string to a snake string (XxYy to xx_yy)

func StringToBytes

func StringToBytes(s string) []byte

StringToBytes convert string type to []byte type. NOTE: panic if modify the member value of the []byte.

func ValueIndirect

func ValueIndirect(val reflect.Value) reflect.Value

ValueIndirect ...值类型

Types

type AfterHandler

type AfterHandler func(ctx *Context, method string, params []reflect.Value, result []reflect.Value) (err error)

type BeforeHandler

type BeforeHandler func(ctx *Context, method string, params []reflect.Value) (err error)

type Context

type Context struct {
	context.Context

	Body  []byte
	Store sync.Map
	// contains filtered or unexported fields
}

func (*Context) App

func (c *Context) App() RPCServer

func (*Context) BeginRequest

func (c *Context) BeginRequest(w http.ResponseWriter, r *http.Request) *Context

BeginRequest ......

func (*Context) BeginWithContext

func (c *Context) BeginWithContext(ctx context.Context, w http.ResponseWriter, r *http.Request) *Context

BeginWithContext ... start a new context with the given context

func (*Context) Do

func (c *Context) Do(handlers ...Handler) error

Do ......

func (*Context) IsStopped

func (c *Context) IsStopped() bool

IsStopped ......

func (*Context) Method

func (c *Context) Method() string

Method ......

func (*Context) Msg

func (c *Context) Msg() *RPCMessage

func (*Context) Next

func (c *Context) Next() (err error)

func (*Context) Release

func (c *Context) Release()

Release ......

func (*Context) Request

func (c *Context) Request() *http.Request

Request ......

func (*Context) SetContext

func (c *Context) SetContext(ctx context.Context) *Context

SetContext ......

func (*Context) SetMsg

func (c *Context) SetMsg(msg *RPCMessage)

func (*Context) Stop

func (c *Context) Stop()

Stop ......

func (*Context) Writer

func (c *Context) Writer() http.ResponseWriter

Writer ......

type ContextPool

type ContextPool struct {
	*sync.Pool
}

func (*ContextPool) Acquire

func (p *ContextPool) Acquire() *Context

Acquire ...

func (*ContextPool) Release

func (p *ContextPool) Release(c *Context)

Release ...

type Error

type Error struct {
	Code    ErrorCode   `json:"code"`
	Message string      `json:"message,omitempty"`
	Data    interface{} `json:"data,omitempty"`
}

Error ... Error codes

func NewError

func NewError(code ErrorCode, Msg string, data ...interface{}) *Error

NewError ...

func (*Error) Error

func (e *Error) Error() string

func (*Error) ErrorCode added in v1.0.0

func (e *Error) ErrorCode() int

func (*Error) ErrorData added in v1.0.0

func (e *Error) ErrorData() interface{}

type ErrorCode

type ErrorCode int

ErrorCode ... Error codes

const (
	ErrParse          ErrorCode = -32700
	ErrInvalidRequest ErrorCode = -32600
	ErrNoMethod       ErrorCode = -32601
	ErrBadParams      ErrorCode = -32602
	ErrInternal       ErrorCode = -32603
	ErrServer         ErrorCode = -32000

	ErrAuthorization ErrorCode = 401
	ErrForbidden     ErrorCode = 403
)

declared ...

type ForbiddenError

type ForbiddenError string

ForbiddenError ...

func (ForbiddenError) Error

func (e ForbiddenError) Error() string

type Handler

type Handler func(c *Context) (err error)

type HandlerCrypto added in v1.0.0

type HandlerCrypto struct {
	Key       string
	UseBase64 bool
}

func ParseHandlerCrypto added in v1.0.0

func ParseHandlerCrypto(str string) *HandlerCrypto

ParseHandlerCrypto ... str format: base64:xxx or xxx if str is empty, return nil if str is not start with base64:, return &HandlerCrypto{Key: str} if str is start with base64:, return &HandlerCrypto{Key: str[7:], UseBase64: true} if str[7:] is empty, return nil

type Handlers

type Handlers []Handler

func UpsertHandlers

func UpsertHandlers(handlers Handlers, newHandlers Handlers) Handlers

type IRPCError

type IRPCError interface {
	RPCError() *Error
}

type ItfAfterCall

type ItfAfterCall interface {
	AfterCall(ctx *Context, method string, args []reflect.Value, results []reflect.Value) error
}

type ItfBeforeCall

type ItfBeforeCall interface {
	BeforeCall(ctx *Context, method string, args []reflect.Value) error
}

type ItfConstructor

type ItfConstructor interface {
	Constructor()
}

ItfConstructor ...

type ItfExcludeMethod

type ItfExcludeMethod interface {
	ExcludeMethod() []string
}

ItfExcludeMethod ...

type ItfJ2rpcError added in v1.0.0

type ItfJ2rpcError interface {
	ErrorCode() int
	Error() string
	ErrorData() interface{}
}

type ItfNamespaceName

type ItfNamespaceName interface {
	J2rpcNamespaceName() string
}

ItfNamespaceName ...

type ItfStartup

type ItfStartup interface {
	Startup(rs RPCServer)
}

type Level

type Level uint32

Level type

const (
	PanicLevel Level = iota
	FatalLevel
	ErrorLevel
	WarnLevel
	InfoLevel
	DebugLevel
	TraceLevel
)

level

func ParseLevel

func ParseLevel(lvl string) Level

ParseLevel takes a string level and returns the log level constant.

func (*Level) MarshalText

func (l *Level) MarshalText() ([]byte, error)

MarshalText ...

func (*Level) String

func (l *Level) String() string

Convert the Level to a string. E.g. PanicLevel becomes "panic".

func (*Level) UnmarshalText

func (l *Level) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler.

type LevelLogger

type LevelLogger interface {
	SetOutput(io.Writer)
	Writer() io.Writer

	Print(...interface{})
	Printf(string, ...interface{})
	Println(...interface{})

	Fatal(...interface{})
	Fatalf(string, ...interface{})
	Fatalln(...interface{})

	Panic(...interface{})
	Panicf(string, ...interface{})
	Panicln(...interface{})

	Debugf(string, ...interface{})
	Infof(string, ...interface{})
	Warnf(string, ...interface{})
	Errorf(string, ...interface{})
}

LevelLogger ...

func NewLevelLogger

func NewLevelLogger(prefix string, out ...io.Writer) LevelLogger

NewLevelLogger ...

type Option

type Option struct {
	SnakeNamespace bool
	Service        interface{}
	Logger         LevelLogger
	//CryptoKey  request/response crypto key, default is nil (no crypto)
	CryptoKey string
}

Option ...

func (*Option) ParseRPCBody added in v1.0.0

func (o *Option) ParseRPCBody(body []byte, isDecrypt bool) (_ []byte, err error)

func (*Option) SetService

func (o *Option) SetService(s interface{}) *Option

SetService ...

type RPCMessage

type RPCMessage struct {
	ID      json.RawMessage `json:"id,omitempty"`
	Version string          `json:"jsonrpc,omitempty"`
	Method  string          `json:"method,omitempty"`
	Params  json.RawMessage `json:"params,omitempty"`
	Result  json.RawMessage `json:"result,omitempty"`
	Error   *Error          `json:"error,omitempty"`
}

RPCMessage A value of this type can a JSON-RPC request, notification, successful response or error response. Which one it is depends on the fields.

func RPCError

func RPCError(err error) *RPCMessage

func RPCResult

func RPCResult(result interface{}) *RPCMessage

type RPCServer

type RPCServer interface {
	SetCallBefore(before BeforeHandler)
	SetCallAfter(after AfterHandler)
	GetContext() *Context
	Use(handlers ...Handler)
	Opt() *Option
	Logger() LevelLogger
	SetLogger(logger LevelLogger)
	ServeHTTP(w http.ResponseWriter, r *http.Request)
	RegisterForApp(app interface{})
	Register(receiver interface{}, names ...string)
	Handler(ctx *Context)
	Stop()
	NamespaceService(method string) interface{}
}

RPCServer ...

func New

func New(opts ...*Option) RPCServer

New ...

type TokenError

type TokenError string

TokenError ...

func (TokenError) Error

func (t TokenError) Error() string

Jump to

Keyboard shortcuts

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