Skip to content

Commit ce2598f

Browse files
authored
Merge pull request #41 from nubank/rm-promesa-as-default-applicative-engine
Remove the default engine behavior in internal applicative eval functions
2 parents ed9624c + ebc2fa6 commit ce2598f

File tree

4 files changed

+39
-44
lines changed

4 files changed

+39
-44
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Changelog
22

3+
## 2.0.2 / 2024-11-14
4+
- Remove the default engine behavior in internal applicative eval functions
5+
36
## 2.0.1 / 2024-11-07
47
- Allow implicit args for nodely syntax macros (e.g. >leaf) to include namespaces in the implicit arg symbols.
58

project.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
(defproject dev.nu/nodely "2.0.1"
1+
(defproject dev.nu/nodely "2.0.2"
22
:description "Decoupling data fetching from data dependency declaration"
33
:url "https://github.com/nubank/nodely"
44
:license {:name "MIT"}

src/nodely/engine/applicative.clj

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
[clojure.pprint :as pp]
66
[nodely.data :as data]
77
[nodely.engine.applicative.core :as app]
8-
[nodely.engine.applicative.promesa :as promesa]
98
[nodely.engine.applicative.protocols :as protocols]
109
[nodely.engine.core :as core]
1110
[nodely.engine.lazy-env :as lazy-env]))
@@ -75,37 +74,27 @@
7574
(validator node-ret node)))
7675

7776
(defn eval-key
78-
([env k]
79-
(eval-key env k {}))
80-
([env k opts]
81-
(let [opts (merge {::context promesa/context} opts)
82-
lazy-env (lazy-env/lazy-env env eval-in-context opts)]
83-
(::data/value (protocols/-extract (get lazy-env k))))))
77+
[env k opts]
78+
(let [lazy-env (lazy-env/lazy-env env eval-in-context opts)]
79+
(::data/value (protocols/-extract (get lazy-env k)))))
8480

8581
(defn eval-key-contextual
86-
([env k]
87-
(eval-key env k {}))
88-
([env k opts]
89-
(let [opts (merge {::context promesa/context} opts)
90-
lazy-env (lazy-env/lazy-env env eval-in-context opts)]
91-
(app/fmap ::data/value (get lazy-env k)))))
82+
[env k opts]
83+
(let [lazy-env (lazy-env/lazy-env env eval-in-context opts)]
84+
(app/fmap ::data/value (get lazy-env k))))
9285

9386
(defn eval-key-channel
94-
([env k]
95-
(eval-key-channel env k {}))
96-
([env k opts]
97-
(let [contextual-v (eval-key-contextual env k opts)
98-
chan (async/promise-chan)]
99-
(app/fmap (partial async/put! chan) contextual-v)
100-
chan)))
87+
[env k opts]
88+
(let [contextual-v (eval-key-contextual env k opts)
89+
chan (async/promise-chan)]
90+
(app/fmap (partial async/put! chan) contextual-v)
91+
chan))
10192

10293
(defn eval
103-
([env k]
104-
(eval env k {::context promesa/context}))
105-
([env k opts]
106-
(let [lazy-env (lazy-env/lazy-env env eval-in-context opts)]
107-
(protocols/-extract (get lazy-env k)) ;; ensures k is resolved
108-
(merge env
109-
(reduce (fn [acc [k v]] (assoc acc k (protocols/-extract v)))
110-
{}
111-
(lazy-env/scheduled-nodes lazy-env))))))
94+
[env k opts]
95+
(let [lazy-env (lazy-env/lazy-env env eval-in-context opts)]
96+
(protocols/-extract (get lazy-env k)) ;; ensures k is resolved
97+
(merge env
98+
(reduce (fn [acc [k v]] (assoc acc k (protocols/-extract v)))
99+
{}
100+
(lazy-env/scheduled-nodes lazy-env)))))

test/nodely/engine/applicative_test.clj

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,13 @@
124124

125125
(deftest eval-key-test
126126
(testing "eval promise"
127-
(is (match? 3 (applicative/eval-key test-env :c))))
127+
(is (match? 3 (applicative/eval-key test-env :c {::applicative/context promesa/context}))))
128128
(testing "async works"
129-
(let [[time-ns result] (criterium/time-body (applicative/eval-key test-env+delay :d))]
129+
(let [[time-ns result] (criterium/time-body (applicative/eval-key test-env+delay :d {::applicative/context promesa/context}))]
130130
(is (match? {:a 3 :b 6 :c 9} result))
131131
(is (match? (matchers/within-delta 100000000 1000000000) time-ns))))
132132
(testing "tricky example"
133-
(is (match? 4 (applicative/eval-key tricky-example :z)))))
133+
(is (match? 4 (applicative/eval-key tricky-example :z {::applicative/context promesa/context})))))
134134

