Back

find-derived (clj)

(source)

function

(find-derived m k)
Return a seq of all entries in a map, m, where the key is derived from the a candidate key, k. If there are no matching keys, nil is returned. The candidate key may be a keyword, or vector of keywords.

Examples

integrant
(ns integrant.core-test
  (:require #?(:clj  [clojure.test :refer [are deftest is testing]]
               :cljs [cljs.test :refer-macros [are deftest is testing]])
            [integrant.core :as ig]
            [weavejester.dependency :as dep]))

(deftest find-derived-1-test
  (testing "missing key"
    (is (nil? (ig/find-derived-1 {} ::p))))

  (testing "derived key"
    (is (= (ig/find-derived-1 {::a "x" ::p "y"} ::pp)
           [::p "y"])))

  (testing "ambiguous key"
    (is (thrown-with-msg?
         #?(:clj clojure.lang.ExceptionInfo :cljs cljs.core.ExceptionInfo)
         (re-pattern (str "Ambiguous key: " ::pp "\\. "
                          "Found multiple candidates: " ::p ", " ::pp))
         (ig/find-derived-1 {::a "x" ::p "y", ::pp "z"} ::pp))))

  (testing "composite key"
    (is (= (ig/find-derived-1 {::a "x" [::b ::x] "y"} ::x)
           [[::b ::x] "y"]))))

(deftest find-derived-test
  (testing "missing key"
    (is (nil? (ig/find-derived {} ::p))))

  (testing "derived key"
    (is (= (ig/find-derived {::a "x" ::p "y" ::pp "z"} ::pp)
           [[::p "y"] [::pp "z"]])))

  (testing "ambiguous key"
    (is (= (ig/find-derived {::a "x" ::p "y" ::pp "z"} ::ppp)
           [[::p "y"] [::pp "z"]])))

  (testing "composite key"
    (is (= (ig/find-derived {::a "x" [::b ::x] "y", [::b ::y] "z"} ::b)
           [[[::b ::x] "y"] [[::b ::y] "z"]]))))