spec

package
v0.0.27 Latest Latest
Warning

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

Go to latest
Published: Jun 16, 2022 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

View Source
const (
	JsonRpcVersion = "2.0"
)

Variables

This section is empty.

Functions

func JrpcTypeString

func JrpcTypeString(tp JrpcType) string

func JsonTypeString

func JsonTypeString(tp JsonType) string

Types

type BatchRequest

type BatchRequest []Request

If the batch rpc call itself fails to be recognized as an valid JSON or as an Array with at least one value, the response from the Server MUST be a single Response object.

If there are no Response objects contained within the Response array as it is to be sent to the client, the server MUST NOT return an empty Array and should return nothing at all. I.E. Client send: "[]" => Server does not reply at all

func ParseBatchRequest

func ParseBatchRequest(data []byte) *BatchRequest

Decodes byte slice to BatchRequest object and returns pointer to it. If the data was not compatible with an object this func will return nil

type BatchResponse

type BatchResponse []Response

If the batch rpc call itself fails to be recognized as an valid JSON or as an Array with at least one value, the response from the Server MUST be a single Response object.

If there are no Response objects contained within the Response array as it is to be sent to the client, the server MUST NOT return an empty Array and should return nothing at all. I.E. Client send: "[]" => Server does not reply at all

func ParseBatchResponse

func ParseBatchResponse(data []byte) *BatchResponse

Decodes byte slice to BatchResponse object and returns pointer to it. If the data was not compatible with an object this func will return nil

type Error

type Error struct {

	// A Number that indicates the error type that occurred.
	// This MUST be an integer.
	Code ErrorCode `json:"code"`

	// A String providing a short description of the error.
	// The message SHOULD be limited to a concise single sentence.
	Message ErrorMsg `json:"message"`

	// A Primitive or Structured value that contains additional information about the error.
	// This may be omitted.
	// The value of this member is defined by the Server (e.g. detailed error information, nested errors etc.).
	Data interface{} `json:"data,omitempty"`
}

func NewError

func NewError(code ErrorCode, data interface{}) *Error

Returns new Error object with provided ErrorCode and data. Sets Error message using ErrorCode

func ParseError

func ParseError(data []byte) *Error

Decodes byte slice to Error object and returns pointer to it. If the data was not compatible with an object this func will return nil

type ErrorCode

type ErrorCode int
const (
	ParseErrorCode     ErrorCode = -32700
	InvalidRequestCode ErrorCode = -32600
	MethodNotFoundCode ErrorCode = -32601
	InvalidParamsCode  ErrorCode = -32602
	InternalErrorCode  ErrorCode = -32603
)

type ErrorMsg

type ErrorMsg string
const (
	ParseErrorMsg     ErrorMsg = "Parse error"
	InvalidRequestMsg ErrorMsg = "Invalid Request"
	MethodNotFoundMsg ErrorMsg = "Method not found"
	InvalidParamsMsg  ErrorMsg = "Invalid params"
	InternalErrorMsg  ErrorMsg = "Internal error"
	ServerErrorMsg    ErrorMsg = "Server error"
)

func ErrorMessage

func ErrorMessage(code ErrorCode) ErrorMsg

type JrpcType

type JrpcType int

JrpcType represents all JsonRpc specification types

const (
	TypeNone JrpcType = iota
	TypeBatchRequest
	TypeBatchResponse
	TypeError
	TypeNotification
	TypeRequest
	TypeResponse
)

func GetJrpcType

func GetJrpcType(data []byte) (interface{}, JrpcType)

Converts byte slice to JsonRpc and returns map[Object] and Type. [Request, Response, Notification, Error, BatchRequest, BatchResponse, None]

func Parse

func Parse(data []byte) (interface{}, JrpcType)

This function is more as an example and its own implementation is preferred.

// Example for own implementation:
obj, tp := GetJrpcType(byteSlice) // would be wise to use switch{} on tp
req := spec.Request{}
mapstructure.Decode(obj, &req)

