https://github.com/active-group/active-data-translate
A library to define data translations based on realms.
https://github.com/active-group/active-data-translate
Last synced: 4 months ago
JSON representation
A library to define data translations based on realms.
- Host: GitHub
- URL: https://github.com/active-group/active-data-translate
- Owner: active-group
- Created: 2024-10-18T13:05:53.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-02-28T09:12:46.000Z (over 1 year ago)
- Last Synced: 2025-07-26T20:32:43.037Z (11 months ago)
- Language: Clojure
- Size: 86.9 KB
- Stars: 0
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
[](https://clojars.org/de.active-group/active-data-translate)
[](https://cljdoc.org/d/de.active-group/active-data-translate/CURRENT)
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.
[Latest Version](https://clojars.org/de.active-group/active-data-translate)
[API Docs](https://cljdoc.org/d/de.active-group/active-data-translate/CURRENT)
## Introduction
Given some data described by realms, for example a record:
```clojure
(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:
```clojure
(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:
```clojure
(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"})
```
## License
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.