aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoão Távora2023-12-11 07:10:00 -0600
committerJoão Távora2023-12-11 07:10:00 -0600
commit5bbe6a5318ae2df8f29c160ccdd186ff71d0a87c (patch)
treefe963eb52898aecd97c3bae3b23f4193eab5a436
parentf6dc2cff9952197c3272c7a4f5e435d3e411fed6 (diff)
downloademacs-feature/jsonrpc-support-dap.tar.gz
emacs-feature/jsonrpc-support-dap.zip
Jsonrpc: rework jsonrpc-convert-to-endpoint againfeature/jsonrpc-support-dap
* lisp/jsonrpc.el (jsonrpc-convert-to-endpoint): Rework protocol, rework default implementation, rework docstring. (jsonrpc-convert-from-endpoint): Rework docstring. (jsonrpc-connection-send): Call jsonrpc-convert-to-endpoint with new protocol.
-rw-r--r--lisp/jsonrpc.el42
1 files changed, 31 insertions, 11 deletions
diff --git a/lisp/jsonrpc.el b/lisp/jsonrpc.el
index dc06358cea2..697fe8c8b88 100644
--- a/lisp/jsonrpc.el
+++ b/lisp/jsonrpc.el
@@ -134,15 +134,35 @@ immediately."
134 t)) 134 t))
135 135
136;;; API optional 136;;; API optional
137(cl-defgeneric jsonrpc-convert-to-endpoint (connection message method) 137(cl-defgeneric jsonrpc-convert-to-endpoint (connection message subtype)
138 "Convert JSONRPC message to a JSONRPCesque message accepted by endpoint. 138 "Convert MESSAGE to JSONRPCesque message accepted by endpoint.
139METHOD duplicates MESSAGE's `:method' property for requests and 139MESSAGE is a plist, jsonrpc.el's internal representation of a
140notifications. Return a plist." 140JSONRPC message. SUBTYPE is one of `request', `reply' or
141 (:method (_s message _method) `(:jsonrpc "2.0" ,@message))) 141`notification'.
142
143Return a plist to be serialized to JSON with `json-serialize' and
144transmitted to endpoint."
145 ;; TODO: describe representations and serialization in manual and
146 ;; link here.
147 (:method (_s message subtype)
148 `(:jsonrpc "2.0"
149 ,@(if (eq subtype 'reply)
150 ;; true JSONRPC doesn't have `method'
151 ;; fields in responses.
152 (cl-loop for (k v) on message by #'cddr
153 unless (eq k :method)
154 collect k and collect v)
155 message))))
142 156
143;;; API optional 157;;; API optional
144(cl-defgeneric jsonrpc-convert-from-endpoint (connection remote-message) 158(cl-defgeneric jsonrpc-convert-from-endpoint (connection remote-message)
145 "Convert JSONRPC-esque REMOTE-MESSAGE to a JSONRPC message plist." 159 "Convert JSONRPC-esque REMOTE-MESSAGE to a plist.
160REMOTE-MESSAGE is a plist read with `json-parse'.
161
162Return a plist of jsonrpc.el's internal representation of a
163JSONRPC message."
164 ;; TODO: describe representations and serialization in manual and
165 ;; link here.
146 (:method (_s remote-message) remote-message)) 166 (:method (_s remote-message) remote-message))
147 167
148 168
@@ -463,7 +483,10 @@ connection object, called when the process dies.")
463 ((symbolp method) (symbol-name method)) 483 ((symbolp method) (symbol-name method))
464 ((stringp method) method) 484 ((stringp method) method)
465 (t (error "[jsonrpc] invalid method %s" method))))) 485 (t (error "[jsonrpc] invalid method %s" method)))))
466 (let* ((converted (jsonrpc-convert-to-endpoint connection args method)) 486 (let* ((subtype (cond ((or result-supplied-p error) 'reply)
487 (id 'request)
488 (method 'notification)))
489 (converted (jsonrpc-convert-to-endpoint connection args subtype))
467 (json (jsonrpc--json-encode converted)) 490 (json (jsonrpc--json-encode converted))
468 (headers 491 (headers
469 `(("Content-Length" . ,(format "%d" (string-bytes json))) 492 `(("Content-Length" . ,(format "%d" (string-bytes json)))
@@ -474,10 +497,7 @@ connection object, called when the process dies.")
474 (cl-loop for (header . value) in headers 497 (cl-loop for (header . value) in headers
475 concat (concat header ": " value "\r\n") into header-section 498 concat (concat header ": " value "\r\n") into header-section
476 finally return (format "%s\r\n%s" header-section json))) 499 finally return (format "%s\r\n%s" header-section json)))
477 (jsonrpc--log-event connection converted 'client 500 (jsonrpc--log-event connection converted 'client subtype)))
478 (cond ((or result-supplied-p error) 'reply)
479 (id 'request)
480 (method 'notification)))))
481 501
482(defun jsonrpc-process-type (conn) 502(defun jsonrpc-process-type (conn)
483 "Return the `process-type' of JSONRPC connection CONN." 503 "Return the `process-type' of JSONRPC connection CONN."