Skip to content

Commit

Permalink
Definition updates.
Browse files Browse the repository at this point in the history
- Add `parameter` constructor function to declare parameters
  outside of configuration specification and configuration
  definitions
- Deprecate `define-configuration-specification` and
  `define-configuration` in favour of the new
  `configuration-specification` and `configuration` constructor
  functions.
- Implement equality for sources.
  • Loading branch information
tobyclemson committed May 31, 2024
1 parent 68a4ec6 commit a65e100
Show file tree
Hide file tree
Showing 3 changed files with 218 additions and 107 deletions.
60 changes: 40 additions & 20 deletions src/configurati/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -24,36 +24,50 @@
(defn multi-source [& sources]
(conf-sources/->MultiConfigurationSource sources))

(defn with-parameter [parameter-name & {:as options}]
(let [defaults {:nilable false
:type :any}
base {:name parameter-name}]
[:parameter (conf-param/map->ConfigurationParameter
(clojure.core/merge defaults base options))]))
(defn parameter? [value]
(satisfies? conf-param/Processable value))

(defn- element [element-type value]
[element-type value])

(defn parameter
([parameter-name]
(parameter parameter-name {}))
([parameter-name options]
(let [defaults {:nilable false
:type :any}
base {:name parameter-name}]
(conf-param/map->ConfigurationParameter
(clojure.core/merge defaults base options)))))

(defn with-parameter [parameter-element-or-name & {:as options}]
(element :parameter
(if (parameter? parameter-element-or-name)
parameter-element-or-name
(parameter parameter-element-or-name options))))

(defn with-middleware [middleware]
(fn [source]
(conf-sources/->FnConfigurationSource (partial middleware source))))

(defn with-source
([source] [:source source])
([source & middleware-fns]
[:source (reduce
(fn [source middleware-fn]
(middleware-fn source))
source
middleware-fns)]))
(defn with-source [source & middleware-fns]
(element :source
(reduce
(fn [source middleware-fn]
(middleware-fn source))
source
middleware-fns)))

(defn with-key-fn [f]
[:key-fn f])
(element :key-fn f))

(defn with-specification [specification]
[:specification specification])
(element :specification specification))

(defn with-transformation [transformation]
[:transformation transformation])
(element :transformation transformation))

(defn define-configuration-specification [& args]
(defn configuration-specification [& args]
(let [elements
(group-by first args)
parameters
Expand All @@ -65,7 +79,7 @@
(conf-spec/->ConfigurationSpecification
parameters key-fn transformation)))

(defn define-configuration [& args]
(defn configuration [& args]
(let [elements (group-by first args)

top-level-parameters
Expand Down Expand Up @@ -98,8 +112,14 @@
source (conf-sources/->MultiConfigurationSource sources)]
(conf-def/->ConfigurationDefinition source specifications)))

(defn ^:deprecated define-configuration-specification [& args]
(apply configuration-specification args))

(defn ^:deprecated define-configuration [& args]
(apply configuration args))

(defn resolve [definition]
(configurati.definition/resolve definition))

(defn merge [& definitions]
(apply configurati.definition/merge definitions))
(apply configurati.definition/merge definitions))
91 changes: 82 additions & 9 deletions src/configurati/sources.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[clj-yaml.core :as yaml]
[medley.core :as medley])
(:import
[clojure.lang ILookup]))
[clojure.lang IHashEq ILookup]))

(defn- prefix-keyword [prefix k]
(if prefix
Expand Down Expand Up @@ -36,53 +36,126 @@
(convert-keys-to-kebab-case)))

(deftype MapConfigurationSource
[m]
[config-map]

ILookup
(valAt [_ parameter-name]
(get m parameter-name))
(get config-map parameter-name))
(valAt [_ parameter-name default]
(get m parameter-name default)))
(get config-map parameter-name default))

Object
(equals [_ other]
(and (instance? MapConfigurationSource other)
(= config-map (.-config-map ^MapConfigurationSource other))))
(hashCode [_]
(.hashCode config-map))

IHashEq
(hasheq [_]
(hash config-map)))

(deftype EnvConfigurationSource
[prefix]

ILookup
(valAt [_ parameter-name]
(get (read-system-env) (prefix-keyword prefix parameter-name)))
(valAt [_ parameter-name default]
(get (read-system-env) (prefix-keyword prefix parameter-name) default)))
(get (read-system-env) (prefix-keyword prefix parameter-name) default))

Object
(equals [_ other]
(and (instance? EnvConfigurationSource other)
(= prefix (.-prefix ^EnvConfigurationSource other))))
(hashCode [_]
(.hashCode prefix))

IHashEq
(hasheq [_]
(hash prefix)))

(deftype EnvironConfigurationSource
[prefix]

ILookup
(valAt [_ parameter-name]
(environ/env (prefix-keyword prefix parameter-name)))
(valAt [_ parameter-name default]
(environ/env (prefix-keyword prefix parameter-name) default)))
(environ/env (prefix-keyword prefix parameter-name) default))

Object
(equals [_ other]
(and (instance? EnvironConfigurationSource other)
(= prefix (.-prefix ^EnvironConfigurationSource other))))
(hashCode [_]
(.hashCode prefix))

IHashEq
(hasheq [_]
(hash prefix)))

(deftype YamlFileConfigurationSource
[path prefix]

ILookup
(valAt [_ parameter-name]
(let [contents (read-yaml-configuration-file path)]
(get contents (prefix-keyword prefix parameter-name))))
(valAt [_ parameter-name default]
(let [contents (read-yaml-configuration-file path)]
(get contents (prefix-keyword prefix parameter-name) default))))
(get contents (prefix-keyword prefix parameter-name) default)))

Object
(equals [_ other]
(and (instance? YamlFileConfigurationSource other)
(= path (.-path ^YamlFileConfigurationSource other))
(= prefix (.-prefix ^YamlFileConfigurationSource other))))
(hashCode [_]
(.hashCode [path prefix]))

IHashEq
(hasheq [_]
(hash [path prefix])))

(deftype FnConfigurationSource
[source-fn]

ILookup
(valAt [_ parameter-name]
(source-fn parameter-name))
(valAt [_ parameter-name default]
(or (source-fn parameter-name) default)))
(or (source-fn parameter-name) default))

Object
(equals [_ other]
(and (instance? FnConfigurationSource other)
(= source-fn (.-source-fn ^FnConfigurationSource other))))
(hashCode [_]
(.hashCode source-fn))

IHashEq
(hasheq [_]
(hash source-fn)))

(deftype MultiConfigurationSource
[sources]

ILookup
(valAt [_ parameter-name]
(medley/find-first
#(not (nil? %))
(map parameter-name sources)))
(valAt [this parameter-name default]
(get this parameter-name default)))
(get this parameter-name default))

Object
(equals [_ other]
(and (instance? MultiConfigurationSource other)
(= sources (.-sources ^MultiConfigurationSource other))))
(hashCode [_]
(.hashCode sources))

IHashEq
(hasheq [_]
(hash sources)))
Loading

0 comments on commit a65e100

Please sign in to comment.