Muotti
Noun.
- mould/mold (hollow form or matrix for shaping a fluid or plastic substance)
- cast (mould used to make cast objects)
- die
- form (thing that gives shape to other things as in a mold)
(source: https://en.wiktionary.org/wiki/muotti)
Muotti is a graph based value transformer library which aims to solve value transformation by utilizing a digraph of known transformations to produce a transformer chain which is then used to perform the actual transformation.
Given a map of adjacencies - that is, edges of a graph - with validation and transformer functions:
(require '[muotti.core :as muotti])
(def config {:transformations {[:keyword :string] {:validator keyword?
:transformer name}
[:string :number] {:validator string?
:transformer parse-long}
[:string :boolean] {:validator string?
:transformer boolean}
[:number :string] {:validator number?
:transformer str}}})
a transformer can be created:
(def t (muotti/->transformer config))
which is then immediately usable for transforming values:
(muotti/transform t :keyword :number :123)
; => 123
(muotti/transform t :number :boolean 123)
; => true ;; non-empty values are treated as ´true´ by clojure.core/boolean
Unresolvable transformations return a special value:
(muotti/transform t :keyword :double :3.14)
; => ::unknown-path
Transformer chain validation errors also return a special value:
(def broken-adjacency {:transformations {[:a :b] {:validator keyword?
:transformer str}}})
(def t2 (muotti/->transformer broken-adjacency))
(muotti/transform t2 :a :b "not a number")
;; => ::invalid-value
Muotti is made to complement Malli's decoding and encoding capabilities through Malli's Value Transformation capability.
Create a Malli transformer and then use it to call eg. malli.core/decode
with the transformer:
(require '[malli.core :as malli])
(require '[muotti.malli :as mm])
(malli/decode
[:map
[:a {:muotti/ignore true} :uuid]
[:b :int]]
{:a :invalid
:b "123"}
(mm/transformer (muotti/->transformer mm/malli-config)))
;;=> {:a nil, :b 123}
The following transformations are pre-defined in the muotti.malli/malli-config
.
It is possible to output the graph contained by the transformer as DOT:
(->> (muotti/->transformer mm/malli-config)
(muotti/graph-dot)
(spit "/tmp/graph.dot"))
The resulting file can be input into GraphViz:
dot -Tpng /tmp/graph.dot > graph.png
which results in
:muotti/default
- allow Malli transformer to inject a default value for nil
valuesCan you improve this documentation?Edit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close