Decodes byte slice to one of JsonRpc types and returns data and JsonRpc type. for the working project.

// Example using Array:
dataArray := []byte(`[{"jsonrpc":"2.0","method":"calc_add","params":[7,3],"id":4418}]`)
ob, tp := spec.GetObject(dataArray)
if tp == spec.TypeBatchRequest {
	request := ob.(spec.Request)
	fmt.Println(request)
}

// Example using Object:
dataObject := []byte(`{"jsonrpc":"2.0","method":"calc_add","params":[7,3],"id":4418}`)
ob, tp := spec.GetObject(dataArray)
if tp == spec.TypeRequest {
	request := ob.(spec.Request)
	fmt.Println(request)
}

type JsonType

type JsonType int

JsonType represents json Array and Object

const (
	TypeJsonInvalid JsonType = iota
	TypeJsonArray
	TypeJsonObject
)

func GetJsonType

func GetJsonType(data []byte) JsonType

Checks if is json type [Array, Object, None]

type Notification

type Notification struct {
	// JSON-RPC protocol. MUST be exactly "2.0"
	Jsonrpc string `json:"jsonrpc"`

	// A String containing the name of the method to be invoked
	Method string `json:"method"`

	// A Structured value (array or object) that holds the parameter values to be
	// used during the invocation of the method.
	//
	// This member MAY be omitted.
	//
	// Array or Object from javascript JSON
	Params interface{} `json:"params"`
}

func NewNotification

func NewNotification() Notification

Returns new notification object with JsonRpc version attached

func ParseNotification

func ParseNotification(data []byte) *Notification

Decodes byte slice to Notification object and returns pointer to it. If the data was not compatible with an object this func will return nil

type Request

type Request struct {

	// JSON-RPC protocol. MUST be exactly "2.0"
	Jsonrpc string `json:"jsonrpc"`

	// A String containing the name of the method to be invoked
	Method string `json:"method"`

	// A Structured value that holds the parameter values to be
	// used during the invocation of the method.
	//
	// This member MAY be omitted.
	//
	// Array or Object from javascript JSON
	Params interface{} `json:"params"`

	// An identifier established by the Client that MUST contain
	// a String, Number, or NULL value
	// (this implementation ignores NULL)
	//
	// If it is not included it is assumed to be a notification.
	//
	// The Server MUST reply with the same value in the Response object if included.
	ID interface{} `json:"id,omitempty"`
}

func NewRequest

func NewRequest() Request

Returns new Request object with added JsonRpc version

func ParseRequest

func ParseRequest(data []byte) *Request

Decodes byte slice to Request object and returns pointer to it. If the data was not compatible with an object this func will return nil

func (*Request) IsNotification

func (r *Request) IsNotification() bool

Checks if request is a notification

type Response

type Response struct {
	// JSON-RPC protocol. MUST be exactly "2.0"
	Jsonrpc string `json:"jsonrpc"`

	// This member is REQUIRED on success.
	// This member MUST NOT exist if there was an error invoking the method.
	// The value of this member is determined by the method invoked on the Server.
	Result interface{} `json:"result,omitempty"`

	// This member is REQUIRED on error.
	// This member MUST NOT exist if there was no error triggered during invocation.
	// The value for this member MUST be an Object as defined in section 5.1.
	Error *Error `json:"error,omitempty"`

	// This member is REQUIRED.
	// It MUST be the same as the value of the id member in the Request Object.
	// If there was an error in detecting the id in the Request object
	// (e.g. Parse error/Invalid Request), it MUST be Null.
	ID interface{} `json:"id"`
}

func NewResponse

func NewResponse(id interface{}, result interface{}) Response

Returns new Response object with provided ID and result object

func NewResponseError

func NewResponseError(id interface{}, err Error) Response

Returns new error Response object with provided ID and error object

func ParseResponse

func ParseResponse(data []byte) *Response

Decodes byte slice to Response object and returns pointer to it. If the data was not compatible with an object this func will return nil

Jump to

Keyboard shortcuts

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