Clojure 棱柱模式强制-重命名映射键
我试图使用棱柱模式(1.0.4)强制映射 我想强迫你Clojure 棱柱模式强制-重命名映射键,clojure,plumatic-schema,Clojure,Plumatic Schema,我试图使用棱柱模式(1.0.4)强制映射 我想强迫你 {:a 1} 到 对架构使用自定义匹配器: {:b s/Int} 但此代码不起作用: (require '[schema.core :as s]) (require '[schema.coerce :as coerce]) ((coerce/coercer {:b s/Int} (fn [s] (when (= s s/Keyword)
{:a 1}
到
对架构使用自定义匹配器:
{:b s/Int}
但此代码不起作用:
(require '[schema.core :as s])
(require '[schema.coerce :as coerce])
((coerce/coercer {:b s/Int}
(fn [s]
(when (= s s/Keyword)
(fn [x]
(if (= x :a)
:b
x)))))
{:a 1})
输出:
#schema.utils.ErrorContainer{:error {:b missing-required-key, :a disallowed-key}}
{:b Int} {:a 1}
=>
#schema.utils.ErrorContainer{:error {:b missing-required-key, :a disallowed-key}}
我尝试通过运行以下代码对其进行调试,该代码匹配架构中的所有内容,并输出当前值和匹配的架构:
((coerce/coercer {:b s/Int}
(fn [s]
(when true
(fn [x]
(println s x)
x))))
{:a 1})
输出:
#schema.utils.ErrorContainer{:error {:b missing-required-key, :a disallowed-key}}
{:b Int} {:a 1}
=>
#schema.utils.ErrorContainer{:error {:b missing-required-key, :a disallowed-key}}
看起来匹配器一到地图就爆炸了?模式首先将地图分解为与模式匹配的片段,然后将每个MapEntry强制为相应的MapEntry模式,依此类推。在你的情况下,这种分解是失败的,因此你永远无法找到关键 要实现您想要的功能,您必须将强制附加到映射模式,并在强制功能中使用例如
clojure.set/rename key
:
(def Foo {:b s/Int})
((coerce/coercer
Foo
(fn [s]
(when (= s Foo)
#(clojure.set/rename-keys % {:a :b}))))
{:a 1})