克洛朱尔.在地图中创建具有动态名称关键字的规范



我有这张地图:

{:60 {:id 60, :quote "Lorem ipsum doloret sit anem", :author "foo name", :total 61}
:72 {:id 72, :quote "Excepteur sint occaecat cupidatat non", :author "Nietzsche", :total 61}
:56 {:id 56, :quote "Ut enim ad minim veniam, quis nostrud ", :author "O. Wilde", :total 61}
:58 {:id 58, :quote "Duis aute irure dolor in reprehenderit", :author "your mama", :total 61}}

我正在尝试创建它的规格,我想我有地图"内部":

(s/def ::id     (s/and int? pos?))
(s/def ::quote  (s/and string? #(>= (count %) 8)))
(s/def ::author (s/and string? #(>= (count %) 6)))
(s/def ::total  (s/and int? pos?))
(s/def ::quotes (s/and
(s/map-of ::id ::quote ::author ::total)
#(instance? PersistentTreeMap %)             ;; is a sorted-map (not just a map)
))

但是地图关键字是动态创建的,因此它们没有名称,我如何为这些类型的关键字定义规范并将其添加到(s/map-of函数?

只需使用整数作为映射键。关键字化任意字符串通常是错误的;关键字化数字是一个特别明显的反模式。那么规范很简单:它只是一个带有 int 键的地图。

map-of接受一个键谓词,一个值谓词和可选参数 - 而不是关键字列表。要定义映射中的关键字列表,您可以将 keys 函数与:req-un参数一起使用,因为您使用的是非限定键。

由于您尚未指定如何限制地图关键字,因此我假设它们可以是任何关键字。如果是这种情况,那么您可以更改以下内容,

(s/def ::key keyword?)
(s/def ::quotes (s/and
(s/map-of ::key (s/keys :req-un [::id
::quote
::author
::total]))
#(instance? clojure.lang.PersistentTreeMap %)))

使用上面的示例地图,我们可以看到此规范定义是对应的。

user=> (s/valid? ::quotes 
#=>           (into (sorted-map)
#=>                 {:60 {:id     60
#=>                       :quote  "Lorem ipsum doloret sit anem"
#=>                       :author "foo name"
#=>                       :total  61}
#=>                  :72 {:id     72
#=>                       :quote  "Excepteur sint occaecat cupidatat non"
#=>                       :author "Nietzsche"
#=>                       :total  61}
#=>                  :56 {:id     56
#=>                       :quote  "Ut enim ad minim veniam, quis nostrud "
#=>                       :author "O. Wilde"
#=>                       :total  61}
#=>                  :58 {:id     58
#=>                       :quote  "Duis aute irure dolor in reprehenderit"
#=>                       :author "your mama"
#=>                       :total  61}}))
true

最新更新