map - 在 Clojure 中,是否有一种惯用的方式在宏定义中解构 map ?

标签 map macros clojure noir destructuring

我一直在 Web 项目中使用 noir,我想限制用户访问,这取决于他们对 defpage 宏定义的所有可能路由的访问级别(和子级别)。所以本来我有

(defpage [:post "/mysite"] {:as input}
  (if-not (has-reqd-user-level? :levelX :sublevelY "/grantedRoute")
    (noir.response/redirect "/insufficientRights")
    ...))

然后我认为这会摆脱样板代码:

(defmacro defpage-with-user-level [level sublevel granted-route route data expr]
  `(defpage ~route ~data
     (if-not (has-reqd-user-level? ~level ~sublevel ~granted-route)
       (noir.response/redirect "/insufficientRights")
       ~expr)))

最后,我们使用如下:

(defpage-with-user-level :levelX :sublevelY "/grantedRoute"
  [:post "/mysite"] {:as input}
  (html
    [:body [:h1 (str "Hello " (:name input) "!")]]))

但正如 Rich Hickey 发表的这篇文章中所述,https://groups.google.com/forum/#!msg/clojure/4II-HKr_Pu0/2IcKit99cagJ ,由于位置绑定(bind),感觉有点尴尬,当已经存在 map 时,这不是惯用的。

但是,我一直在寻找一些关于在宏中使用解构绑定(bind)的示例或讨论,遗憾的是,我没有找到任何明确的用法,因为它一直在传递未计算的表达式。

于是,我想到了以下解决方案:

(defmacro defpage-with-user-level [dts expr]
  `(defpage (:route ~dts) (:data ~dts)
     (if-not (has-reqd-user-level? (:level ~dts) (:sublevel ~dts) (:granted-route ~dts))
       (noir.response/redirect "/insufficientRights")
       ~expr)))

但是现在,并不清楚如何像上面的示例那样将映射局部变量的数据映射从 :get 和 :post 传递到局部变量。

我是否正确地保留了我的第一次尝试,还是我真的需要使用第二种方法?我希望不是。还有其他选择吗?请让我知道。

最佳答案

您的第一个解决方案很好。 Rich 所说的是使用普通的旧 map 来传递数据,而不是为每种类型的数据创建新的类型/类。例如:您可以使用简单的映射来表示用户信息,而不是创建一个类来表示用户数据。

就您的第二次尝试而言,您可以在宏中使用 map 解构:

(defmacro defpage-with-user-level [{:keys [route data level sublevel granted-route]} expr]
  `(defpage ~route ~data
     (if-not (has-reqd-user-level? ~level ~sublevel ~granted-route)
       (noir.response/redirect "/insufficientRights")
       ~expr)))

(defpage-with-user-level {:level :levelX 
                          :sublevel :sublevelY 
                          :granted-route "/grantedRoute"
                          :route [:post "/mysite"] 
                          :data {:as input}}
  (html
    [:body [:h1 (str "Hello " (:name input) "!")]]))

关于map - 在 Clojure 中,是否有一种惯用的方式在宏定义中解构 map ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15447047/

相关文章:

c++ - 错误 : c++ [map] does not name a type

c++ - 通过 mapped_type 的 r 值 move 插入映射条目

c++ - 为什么此代码使用带字符串的映射 (C++) 会出现运行时错误?

macros - 如何在 Rust 宏中通过引用捕获

c - 如何定义一个宏到一个宏

performance - 提高 ClojureScript 程序的性能

map - OpenLayers 投影

c++ - 当路径中存在同名文件时,跨平台方式包含系统头文件?

clojure - 如何在 clojure 中调用嵌套模板化构造函数

java - Clojure deftype 引用其他类型