jsonrpc2

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeJSON

func DecodeJSON(raw json.RawMessage, v interface{}) error

Decode anything from JSON

func EncodeJSON

func EncodeJSON(v interface{}) (json.RawMessage, error)

Encode anything as JSON, but marshal nil slices as [], and nil maps as {}

func NewConn

func NewConn(parentCtx context.Context, transport Transport, handler Handler) *connImpl

Types

type Conn

type Conn interface {
	Call(method string, params interface{}, result interface{}) error
	Notify(method string, params interface{}) error
	Context() context.Context
	Close()
}

type Error

type Error struct {
	Code    ErrorCode        `json:"code"`
	Message string           `json:"message"`
	Data    *json.RawMessage `json:"data"`
}

A JSON-RPC 2.0 error, see https://www.jsonrpc.org/specification#error_object

func (*Error) Error

func (e *Error) Error() string

implement Go standard Error interface

func (*Error) GetData

func (e *Error) GetData(v interface{}) error

Gets the free-form "data" field. Note: this will panic if Data is nil, check that first.

func (*Error) SetData

func (e *Error) SetData(v interface{}) error

Sets the free-form "data" field

type ErrorCode

type ErrorCode = int64
const (
	CodeParseError     ErrorCode = -32700
	CodeInvalidRequest ErrorCode = -32600
	CodeMethodNotFound ErrorCode = -32601
	CodeInvalidParams  ErrorCode = -32602
	CodeInternalError  ErrorCode = -32603
)

Standard JSON-RPC2 error codes, see https://www.jsonrpc.org/specification#error_object

type Handler

type Handler interface {
	// Handle a request, returning either a result or an error (but not both)
	// If both are returned, a non-nil Error takes priority
	HandleRequest(conn Conn, req Request) (interface{}, error)

	// Handle a notification, returning nothing.
	HandleNotification(conn Conn, notif Notification)
}

A Handler reacts to incoming requests or notifications

type ID

type ID = int64

type Message

type Message struct {
	// *must* be set to "2.0"
	JsonRPC string `json:"jsonrpc"`

	// can be nil if notification or id-less error
	ID *ID `json:"id,omitempty"`
	// can be nil if result / error
	Method *string `json:"method,omitempty"`
	// can be nil if response
	Params *json.RawMessage `json:"params,omitempty"`
	// can be nil if request / notification
	Result *json.RawMessage `json:"result,omitempty"`
	// can be nil if success
	Error *Error `json:"error,omitempty"`
}

type Notification

type Notification struct {
	Method string
	Params *json.RawMessage
}

A JSON-RPC2 notification, see https://www.jsonrpc.org/specification#notification

type OutgoingCall

type OutgoingCall func(msg Message)

type ReadWriteClose

type ReadWriteClose interface {
	io.Reader
	io.Writer
	io.Closer
}

type Request

type Request struct {
	ID     ID
	Method string
	Params *json.RawMessage
}

A JSON-RPC2 request, see https://www.jsonrpc.org/specification#request_object

type Transport

type Transport interface {
	Read() ([]byte, error)
	Write(msg []byte) error
	Close() error
}

func NewRwcTransport

func NewRwcTransport(rwc ReadWriteClose) Transport

Jump to

Keyboard shortcuts

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