Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/transferwise/terra
A minimalistic library for object hydration. Useful for data to object reconstruction mechanics.
https://github.com/transferwise/terra
hydration persistence
Last synced: 3 months ago
JSON representation
A minimalistic library for object hydration. Useful for data to object reconstruction mechanics.
- Host: GitHub
- URL: https://github.com/transferwise/terra
- Owner: transferwise
- License: apache-2.0
- Created: 2017-03-03T15:03:01.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2023-04-07T01:25:50.000Z (almost 2 years ago)
- Last Synced: 2024-04-14T05:18:25.849Z (10 months ago)
- Topics: hydration, persistence
- Language: Java
- Size: 59.6 KB
- Stars: 1
- Watchers: 109
- Forks: 5
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Terra [![CircleCI](https://circleci.com/gh/transferwise/terra/tree/master.svg?style=shield)](https://circleci.com/gh/transferwise/terra/tree/master)
A minimalistic library for _object hydration_. Useful for data to object reconstruction mechanics.
## Installation
Just add the following configuration to your `build.gradle` file
```gradle
allprojects {
repositories {
maven { url 'https://jitpack.io' }
}
}dependencies {
compile 'com.github.transferwise:terra:1.0.0'
}
```## Usage
Given a value object like the following
```java
import java.util.regex.Pattern;class Email {
private static final Pattern FORMAT =
Pattern.compile("^[A-Z0-9._%-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$", Pattern.CASE_INSENSITIVE);
private final String value;
Email(String aValue) {
if (!FORMAT.matcher(aValue).matches()) {
throw new RuntimeException("Email " + aValue + " is incorrect");
}
value = aValue;
}
public String getValue() {
return value;
}
}
```Imagine you'd want to rebuild the object, bypassing the constructor validation.
```java
import static com.transferwise.terra.Terra.hydrate;
class Example {
public static void main(String[] args) {
Email e = hydrate(Email.class, "value", "[email protected]");
System.out.println(e.getValue());
}
}
```#### Why would we disable the constructor?
This is a very common behaviour when you reconstruct objects from data stored in your persistence mechanism. The validation rules usually live in the write model, not in the read model. Business constraints change all the time and you might end up with data that does not follow these rules at a given moment.