Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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.

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.