common-lisp - Lisp Webtales 第 5 章 - 如何让它工作

标签 common-lisp web-frameworks

继我的解决方案之后,将第 4 章中的代码用作 here , 我需要一些帮助调试 lisp webtales 第 5 章中的应用程序“linkdemo” .

我已经仔细输入了所有代码并消除了书中包含的一个错字。

CCL如果我发出 (ql:quickload "linkdemo")我明白了:

CCL is free software.  It is distributed under the terms of the Apache
Licence, Version 2.0.
? (ql:quickload "linkdemo")
To load "linkdemo":
  Load 1 ASDF system:
    linkdemo
; Loading "linkdemo"
.
;;; Checking for wide character support... yes, using code points.
;;; Checking for wide character support... yes, using code points.
;;; Building Closure with CHARACTER RUNES
.
> Error: The value NIL is not of the expected type STRING.
> While executing: ENSURE-SIMPLE-STRING, in process listener(1).
> Type :POP to abort, :R for a list of available restarts.
> Type :? for other options.
1 > 

如果我输入 :R我明白了:

1 > :R
>   Type (:C <n>) to invoke one of the following restarts:
0. Return to break level 1.
1. #<RESTART ABORT-BREAK #x133AEDD>
2. Retry compiling #P"/Users/m/quicklisp/local-projects/linkdemo/defmodule.lisp"
3. Skip compiling #P"/Users/m/quicklisp/local-projects/linkdemo/defmodule.lisp"
4. Retry compiling #<CL-SOURCE-FILE "linkdemo" "defmodule">.
5. Continue, treating compiling #<CL-SOURCE-FILE "linkdemo" "defmodule"> as having been successful.
6. Retry ASDF operation.
7. Retry ASDF operation after resetting the configuration.
8. Give up on "linkdemo"
9. Return to toplevel.
10. #<RESTART ABORT-BREAK #x133C53D>
11. Reset this thread
12. Kill this thread
1 > 

我从中推断出错误在 defmodule.lisp .

然后,我发现如果我注释掉 defmodule.lisp 中的以下整个 block :

