From 0e6ad43b6ccdf3c67d1e2f6fe2dcfab3e4cc3552 Mon Sep 17 00:00:00 2001 From: Uko Kokņevičs Date: Mon, 13 Oct 2025 06:06:51 +0300 Subject: Improve define-tg-method --- src/bot/method-macros.lisp | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'src/bot/method-macros.lisp') 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 @@ (defpackage :ukkoclot/bot/method-macros (:use :c2cl :iterate) (:import-from :ukkoclot/state :bot) + (:import-from :ukkoclot/strings :ends-with :lisp->camel-case) (:import-from :ukkoclot/transport :do-call) (:export :define-tg-method)) (in-package :ukkoclot/bot/method-macros) @@ -25,6 +26,12 @@ (iter (for param-spec in param-specs) (collect (apply #'make-param param-spec)))) + (defun path-from-name (name) + (let ((str (lisp->camel-case (symbol-name name)))) + (if (ends-with str "%") + (subseq str 0 (- (length str) 1)) + str))) + (defun emit-append-to-args (param args) `(setf ,args (acons ',(param-name param) ,(param-name param) ,args))) @@ -35,7 +42,7 @@ (defun emit-defun-arg (param) `(,(param-name param) ,(param-default param))) - (defun emit-defun (name return-type params method path) + (defun emit-defun (name return-type params method) (let ((revparams (reverse params)) (args (gensym "ARGS")) (bot (gensym "BOT"))) @@ -48,7 +55,7 @@ ,(param-default param)) ,(emit-append-to-args param args)) (emit-append-to-args param args)))) - (do-call ,bot ,method ,path ',return-type ,args))))) + (do-call ,bot ,method ,(path-from-name name) ',return-type ,args))))) (defun emit-ftype (name return-type params) `(declaim (ftype (function (bot &key ,@(iter (for param in params) @@ -56,11 +63,10 @@ ,return-type) ,name)))) -;; TODO: Automatically derive path from name (defmacro define-tg-method ( - (name type path &optional (method :POST)) + (name type &optional (method :POST)) &body param-specs) (let ((params (parse-param-specs param-specs))) `(progn ,(emit-ftype name type params) - ,(emit-defun name type params method path)))) + ,(emit-defun name type params method)))) -- cgit v1.2.3