Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve qualified keywords to step functions #32

Merged
merged 3 commits into from
Mar 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,29 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [master](https://github.com/elasticpath/fonda/compare/v0.0.2...HEAD) - UNRELEASED
## [master](https://github.com/elasticpath/fonda/compare/v0.2.1...HEAD) - UNRELEASED

### Added

- Resolve qualified keywords to step functions. [#32](https://github.com/elasticpath/fonda/pull/32).

## [v0.2.1](https://github.com/elasticpath/fonda/compare/v0.0.2...v0.2.1) - 2018-02-12

### Changed

- Remove the ctx parameter to execute. [#21](https://github.com/elasticpath/fonda/pull/21)

## [v0.0.2](https://github.com/elasticpath/fonda/compare/v0.0.1...v0.0.2) - 2018-12-13

### Changed

- Add a simple test in `examples` [#20](https://github.com/elasticpath/fonda/pull/20)
- Introduced the notion of Log Map that is a subset of the Fonda Context passed to the logger functions
- Added the steps stack into the Fonda Context and the Log Map [#15](https://github.com/elasticpath/fonda/pull/16)
- Loggers are no longer blocking, and their result is ignored [#15](https://github.com/elasticpath/fonda/pull/16)
- No default loggers anymore [#15](https://github.com/elasticpath/fonda/pull/16)
- Move the :initial-ctx to the config and rename runtime to FondaContext [#15](https://github.com/elasticpath/fonda/pull/16)
- Eradicate set- from the code base [#8](https://github.com/elasticpath/fonda/pull/8)
- Add a simple test in `examples`. [#20](https://github.com/elasticpath/fonda/pull/20)
- Introduced the notion of Log Map that is a subset of the Fonda Context passed to the logger functions.
- Added the steps stack into the Fonda Context and the Log Map. [#15](https://github.com/elasticpath/fonda/pull/16)
- Loggers are no longer blocking, and their result is ignored. [#15](https://github.com/elasticpath/fonda/pull/16)
- No default loggers anymore. [#15](https://github.com/elasticpath/fonda/pull/16)
- Move the :initial-ctx to the config and rename runtime to FondaContext. [#15](https://github.com/elasticpath/fonda/pull/16)
- Eradicate set- from the code base. [#8](https://github.com/elasticpath/fonda/pull/8)

## [v0.0.1](https://github.com/elasticpath/fonda/compare/ece2cb8...v0.0.1) - 2018-11-17

Expand Down
8 changes: 4 additions & 4 deletions deps.edn
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{:paths ["src"]
:aliases {:test {:extra-paths ["test"]}
:dev {:extra-deps {org.clojure/clojure {:mvn/version "1.10.0-beta6"}
org.clojure/clojurescript {:mvn/version "1.10.439"}
expound {:mvn/version "0.7.1"}
:dev {:extra-deps {org.clojure/clojure {:mvn/version "1.10.0"}
org.clojure/clojurescript {:mvn/version "1.10.520"}
expound {:mvn/version "0.7.2"}
org.clojure/test.check {:mvn/version "0.10.0-alpha3" :exclusions [org.clojure/clojure]}
orchestra {:mvn/version "2018.11.07-1"}}}
orchestra {:mvn/version "2018.12.06-2"}}}
:pack {:extra-deps {pack/pack.alpha {:git/url "https://github.com/juxt/pack.alpha.git"
:sha "90a84a01c365fdac224bf4eef6e9c8e1d018a29e"}}}
:deploy {:extra-deps {deps-deploy {:git/url "https://github.com/slipset/deps-deploy.git"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"deploy:snapshot": "make clean && make jar && scripts/prepare-release --snapshot -s && scripts/deploy-jar -j fonda.jar"
},
"devDependencies": {
"shadow-cljs": "2.7.4"
"shadow-cljs": "2.8.15"
},
"dependencies": {}
}
2 changes: 1 addition & 1 deletion src/fonda/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
:on-success on-success
:on-anomaly on-anomaly
:on-exception on-exception
:queue (st/steps->queue steps)
:queue (into #queue [] st/xf steps)
:stack []})
(e/execute-steps)
(e/execute-loggers)
Expand Down
4 changes: 1 addition & 3 deletions src/fonda/execute.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,6 @@
;;;;;;;;;;;;
;; PUBLIC ;;
;;;;;;;;;;;;
(s/fdef execute-taps
:args (s/cat :fonda-ctx ::r/fonda-context))

(defn execute-loggers
"Executes one of the global tap functions.
Expand Down Expand Up @@ -112,4 +110,4 @@
(-> fonda-ctx
(assoc :queue (pop queue))
(assoc :stack (conj stack step))
(try-step step)))))))
(try-step step)))))))
26 changes: 26 additions & 0 deletions src/fonda/meta.cljc
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
(ns fonda.meta)

;; originally from the majestic work of Michael Drogalis in onyx
;; https://github.com/onyx-platform/onyx/blob/0.14.x/src/onyx/static/util.cljc

#?(:cljs
(defn munge-fn-name [kw]
(str (munge-str (str (namespace kw)))
"."
(munge-str (str (name kw))))))

#?(:cljs
(defn resolve-dynamic [kw]
(js/eval (munge-fn-name kw))))

(defn kw->fn
[kw]
#?(:clj
(try
(let [user-ns (symbol (namespace kw))
user-fn (symbol (name kw))]
(or (ns-resolve user-ns user-fn)
(throw (Exception.))))
(catch Throwable e
(throw (ex-info (str "Could not resolve symbol on the classpath, did you require the file that contains the symbol " kw "?") {:kw kw})))))
#?(:cljs (resolve-dynamic kw)))
38 changes: 21 additions & 17 deletions src/fonda/step.cljs
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
(ns fonda.step
(:require [cljs.spec.alpha :as s]))
(:require [cljs.spec.alpha :as s]
[fonda.meta :as meta]))

;; Common for all steps
(s/def ::name (s/or :k keyword? :s string?))
(s/def ::step-common
(s/keys :req-un [::name]))

;; Tap step
(s/def ::tap fn?)
(s/def ::tap (s/or :function fn? :qualified-keyword qualified-keyword?))
(s/def ::tap-step
(s/merge
::step-common
(s/keys :req-un [::tap])))
(s/merge ::step-common (s/keys :req-un [::tap])))

;; Processor step
(s/def ::path vector?)
(s/def ::processor fn?)
(s/def ::processor (s/or :function fn? :qualified-keyword qualified-keyword?))
(s/def ::processor-step
(s/merge
::step-common
(s/keys :req-un [::processor ::path])))
(s/merge ::step-common (s/keys :req-un [::processor ::path])))

(s/def ::step
(s/or :tap-step ::tap-step
Expand All @@ -42,11 +39,18 @@
;; Path were to attach the processor result on the context
path])

(defn steps->queue [steps]
(->> steps
(mapv (fn [{:as m :keys [tap processor]}]
(cond
tap (map->Tap m)
processor (map->Processor m)
:else (throw (ex-info "The step doesn't have tap neither resolve" m)))))
(into #queue [])))
(defn resolve-function
[fn-or-keyword]
(if (qualified-keyword? fn-or-keyword)
(meta/kw->fn fn-or-keyword)
fn-or-keyword))

(defn step->record
[{:keys [tap processor] :as step}]
(cond
tap (map->Tap (update step :tap resolve-function))
processor (map->Processor (update step :processor resolve-function))))

(def ^{:doc "Step transducer."}
xf
(map step->record))
2 changes: 1 addition & 1 deletion test/fonda/core_test.cljs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(ns fonda.core-test
(:require [cljs.test :refer-macros [deftest is testing run-tests use-fixtures async]]
(:require [cljs.test :refer-macros [deftest is testing async]]
[orchestra-cljs.spec.test :as orchestra]
[fonda.core :as fonda]
[fonda.anomaly]))
Expand Down
2 changes: 1 addition & 1 deletion test/fonda/loggers_test.cljs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(ns fonda.loggers-test
(:require [cljs.test :refer-macros [deftest is testing run-tests use-fixtures async]]
(:require [cljs.test :refer-macros [deftest is testing async]]
[orchestra-cljs.spec.test :as orchestra]
[fonda.core :as fonda]
[fonda.anomaly]))
Expand Down
23 changes: 11 additions & 12 deletions test/fonda/step_test.cljs
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
(ns fonda.step-test
(:require [cljs.test :refer-macros [deftest is testing run-tests use-fixtures async]]
(:require [cljs.test :refer-macros [deftest is testing]]
[orchestra-cljs.spec.test :as orchestra]
[fonda.step :as step]))

(orchestra/instrument)

(deftest empty-steps-test
(is (empty? (step/steps->queue []))))
(deftest processor-step-test
(let [step (step/step->record {:processor :cljs.core/inc
:path [:test]})]
(is (record? step) "the step should be a record")
(is (fn? (:processor step)) "the :processor key should become a function")))

(deftest steps-with-no-taps-test
(let [steps [{:processor identity
:name "test1"
:path [:test1]}

{:processor identity
:name "test2"
:path [:test2]}]]
(is (= steps (->> (step/steps->queue steps) (mapv #(into {} %)))))))
(deftest tap-step-test
(let [step (step/step->record {:tap :cljs.core/println
:path [:test]})]
(is (record? step) "the step should be a record")
(is (fn? (:tap step)) "the :tap key should become a function")))
Loading