1
讓我們看看在Clojure的規格Guide給出的例子爲clojure.spec/merge
發生器的兩套鑰匙的聯盟使用Clojure規格命名參數
(require '[clojure.spec :as spec]
'[clojure.spec.gen :as gen])
(spec/def :animal/kind string?)
(spec/def :animal/says string?)
(spec/def :animal/common (spec/keys :req [:animal/kind :animal/says]))
(spec/def :dog/tail? boolean?)
(spec/def :dog/breed string?)
(spec/def :animal/dog (spec/merge :animal/common
(spec/keys :req [:dog/tail? :dog/breed])))
從這個規範我們都可以產生數據,進而對其進行驗證:
(gen/generate (spec/gen :animal/dog))
=> {:animal/kind "bB", :animal/says "z9C0T465Q8OPXn5dUB8Wqk8K5Jnn",
:dog/tail? false, :dog/breed "B2MLQnj"}
(spec/valid? :animal/dog
{:animal/kind "bB", :animal/says "z9C0T465Q8OPXn5dUB8Wqk8K5Jnn",
:dog/tail? false, :dog/breed "B2MLQnj"})
=> true
但是,如果我們稍微修改的規範,這樣,這是對的命名參數而不是地圖的序列,像
(spec/def :animal/common (spec/keys* :req [:animal/kind :animal/says]))
(spec/def :animal/dog (spec/merge :animal/common
(spec/keys* :req [:dog/tail? :dog/breed])))
,我們仍然可以驗證數據,對規範:
(spec/valid? :animal/dog
'(:animal/kind "dog"
:animal/says "woof"
:dog/tail? true
:dog/breed "retriever"))
=> true
但我們確實失去了產生數據的能力:
(gen/generate (spec/gen :animal/dog))
; 1. Unhandled clojure.lang.ExceptionInfo
; Couldn't satisfy such-that predicate after 100 tries.
這是站在我這邊的錯誤,在規範的執行錯誤,或只是clojure.spec/merge
的意圖工作的方式?我們可以通過附加發生器來解決這個問題嗎?