Fork me on GitHub
#component
<
2017-04-05
>
josh.freckleton22:04:56

I have a component that shouldn't run in a dev environment Is there a preferred way for conditionally placing components into the system?

noisesmith22:04:06

I use a hash-map each for the base system and the deps, and in some cases I merge components conditionally in the maps before starting, in others I dissoc conditionally

noisesmith22:04:23

the former for optional prod-only things, the latter for special dev-only minimal systems

noisesmith22:04:09

of course this means using apply instead of directly calling system-map

noisesmith22:04:46

relevant snippet:

(let [system (virgin-system config-options)
        deps (into {}
                   (filter (fn [[k v]]
                             (and (contains? system k)
                                  (every? #(contains? system %) (vals v))))
                           kingfisher-deps))
        system (mapcat (fn [[k v :as entry]]
                         (when (contains? deps k)
                           entry))
                       system)]
    (-> (apply component/system-map system)
        (component/system-using deps)))

josh.freckleton23:04:18

this exactly what i needed, thank you!

josh.freckleton23:04:36

ya I'd wondered about the system-map thing, but your apply solution is perfect

josh.freckleton23:04:55

is a hash-map different from a map?

josh.freckleton23:04:04

is it that the keys are hashed for faster lookup?

josh.freckleton23:04:09

and maps aren't?

noisesmith23:04:19

no, it's a map (I just say hash-map to differentiate from the function map is all)

josh.freckleton23:04:40

oh nice. underneath, the normal map is still a hash map then?

noisesmith23:04:12

clojure maps technically are either array-maps or hash-maps depending on size, but when they get big enough the array-maps auto-promote to hash-map

noisesmith23:04:08

oops, just too small

josh.freckleton23:04:07

interesting, clojure really handles a lot of neat things behind the scenes...

josh.freckleton23:04:11

thanks for pointing this out!

noisesmith23:04:04

oh, the other thing is that the virgin-system function takes a set of keys to dissoc as part of the config - the code I already shared prunes all deps that need those components