summaryrefslogtreecommitdiff
path: root/src/bot/method-macros.lisp
diff options
context:
space:
mode:
Diffstat (limited to 'src/bot/method-macros.lisp')
-rw-r--r--src/bot/method-macros.lisp16
1 files changed, 11 insertions, 5 deletions
diff --git a/src/bot/method-macros.lisp b/src/bot/method-macros.lisp
index 0500de9..d4f04ad 100644
--- a/src/bot/method-macros.lisp
+++ b/src/bot/method-macros.lisp
@@ -3,6 +3,7 @@
3(defpackage :ukkoclot/bot/method-macros 3(defpackage :ukkoclot/bot/method-macros
4 (:use :c2cl :iterate) 4 (:use :c2cl :iterate)
5 (:import-from :ukkoclot/state :bot) 5 (:import-from :ukkoclot/state :bot)
6 (:import-from :ukkoclot/strings :ends-with :lisp->camel-case)
6 (:import-from :ukkoclot/transport :do-call) 7 (:import-from :ukkoclot/transport :do-call)
7 (:export :define-tg-method)) 8 (:export :define-tg-method))
8(in-package :ukkoclot/bot/method-macros) 9(in-package :ukkoclot/bot/method-macros)
@@ -25,6 +26,12 @@
25 (iter (for param-spec in param-specs) 26 (iter (for param-spec in param-specs)
26 (collect (apply #'make-param param-spec)))) 27 (collect (apply #'make-param param-spec))))
27 28
29 (defun path-from-name (name)
30 (let ((str (lisp->camel-case (symbol-name name))))
31 (if (ends-with str "%")
32 (subseq str 0 (- (length str) 1))
33 str)))
34
28 (defun emit-append-to-args (param args) 35 (defun emit-append-to-args (param args)
29 `(setf ,args (acons ',(param-name param) ,(param-name param) ,args))) 36 `(setf ,args (acons ',(param-name param) ,(param-name param) ,args)))
30 37
@@ -35,7 +42,7 @@
35 (defun emit-defun-arg (param) 42 (defun emit-defun-arg (param)
36 `(,(param-name param) ,(param-default param))) 43 `(,(param-name param) ,(param-default param)))
37 44
38 (defun emit-defun (name return-type params method path) 45 (defun emit-defun (name return-type params method)
39 (let ((revparams (reverse params)) 46 (let ((revparams (reverse params))
40 (args (gensym "ARGS")) 47 (args (gensym "ARGS"))
41 (bot (gensym "BOT"))) 48 (bot (gensym "BOT")))
@@ -48,7 +55,7 @@
48 ,(param-default param)) 55 ,(param-default param))
49 ,(emit-append-to-args param args)) 56 ,(emit-append-to-args param args))
50 (emit-append-to-args param args)))) 57 (emit-append-to-args param args))))
51 (do-call ,bot ,method ,path ',return-type ,args))))) 58 (do-call ,bot ,method ,(path-from-name name) ',return-type ,args)))))
52 59
53 (defun emit-ftype (name return-type params) 60 (defun emit-ftype (name return-type params)
54 `(declaim (ftype (function (bot &key ,@(iter (for param in params) 61 `(declaim (ftype (function (bot &key ,@(iter (for param in params)
@@ -56,11 +63,10 @@
56 ,return-type) 63 ,return-type)
57 ,name)))) 64 ,name))))
58 65
59;; TODO: Automatically derive path from name
60(defmacro define-tg-method ( 66(defmacro define-tg-method (
61 (name type path &optional (method :POST)) 67 (name type &optional (method :POST))
62 &body param-specs) 68 &body param-specs)
63 (let ((params (parse-param-specs param-specs))) 69 (let ((params (parse-param-specs param-specs)))
64 `(progn 70 `(progn
65 ,(emit-ftype name type params) 71 ,(emit-ftype name type params)
66 ,(emit-defun name type params method path)))) 72 ,(emit-defun name type params method))))