diff --git a/src/minimallist/core.cljc b/src/minimallist/core.cljc index 1b6bc18..26e4051 100644 --- a/src/minimallist/core.cljc +++ b/src/minimallist/core.cljc @@ -132,9 +132,8 @@ (-valid? context (:count-model model) (count data))) (implies (contains? model :condition-model) (-valid? context (:condition-model model) data))) - :transform (and (implies (contains? model :condition-model) - (-valid? context (:condition-model model) data)) - (-valid? context (:child-model model) ((:destruct model) data))) + :transform (and (-valid? context (:outer-model model) data) + (-valid? context (:inner-model model) ((:outer->inner model identity) data))) :let (-valid? (into context (:bindings model)) (:body model) data) :ref (-valid? context (get context (:key model)) data))) @@ -312,12 +311,11 @@ (-valid? context (:condition-model model) data))} {:valid? false})) {:valid? false}) - :transform (if (implies (contains? model :condition-model) - (-valid? context (:condition-model model) data)) - (let [description (-describe context (:child-model model) ((:destruct model) data))] + :transform (if (-valid? context (:outer-model model) data) + (let [description (-describe context (:inner-model model) ((:outer->inner model identity) data))] (if (:valid? description) {:valid? true - :desc ((:construct model) (:desc description))} + :desc ((:outer<-inner model identity) (:desc description))} {:valid? false})) {:valid? false}) :let (-describe (into context (:bindings model)) (:body model) data) diff --git a/src/minimallist/generator.cljc b/src/minimallist/generator.cljc index bc4162b..16abb34 100644 --- a/src/minimallist/generator.cljc +++ b/src/minimallist/generator.cljc @@ -100,7 +100,7 @@ [stack walked-bindings] (map-indexed vector entries))))) :transform (-> [[stack walked-bindings] model] - (reduce-update :child-model walk (conj path :child-model))) + (reduce-update :inner-model walk (conj path :inner-model))) :let (let [[[stack' walked-bindings'] walked-body] (walk [(conj stack {:bindings (:bindings model) :path (conj path :bindings)}) walked-bindings] @@ -171,7 +171,7 @@ (map (comp ::leaf-distance :model)))] (when (every? some? distances) (inc (reduce max 0 distances)))) - :transform (some-> (-> model :child-model ::leaf-distance) inc) + :transform (some-> (-> model :inner-model ::leaf-distance) inc) :let (some-> (-> model :body ::leaf-distance) inc) :ref (let [key (:key model) index (find-stack-index stack key) @@ -224,7 +224,7 @@ (map (comp ::min-cost :model))) content-cost (when (every? some? vals) (reduce + vals))] (some-> content-cost (+ container-cost))) - :transform (some-> (::min-cost (:child-model model)) inc) + :transform (some-> (::min-cost (:inner-model model)) inc) :let (::min-cost (:body model)) :ref (let [key (:key model) index (find-stack-index stack key)] @@ -495,9 +495,9 @@ inside-list? (gen/fmap (partial apply list)))))) (contains? model :condition-model) (gen/such-that (partial m/valid? context (:condition-model model)))) - :transform (cond->> (generator context (:child-model model) budget) - (contains? model :construct) (gen/fmap (:construct model)) - (contains? model :condition-model) (gen/such-that (partial m/valid? context (:condition-model model)))) + :transform (->> (generator context (:inner-model model) budget) + (gen/fmap (:outer<-inner model identity)) + (gen/such-that (partial m/valid? context (:outer-model model)))) :let (generator (merge context (:bindings model)) (:body model) budget) diff --git a/src/minimallist/helper.cljc b/src/minimallist/helper.cljc index 6349084..8a2d969 100644 --- a/src/minimallist/helper.cljc +++ b/src/minimallist/helper.cljc @@ -208,14 +208,16 @@ (defn transform "Transformation of a data matching the model. - `destruct` is used during validation and parsing, and - `construct` is used during parsing and generation." - ([model destruct] - {:type :transform - :child-model model - :destruct destruct}) - ([model destruct construct] - (assoc (transform model destruct) :construct construct))) + `outer-model` is the model viewed from outside this node. + `inner-model` is the model used for the inside of the node. + `outer->inner` is transforming data during validation and parsing, and + `outer<-inner` is transforming data during parsing and generation." + [outer-model inner-model outer->inner outer<-inner] + {:type :transform + :outer-model outer-model + :inner-model inner-model + :outer->inner outer->inner + :outer<-inner outer<-inner}) (defn let "Model with local model definitions." diff --git a/src/minimallist/minimap.cljc b/src/minimallist/minimap.cljc index d539f3a..3be7fed 100644 --- a/src/minimallist/minimap.cljc +++ b/src/minimallist/minimap.cljc @@ -61,10 +61,10 @@ [:condition-model (h/ref 'model)]) (h/with-condition (h/fn #(<= (:min %) (:max %)))))] [:transform (-> (h/map [:type (h/val :transform)] - [:child-model (h/ref 'model)] - [:destruct (h/fn fn?)]) - (h/with-optional-entries [:construct (h/fn fn?)] - [:condition-model (h/ref 'model)]))] + [:outer-model (h/ref 'model)] + [:inner-model (h/ref 'model)]) + (h/with-optional-entries [:outer->inner (h/fn fn?)] + [:outer<-inner (h/fn fn?)]))] [:let (h/map [:type (h/val :let)] [:bindings (h/map-of (h/fn any?) (h/ref 'model))] diff --git a/test/minimallist/core_test.cljc b/test/minimallist/core_test.cljc index 1ed998b..7581166 100644 --- a/test/minimallist/core_test.cljc +++ b/test/minimallist/core_test.cljc @@ -226,10 +226,10 @@ [:div {:a 1} "hei" [:p {} {} "bonjour"]]] ;; transform - (-> (h/transform (h/sequence-of (h/enum #{"A" "T" "G" "C"})) - #(mapv str (seq %)) - #(apply str %)) - (h/with-condition (h/fn string?))) + (h/transform (h/fn string?) + (h/sequence-of (h/enum #{"A" "T" "G" "C"})) + #(mapv str (seq %)) + #(apply str %)) ["" "A" "CGATCAT"] [:foobar "CGAUCAU" "AOEU"] @@ -452,10 +452,10 @@ [1 "a" 2 "b" 3 "c"] :invalid] ;; transform - (-> (h/transform (h/sequence-of (h/enum #{"A" "T" "G" "C"})) - #(mapv str (seq %)) - #(apply str %)) - (h/with-condition (h/fn string?))) + (h/transform (h/fn string?) + (h/sequence-of (h/enum #{"A" "T" "G" "C"})) + #(mapv str (seq %)) + #(apply str %)) ["" "" "A" "A" "CGATCAT" "CGATCAT" diff --git a/test/minimallist/generator_test.cljc b/test/minimallist/generator_test.cljc index 6095a07..b968a61 100644 --- a/test/minimallist/generator_test.cljc +++ b/test/minimallist/generator_test.cljc @@ -427,10 +427,10 @@ (is (every? (partial valid? model) (tcg/sample (gen model))))) - (let [model (-> (h/transform (h/sequence-of (h/enum #{"A" "T" "G" "C"})) - #(mapv str (seq %)) - #(apply str %)) - (h/with-condition (h/fn string?)))] + (let [model (h/transform (h/fn string?) + (h/sequence-of (h/enum #{"A" "T" "G" "C"})) + #(mapv str (seq %)) + #(apply str %))] (is (every? (partial valid? model) (tcg/sample (gen model)))))