Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Clojure 棱柱模式强制-重命名映射键_Clojure_Plumatic Schema - Fatal编程技术网

Clojure 棱柱模式强制-重命名映射键

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)

我试图使用棱柱模式(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)
                     (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})