我不明白为什么出现以下规格错误。
谁能告诉我为什么?
(ns spec-test
(:require [clojure.spec.alpha :as s]
[clojure.spec.test.alpha :as stest]))
;;; spec definitions
(s/def :msds.fontspec/tag #{:fontspec})
(s/def ::id string?)
(s/def ::size int?)
(s/def ::family string?)
(s/def ::color string?)
(s/def :msds.fontspec/attrs
(s/keys :req-un [::id ::size ::family ::color]))
(s/def :msds.fontspec/content (s/cat))
(s/def ::fontspec
(s/keys :req-un [:msds.fontspec/tag :msds.fontspec/attrs :msds.fontspec/content]))
(s/def ::fontspecs (s/* ::fontspec))
;;; spec application
(defn transform-fontspecs
[fontspecs]
(->> fontspecs
(map (fn [{:keys [attrs] :as fontspec}]
[(get attrs :id) (dissoc attrs :id)] ))
(into {}) ))
(s/fdef transform-fontspecs
:args (s/cat :fontspec ::fontspecs))
(stest/instrument `transform-fontspecs)
;;; test code
(def test-sample
'({:tag :fontspec,
:attrs {:id "283f1EV", :size 21, :family "c2S6", :color ""},
:content ()}
{:tag :fontspec,
:attrs
{:id "BzYMt4eEm", :size -5, :family "q0xhOE3", :color "bhV3PsVh"},
:content ()}
{:tag :fontspec,
:attrs {:id "uA2i", :size -5, :family "HHGoD", :color "J47ZX935"},
:content ()}))
(transform-fontspecs test-sample)
以下是错误说明。
2. Unhandled clojure.lang.Compiler$CompilerException Error compiling src/spec_test.clj at (55:1) #:clojure.error{:phase :compile-syntax-check, :line 55, :column 1, :source "/home/philos/work/metapdf/src/spec_test.clj"} Compiler.java: 7648 clojure.lang.Compiler/load REPL: 1 metapdf.core/eval19243 REPL: 1 metapdf.core/eval19243 Compiler.java: 7177 clojure.lang.Compiler/eval Compiler.java: 7132 clojure.lang.Compiler/eval core.clj: 3214 clojure.core/eval core.clj: 3210 clojure.core/eval main.clj: 437 clojure.main/repl/read-eval-print/fn main.clj: 437 clojure.main/repl/read-eval-print main.clj: 458 clojure.main/repl/fn main.clj: 458 clojure.main/repl main.clj: 368 clojure.main/repl RestFn.java: 1523 clojure.lang.RestFn/invoke interruptible_eval.clj: 79 nrepl.middleware.interruptible-eval/evaluate interruptible_eval.clj: 55 nrepl.middleware.interruptible-eval/evaluate interruptible_eval.clj: 142 nrepl.middleware.interruptible-eval/interruptible-eval/fn/fn AFn.java: 22 clojure.lang.AFn/run session.clj: 171 nrepl.middleware.session/session-exec/main-loop/fn session.clj: 170 nrepl.middleware.session/session-exec/main-loop AFn.java: 22 clojure.lang.AFn/run Thread.java: 748 java.lang.Thread/run 1. Caused by clojure.lang.ExceptionInfo Spec assertion failed. Spec: #object[clojure.spec.alpha$regex_spec_impl$reify__2509 0x3660c4ee "clojure.spec.alpha$regex_spec_impl$reify__2509@3660c4ee"] Value: (({:tag :fontspec, :attrs {:id "283f1EV", :size 21, :family "c2S6", :color ""}, :content ()} {:tag :fontspec, :attrs {:id "BzYMt4eEm", :size -5, :family "q0xhOE3", :color "bhV3PsVh"}, :content ()} {:tag :fontspec, :attrs {:id "uA2i", :size -5, :family "HHGoD", :color "J47ZX935"}, :content ()})) Problems: val: ({:tag :fontspec, :attrs {:id "283f1EV", :size 21, :family "c2S6", :color ""}, :content ()} {:tag :fontspec, :attrs {:id "BzYMt4eEm", :size -5, :family "q0xhOE3", :color "bhV3PsVh"}, :content ()} {:tag :fontspec, :attrs {:id "uA2i", :size -5, :family "HHGoD", :color "J47ZX935"}, :content ()}) in: [0] failed: map? spec: :spec-test/fontspec alpha.clj: 132 clojure.spec.test.alpha/spec-checking-fn/conform! alpha.clj: 140 clojure.spec.test.alpha/spec-checking-fn/fn RestFn.java: 408 clojure.lang.RestFn/invoke spec_test.clj: 55 spec-test/eval19293 spec_test.clj: 55 spec-test/eval19293 Compiler.java: 7177 clojure.lang.Compiler/eval Compiler.java: 7636 clojure.lang.Compiler/load REPL: 1 metapdf.core/eval19243 REPL: 1 metapdf.core/eval19243 Compiler.java: 7177 clojure.lang.Compiler/eval Compiler.java: 7132 clojure.lang.Compiler/eval core.clj: 3214 clojure.core/eval core.clj: 3210 clojure.core/eval main.clj: 437 clojure.main/repl/read-eval-print/fn main.clj: 437 clojure.main/repl/read-eval-print main.clj: 458 clojure.main/repl/fn main.clj: 458 clojure.main/repl main.clj: 368 clojure.main/repl RestFn.java: 1523 clojure.lang.RestFn/invoke interruptible_eval.clj: 79 nrepl.middleware.interruptible-eval/evaluate interruptible_eval.clj: 55 nrepl.middleware.interruptible-eval/evaluate interruptible_eval.clj: 142 nrepl.middleware.interruptible-eval/interruptible-eval/fn/fn AFn.java: 22 clojure.lang.AFn/run session.clj: 171 nrepl.middleware.session/session-exec/main-loop/fn session.clj: 170 nrepl.middleware.session/session-exec/main-loop AFn.java: 22 clojure.lang.AFn/run .lang.Thread/run
答案 0 :(得分:4)
您应该检查Problems
和val
所在的部分。
问题是您使用的是failed: map?
之类的正则表达式操作,当嵌套时,它会对结构进行某种程度的展平,因此它期望使用映射而不是集合。
您应该在这里使用s/*
,恕我直言。
参见https://clojure.org/guides/spec#_collections
同样,coll-of
规范可能应该更新为不使用:msds.fontspec/content
,而仅使用cat
或类似的东西。