gpt4 book ai didi

clojure - 使用 hugSQL def-db-fns 宏时如何使用 clj-kond 避免未解析的符号?

转载 作者:行者123 更新时间:2023-12-03 15:54:44 24 4
gpt4 key购买 nike

我使用 VS Code Calva 编写 Clojure扩展名,它使用 clj-kondo对我的代码执行静态分析。

我正在使用 HugSQL从 SQL 查询和语句创建 Clojure 函数。

我知道我可以处理数据库连接和 HugSQL 与库的集成,如 conman。 ,事实上我过去使用过它并且我喜欢它,但这次我想保持原样并自己与 HugSQL 交谈。

HugSQL 的 def-db-fns宏接受一个 SQL 文件并根据该文件中包含的 SQL 查询和语句创建 Clojure 函数。

我下面的代码有效,但 clj-kondo 提示 seed-mytable!是一个 Unresolved 符号。

(ns my-app.db
"This namespace represents the bridge between the database world and the clojure world."
(:require [environ.core :refer [env]]
[hugsql.core :as hugsql]
[nano-id.core :refer [nano-id]]))

;; This create the function seed-mytable!, but clj-kondo doesn't (cannot?) know it.
(hugsql/def-db-fns "sql/mytable.sql")

;; The functions created by HugSQL can accept a db-spec, a connection, a connection pool,
;; or a transaction object. Let's keep it simple and use a db-spec for a SQLite database.
(def db-spec {:classname "org.sqlite.JDBC"
:subprotocol "sqlite"
:subname (env :database-subname)})

(defn db-seed
"Populate the table with some fakes."
[]
(let [fakes [[(nano-id) "First fake title" "First fake content"]
[(nano-id) "Second fake title" "Second fake content"]]]

;; clj-kondo complains that seed-my-table! is an unresolved symbol
(seed-mytable! db-spec {:fakes fakes})))

我明白为什么 clj-kondo 提示: seed-mytable!没有在任何地方定义,它在调用 def-db-fns 时被“注入(inject)”到这个命名空间中。宏。

有没有办法在调用 hugsql/def-db-fns 后告诉 clj-kondo?宏这个符号确实存在吗?

可能它没那么有用,但这是我用 HugSQL 加载的 SQL 文件。
-- :name seed-mytable!
-- :command :execute
-- :result :affected
-- :doc Seed the `mytable` table with some fakes.
INSERT INTO mytable (id, title, content)
VALUES :t*:fakes;

最佳答案

来自 clj-kondo documentation :

有时 vars 是通过执行宏来引入的,例如使用 HugSQL 时的def-db-fns .您可以使用 declare 来抑制有关这些变量的警告。 .例子:

(ns hugsql-example
(:require [hugsql.core :as hugsql]))

(declare select-things)

;; this will define a var #'select-things:
(hugsql/def-db-fns "select_things.sql")

(defn get-my-things [conn params]
(select-things conn params))

如果 HugSQL 引入的符号数量变得太笨重,请考虑
引入一个单独的命名空间,HugSQL 在其中生成变量: foo.db.hugsql .然后您可以从 foo.db 引用此命名空间和 (require '[foo.db.hugsql :as sql]) (sql/insert! ...)而 clj-kondo 不会
提示这个。

关于clojure - 使用 hugSQL def-db-fns 宏时如何使用 clj-kond 避免未解析的符号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61727582/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com