💄 Extract a function to common module

This commit is contained in:
Andrés Moya 2026-10-06 15:49:18 +02:00
parent 6fca46caab
commit 526ae81396
3 changed files with 53 additions and 10 deletions

View File

@ -10,6 +10,7 @@
[app.common.data.macros :as dm]
[app.common.exceptions :as ex]
[app.common.files.helpers :as cfh]
[app.common.files.variant :as cfv]
[app.common.path-names :as cpn]
[app.common.schema :as sm]
[app.common.types.component :as ctk]
@ -592,15 +593,7 @@
"(empty)" ;; this may occur if the name contains only separators
clean-path)
extract-properties-names
(fn [shape]
;; Get the names of the properties of the shape's component
(->> shape
(#(ctkl/get-component file-data (:component-id %) true))
:variant-properties
(map :name)))
prop-names (extract-properties-names first-child)]
prop-names (cfv/extract-properties-names file-data first-child)]
(when-not (= container-name clean-path)
(report-error :variant-container-bad-name
@ -621,7 +614,7 @@
(str/ffmt "Main instance in variant % should have the variant-id of the container but has %" (:id child) (:variant-id child))
child file page
:variant-id shape-id))
(when (not= prop-names (extract-properties-names child))
(when (not= prop-names (cfv/extract-properties-names file-data child))
(report-error :invalid-variant-properties
(str/ffmt "Variant % has invalid properties %" (:id child) (vec prop-names))
child file page

View File

@ -36,6 +36,18 @@
reverse)
[]))))
(defn extract-properties-names
"Get the names of the properties of the component of which this shape
is main instance."
[data main-instance]
(if (and (ctk/main-instance? main-instance)
(ctv/variant-shape? main-instance))
(->> main-instance
(#(ctkl/get-component data (:component-id %) true))
:variant-properties
(map :name))
[]))
(defn extract-properties-values
"Get a map of variant property names to their distinct possible values,
collected from all components that belong to the variant container.
@ -60,6 +72,7 @@
Example return:
[<main-shape-a-id> <main-shape-b-id>]"
[data component]
(assert (ctv/valid-variant-component? component) "expected valid component variant")
(when-let [variant-id (:variant-id component)]
(let [page-id (:main-instance-page component)
objects (-> (dm/get-in data [:pages-index page-id])

View File

@ -11,6 +11,7 @@
[app.common.test-helpers.compositions :as tho]
[app.common.test-helpers.files :as thf]
[app.common.test-helpers.ids-map :as thi]
[app.common.test-helpers.shapes :as ths]
[app.common.test-helpers.variants :as thv]
[app.common.uuid :as uuid]
[clojure.test :as t]))
@ -64,6 +65,42 @@
(t/is (= (:variant-id (first result)) (thi/id :v01)))
(t/is (= (:variant-id (second result)) (thi/id :v01)))))
;; ============================================================
;; extract-properties-names
;; ============================================================
(t/deftest extract-properties-names-non-component
(let [file (-> (thf/sample-file :file1)
(ths/add-sample-shape :s01))
data (:data file)
shape (ths/get-shape file :s01)]
(t/is (= (fv/extract-properties-names data shape)
[]))))
(t/deftest extract-properties-names-non-variant
(let [file (-> (thf/sample-file :file1)
(tho/add-simple-component :c01 :m01 :s01))
data (:data file)
main (ths/get-shape file :m01)]
(t/is (= (fv/extract-properties-names data main)
[]))))
(t/deftest extract-properties-names-normal
(let [file (-> (thf/sample-file :file1)
(thv/add-variant :v01 :c01 :m01 :c02 :m02))
data (:data file)
main1 (ths/get-shape file :m01)]
(t/is (= (fv/extract-properties-names data main1)
["Property 1"]))))
(t/deftest extract-properties-names-two-properties
(let [file (-> (thf/sample-file :file1)
(thv/add-variant-two-properties :v01 :c01 :m01 :c02 :m02))
data (:data file)
main1 (ths/get-shape file :m01)]
(t/is (= (fv/extract-properties-names data main1)
["Property 1" "Property 2"]))))
;; ============================================================
;; extract-properties-values
;; ============================================================