Использование spec/merge с spec/multi-spec

Могу ли я использовать s/merge с s/multi-spec? Например

(require '[clojure.spec :as s])

(s/def :field/common (s/keys :req-un [:field/type :field/name]
                             :opt-un [:field/default]))

(s/def :field/max-length int?)

(defmulti field-type :type)
(defmethod field-type :character [_]
  (s/merge :field/common
           (s/keys :req-un [:field/max-length])))
(defmethod field-type :foreign-key [_]
  (s/merge :field/common
           (s/keys :req-un [:field/references])))
(defmethod field-type :int [_]
  ;; ??? what to do here? - I only want the common keys
  :field/common)
(defmethod field-type :boolean [_]
  ;; ??? what to do here? - I only want the common keys
  :field/common)

(s/def ::field (s/multi-spec field-type :field/type))

Что бы я положил на :int а также :boolean методы? Мне нужны только общие поля.

1 ответ

Не уверен, что это именно то, что вам нужно, но я думаю, вы бы использовали обычный метод мультиметодного наследования.

(require '[clojure.spec :as s])

(s/def :field/common (s/keys :req-un [:field/type :field/name]
                             :opt-un [:field/default]))

(s/def :field/max-length int?)

(defmulti field-type :type)
(defmethod field-type :character [_]
  (s/merge :field/common
           (s/keys :req-un [:field/max-length])))
(defmethod field-type :foreign-key [_]
  (s/merge :field/common
           (s/keys :req-un [:field/references])))

(defmethod field-type :field/common [_]
  :field/common)

(derive :int :field/common)
(derive :boolean :field/common)

(s/def ::field (s/multi-spec field-type :field/type))
Другие вопросы по тегам