|
|
@@ -0,0 +1,105 @@
|
|
|
+package utils
|
|
|
+
|
|
|
+import "encoding/json"
|
|
|
+
|
|
|
+type RpcErrorCode int
|
|
|
+
|
|
|
+const (
|
|
|
+ ErrParse RpcErrorCode = -32700 // Parse error (解析错误)
|
|
|
+ ErrInvalidRequest RpcErrorCode = -32600 // Invalid Request (无效请求)
|
|
|
+ ErrMethodNotFound RpcErrorCode = -32601 // Method not found (无效方法)
|
|
|
+ ErrInvalidParams RpcErrorCode = -32602 // Invalid params (无效参数)
|
|
|
+ ErrInternal RpcErrorCode = -32603 // Internal error (内部错误)
|
|
|
+)
|
|
|
+
|
|
|
+type RpcRequest struct {
|
|
|
+ JSONRPC string `json:"jsonrpc"` // 版本号, 固定: "2.0"
|
|
|
+ Method string `json:"method"` // 调用方法, 执行函数名
|
|
|
+ Params json.RawMessage `json:"params,omitempty"` // 请求参数, 可选可为空
|
|
|
+ ID int `json:"id"` // 请求ID, 用于标识请求
|
|
|
+}
|
|
|
+
|
|
|
+type RpcError struct {
|
|
|
+ Code RpcErrorCode `json:"code"` // 错误码
|
|
|
+ Message string `json:"message"` // 错误信息
|
|
|
+}
|
|
|
+
|
|
|
+type RpcResponse struct {
|
|
|
+ JSONRPC string `json:"jsonrpc"` // 版本号, 固定: "2.0"
|
|
|
+ Result json.RawMessage `json:"result,omitempty"` // 响应结果, 可选可为空
|
|
|
+ Error *RpcError `json:"error,omitempty"` // 错误信息, 可选可为空
|
|
|
+ ID int `json:"id"` // 应答ID, 响应匹配请求
|
|
|
+}
|
|
|
+
|
|
|
+// 解析请求数据
|
|
|
+func ParseRequest(jsonStr string) (*RpcRequest, error) {
|
|
|
+ var req RpcRequest
|
|
|
+ if err := json.Unmarshal([]byte(jsonStr), &req); err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ return &req, nil
|
|
|
+}
|
|
|
+
|
|
|
+// 构建响应数据
|
|
|
+func BuildResponseResult(id int, result any) (*RpcResponse, error) {
|
|
|
+ b, err := json.Marshal(result)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ return &RpcResponse{
|
|
|
+ JSONRPC: "2.0",
|
|
|
+ Result: b,
|
|
|
+ ID: id,
|
|
|
+ }, nil
|
|
|
+}
|
|
|
+
|
|
|
+// 构建错误响应
|
|
|
+func BuildResponseError(id int, code RpcErrorCode, message string) *RpcResponse {
|
|
|
+ return &RpcResponse{
|
|
|
+ JSONRPC: "2.0",
|
|
|
+ Error: &RpcError{
|
|
|
+ Code: code,
|
|
|
+ Message: message,
|
|
|
+ },
|
|
|
+ ID: id,
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// 构建解析错误
|
|
|
+func ResponseParseError(id int) *RpcResponse {
|
|
|
+ return BuildResponseError(id, ErrParse, "Parse error")
|
|
|
+}
|
|
|
+
|
|
|
+// 构建无效请求
|
|
|
+func ResponseInvalidRequest(id int) *RpcResponse {
|
|
|
+ return BuildResponseError(id, ErrInvalidRequest, "Invalid Request")
|
|
|
+}
|
|
|
+
|
|
|
+// 构建无效方法
|
|
|
+func ResponseMethodNotFound(id int) *RpcResponse {
|
|
|
+ return BuildResponseError(id, ErrMethodNotFound, "Method not found")
|
|
|
+}
|
|
|
+
|
|
|
+// 构建无效参数
|
|
|
+func ResponseInvalidParams(id int) *RpcResponse {
|
|
|
+ return BuildResponseError(id, ErrInvalidParams, "Invalid params")
|
|
|
+}
|
|
|
+
|
|
|
+// 构建内部错误
|
|
|
+func ResponseInternalError(id int) *RpcResponse {
|
|
|
+ return BuildResponseError(id, ErrInternal, "Internal error")
|
|
|
+}
|
|
|
+
|
|
|
+// 构建用户错误
|
|
|
+func ResponseCustomError(id int, code RpcErrorCode, message string) *RpcResponse {
|
|
|
+ return BuildResponseError(id, code, message)
|
|
|
+}
|
|
|
+
|
|
|
+// 应答转字符串
|
|
|
+func (resp *RpcResponse) ToString() (string, error) {
|
|
|
+ b, err := json.Marshal(resp)
|
|
|
+ if err != nil {
|
|
|
+ return "", err
|
|
|
+ }
|
|
|
+ return string(b), nil
|
|
|
+}
|