Transmute

March 5, 2017 · View on GitHub

It's very common to convert data from a type to another and there's plenties of APIs to achieve it in .NET. Transmute was created to have a single centralized channel for all conversions.

Install

Available on NuGet

NuGet

Quickstart

//.netstandard
var b = Transmuter.Default.Convert<bool>(5); // true
var d = Transmuter.Default.Convert<DateTime>(1495820216); // 26/5/2017

//Xamarin.iOS
var c = Transmuter.Default.Convert<UIColor>(0xFF0000); // red

Functionalities

Custom converter

You can register a custom converters at any time with Register function :

Transmuter.Default.Register<T1,T2>(t1 => /* to t2 */);

If a converter already exists for those types, it will be replaced by yours.

Composability

Transmuter will try to find a way to convert a value to a the target type if a path exists.

So if you register those converters :

Transmuter.Default.Register<T1,T2>(...);
Transmuter.Default.Register<T2,T3>(...);
Transmuter.Default.Register<T3,T4>(...);

This conversions will succeed :

T1 t1 = ...;
T4 t4 = Transmuter.Default.Convert<T4>(t1); // T1 -> T2,T2 -> T3,T3 -> T4

Array conversions

If the source type and target type are arrays, and a converter has been registered to convert a source item to a target item, a converter will be available too :

var source = new[] { 10, 11, 12 };
var target = transmuter.Convert<string[]>(source); // { "10", "11", "12" }

Default converters

All platforms

FromToDescription
shortbyte[]BitConverter
intbyte[]BitConverter
longbyte[]BitConverter
floatbyte[]BitConverter
doublebyte[]BitConverter
boolbyte[]BitConverter
byte[]shortBitConverter
byte[]intBitConverter
byte[]longBitConverter
byte[]floatBitConverter
byte[]doubleBitConverter
byte[]boolBitConverter
intshortConvert.ChangeType
shortintConvert.ChangeType
intlongConvert.ChangeType
longintConvert.ChangeType
intfloatConvert.ChangeType
floatintConvert.ChangeType
intdoubleConvert.ChangeType
doubleintConvert.ChangeType
floatdoubleConvert.ChangeType
doublefloatConvert.ChangeType
intbool> 0 ?
boolinttrue ? 1 : 0
shortstringToString
intstringToString
longstringToString
floatstringToString
doublestringToString
boolstringToString
longDateTimefrom timestamp (ms)
DateTimelongto timestamp (ms)

Xamarin.iOS

FromToDescription
DateTimeNSDateconversion
NSDateDateTimeconversion
CGRectint[]{ x,y,w,h }
int[]CGRect{ x,y,w,h }
CGRectfloat[]{ x,y,w,h }
float[]CGRect{ x,y,w,h }
intUIColor0xAARRGGBB
UIColorint0xAARRGGBB
bytes[]UIColorAA,RR,GG,BB
UIColorbytes[]AA,RR,GG,BB
boolUIColortrue ? UIColor.Green : UIColor.Red
UIColorboolvalue == UIColor.Green
stringUIImageNSData.FromFile -> UIImage.LoadFromData

Xamarin.Android

FromToDescription
boolViewStatestrue ? ViewStates.Visible : ViewStates.Gone
ViewStatesboolvalue == ViewStates.Visible
intColor0xAARRGGBB
Colorint0xAARRGGBB
bytes[]ColorAA,RR,GG,BB
Colorbytes[]AA,RR,GG,BB
stringBitmapBitmapFactory.DecodeFile

Roadmap / Ideas

  • Add collection conversion
  • Manage conversion errors
  • Adding Windows platform (with IValueConverter builder)

Contributions

Contributions are welcome! If you find a bug please report it and if you want a feature please report it.

If you want to contribute code please file an issue and create a branch off of the current dev branch and file a pull request.

License

MIT © Aloïs Deniel