Documentation ¶
Index ¶
- Constants
- Variables
- func Call[T any](ctx context.Context, c Conn, method string, args ...any) (*T, error)
- func CallInto(ctx context.Context, c Conn, result any, method string, args ...any) error
- func ContextWithConn(ctx context.Context, c Conn) context.Context
- func Do[T any](ctx context.Context, c Conn, method string, args any) (*T, error)
- func EncodeError(enc *jx.Encoder, err error) error
- func IsBatchMessage(raw json.RawMessage) bool
- func MakeJrpcErr(s string) error
- func MarshalMessage(m *Message, enc *jx.Encoder) error
- func NewNull() json.RawMessage
- func UnmarshalMessage(m *Message, dec *jx.Decoder) error
- func WrapJrpcErr(err error) error
- type BatchCaller
- type BatchElem
- type ChainHandler
- type Closeder
- type Conn
- type DataError
- type Doer
- type DummyClient
- func (d *DummyClient) BatchCall(ctx context.Context, b ...*BatchElem) error
- func (d *DummyClient) Close() error
- func (d *DummyClient) Closed() <-chan struct{}
- func (d *DummyClient) Do(ctx context.Context, result any, method string, params any) error
- func (d *DummyClient) Mount(_ Middleware)
- func (d *DummyClient) Notify(ctx context.Context, method string, params any) error
- type Error
- type ErrorInvalidMessage
- type ErrorInvalidParams
- type ErrorInvalidRequest
- type ErrorMethodNotFound
- type ErrorParse
- type ErrorSubscriptionNotFound
- type HTTPError
- type Handler
- type HandlerFunc
- type HttpInfo
- type ID
- type JrpcErr
- type JsonError
- type Message
- type Middleware
- type Middlewares
- type Mounter
- type Notifier
- type PeerInfo
- type Reader
- type ReaderWriter
- type Request
- func (r *Request) Context() context.Context
- func (r *Request) MarshalJSON() ([]byte, error)
- func (r *Request) Msg() Message
- func (r *Request) ParamArray(a ...any) error
- func (r *Request) Remote() string
- func (r *Request) UnmarshalJSON(xs []byte) error
- func (r *Request) WithContext(ctx context.Context) *Request
- type RequestField
- type ResponseWriter
- type StreamingConn
- type Writer
Constants ¶
const ( ErrorCodeDefault = -32000 ErrorCodeApplication = -32080 ErrorCodeJrpc = -42000 )
const VersionString = "2.0"
Version represents a JSON-RPC version.
Variables ¶
var (
ErrIllegalExtraField = errors.New("invalid extra field")
)
var Null = json.RawMessage("null")
Functions ¶
func ContextWithConn ¶
ClientFromContext retrieves the client from the context, if any. This can be used to perform 'reverse calls' in a handler method.
func IsBatchMessage ¶
func IsBatchMessage(raw json.RawMessage) bool
isBatch returns true when the first non-whitespace characters is '['
func MakeJrpcErr ¶
func NewNull ¶
func NewNull() json.RawMessage
func WrapJrpcErr ¶
Types ¶
type BatchCaller ¶
type BatchElem ¶
type BatchElem struct { Method string Params any IsNotification bool // The result is unmarshaled into this field. Result must be set to a // non-nil pointer value of the desired type, otherwise the response will be // discarded. Result any // Error is set if the server returns an error for this request, or if // unmarshaling into Result fails. It is not set for I/O errors. Error error }
BatchElem is an element in a batch request.
type ChainHandler ¶
type ChainHandler struct { Endpoint Handler Middlewares Middlewares // contains filtered or unexported fields }
ChainHandler is a Handler with support for handler composition and execution.
func (*ChainHandler) ServeRPC ¶
func (c *ChainHandler) ServeRPC(w ResponseWriter, r *Request)
type DataError ¶
type DataError interface { Error() string // returns the message ErrorCode() int // returns the error code ErrorData() any // returns the error data }
A DataError contains some data in addition to the error message.
type DummyClient ¶
type DummyClient struct{}
func (*DummyClient) BatchCall ¶
func (d *DummyClient) BatchCall(ctx context.Context, b ...*BatchElem) error
func (*DummyClient) Close ¶
func (d *DummyClient) Close() error
func (*DummyClient) Closed ¶
func (d *DummyClient) Closed() <-chan struct{}
func (*DummyClient) Mount ¶
func (d *DummyClient) Mount(_ Middleware)
type ErrorInvalidMessage ¶
type ErrorInvalidMessage struct {
// contains filtered or unexported fields
}
received message is invalid
func (*ErrorInvalidMessage) Error ¶
func (e *ErrorInvalidMessage) Error() string
func (*ErrorInvalidMessage) ErrorCode ¶
func (e *ErrorInvalidMessage) ErrorCode() int
type ErrorInvalidParams ¶
type ErrorInvalidParams struct {
// contains filtered or unexported fields
}
unable to decode supplied params, or an invalid number of parameters
func NewInvalidParamsError ¶
func NewInvalidParamsError(message string) *ErrorInvalidParams
func (*ErrorInvalidParams) Error ¶
func (e *ErrorInvalidParams) Error() string
func (*ErrorInvalidParams) ErrorCode ¶
func (e *ErrorInvalidParams) ErrorCode() int
type ErrorInvalidRequest ¶
type ErrorInvalidRequest struct {
// contains filtered or unexported fields
}
received message isn't a valid request
func NewInvalidRequestError ¶
func NewInvalidRequestError(message string) *ErrorInvalidRequest
func (*ErrorInvalidRequest) Error ¶
func (e *ErrorInvalidRequest) Error() string
func (*ErrorInvalidRequest) ErrorCode ¶
func (e *ErrorInvalidRequest) ErrorCode() int
type ErrorMethodNotFound ¶
type ErrorMethodNotFound struct {
// contains filtered or unexported fields
}
func NewMethodNotFoundError ¶
func NewMethodNotFoundError(method string) *ErrorMethodNotFound
func (*ErrorMethodNotFound) Error ¶
func (e *ErrorMethodNotFound) Error() string
func (*ErrorMethodNotFound) ErrorCode ¶
func (e *ErrorMethodNotFound) ErrorCode() int
type ErrorParse ¶
type ErrorParse struct {
// contains filtered or unexported fields
}
Invalid JSON was received by the server.
func (*ErrorParse) Error ¶
func (e *ErrorParse) Error() string
func (*ErrorParse) ErrorCode ¶
func (e *ErrorParse) ErrorCode() int
type ErrorSubscriptionNotFound ¶
type ErrorSubscriptionNotFound struct {
// contains filtered or unexported fields
}
func (*ErrorSubscriptionNotFound) Error ¶
func (e *ErrorSubscriptionNotFound) Error() string
func (*ErrorSubscriptionNotFound) ErrorCode ¶
func (e *ErrorSubscriptionNotFound) ErrorCode() int
type HTTPError ¶
HTTPError is returned by client operations when the HTTP status code of the response is not a 2xx status.
type Handler ¶
type Handler interface {
ServeRPC(w ResponseWriter, r *Request)
}
http.handler, but for jrpc
type HandlerFunc ¶
type HandlerFunc func(w ResponseWriter, r *Request)
http.HandlerFunc,but for jrpc
func (HandlerFunc) ServeRPC ¶
func (fn HandlerFunc) ServeRPC(w ResponseWriter, r *Request)
type ID ¶
type ID json.RawMessage
ID is a Request identifier.
alternatively, ID can be null
func NewNullIDPtr ¶
func NewNullIDPtr() *ID
func NewNumberIDPtr ¶
func NewStringIDPtr ¶
func (ID) MarshalJSON ¶
MarshalJSON implements json.Marshaler.
func (*ID) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler.
type JsonError ¶
type JsonError struct { Code int `json:"code"` Message string `json:"message"` Data any `json:"data,omitempty"` }
encapsulate json rpc error into struct
type Message ¶
type Message struct { ID *ID `json:"id,omitempty"` Method string `json:"method,omitempty"` Params json.RawMessage `json:"params,omitempty"` Result json.RawMessage `json:"result,omitempty"` Error error `json:"error,omitempty"` ExtraFields []RequestField `json:"-"` }
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 ParseMessage ¶
func ParseMessage(in json.RawMessage) ([]*Message, bool)
parseMessage parses raw bytes as a (batch of) JSON-RPC message(s). There are no error checks in this function because the raw message has already been syntax-checked when it is called. Any non-JSON-RPC messages in the input return the zero value of Message.
func ReadMessage ¶
parseMessage parses raw bytes as a (batch of) JSON-RPC message(s). There are no error checks in this function because the raw message has already been syntax-checked when it is called. Any non-JSON-RPC messages in the input return the zero value of Message.
func (*Message) MarshalJSON ¶
func (*Message) UnmarshalJSON ¶
type Middleware ¶
type Middlewares ¶
type Middlewares []Middleware
Middlewares type is a slice of standard middleware handlers with methods to compose middleware chains and Handler's.
func Chain ¶
func Chain(middlewares ...func(Handler) Handler) Middlewares
Chain returns a Middlewares type from a slice of middleware handlers.
func (Middlewares) Handler ¶
func (mws Middlewares) Handler(h Handler) Handler
Handler builds and returns a Handler from the chain of middlewares, with `h Handler` as the final handler.
func (Middlewares) HandlerFunc ¶
func (mws Middlewares) HandlerFunc(h HandlerFunc) Handler
HandlerFunc builds and returns a Handler from the chain of middlewares, with `h Handler` as the final handler.
type Mounter ¶
type Mounter interface {
Mount(Middleware)
}
type Reader ¶
type Reader interface { // gets the peer info PeerInfo() PeerInfo // json.RawMessage can be an array of requests. if it is, then it is a batch request ReadBatch(ctx context.Context) (msgs []*Message, batch bool, err error) // closes the connection Close() error }
Reader can write JSON messages to its underlying connection Implementations must be safe for concurrent use
type ReaderWriter ¶
type Request ¶
func NewRawRequest ¶
func NewRequest ¶
NewRequest makes a new request
func NewRequestFromMessage ¶
func (*Request) MarshalJSON ¶
func (*Request) ParamArray ¶
func (*Request) UnmarshalJSON ¶
type RequestField ¶
type RequestField struct { Name string Value json.RawMessage }
RequestField is an idea borrowed from sourcegraphs implementation.
type ResponseWriter ¶
type ResponseWriter interface { Send(v any, err error) error Header() http.Header SetExtraField(k string, v any) error Notify(method string, v any) error }
http.ResponseWriter interface, but for jrpc
type StreamingConn ¶
func StreamingConnFromContext ¶
func StreamingConnFromContext(ctx context.Context) (StreamingConn, bool)
type Writer ¶
type Writer interface { // write json blob to stream io.Writer // Flush flushes the writer to the stream between messages Flush() error // Closed returns a channel which is closed when the connection is closed. Closed() <-chan struct{} // RemoteAddr returns the peer address of the connection. RemoteAddr() string }
Writer can write bytes messages to their underlying connection. Implementations must be safe for concurrent use.