jsonrpc

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2021 License: MIT Imports: 8 Imported by: 0

README

jsonrpc

jsonrpc proto and schema in golang

Documentation

Overview

error defination https://www.jsonrpc.org/specification#error_object

Index

Constants

View Source
const (
	MTRequest = 1
	MTNotify  = 2
	MTResult  = 3
	MTError   = 4
)

Variables

View Source
var (
	ErrServerError = &RPCError{100, "server error", nil}
	ErrNilId       = &RPCError{102, "nil message id", nil}

	ErrMethodNotFound = &RPCError{-32601, "method not found", nil}

	ErrEmptyMethod = &RPCError{-32601, "empty method", nil}

	ErrParseMessage   = &RPCError{-32700, "parse error", nil}
	ErrInvalidRequest = &RPCError{-32600, "invalid request", nil}

	ErrInternalError = &RPCError{-32603, "internal error", nil}

	ErrMessageType = &RPCError{105, "wrong message type", nil}

	ErrTimeout     = &RPCError{200, "request timeout", nil}
	ErrBadResource = &RPCError{201, "bad resource", nil}
	ErrLiveExit    = &RPCError{202, "live exit", nil}

	ErrNotAllowed = &RPCError{406, "type not allowed", nil}
	ErrAuthFailed = &RPCError{401, "auth failed", nil}
)

Functions

func ConvertInt

func ConvertInt(v interface{}) int

func ConvertString

func ConvertString(v interface{}) string

func ConvertStringList

func ConvertStringList(v interface{}) []string

func EncodePretty

func EncodePretty(msg IMessage) (string, error)

IMessage methods

func ErrorResponse

func ErrorResponse(w http.ResponseWriter, r *http.Request, err error, status int, message string)

func GuessJson

func GuessJson(input string) (interface{}, error)

func GuessJsonArray

func GuessJsonArray(inputArr []string) ([]interface{}, error)

func IsMethod

func IsMethod(name string) bool

method names

func IsPublicMethod

func IsPublicMethod(name string) bool

func MarshalJson

func MarshalJson(data interface{}) (string, error)

func MessageBytes

func MessageBytes(msg IMessage) ([]byte, error)

func MessageInterface

func MessageInterface(msg IMessage) interface{}

func MessageJson

func MessageJson(msg IMessage) *simplejson.Json

A Message object to Json object for further trans

func MessageString

func MessageString(msg IMessage) string

func MustFloat

func MustFloat(input interface{}, prefix string) float64

func MustInt

func MustInt(input interface{}, prefix string) int64

func MustNumber

func MustNumber(input interface{}, prefix string) json.Number

func ValidateFloat

func ValidateFloat(v interface{}, prefix string) (float64, error)

func ValidateInt

func ValidateInt(v interface{}, prefix string) (int64, error)

func ValidateNumber

func ValidateNumber(v interface{}, prefix string) (json.Number, error)

func ValidateString

func ValidateString(v interface{}, prefix string) (string, error)

Types

type BaseMessage

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

func (BaseMessage) IsError

func (self BaseMessage) IsError() bool

IsError() returns if the message is a ErrorMessage

func (BaseMessage) IsNotify

func (self BaseMessage) IsNotify() bool

IsNotify() returns if the message is a NotifyMessage

func (BaseMessage) IsRequest

func (self BaseMessage) IsRequest() bool

IsRequest() returns if the message is a RequestMessage

func (BaseMessage) IsRequestOrNotify

func (self BaseMessage) IsRequestOrNotify() bool

IsRequestOrNotify() returns if the message is a RequestMessage or NotifyMessage

func (BaseMessage) IsResult

func (self BaseMessage) IsResult() bool

IsResult() returns if the message is a ResultMessage

func (BaseMessage) IsResultOrError

func (self BaseMessage) IsResultOrError() bool

IsResultOrError() returns if the message is a ResultMessage or ErrorMessage

func (*BaseMessage) SetRaw

func (self *BaseMessage) SetRaw(raw *simplejson.Json)

Set raw Json object to skip generating the same raw a little tip

func (*BaseMessage) SetTraceId

func (self *BaseMessage) SetTraceId(traceId string)

func (BaseMessage) TraceId

func (self BaseMessage) TraceId() string

type ErrorMessage

type ErrorMessage struct {
	BaseMessage
	Id    interface{}
	Error *RPCError
}

func NewErrorMessage

func NewErrorMessage(reqmsg IMessage, errbody *RPCError) *ErrorMessage

func RPCErrorMessage

func RPCErrorMessage(reqmsg IMessage, code int, message string, data interface{}) *ErrorMessage

func (*ErrorMessage) GetJson

func (self *ErrorMessage) GetJson() *simplejson.Json

func (ErrorMessage) Log

func (self ErrorMessage) Log() *log.Entry

func (ErrorMessage) MustError

