Skip to content

Enchanced implementation of Leiningen's meta-merge function

Notifications You must be signed in to change notification settings

metosin/ctrl-merge

 
 

Repository files navigation

Ctrl-Merge

Clojars Project

This is a standalone and enhanced implementation of the Leiningen meta-merge function, forked from Meta-Merge. It's particularly useful for merging configuration maps.

Usage

The merge function recursively merges two data structures.

(require '[ctrl-merge.core :as cm])

(cm/merge {:a [:b :c]} {:a [:d]})
; => {:a [:b :c :d]}

Metadata

Metadata hints can be provided to override the default behavior:

Prepend

When merge is called with a hash-map that has ^:prepend on it, the function will take the values on the right and prepend them to the set on the left (the default would append them as you can see above). You can see this below.

(cm/merge {:a [:b :c]} {:a ^:prepend [:d]})
; => {:a [:d :b :c]}

Replace

When merge is called with a hash-map that has ^:replace on it, the function will take the values on the right and completely replace the ones on the left. You can see this below:

(cm/merge {:a [:b :c]} {:a ^:replace [:d]})
; => {:a [:d]}

With replace the map to the right takes precedence.

Displace

When merge is called with a hash-map that has ^:displace on it, the function will take the values on the right, only if there are no values on the left. You can see this below:

(cm/merge {:a [:b :c]} {:a ^:displace [:d]})
; => {:a [:b :c]}

With displace the map to the left takes precedence. This makes displace a good solution for default values.

Options

merge can be configured using extra options argument. Supported options

Key Description
:replace-nil nil value on right position wins
(cm/merge {:a {:b 1}} {:a nil} {:replace-nil true})
; => {:a nil}

License

Copyright © 2023 Phil Hagelberg, James Reeves, Metosin and all the Leiningen contributors.

Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.

About

Enchanced implementation of Leiningen's meta-merge function

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Clojure 91.8%
  • Shell 8.2%