twitter/bijection

Name: bijection

Owner: Twitter, Inc.

Description: Reversible conversions between types

Created: 2012-12-30 22:22:25.0

Updated: 2018-01-11 18:13:59.0

Pushed: 2017-09-14 00:39:50.0

Homepage:

Size: 2103

Language: Scala

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Bijection

Build Status Codecov Latest version Chat

A Bijection is an invertible function that converts back and forth between two types, with the contract that a round-trip through the Bijection will bring back the original object. Moreover, the inverse has the same property.

See the current API documentation for more information.

Examples:
sbt bijection-core/console
a> import com.twitter.bijection._
a> Bijection[Int, java.lang.Integer](42)
: java.lang.Integer = 42

In addition to Bijection, we have Injection. An Injection embeds a type A in a larger space of type B. Every item from A can be round-tripped through B, but not every B can be mapped to A. So Injection is like a pair of function: A => B, B => Try[A].

rt com.twitter.bijection._

a> Injection[Int, String](100)
: String = 100

a> Injection.invert[Int, String](res0)
: Try[Int] = Success(100)

If we want to treat an Injection like a Bijection (over a restricted subspace of the larger set), we use the B @@ Rep[A] syntax, for instance: String @@ Rep[Int]

ction[Int, String @@ Rep[Int]](100)
: com.twitter.bijection.package.@@[String,com.twitter.bijection.Rep[Int]] = 100

Use invert to reverse the transformation:

a> Bijection.invert[Int, String @@ Rep[Int]](res2)
: Int = 100

If you import Conversion.asMethod you can use .as[T] to use an available Bijection/Injection to T:

a> import com.twitter.bijection.Conversion.asMethod
rt com.twitter.bijection.Conversion.asMethod

a> 1.as[java.lang.Integer]
: java.lang.Integer = 1

Bijections and Injections can also be composed. As with functions, andThen composes forward, compose composes backward.

This example round-trips a long into a GZipped base64-encoded string:

a> val injection = Injection.long2BigEndian andThen Bijection.bytes2GZippedBase64
ction: com.twitter.bijection.Injection[Long,Array[Byte]] = <function1>

a> injection(123456789L)
: com.twitter.bijection.GZippedBase64String = GZippedBase64String(H4sIAAAAAAAAAGNgYGBgjz4rCgBpa5WLCAAAAA==)

a> injection.invert(res1)
: Try[Long] = Success(123456789)

When you have bijections between a path of items you can Bijection.connect or Injection.connect them:

a> import com.twitter.bijection.Injection.connect
rt com.twitter.bijection.Injection.connect

a> import com.twitter.bijection.Base64String
rt com.twitter.bijection.Base64String

a> import Conversion.asMethod
rt Conversion.asMethod

a> implicit val long2String2Bytes2B64 = connect[Long,String,Array[Byte],Base64String]
ng2Long2Bytes2B64: com.twitter.bijection.Bijection[String,com.twitter.bijection.Base64String] = <function1>

a> 243L.as[Base64String]
: com.twitter.bijection.Base64String = Base64String(MjQz)

a> long2String2Bytes2B64.invert(res0)
: Try[Long] = Success(243)
Supported Bijections/Injections

Bijection implicitly supplies Bijections between:

Additionally there is a method to generate Bijections between most of Scala's built in types:

If you see a reversible conversion that is not here and related to types in the standard library of Java or Scala, please contribute!

Serialization via Bufferable

Bufferable[T] handles putting and getting a type T into a ByteBuffer in a composable way. Bufferable[T] instances for all primitives/tuples/containers are provided. Bijections and Injections to any of these types give you binary serialization via Bufferable.

Documentation

To learn more and find links to tutorials and information around the web, check out the Bijection Wiki.

The latest ScalaDocs are hosted on Bijection's Github Project Page.

Get Involved + Code of Conduct

Pull requests and bug reports are always welcome!

Discussion occurs primarily on the Bijection mailing list. Issues should be reported on the GitHub issue tracker.

We use a lightweight form of project governence inspired by the one used by Apache projects. Please see Contributing and Committership for our code of conduct and our pull request review process. The TL;DR is send us a pull request, iterate on the feedback + discussion, and get a +1 from a Committer in order to get your PR accepted.

The current list of active committers (who can +1 a pull request) can be found here: Committers

A list of contributors to the project can be found here: Contributors

Maven

Bijection modules are available on maven central. The current groupid and version for all modules is, respectively, "com.twitter" and 0.9.5.

Current published artifacts are

Every artifact is published against Scala "2.10", "2.11" and "2.12". To pull in the jars, make sure to add your desired scala version as a suffix, ie:

bijection-core_2.10 or bijection-core_2.11 or bijection-core_2.12.

Chat

Gitter

Authors
License

Copyright 2012 Twitter, Inc.

Licensed under the Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.