Back
get-in (clj)
(source)function
(get-in m ks)
(get-in m ks not-found)
Returns the value in a nested associative structure,
where ks is a sequence of keys. Returns nil if the key
is not present, or the not-found value if supplied.
Examples
originrose/cortex
(ns think.cortex.keras.core-test
(:require [clojure.test :refer :all]
[think.cortex.keras.core :as keras]
[cortex.nn.network :as network]
[clojure.core.matrix :as m]
[cortex.verify.nn.import :as import]
[cortex.graph :as graph]))
;; overlapping layers in keras and cortex should match,
;; in the same order
(is (= (remove #(or (.contains ^String (str %) "flatten")
(.contains ^String (str %) "input")
(.contains ^String (str %) "add"))
(map #(keyword (:name %)) (get-in keras-model [:config :layers])))
(->> (rest model-desc) ;; drop first nil (input)
(remove #(or (= (:type %) :split) ;; filter out splits and joins
(= (:type %) :join)
;; filter out last activation layer (own layer in cortex, not in keras)
(clojure.string/ends-with? (name (:id %)) "activation")
))
(map :id))))
;; check that for each "Add", there is one split and one join
(is (= 16
(count (filter #(= (:class_name %) "Add") (get-in keras-model [:config :layers])))
(count (filter #(= (:type %) :split) model-desc))
(count (filter #(= (:type %) :join) model-desc))))))
clojure-lsp/clojure-lsp
(ns clojure-lsp.feature.file-management-test
(:require
[clojure-lsp.feature.file-management :as f.file-management]
[clojure-lsp.shared :as shared]
[clojure-lsp.test-helper :as h]
[clojure.core.async :as async]
[clojure.test :refer [are deftest is testing]]
[medley.core :as medley]))
(deftest did-close
(swap! (h/db*) medley/deep-merge {:settings {:source-paths #{(h/file-path "/user/project/src/clj")}}
:project-root-uri (h/file-uri "file:///user/project")})
(h/load-code-and-locs "(ns foo) a b c" (h/file-uri "file:///user/project/src/clj/foo.clj"))
(h/load-code-and-locs "(ns bar) d e f" (h/file-uri "file:///user/project/src/clj/bar.clj"))
(h/load-code-and-locs "(ns some-jar)" (h/file-uri "file:///some/path/to/jar.jar:/some/file.clj"))
(testing "when file exists on disk"
(let [mock-diagnostics-chan (async/chan 1)
uri (h/file-uri "file:///user/project/src/clj/foo.clj")]
(with-redefs [shared/file-exists? (constantly true)]
(f.file-management/did-close uri (assoc (h/components)
:diagnostics-chan mock-diagnostics-chan)))
(is (get-in (h/db) [:analysis uri]))
(is (get-in (h/db) [:findings uri]))
(is (seq (get-in (h/db) [:dep-graph 'foo :uris])))
(is (get-in (h/db) [:documents uri]))
(h/assert-no-take mock-diagnostics-chan 500)))
(testing "when local file not exists on disk"
(let [mock-diagnostics-chan (async/chan 1)
uri (h/file-uri "file:///user/project/src/clj/bar.clj")]
(with-redefs [shared/file-exists? (constantly false)]
(f.file-management/did-close uri (assoc (h/components)
:diagnostics-chan mock-diagnostics-chan)))
(is (nil? (get-in (h/db) [:analysis uri])))
(is (nil? (get-in (h/db) [:findings uri])))
(is (not (seq (get-in (h/db) [:dep-graph 'bar :uris]))))
(is (nil? (get-in (h/db) [:documents uri])))
(is (= {:uri uri
:diagnostics []}
(h/take-or-timeout mock-diagnostics-chan 500)))))
(testing "when file is external we do not remove analysis but publish empty diagnostics"
(let [mock-diagnostics-chan (async/chan 1)
uri (h/file-uri "file:///some/path/to/jar.jar:/some/file.clj")]
(with-redefs [shared/file-exists? (constantly false)]
(f.file-management/did-close uri (assoc (h/components)
:diagnostics-chan mock-diagnostics-chan)))
(is (get-in (h/db) [:analysis uri]))
(is (get-in (h/db) [:findings uri]))
(is (seq (get-in (h/db) [:dep-graph 'some-jar :uris])))
(is (get-in (h/db) [:documents uri]))
(is (= {:uri uri
:diagnostics []}
(h/take-or-timeout mock-diagnostics-chan 500))))))
(deftest did-open
(testing "on an empty file"
(let [mock-edits-chan (async/chan 1)
mock-diagnostics-chan (async/chan 1)
uri (h/file-uri "file:///user/project/src/aaa/bbb.clj")]
(swap! (h/db*) shared/deep-merge {:settings {:auto-add-ns-to-new-files? true
:source-paths #{(h/file-path "/user/project/src")}}
:project-root-uri (h/file-uri "file:///user/project")})
(h/load-code-and-locs "" uri (assoc (h/components)
:edits-chan mock-edits-chan
:diagnostics-chan mock-diagnostics-chan))
(is (get-in (h/db) [:analysis uri]))
(is (get-in (h/db) [:findings uri]))
;; The ns won't be in the dep graph until after the edit adding it is applied.
(is (not (contains? (get (h/db) :dep-graph) 'aaa.bbb)))
(is (get-in (h/db) [:documents uri]))
(testing "should publish empty diagnostics"
(is (= {:uri uri, :diagnostics []}
(h/take-or-timeout mock-diagnostics-chan 500))))
(testing "should add ns"
(is (= {:changes
{uri
[{:range
{:start {:line 0, :character 0},
:end {:line 999998, :character 999998}},
:new-text "(ns aaa.bbb)"}]}}
(h/take-or-timeout mock-edits-chan 500)))))))
(deftest did-change
(let [mock-changes-chan (async/chan 1)
original-text (h/code "(ns aaa)"
"(def foo 1)")
edited-text (h/code "(ns aaa)"
"(def bar 1)")]
(h/load-code-and-locs original-text)
(f.file-management/did-change h/default-uri
[{:text "bar"
:range {:start {:line 1 :character 5}
:end {:line 1 :character 8}}}]
2
(assoc (h/components)
:current-changes-chan mock-changes-chan))
(is (= 2 (get-in (h/db) [:documents h/default-uri :v])))
(is (= edited-text (get-in (h/db) [:documents h/default-uri :text])))
(is (= {:uri h/default-uri, :text edited-text, :version 2}
(h/take-or-timeout mock-changes-chan 500)))))
PrecursorApp/precursor
(ns pc.http.routes.api
(:require [cemerick.url :as url]
[cheshire.core :as json]
[clojure.core.memoize :as memo]
[clojure.string :as str]
[clojure.tools.reader.edn :as edn]
[crypto.equality :as crypto]
[defpage.core :as defpage :refer (defpage)]
[pc.auth :as auth]
[pc.crm :as crm]
[pc.datomic :as pcd]
[pc.early-access]
[pc.http.doc :as doc-http]
[pc.http.team :as team-http]
[pc.http.handlers.custom-domain :as custom-domain]
[pc.models.chat-bot :as chat-bot-model]
[pc.models.doc :as doc-model]
[pc.models.flag :as flag-model]
[pc.models.team :as team-model]
[pc.profile :as profile]
[ring.middleware.anti-forgery :as csrf]
[slingshot.slingshot :refer (try+ throw+)]))
(defpage new [:post "/api/v1/document/new"] [req]
(let [params (some-> req :body slurp edn/read-string)
read-only? (:read-only params)
doc-name (:document/name params)]
(if-not (:subdomain req)
(let [cust-uuid (get-in req [:auth :cust :cust/uuid])
intro-layers? (:intro-layers? params)
doc (doc-model/create-public-doc!
(merge {:document/chat-bot (rand-nth chat-bot-model/chat-bots)}
(when cust-uuid {:document/creator cust-uuid})
(when read-only? {:document/privacy :document.privacy/read-only})
(when doc-name {:document/name doc-name})))]
(when intro-layers?
(doc-http/add-intro-layers doc))
{:status 200 :body (pr-str {:document (doc-model/read-api doc)})})
(if (and (:team req)
(auth/logged-in? req)
(auth/has-team-permission? (pcd/default-db) (:team req) (:auth req) :admin))
(let [doc (doc-model/create-team-doc!
(:team req)
(merge {:document/chat-bot (rand-nth chat-bot-model/chat-bots)}
(when-let [cust-uuid (get-in req [:cust :cust/uuid])]
{:document/creator cust-uuid})
(when read-only?
{:document/privacy :document.privacy/read-only})
(when doc-name
{:document/name doc-name})))]
{:status 200 :body (pr-str {:document (doc-model/read-api doc)})})
{:status 400 :body (pr-str {:error :unauthorized-to-team
:redirect-url (str (url/map->URL {:host (profile/hostname)
:protocol (if (profile/force-ssl?)
"https"
(name (:scheme req)))
:port (if (profile/force-ssl?)
(profile/https-port)
(profile/http-port))
:path "/new"
:query (:query-string req)}))
:msg "You're unauthorized to make documents in this subdomain. Please request access."})}))))
(defpage create-team [:post "/api/v1/create-team"] [req]
(let [params (some-> req :body slurp edn/read-string)
subdomain (some-> params :subdomain str/lower-case str/trim)
coupon-code (some-> params :coupon-code)
cust (get-in req [:auth :cust])]
(cond (empty? cust)
{:status 400 :body (pr-str {:error :not-logged-in
:msg "You must log in first."})}
(defpage early-access [:post "/api/v1/early-access"] [req]
(if-let [cust (get-in req [:auth :cust])]
(do
(pc.early-access/create-request cust (edn/read-string (slurp (:body req))))
(pc.early-access/approve-request cust)
{:status 200 :body (pr-str {:msg "Thanks!" :access-request-granted? true})})
{:status 401 :body (pr-str {:error :not-logged-in
:msg "Please log in to request early access."})}))
(defpage create-solo-trial [:post "/api/v1/create-solo-trial"] [req]
(if-let [cust (get-in req [:auth :cust])]
(do
(flag-model/add-flag cust :flags/private-docs)
{:status 200 :body (pr-str {:msg "Thanks!" :solo-plan-created? true})})
{:status 401 :body (pr-str {:error :not-logged-in
:msg "Please log in to request early access."})}))
;; proxy for the dribbble API, to be used by our dribbble cards on the blog
(defpage dribbble-profile "/api/v1/dribbble/users/:username" [req]
(let [username (get-in req [:params :username])]
(if (contains? dribbble-user-whitelist username)
{:body (json/encode (get-dribbble-profile username))
:status 200
:headers {"Content-Type" "application/json; charset=utf-8"}}
(throw+ {:status 400
:public-message "Sorry, this user isn't on the whitelist."}))))
typedclojure/typedclojure
(ns ^:no-doc typed.ann.clojure
"Type annotations for the base Clojure distribution."
#?(:cljs (:require-macros [typed.ann-macros.clojure :as macros]))
(:require [clojure.core :as cc]
[typed.clojure :as t]
#?(:clj [typed.ann-macros.clojure :as macros])
#?(:clj typed.ann.clojure.jvm) ;; jvm annotations
#?(:clj clojure.core.typed))
#?(:clj
(:import (clojure.lang PersistentHashSet PersistentList
APersistentMap #_IPersistentCollection
#_ITransientSet
IRef)
(java.util Comparator Collection))))
cc/get-in [t/Any (t/Nilable t/AnySeqable) (t/? t/Any) :-> t/Any]
bsless/clj-fast
(ns clj-fast.clojure.core-test
(:require [clj-fast.clojure.core :as sut]
[clojure.test :as t]))
(t/deftest test-get
(let [m {:a 1, :b 2, :c {:d 3, :e 4}, :f nil, :g false, nil {:h 5}}]
(t/is (thrown? IllegalArgumentException (get-in {:a 1} 5)))
(t/are [x y] (= x y)
(sut/get m :a) 1
(sut/get m :e) nil
(sut/get m :e 0) 0
(sut/get m nil) {:h 5}
(sut/get m :b 0) 2
(sut/get m :f 0) nil
(sut/get-in m [:c :e]) 4
(sut/get-in m '(:c :e)) 4
(sut/get-in m '[:c :e]) 4
(sut/get-in m [:c :x]) nil
(sut/get-in m [:f]) nil
(sut/get-in m [:g]) false
(sut/get-in m [:h]) nil
(sut/get-in m []) m
(sut/get-in m nil) m
(sut/get-in m [:c :e] 0) 4
(sut/get-in m '(:c :e) 0) 4
(sut/get-in m [:c :x] 0) 0
(sut/get-in m [:b] 0) 2
(sut/get-in m [:f] 0) nil
(sut/get-in m [:g] 0) false
(sut/get-in m [:h] 0) 0
(sut/get-in m [:x :y] {:y 1}) {:y 1}
(sut/get-in m [] 0) m
(sut/get-in m nil 0) m)))
(t/deftest test-get-in
(let [m {:a {:b 1}}]
(t/testing "get-in"
(t/is (nil? (sut/get-in m [:c])))
(t/is (nil? (sut/get-in m [:c :d])))
(t/is (= {:b 1} (sut/get-in m [:a])))
(t/is (= 1 (sut/get-in m [:a :b])))
(t/is (= 1 (sut/get-in m '(:a :b)))))
))