| 80 | } |
| 81 | |
| 82 | func ParseMessage(data string) any { |
| 83 | var obj map[string]any |
| 84 | if err := json.Unmarshal([]byte(data), &obj); err != nil { |
| 85 | return nil |
| 86 | } |
| 87 | if obj["jsonrpc"] != "2.0" { |
| 88 | return nil |
| 89 | } |
| 90 | _, hasID := obj["id"] |
| 91 | _, hasMethod := obj["method"] |
| 92 | _, hasResult := obj["result"] |
| 93 | _, hasError := obj["error"] |
| 94 | if hasMethod && !hasID { |
| 95 | return JSONRPCNotification{JSONRPC: "2.0", Method: stringValue(obj["method"]), Params: mapValue(obj["params"])} |
| 96 | } |
| 97 | if hasMethod && hasID { |
| 98 | return JSONRPCRequest{JSONRPC: "2.0", ID: intValue(obj["id"]), Method: stringValue(obj["method"]), Params: mapValue(obj["params"])} |
| 99 | } |
| 100 | if hasID && hasError { |
| 101 | errMap := mapValue(obj["error"]) |
| 102 | if errMap == nil { |
| 103 | return nil |
| 104 | } |
| 105 | return JSONRPCError{JSONRPC: "2.0", ID: intValue(obj["id"]), Error: map[string]any{ |
| 106 | "code": intValue(errMap["code"]), |
| 107 | "message": stringValue(errMap["message"]), |
| 108 | "data": errMap["data"], |
| 109 | }} |
| 110 | } |
| 111 | if hasID && hasResult { |
| 112 | return JSONRPCResponse{JSONRPC: "2.0", ID: intValue(obj["id"]), Result: obj["result"]} |
| 113 | } |
| 114 | return nil |
| 115 | } |
| 116 | |
| 117 | func MakeRequest(method string, params map[string]any, requestID int) JSONRPCRequest { |
| 118 | return JSONRPCRequest{JSONRPC: "2.0", ID: requestID, Method: method, Params: params} |