Skip to content

Commit

Permalink
Rename properties on the :transform node, and changed its structure.
Browse files Browse the repository at this point in the history
  • Loading branch information
green-coder committed Aug 11, 2020
1 parent 6385ef2 commit 80442e4
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 37 deletions.
12 changes: 5 additions & 7 deletions src/minimallist/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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)))

Expand Down Expand Up @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions src/minimallist/generator.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)]
Expand Down Expand Up @@ -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)

Expand Down
18 changes: 10 additions & 8 deletions src/minimallist/helper.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down
8 changes: 4 additions & 4 deletions src/minimallist/minimap.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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))]
Expand Down
16 changes: 8 additions & 8 deletions test/minimallist/core_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

Expand Down Expand Up @@ -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"
Expand Down
8 changes: 4 additions & 4 deletions test/minimallist/generator_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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)))))

Expand Down

0 comments on commit 80442e4

Please sign in to comment.