Add function to check if a token can be placed under a name path

This commit is contained in:
Florian Schroedl
2024-06-28 14:39:36 +02:00
parent 48a7c52664
commit 174d91a519
3 changed files with 53 additions and 5 deletions

View File

@@ -18,3 +18,31 @@
(let [path (token-name->path name)]
(assoc-in acc path token))))
{} tokens))
(defn token-name-path-exists?
"Traverses the path from `token-name` down a `token-tree` and checks if a token at that path exists.
It's not allowed to create a token inside a token. E.g.:
Creating a token with
{:name \"foo.bar\"}
in the tokens tree:
{\"foo\" {:name \"other\"}}"
[token-name token-names-tree]
(let [name-path (token-name->path token-name)
result (reduce
(fn [acc cur]
(let [target (get acc cur)]
(cond
;; Path segment doesn't exist yet
(nil? target) (reduced false)
;; A token exists at this path
(:name target) (reduced true)
;; Continue traversing the true
:else target)))
token-names-tree name-path)]
(if (map? result)
(some? (:name result))
result)))