[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/srht 54cb036635: Add GraphQL serializer.
|
From: |
ELPA Syncer |
|
Subject: |
[elpa] externals/srht 54cb036635: Add GraphQL serializer. |
|
Date: |
Fri, 10 Nov 2023 15:58:33 -0500 (EST) |
branch: externals/srht
commit 54cb0366356d1bfe6ae208634012f70c14d39025
Author: Aleksandr Vityazev <avityazev@posteo.org>
Commit: Aleksandr Vityazev <avityazev@posteo.org>
Add GraphQL serializer.
* lisp/srht-gql: New file.
# ---
---
lisp/srht-gql.el | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 76 insertions(+)
diff --git a/lisp/srht-gql.el b/lisp/srht-gql.el
new file mode 100644
index 0000000000..9e510bff6a
--- /dev/null
+++ b/lisp/srht-gql.el
@@ -0,0 +1,76 @@
+;;; srht-gql.el --- Sexp to GraphQL -*- lexical-binding: t; -*-
+
+;; Copyright © 2022-2023 Free Software Foundation, Inc.
+
+;; Created: <2023-11-10 Fri>
+
+;; This file is part of GNU Emacs.
+
+;; This program is free software; you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+
+;; This program is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+;;; Commentary:
+
+;;
+
+;;; Code:
+
+(declare-function json-encode "json" (object))
+(declare-function map-apply "map")
+
+(defun srht-gql--string (val)
+ (pcase val
+ ((pred symbolp)
+ (symbol-name val))
+ ((pred integerp)
+ (number-to-string val))
+ ((pred stringp)
+ (json-encode val))))
+
+(defun srht-gql--string-join (plist)
+ (let ((lst (map-apply (lambda (kw val)
+ (concat (substring (symbol-name kw) 1) ": "
+ (srht-gql--string val)))
+ plist)))
+ (mapconcat #'identity lst ", ")))
+
+(defun srht-gql--serialize-args (args)
+ "Serialize type arguments ARGS."
+ (when args
+ (format "(%s)" (srht-gql--string-join args))))
+
+(defun srht-gql--serialize-fields (lst)
+ "Serialize all type fields from the LST."
+ (let* ((type (plist-get lst :type))
+ (args (plist-get lst :arguments))
+ (fields (if type (plist-get lst :fields) lst)))
+ (concat (when type (symbol-name type))
+ (when args (srht-gql--serialize-args args))
+ "{"
+ (seq-reduce (lambda (acc field)
+ (concat acc " "
+ (if (symbolp field)
+ (symbol-name field)
+ (srht-gql--serialize-fields field))))
+ fields "")
+ "}")))
+
+(defun srht-gql-serialize (query)
+ "Serialize GraphQL QUERY."
+ (let ((q (symbol-name (plist-get query :query)))
+ (args (srht-gql--serialize-args (plist-get query :arguments)))
+ (fields (srht-gql--serialize-fields (plist-get query :fields))))
+ (concat "{" q args fields"}")))
+
+(provide 'srht-gql)
+;;; srht-gql.el ends here
| [Prev in Thread] |
Current Thread |
[Next in Thread] |
- [elpa] externals/srht 54cb036635: Add GraphQL serializer.,
ELPA Syncer <=