(restas:define-policy datastore
  (:interface-package #:linkdemo.policy.datastore)
  (:interface-method-template "DATASTORE-~A")
  (:internal-package #:linkdemo.datastore)

  (define-method init ()                                    
    "Initiate the datastore")

  (define-method find-user (username)                       
    "Find the user by username")

  (define-method auth-user (username password)
    "Check if a user exists and has the supplied password")

  (define-method register-user (username password)
    "Register a new user")

  (define-method upvoted-p (link-id username)
    "Check if a user has upvoted a link")

  (define-method upvote (link-id user)
    "Upvote a link")

  (define-method post-link (url title user)
    "Post a new link")

  (define-method get-all-links (&optional user)
    "Get all of the links in the datastore")

  (define-method upvote-count (link-id)
    "Get the number of upvotes for a given link"))

然后重新发布(ql:quickload "linkdemo") ,我没有错误。

(另外,如果我再次评论它,并使用快速加载重新加载,我也没有收到错误。我不确定那里发生了什么或原因。)

无论我是否快速加载了上面有问题的部分,我都可以在 pg-datastore.lisp 中探索一个功能。 ,像这样:

1 > (linkdemo.pg-datastore::hash-password "42")
(:PASSWORD-HASH "71a8c8f54475acb5afee9eae061fa5f7ba838215f280259855e59a7c0ac768f8" :SALT "a283c5328f67c36595dd7277c54342f3")

似乎还不能尝试启动服务器,因为需要第 6 章中的代码才能完全发挥作用(我们还没有路由,只有带有 postmodern 的 DAO 层)。

所以不知道如何继续测试第 5 章中的代码/调试这个。
该错误似乎是restas本身的东西-也许。

寻求有关如何进行的任何有根据的猜测。

只需执行 (ql:quickload "restas") 即可重现该错误。然后评估上面给出的 block 。这给出了 Error: The value NIL is not of the expected type STRING.我不熟悉在 LISP 中使用其他人的代码,所以我对如何进行有点困惑。

更多信息 :这是对 define-method 的调用给出这个错误,源是文件policy.lisp here ,目前这有点超出我的理解。

我试过sbcl也:

* (ql:quickload "linkdemo")
To load "linkdemo":
  Load 1 ASDF system:
    linkdemo
; Loading "linkdemo"
.
;;; Checking for wide character support... WARNING: Lisp implementation doesn't use UTF-16, but accepts surrogate code points.
 yes, using code points.
;;; Building Closure with CHARACTER RUNES
.........
debugger invoked on a SB-KERNEL:CASE-FAILURE in thread
#<THREAD "main thread" RUNNING {10004F04C3}>:
  NIL fell through ETYPECASE expression.
  Wanted one of (SIMPLE-STRING STRING SB-FORMAT::FMT-CONTROL).

...

(SB-FORMAT::%FORMAT #<SB-IMPL::CHARACTER-STRING-OSTREAM {1003BD1673}> NIL ("UPVOTE-COUNT") ("UPVOTE-COUNT"))
0] 

最佳答案

仅定义一种方法时,我可以重现测试用例:

(restas:define-policy datastore
  (:interface-package #:linkdemo.policy.datastore)
  (:interface-method-template "DATASTORE-~A")
  (:internal-package #:linkdemo.datastore)
  (define-method init ()                                    
    "Initiate the datastore"))

这里的问题是,当您展开上述宏时,您会获得对 RESTAS::%DEFINE-POLICY 的函数调用。在哪里 :INTERNAL-FUNCTION-TEMPLATE给出 NIL ,这不是适当的格式字符串。这是因为 define-policy 的方式宏解析选项。

这是一个简化的示例:
(defun my-function (&key (x 0))
  (print x))
MY-FUNCTION :x 的默认值为 0 .如果您调用(my-function) ,它打印 0。但宏执行如下操作:
(defmacro my-macro (args)
  (let ((x-arg))
    (loop for (k v) in args 
       do (case k (:x (setf x-arg v))))
    `(my-function :x ,x-arg)))

它遍历参数列表以检查 :x 的某些关键字是否参数存在,然后将一个局部变量(初始化为 nil)设置为相应的值。
在所有情况下,它都会扩展为对 my-function 的调用。并显式绑定(bind) x值为 x-arg ,即使它为零。事实上,如果我们宏展开 (my-macro ()) , 如果给出 (my-function :x nil) .

由于函数已经定义了默认值,我们希望避免在其他地方重复这个默认值,所以宏应该注意不要给出 :x当其对应的值为 nil 时。或者更准确地说,当关键字参数不存在时(这里我不区分两种情况)。通常,这样做如下:
(defmacro my-macro (args)
  (flet ((maybe (k v) (and v (list k v))))
    (let ((x-arg))
      (loop for (k v) in args do (case k (:x (setf x-arg v))))
      `(my-function ,@(maybe :x x-arg)))))
,@拼接运算符用于根据值是否为 nil 将关键字参数注入(inject)到调用中。为 nil 时,拼接空列表。否则,属性列表 (list k v)是拼接的。因此,调用 (my-macro ())现在扩展为 (my-function) ,因此 x将采用其默认值。

这没有经过很好的测试,但我想你可以重新定义 define-policy 如下:
(defmacro define-policy (name &body body)
  (let (methods
        internal-package  internal-function-template
        interface-package interface-method-template)
    (iter (for item in body)
          (case (car item)
            (:internal-package
             (setf internal-package (second item)))
            (:internal-function-template
             (setf internal-function-template (second item)))
            (:interface-package
             (setf interface-package (second item)))
            (:interface-method-template
             (setf interface-method-template (second item)))
            (otherwise
             (cond
               ((string= (car item) "DEFINE-METHOD")
                (push (cdr item) methods))
               (t
                (error "Unknown DEFINE-POLICY option: ~A" item))))))
    (flet ((maybe (key value) (and value (list key value)))
           (maybe/quote (key value) (and value (list key `(quote ,value)))))
      `(eval-when (:compile-toplevel :load-toplevel :execute)
         (%define-policy ',name ',methods
                         ,@(maybe/quote :interface-package interface-package)
                         ,@(maybe :interface-method-template interface-method-template)
                         ,@(maybe/quote :internal-package internal-package)
                         ,@(maybe :internal-function-template internal-function-template))))))

或者,只需添加 :internal-function-template参数(例如 "~A" )到您的宏。

已修补:https://github.com/archimag/restas/commit/81bbbab6b36f81f846f78e71232e9d3d15f6d952

关于common-lisp - Lisp Webtales 第 5 章 - 如何让它工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58257183/

相关文章:

sql-server - 使用集成身份验证将 Windows 上的 SBCL 连接到 SQL Server

java - Wicket 的 Scala 版本

ruby - 做微框架是什么意思?

common-lisp - common lisp 中的 union,保留原始列表中元素的顺序

common-lisp - 循环宏和闭包的意外行为

scala - 在 FP 中处理 POST 的*正确*方法是什么?

web-applications - 解决双重提交问题

java - 是否有任何在编译时检查数据绑定(bind)的 JVM Web 框架?

macros - 定义一个宏来获取输入流

python - 如何以编程方式将注释插入 Microsoft Word 文档?