This goal of this library is to facilitate translating back and forth between data in a form described by [[realms]] and a different form, usually EDN or Transit.
Given some data described by realms, for example a record:
(require '[active.data.record :as r])
(require '[active.data.realm :as realm])
(r/def-record user
[user-id realm/integer
user-name realm/string])
We can define a format that translates between instances of the record an EDN compatible map representation of the record, and identity translations for strings and integers:
(require '[active.data.translate.format :as format])
(require '[active.data.translate.formatter :as formatter])
(def my-edn-format
(format :my-edn-format
{realm/string formatter/id
realm/integer formatter/id
user (formatter/record-map user {user-id :id
user-name :name})}))
Using the format, we can create functions that translate between the two:
(require '[active.data.translate.core :as translate])
(def edn-from-user (translate/translator-from user my-edn-format))
(def edn-to-user (translate/translator-to user my-edn-format))
(= {:id 1 :name "Alice"}
(edn-from-user (user user-id 1 user-name "Alice"))
(= (user user-id 1 user-name "Alice")
(edn-to-user {:id 1 :name "Alice"})
Copyright © 2024 Active Group GmbH
This program and the accompanying materials are made available under the terms of the Eclipse Public License 2.0 which is available at http://www.eclipse.org/legal/epl-2.0.
This Source Code may also be made available under the following Secondary Licenses when the conditions for such availability set forth in the Eclipse Public License, v. 2.0 are satisfied: GNU General Public License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any later version, with the GNU Classpath Exception which is available at https://www.gnu.org/software/classpath/license.html.
Can you improve this documentation?Edit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close