项目作者: Aitem

项目描述 :
BDM is super simple, fully declarative clj/cljc bi directional mapper.
高级语言: Clojure
项目地址: git://github.com/Aitem/bdm.git
创建时间: 2020-07-26T20:05:03Z
项目社区:https://github.com/Aitem/bdm

开源协议:MIT License

下载


BiDirectionalMapper

Clojars Project
ci

BDM is super simple, fully declarative clj/cljc bi directional mapper that allow convert your data from A to B and back.

Usage

  1. (ns user
  2. (:require [bdm.core as bdm]))
  3. (def person
  4. {:name "John"
  5. :lastname "Smith"
  6. :contacts [{:type "email"
  7. :value "john@smith.com"}
  8. {:type "phone"
  9. :use "home"
  10. :value "12223334455"}
  11. {:type "phone"
  12. :use "work"
  13. :value "15556667788"}]})
  14. (def mapping
  15. [[[:name]]
  16. [[:lastname]]
  17. [[:emails] [:contacts {:get [:= :type "email"]} :#]]
  18. [[:phones] [:contacts {:get [:= :type "phone"]} :#]]])
  19. (bdm/import person mapping)
  20. ;; Will return
  21. ;; {:name "John",
  22. ;; :lastname "Smith",
  23. ;; :emails [{:type "email", :value "john@smith.com"}],
  24. ;; :phones [{:type "phone", :use "home", :value "12223334455"}
  25. ;; {:type "phone", :use "vork", :value "15556667788"}]}
  26. ;; We can do import/export roundtrip
  27. (= person (bdm/export (bdm/import person mapping) mapping)) ;; => true

Concepts

Mapping

Mapping is a array of paths pairs. First path of pair used as setter rule and second as a getter rule.

  1. (def mapping
  2. [[[:username] [:login]]])
  3. (def user
  4. {:login "super-user"})
  5. (bdm/import user mapping)
  6. ;; will return
  7. ;; {:username "super-user"}
  8. (def imported-user
  9. {:username "super-user"})
  10. (bdm/export imported-user mapping)
  11. ;; will return
  12. ;; {:login "super-user"}

In this sample we take value from user by path [:login] and set this value into result with path [:username].

Path can be deep

  1. (def window-mapping
  2. [[[:hash] [:window :location :hash]]
  3. [[:path] [:window :location :pathname]]])
  4. (def window
  5. {:window {:location {:hash "#about"
  6. :pathname "/index.html"}}})
  7. (bdm/import window window-mapping)
  8. ;; Will return
  9. ;; {:hash "#about"
  10. ;; :path "/index.html"}
  11. (def location
  12. {:hash "#contact"
  13. :path "/spa.html"})
  14. (bdm/export location window-mapping)
  15. ;; Will return
  16. ;; {:window {:location {:hash "#contact"
  17. ;; :pathname "/spa.html"}}})

Path

Short alias

Predicates and arrays

Setters

Submapping

Develop

  1. # run repl
  2. $ make repl
  3. # run test
  4. $ make test

Extra

For more details and usage details see tests.

MIT License Copyright (c) 2020 Marat Surmashev