Minor refactoring.

This commit is contained in:
Simon Brooke 2022-12-21 22:35:31 +00:00
parent 1b2adb5296
commit 21220970a8

View file

@ -117,37 +117,36 @@
:fault fault :fault fault
:narrative (messages fault))) :narrative (messages fault)))
(defmacro nil-if-empty
"if `x` is an empty collection, return `nil`; else return `x`."
[x]
`(if (coll? ~x)
(if (empty? ~x) nil ~x)
~x))
(defn object-faults (defn object-faults
"Return a list of faults found in object `x`, or `nil` if none are." "Return a list of faults found in object `x`, or `nil` if none are."
[x] [x]
(let [faults (remove (nil-if-empty
empty? (remove empty?
(list (list
(when-not (map? x) (when-not (map? x)
(make-fault-object (make-fault-object :critical :not-an-object))
:critical
:not-an-object))
(when-not (when-not
(has-context? x) (has-context? x)
(make-fault-object (make-fault-object :should :no-context))
:should
:no-context))
(when-not (:type x) (when-not (:type x)
(make-fault-object (make-fault-object :minor :no-type))
:minor
:no-type))
(when-not (and (map? x) (contains? x :id)) (when-not (and (map? x) (contains? x :id))
(make-fault-object (make-fault-object :minor :no-id-transient))))))
:minor
:no-id-transient))))]
(if (empty? faults) nil faults)))
(defn persistent-object-faults (defn persistent-object-faults
"Return a list of faults found in persistent object `x`, or `nil` if none are." "Return a list of faults found in persistent object `x`, or `nil` if none are."
[x] [x]
(let [faults (concat (nil-if-empty
(object-faults x)
(remove empty? (remove empty?
(concat
(object-faults x)
(list (list
(if (contains? x :id) (if (contains? x :id)
(try (let [id (URI. (:id x))] (try (let [id (URI. (:id x))]
@ -157,6 +156,5 @@
(make-fault-object :must :id-not-uri)) (make-fault-object :must :id-not-uri))
(catch NullPointerException _ (catch NullPointerException _
(make-fault-object :must :null-id-persistent))) (make-fault-object :must :null-id-persistent)))
(make-fault-object :must :no-id-persistent)))))] (make-fault-object :must :no-id-persistent)))))))
(if (empty? faults) nil faults)))