rpc.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Author: NiuJiuRu
  2. // Email: niujiuru@qq.com
  3. package jsonrpc2
  4. import (
  5. "encoding/json"
  6. "fmt"
  7. )
  8. type ErrorCode int
  9. const (
  10. ErrParse ErrorCode = -32700 // 解析错误: "Parse error"
  11. ErrInvalidRequest ErrorCode = -32600 // 无效请求: "Invalid request"
  12. ErrMethodNotFound ErrorCode = -32601 // 无效方法: "Method not found"
  13. ErrInvalidParams ErrorCode = -32602 // 无效参数: "Invalid params"
  14. ErrInternal ErrorCode = -32603 // 内部错误: "Internal error"
  15. )
  16. type Request struct {
  17. JSONRPC string `json:"jsonrpc"` // 版本号, 固定: "2.0"
  18. Method string `json:"method"` // 调用方法, 执行函数名
  19. Params json.RawMessage `json:"params,omitempty"` // 请求参数, 可选可为空
  20. ID int `json:"id"` // 请求ID, 用于标识请求
  21. }
  22. type Error struct {
  23. Code ErrorCode `json:"code"` // 错误码
  24. Message string `json:"message"` // 错误信息
  25. }
  26. func (e Error) Error() string {
  27. return e.Message
  28. }
  29. type Response struct {
  30. JSONRPC string `json:"jsonrpc"` // 版本号, 固定: "2.0"
  31. Result json.RawMessage `json:"result,omitempty"` // 响应结果, 可选可为空
  32. Error *Error `json:"error,omitempty"` // 错误信息, 可选可为空
  33. ID int `json:"id"` // 应答ID, 响应匹配请求
  34. }
  35. // 解析请求数据
  36. func ParseRequest(jsonStr string) (*Request, error) {
  37. var req Request
  38. if err := json.Unmarshal([]byte(jsonStr), &req); err != nil {
  39. return nil, err
  40. }
  41. return &req, nil
  42. }
  43. // 构建响应数据
  44. func BuildResult(id int, result any) (*Response, error) {
  45. b, err := json.Marshal(result)
  46. if err != nil {
  47. return nil, err
  48. }
  49. return &Response{
  50. JSONRPC: "2.0",
  51. Result: b,
  52. ID: id,
  53. }, nil
  54. }
  55. // 构建错误响应
  56. func BuildError(id int, code ErrorCode, message string) *Response {
  57. return &Response{
  58. JSONRPC: "2.0",
  59. Error: &Error{
  60. Code: code,
  61. Message: message,
  62. },
  63. ID: id,
  64. }
  65. }
  66. // 构建解析错误
  67. func BuildParseError(id int) *Response {
  68. return BuildError(id, ErrParse, "Parse error")
  69. }
  70. // 构建无效请求
  71. func BuildInvalidRequest(id int) *Response {
  72. return BuildError(id, ErrInvalidRequest, "Invalid request")
  73. }
  74. // 构建无效方法
  75. func BuildMethodNotFound(id int) *Response {
  76. return BuildError(id, ErrMethodNotFound, "Method not found")
  77. }
  78. // 构建无效参数
  79. func BuildInvalidParams(id int) *Response {
  80. return BuildError(id, ErrInvalidParams, "Invalid params")
  81. }
  82. // 构建内部错误
  83. func BuildInternalError(id int) *Response {
  84. return BuildError(id, ErrInternal, "Internal error")
  85. }
  86. // 应答转字符串
  87. func (resp *Response) String() (string, error) {
  88. b, err := json.Marshal(resp)
  89. if err != nil {
  90. return "", fmt.Errorf("marshal response: %w", err)
  91. }
  92. return string(b), nil
  93. }