jsonrpc

package module
v0.0.0-...-12172e9 Latest Latest
Warning

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

Go to latest
Published: Dec 16, 2025 License: MIT Imports: 9 Imported by: 0

README

jsonrpc

A JSON RPC tool kit

Documentation

Index

Constants

View Source
const (
	ErrorCodeParseError     = -32700
	ErrorCodeInvalidRequest = -32600
	ErrorCodeMethodNotFound = -32601
	ErrorCodeInvalidParams  = -32602
	ErrorCodeInternalError  = -32603
	ErrorCodeServerError    = -32000
)

Variables

This section is empty.

Functions

func NewError

func NewError(code int, message string, data any) error

returns a new error

func NewHttpRequest

func NewHttpRequest(url string, req *Request) (*http.Request, error)

Types

type Error

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

func (Error) Error

func (e Error) Error() string

type HandlerFunc

type HandlerFunc func(context.Context, Params) (any, error)

func (HandlerFunc) HandleMethod

func (f HandlerFunc) HandleMethod(c context.Context, p Params) (any, error)

type HttpHandler

type HttpHandler struct {
	Server JsonRpcServer
}

Implements the http.Handle interface

func NewHttpHandler

func NewHttpHandler(server JsonRpcServer) *HttpHandler

func (*HttpHandler) ServeHTTP

func (h *HttpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

type Id

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

A JSON-RPC Id

func NewId

func NewId[T string | constraints.Integer](v T) Id

Create a new JSON-RPC Id

func NullId

func NullId() Id

func (Id) AsInt64

func (i Id) AsInt64() (int64, error)

func (Id) AsString

func (i Id) AsString() (string, error)

func (Id) MarshalJSON

func (id Id) MarshalJSON() ([]byte, error)

func (Id) String

func (id Id) String() string

func (*Id) UnmarshalJSON

func (i *Id) UnmarshalJSON(data []byte) error

type JsonRpcServer

type JsonRpcServer interface {
	ServeJsonRpc(ctx context.Context, req *Request) *Response
}

type Kind

type Kind byte

Kind represents each possible JSON token kind with a single byte, which is conveniently the first byte of that kind's grammar with the restriction that numbers always be represented with '0':

  • 'n': null
  • 'f': false
  • 't': true
  • '"': string
  • '0': number
  • '{': object begin
  • '}': object end
  • '[': array begin
  • ']': array end

An invalid kind is usually represented using 0, but may be non-zero due to invalid JSON data.

func (Kind) String

func (k Kind) String() string

String prints the kind in a humanly readable fashion.

type MethodHandler

type MethodHandler interface {
	HandleMethod(context.Context, Params) (any, error)
}

type Params

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

func NewParams

func NewParams(v any) (*Params, error)

func (Params) ByName

func (p Params) ByName() bool

func (Params) ByPosition

func (p Params) ByPosition() bool

func (Params) Decode

func (p Params) Decode(v any) error

Decode the params into a value

func (Params) IsAbsent

func (p Params) IsAbsent() bool

func (Params) MarshalJSON

func (p Params) MarshalJSON() ([]byte, error)

func (Params) String

func (p Params) String() string

func (*Params) UnmarshalJSON

func (p *Params) UnmarshalJSON(data []byte) error

type Request

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

func NewNotification

func NewNotification(method string, params *Params) *Request

func NewRequest

func NewRequest(method string, params *Params, id Id) *Request

func (*Request) IsNotification

func (r *Request) IsNotification() bool

type Response

type Response struct {
	JsonRpc string          `json:"jsonrpc"`
	Id      Id              `json:"id"`
	Result  json.RawMessage `json:"result,omitempty"`
	Error   *Error          `json:"error,omitempty"`
}

func NewErrorResp

func NewErrorResp(id Id, err *Error) *Response

func NewSuccessResp

func NewSuccessResp(id Id, result json.RawMessage) *Response

func (*Response) IsError

func (r *Response) IsError() bool

func (*Response) IsSuccess

func (r *Response) IsSuccess() bool

type Server

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

func NewServer

func NewServer() *Server

func (*Server) Register

func (s *Server) Register(method string, handler MethodHandler)

func (*Server) RegisterFunc

func (s *Server) RegisterFunc(method string, handler func(context.Context, Params) (any, error))

func (*Server) ServeJsonRpc

func (s *Server) ServeJsonRpc(ctx context.Context, req *Request) *Response

Directories

Path Synopsis
cmd
server command

Jump to

Keyboard shortcuts

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