common

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2024 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	WithoutError   = 0
	ParseError     = -32700
	InvalidRequest = -32600
	MethodNotFound = -32601
	InvalidParams  = -32602
	InternalError  = -32603
	CustomError    = -32000
)
View Source
const (
	JsonRpc = "2.0"
)

Variables

View Source
var CodeMap = map[int]string{
	ParseError:     "Parse error",
	InvalidRequest: "Invalid request",
	MethodNotFound: "Method not found",
	InvalidParams:  "Invalid params",
	InternalError:  "Internal error",
}
View Source
var RequiredFields = map[string]string{
	"id":      "id",
	"jsonrpc": "jsonrpc",
	"method":  "method",
	"params":  "params",
}

Functions

func CE

func CE(id any, jsonRpc string, errMessage string) any

func Capitalize

func Capitalize(str string) string

func Debug

func Debug(msg any)

func E

func E(id any, jsonRpc string, errCode int) any

func FilterRequestBody

func FilterRequestBody(jsonMap map[string]any) map[string]any

func GetResult

func GetResult(b []byte, result any) error

func GetSingleResponse

func GetSingleResponse(jsonData map[string]any, result any) error

func GetStruct

func GetStruct(d any, s any) error

func JsonBatchRs

func JsonBatchRs(data []any) []byte

func JsonRs

func JsonRs(id any, method string, params any) []byte

func ParseRequestBody

func ParseRequestBody(b []byte) (any, error)

func ParseRequestMethod

func ParseRequestMethod(method string) (sName string, mName string, err error)

func ParseResponseBody

func ParseResponseBody(b []byte) (any, error)

func ParseSingleRequestBody

func ParseSingleRequestBody(jsonMap map[string]any) (id any, jsonrpc string, method string, params any, errCode int)

func RegisterMethods

func RegisterMethods(s reflect.Type) map[string]*Method

func Rs

func Rs(id any, method string, params any) any

func S

func S(id any, jsonRpc string, result any) any

Types

type Error

type Error struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
	Data    any    `json:"data"`
}

type ErrorNotifyResponse

type ErrorNotifyResponse struct {
	JsonRpc string `json:"jsonrpc"`
	Error   Error  `json:"error"`
}

type ErrorResponse

type ErrorResponse struct {
	Id      string `json:"id"`
	JsonRpc string `json:"jsonrpc"`
	Error   Error  `json:"error"`
}

type Hooks

type Hooks struct {
	BeforeFunc func(id any, method string, params any) error
	AfterFunc  func(id any, method string, result any) error
}

type Method

type Method struct {
	Name       string
	ParamsType reflect.Type
	ResultType reflect.Type
	Method     reflect.Method
}

func RegisterMethod

func RegisterMethod(rm reflect.Method) *Method

type NotifyRequest

type NotifyRequest struct {
	JsonRpc string `json:"jsonrpc"`
	Method  string `json:"method"`
	Params  any    `json:"params"`
}

type Request

type Request struct {
	Id      string `json:"id"`
	JsonRpc string `json:"jsonrpc"`
	Method  string `json:"method"`
	Params  any    `json:"params"`
}

type Server

type Server struct {
	Sm          sync.Map
	Hooks       Hooks
	RateLimiter *rate.Limiter
}

func (*Server) After

func (svr *Server) After(id any, mName string, result any) error

func (*Server) Before

func (svr *Server) Before(id any, mName string, params any) error

func (*Server) Handler

func (svr *Server) Handler(b []byte) []byte

func (*Server) Register

func (svr *Server) Register(s any) error

func (*Server) RegisterWithName added in v0.0.5

func (svr *Server) RegisterWithName(s any, sname string) error

func (*Server) SingleHandler

func (svr *Server) SingleHandler(jsonMap map[string]any) any

type Service

type Service struct {
	Name string
	V    reflect.Value
	T    reflect.Type
	Mm   map[string]*Method
}

type SingleRequest

type SingleRequest struct {
	Method   string
	Params   any
	Result   any
	Error    *error
	IsNotify bool
}

type SuccessNotifyResponse

type SuccessNotifyResponse struct {
	JsonRpc string `json:"jsonrpc"`
	Result  any    `json:"result"`
}

type SuccessResponse

type SuccessResponse struct {
	Id      string `json:"id"`
	JsonRpc string `json:"jsonrpc"`
	Result  any    `json:"result"`
}

Jump to

Keyboard shortcuts

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