我无法理解以下 clojure.spec 错误
I can't understand the following clojure.spec error
我不明白为什么会出现以下规范错误。
谁能告诉我为什么?
(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
您应该检查 Problems
部分,其中显示 val
和 failed: map?
。
问题是您正在使用像 s/*
这样的正则表达式操作,当嵌套时,它有点扁平化结构,因此它需要一个映射而不是一个集合。
恕我直言,您应该在这里使用 coll-of
。
参见 https://clojure.org/guides/spec#_collections
此外,:msds.fontspec/content
规范可能应该更新为不使用 cat
而只是 vector?
或类似的东西。
我不明白为什么会出现以下规范错误。
谁能告诉我为什么?
(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
您应该检查 Problems
部分,其中显示 val
和 failed: map?
。
问题是您正在使用像 s/*
这样的正则表达式操作,当嵌套时,它有点扁平化结构,因此它需要一个映射而不是一个集合。
恕我直言,您应该在这里使用 coll-of
。
参见 https://clojure.org/guides/spec#_collections
此外,:msds.fontspec/content
规范可能应该更新为不使用 cat
而只是 vector?
或类似的东西。