135135
(deftest eval-key-test-core-async
136136
(testing "eval promise"
@@ -150,7 +150,7 @@
150150
(is (match? {:a {::data/value 2}
151151
:b {::data/value 1}
152152
:c {::data/value 3}}
153-
(applicative/eval test-env :c))))
153+
(applicative/eval test-env :c {::applicative/context promesa/context}))))
154154
(testing "tricky example"
155155
(is (match? {:x (data/value 1)
156156
:y (data/value 2)
@@ -160,29 +160,29 @@
160160
:w (data/value 4)
161161
:z {::data/type :leaf
162162
::data/inputs #{:w}}}
163-
(applicative/eval tricky-example :w)))))
163+
(applicative/eval tricky-example :w {::applicative/context promesa/context})))))
164164

165165
(deftest eval-env-with-sequence
166166
(testing "async response is equal to sync response"
167167
(is (match? (-> (core/resolve :b env-with-sequence) (get :b) ::data/value)
168-
(applicative/eval-key env-with-sequence :b))))
168+
(applicative/eval-key env-with-sequence :b {::applicative/context promesa/context}))))
169169
(testing "sync=async for sequence with nil values"
170170
(is (match? (-> (core/resolve :b env+sequence-with-nil-values) (get :b) ::data/value)
171-
(applicative/eval-key env+sequence-with-nil-values :b))))
171+
(applicative/eval-key env+sequence-with-nil-values :b {::applicative/context promesa/context}))))
172172
(testing "sync=async for sequence returning nil values"
173173
(is (match? (-> (core/resolve :b env+sequence-returning-nil-values) (get :b) ::data/value)
174-
(applicative/eval-key env+sequence-returning-nil-values :b))))
174+
(applicative/eval-key env+sequence-returning-nil-values :b {::applicative/context promesa/context}))))
175175
(testing "async version takes a third of the time of sync version
176176
(runtime diff is 2 sec, within a tolerance of 3ms"
177177
(let [[nanosec-sync _] (criterium/time-body (core/resolve :c env-with-sequence+delay-sync))
178-
[nanosec-async _] (criterium/time-body (applicative/eval-key env-with-sequence+delay :c))]
178+
[nanosec-async _] (criterium/time-body (applicative/eval-key env-with-sequence+delay :c {::applicative/context promesa/context}))]
179179
(is (match? (matchers/within-delta 8000000 2000000000)
180180
(- nanosec-sync nanosec-async)))))
181181
(testing "Actually computes the correct answers"
182-
(is (match? [2 3 4] (applicative/eval-key env-with-sequence+delay :c))))
182+
(is (match? [2 3 4] (applicative/eval-key env-with-sequence+delay :c {::applicative/context promesa/context}))))
183183
(testing "resolve closure sequence"
184184
(is (= [2 4 6]
185-
(applicative/eval-key env-with-closure-sequence :y)))))
185+
(applicative/eval-key env-with-closure-sequence :y {::applicative/context promesa/context})))))
186186

187187
(deftest schema-test
188188
(let [env-with-schema {:a (>value 2)
@@ -192,17 +192,20 @@
192192
:b (>value 1)
193193
:c (yielding-schema (>leaf (+ ?a ?b)) s/Bool)}]
194194
(testing "it should not fail"
195-
(is (match? 3 (applicative/eval-key env-with-schema :c {::applicative/fvalidate schema/fvalidate}))))
195+
(is (match? 3 (applicative/eval-key env-with-schema :c {::applicative/context promesa/context
196+
::applicative/fvalidate schema/fvalidate}))))
196197
(testing "returns ex-info when schema is selected as fvalidate, and schema fn validation is enabled"
197198
(is (thrown-match? clojure.lang.ExceptionInfo
198199
{:type :schema.core/error
199200
:schema java.lang.Boolean
200201
:value 3}
201202
(ex-data
202203
(s/with-fn-validation
203-
(applicative/eval-key env-with-failing-schema :c {::applicative/fvalidate schema/fvalidate}))))))
204+
(applicative/eval-key env-with-failing-schema :c {::applicative/context promesa/context
205+
::applicative/fvalidate schema/fvalidate}))))))
204206
(testing "doesn't validate when validation is disabled"
205-
(is (match? 3 (applicative/eval-key env-with-failing-schema :c {::applicative/fvalidate schema/fvalidate}))))))
207+
(is (match? 3 (applicative/eval-key env-with-failing-schema :c {::applicative/context promesa/context
208+
::applicative/fvalidate schema/fvalidate}))))))
206209

207210
(deftest synchronous-applicative-test
208211
(let [simple-env {:a (>value 2)

0 commit comments

Comments
 (0)