From 526ae81396a16bbcae3564ec4af2c067a1ea13e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Moya?= Date: Tue, 6 Oct 2026 15:49:18 +0200 Subject: [PATCH] :lipstick: Extract a function to common module --- common/src/app/common/files/validate.cljc | 13 ++----- common/src/app/common/files/variant.cljc | 13 +++++++ .../test/common_tests/files/variant_test.cljc | 37 +++++++++++++++++++ 3 files changed, 53 insertions(+), 10 deletions(-) diff --git a/common/src/app/common/files/validate.cljc b/common/src/app/common/files/validate.cljc index 0166be934a..16c0bf56e3 100644 --- a/common/src/app/common/files/validate.cljc +++ b/common/src/app/common/files/validate.cljc @@ -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 diff --git a/common/src/app/common/files/variant.cljc b/common/src/app/common/files/variant.cljc index f43c5ecde5..f0e0c6ae8e 100644 --- a/common/src/app/common/files/variant.cljc +++ b/common/src/app/common/files/variant.cljc @@ -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: [ ]" [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]) diff --git a/common/test/common_tests/files/variant_test.cljc b/common/test/common_tests/files/variant_test.cljc index ef668e0e8c..63f3ca96b2 100644 --- a/common/test/common_tests/files/variant_test.cljc +++ b/common/test/common_tests/files/variant_test.cljc @@ -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 ;; ============================================================