Skip to content

Commit

Permalink
Merge pull request #40 from nubank/fix-namespaced-symbols-bug
Browse files Browse the repository at this point in the history
Allow to include namespaced symbols in the implicit arg symbols
  • Loading branch information
joaopluigi authored Nov 7, 2024
2 parents 16ebe17 + 7b39009 commit ed9624c
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 10 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## 2.0.1 / 2024-11-07
- Allow implicit args for nodely syntax macros (e.g. >leaf) to include namespaces in the implicit arg symbols.

## 2.0.0 / 2024-10-17
- Breaking change: removed transitive dependencies
-- The following dependencies are no longer transitively provided,
Expand Down
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject dev.nu/nodely "2.0.0"
(defproject dev.nu/nodely "2.0.1"
:description "Decoupling data fetching from data dependency declaration"
:url "https://github.com/nubank/nodely"
:license {:name "MIT"}
Expand Down
36 changes: 27 additions & 9 deletions src/nodely/syntax.clj
Original file line number Diff line number Diff line change
@@ -1,24 +1,42 @@
(ns nodely.syntax
(:require
[clojure.set :as set]
[clojure.string :as string]
[clojure.walk :as walk]
[nodely.data :as data]))

(defn- expression-symbols
[expr]
(set (filter (complement seqable?) (tree-seq seqable? seq expr))))

(defn- question-mark->keyword
[s]
(-> (str s) (subs 1) keyword))

(defn- hygienic-expr-and-args
[expr named-args]
(let [symbols-to-swap (filter namespace named-args)
gensyms (reduce (fn [m s] (assoc m s (gensym (name s)))) {} symbols-to-swap)]
(if (seq symbols-to-swap)
{:expr (walk/postwalk-replace gensyms expr)
:args (into (->> gensyms
(map (fn [[k v]] [k [v (question-mark->keyword k)]]))
(into {}))
(->> (set/difference (set named-args)
(set symbols-to-swap))
(map (fn [s] [s [s (question-mark->keyword s)]]))))}
{:expr expr
:args (fn [s] [s (question-mark->keyword s)])})))

(defn- fn-with-arg-map
[args expr]
(let [arg-map (->> args
(map (fn [s]
[s (-> s name (subs 1) keyword)]))
(let [{new-expr :expr
args-mapping :args} (hygienic-expr-and-args expr args)
arg-map (->> args
(map args-mapping)
(into {}))]
(list `fn [(or (not-empty arg-map) '_)]
expr)))

(defn- question-mark->keyword
[s]
(-> (name s) (subs 1) keyword))
new-expr)))

(defn- question-mark-symbols
[expr]
Expand Down Expand Up @@ -49,7 +67,7 @@
[expr]
(let [symbols-to-be-replaced (question-mark-symbols expr)]
(assert-not-shadowing! symbols-to-be-replaced)
(list `data/leaf (mapv (comp keyword #(subs % 1) name) symbols-to-be-replaced)
(list `data/leaf (mapv question-mark->keyword symbols-to-be-replaced)
(fn-with-arg-map symbols-to-be-replaced expr))))

(defn >and
Expand Down
10 changes: 10 additions & 0 deletions test/nodely/syntax_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,13 @@
(testing "Flags a node with the blocking tag"
(is (match? #::data{:type :leaf :inputs #{:x :y} :fn ifn? :tags #{::data/blocking}}
(blocking (>leaf (+ ?x ?y)))))))

(deftest namespaced-question-mark-symbols
(testing "Can make leafs with question mark namespaced symbols"
(is (match? #::data{:type :leaf :inputs #{:foo/bar :baz/quux} :fn ifn?}
(>leaf (+ ?foo/bar ?baz/quux))))
(is (match? #::data{:type :sequence
:input :foo/bar
:process-node #::data{:type :value
:value ifn?}}
(>sequence inc ?foo/bar)))))

0 comments on commit ed9624c

Please sign in to comment.