func (self ErrorMessage) MustError() *RPCError

func (ErrorMessage) MustId

func (self ErrorMessage) MustId() interface{}

func (ErrorMessage) MustMethod

func (self ErrorMessage) MustMethod() string

func (ErrorMessage) MustParams

func (self ErrorMessage) MustParams() []interface{}

func (ErrorMessage) MustResult

func (self ErrorMessage) MustResult() interface{}

type IMessage

type IMessage interface {
	IsRequest() bool
	IsNotify() bool
	IsRequestOrNotify() bool
	IsResult() bool
	IsError() bool
	IsResultOrError() bool

	// TraceId
	SetTraceId(traceId string)
	TraceId() string

	// upvote
	GetJson() *simplejson.Json
	MustId() interface{}
	MustMethod() string
	MustParams() []interface{}
	MustResult() interface{}
	MustError() *RPCError

	Log() *log.Entry
}

func Parse

func Parse(parsed *simplejson.Json) (IMessage, error)

func ParseBytes

func ParseBytes(data []byte) (IMessage, error)

type NotifyMessage

type NotifyMessage struct {
	BaseMessage
	Method string
	Params []interface{}
	// contains filtered or unexported fields
}

func NewNotifyMessage

func NewNotifyMessage(method string, params []interface{}) *NotifyMessage

func (*NotifyMessage) GetJson

func (self *NotifyMessage) GetJson() *simplejson.Json

func (NotifyMessage) Log

func (self NotifyMessage) Log() *log.Entry

func (NotifyMessage) MustError

func (self NotifyMessage) MustError() *RPCError

func (NotifyMessage) MustId

func (self NotifyMessage) MustId() interface{}

func (NotifyMessage) MustMethod

func (self NotifyMessage) MustMethod() string

func (NotifyMessage) MustParams

func (self NotifyMessage) MustParams() []interface{}

func (NotifyMessage) MustResult

func (self NotifyMessage) MustResult() interface{}

type RPCError

type RPCError struct {
	Code    int
	Message string
	Data    interface{}
}

func NewErrMsgType

func NewErrMsgType(additional string) *RPCError

Create a new instance of ErrMessageType additional is the information to help identify error details

func ParamsError

func ParamsError(message string) *RPCError

func (*RPCError) Error

func (self *RPCError) Error() string

String Representation of RPCError object

func (RPCError) ToJson

func (self RPCError) ToJson() *simplejson.Json

Generate json represent of ErrorMessage.body refer to https://www.jsonrpc.org/specification#error_object

func (RPCError) ToMessage

func (self RPCError) ToMessage(reqmsg *RequestMessage) *ErrorMessage

Convert RPCError to ErrorMessage reqmsg is the original RequestMessage instance, the ErrorMessage will copy reqmsg's id property

func (*RPCError) WithData

func (self *RPCError) WithData(data interface{}) *RPCError

WithData create clone this RPCError object with data attached

type RequestMessage

type RequestMessage struct {
	BaseMessage
	Id     interface{}
	Method string
	Params []interface{}
	// contains filtered or unexported fields
}

func NewRequestMessage

func NewRequestMessage(id interface{}, method string, params []interface{}) *RequestMessage

func (RequestMessage) Clone

func (self RequestMessage) Clone(newId interface{}) *RequestMessage

func (*RequestMessage) GetJson

func (self *RequestMessage) GetJson() *simplejson.Json

Get Json

func (RequestMessage) Log

func (self RequestMessage) Log() *log.Entry

Log

func (RequestMessage) MustError

func (self RequestMessage) MustError() *RPCError

MustError

func (RequestMessage) MustId

func (self RequestMessage) MustId() interface{}

MustId

func (RequestMessage) MustMethod

func (self RequestMessage) MustMethod() string

MustMethod

func (RequestMessage) MustParams

func (self RequestMessage) MustParams() []interface{}

MustParams

func (RequestMessage) MustResult

func (self RequestMessage) MustResult() interface{}

MustResult

type ResultMessage

type ResultMessage struct {
	BaseMessage
	Id     interface{}
	Result interface{}
}

func NewResultMessage

func NewResultMessage(reqmsg IMessage, result interface{}) *ResultMessage

func (*ResultMessage) GetJson

func (self *ResultMessage) GetJson() *simplejson.Json

func (ResultMessage) Log

func (self ResultMessage) Log() *log.Entry

func (ResultMessage) MustError

func (self ResultMessage) MustError() *RPCError

func (ResultMessage) MustId

func (self ResultMessage) MustId() interface{}

func (ResultMessage) MustMethod

func (self ResultMessage) MustMethod() string

func (ResultMessage) MustParams

func (self ResultMessage) MustParams() []interface{}

func (ResultMessage) MustResult

func (self ResultMessage) MustResult() interface{}

type UID

type UID string

type CID uint64 type UID uint64

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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