public void Test_Uuid_Timestamp_And_ClockSequence() { DateTime now = DateTime.UtcNow; // UUID V1 : 60-bit timestamp, in 100-ns ticks since 1582-10-15T00:00:00.000 // note: this uuid was generated in Python as 'uuid.uuid1(None, 12345)' on the 2013-09-09 at 14:33:50 GMT+2 var uuid = Uuid128.Parse("14895400-194c-11e3-b039-1803deadb33f"); Assert.That(uuid.Timestamp, Is.EqualTo(135980228304000000L)); Assert.That(uuid.ClockSequence, Is.EqualTo(12345)); Assert.That(uuid.Node, Is.EqualTo(0x1803deadb33f)); // no, this is not my real mac address ! // the Timestamp should be roughly equal to the current UTC time (note: epoch is 1582-10-15T00:00:00.000) var epoch = new DateTime(1582, 10, 15, 0, 0, 0, DateTimeKind.Utc); Assert.That(epoch.AddTicks(uuid.Timestamp).ToString("O"), Is.EqualTo("2013-09-09T12:33:50.4000000Z")); // UUID V3 : MD5 hash of the name //note: this uuid was generated in Python as 'uuid.uuid3(uuid.NAMESPACE_DNS, 'foundationdb.com')' uuid = Uuid128.Parse("4b1ddea9-d4d0-39a0-82d8-9d53e2c42a3d"); Assert.That(uuid.Timestamp, Is.EqualTo(0x9A0D4D04B1DDEA9L)); Assert.That(uuid.ClockSequence, Is.EqualTo(728)); Assert.That(uuid.Node, Is.EqualTo(0x9D53E2C42A3D)); // UUID V5 : SHA1 hash of the name //note: this uuid was generated in Python as 'uuid.uuid5(uuid.NAMESPACE_DNS, 'foundationdb.com')' uuid = Uuid128.Parse("e449df19-a87d-5410-aaab-d5870625c6b7"); Assert.That(uuid.Timestamp, Is.EqualTo(0x410a87de449df19L)); Assert.That(uuid.ClockSequence, Is.EqualTo(10923)); Assert.That(uuid.Node, Is.EqualTo(0xD5870625C6B7)); }
public void Test_Uuid_Version() { //note: these UUIDs are from http://docs.python.org/2/library/uuid.html Assert.That(Uuid128.Parse("a8098c1a-f86e-11da-bd1a-00112444be1e").Version, Is.EqualTo(1)); Assert.That(Uuid128.Parse("6fa459ea-ee8a-3ca4-894e-db77e160355e").Version, Is.EqualTo(3)); Assert.That(Uuid128.Parse("16fd2706-8baf-433b-82eb-8c7fada847da").Version, Is.EqualTo(4)); Assert.That(Uuid128.Parse("886313e1-3b8a-5372-9b90-0c9aee199e5d").Version, Is.EqualTo(5)); }
public void Test_Uuid_Parse() { Uuid128 uuid; uuid = Uuid128.Parse("00010203-0405-0607-0809-0a0b0c0d0e0f"); Assert.That(uuid.ToString(), Is.EqualTo("00010203-0405-0607-0809-0a0b0c0d0e0f")); Assert.That(uuid.ToByteArray(), Is.EqualTo(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 })); uuid = Uuid128.Parse("{00010203-0405-0607-0809-0a0b0c0d0e0f}"); Assert.That(uuid.ToString(), Is.EqualTo("00010203-0405-0607-0809-0a0b0c0d0e0f")); Assert.That(uuid.ToByteArray(), Is.EqualTo(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 })); }
public void Test_Uuid_Vs_Guid() { Guid guid = Guid.NewGuid(); var uuid = new Uuid128(guid); Assert.That(uuid.ToString(), Is.EqualTo(guid.ToString())); Assert.That(uuid.ToGuid(), Is.EqualTo(guid)); Assert.That((Guid)uuid, Is.EqualTo(guid)); Assert.That((Uuid128)guid, Is.EqualTo(uuid)); Assert.That(Uuid128.Parse(guid.ToString()), Is.EqualTo(uuid)); Assert.That(uuid.Equals(guid), Is.True); Assert.That(uuid.Equals((object)guid), Is.True); Assert.That(uuid == guid, Is.True); Assert.That(guid == uuid, Is.True); Assert.That(uuid.Equals(Guid.NewGuid()), Is.False); Assert.That(uuid == Guid.NewGuid(), Is.False); Assert.That(Guid.NewGuid() == uuid, Is.False); }
public void Test_Uuid_Increment() { var @base = Uuid128.Parse("6be5d394-03a6-42ab-aac2-89b7d9312402"); Log(@base); //DumpHexa(@base.ToByteArray()); { // +1 var uuid = @base.Increment(1); Log(uuid); //DumpHexa(uuid.ToByteArray()); Assert.That(uuid.ToString(), Is.EqualTo("6be5d394-03a6-42ab-aac2-89b7d9312403")); } { // +256 var uuid = @base.Increment(256); Log(uuid); //DumpHexa(uuid.ToByteArray()); Assert.That(uuid.ToString(), Is.EqualTo("6be5d394-03a6-42ab-aac2-89b7d9312502")); } { // almost overflow (low) var uuid = @base.Increment(0x553D764826CEDBFDUL); // delta nécessaire pour avoir 0xFFFFFFFFFFFFFFFF a la fin Log(uuid); //DumpHexa(uuid.ToByteArray()); Assert.That(uuid.ToString(), Is.EqualTo("6be5d394-03a6-42ab-ffff-ffffffffffff")); } { // overflow (low) var uuid = @base.Increment(0x553D764826CEDBFEUL); // encore 1 de plus pour trigger l'overflow Log(uuid); //DumpHexa(uuid.ToByteArray()); Assert.That(uuid.ToString(), Is.EqualTo("6be5d394-03a6-42ac-0000-000000000000")); } { // overflow (cascade) var uuid = Uuid128.Parse("ffffffff-ffff-ffff-ffff-ffffffffffff").Increment(1); Log(uuid); //DumpHexa(uuid.ToByteArray()); Assert.That(uuid.ToString(), Is.EqualTo("00000000-0000-0000-0000-000000000000")); } }
public void Test_Uuid_Equality() { Assert.That(Uuid128.Empty.Equals(new Uuid128(new byte[16])), Is.True); Assert.That(Uuid128.Empty.Equals(Uuid128.NewUuid()), Is.False); var uuid1 = Uuid128.NewUuid(); var uuid2 = Uuid128.NewUuid(); Assert.That(uuid1.Equals(uuid1), Is.True); Assert.That(uuid2.Equals(uuid2), Is.True); Assert.That(uuid1.Equals(uuid2), Is.False); Assert.That(uuid2.Equals(uuid1), Is.False); Assert.That(uuid1.Equals((object)uuid1), Is.True); Assert.That(uuid2.Equals((object)uuid2), Is.True); Assert.That(uuid1.Equals((object)uuid2), Is.False); Assert.That(uuid2.Equals((object)uuid1), Is.False); var uuid1b = Uuid128.Parse(uuid1.ToString()); Assert.That(uuid1b.Equals(uuid1), Is.True); Assert.That(uuid1b.Equals((object)uuid1), Is.True); }
/// <summary>Register all the default converters</summary> private static void RegisterDefaultConverters() { //TODO: there is too much generic type combinations! need to refactor this ... RegisterUnsafe <bool, Slice>((value) => Slice.FromByte(value ? (byte)1 : default(byte))); RegisterUnsafe <bool, byte[]>((value) => Slice.FromByte(value ? (byte)1 : default(byte)).GetBytes()); RegisterUnsafe <bool, string>((value) => value ? "true" : "false"); RegisterUnsafe <bool, sbyte>((value) => value ? (sbyte)1 : default(sbyte)); RegisterUnsafe <bool, byte>((value) => value ? (byte)1 : default(byte)); RegisterUnsafe <bool, short>((value) => value ? (short)1 : default(short)); RegisterUnsafe <bool, ushort>((value) => value ? (ushort)1 : default(ushort)); RegisterUnsafe <bool, int>((value) => value ? 1 : default(int)); RegisterUnsafe <bool, uint>((value) => value ? 1U : default(uint)); RegisterUnsafe <bool, long>((value) => value ? 1L : default(long)); RegisterUnsafe <bool, ulong>((value) => value ? 1UL : default(ulong)); RegisterUnsafe <bool, double>((value) => value ? 0.0d : 1.0d); RegisterUnsafe <bool, float>((value) => value ? 0.0f : 1.0f); RegisterUnsafe <int, Slice>((value) => Slice.FromInt32(value)); RegisterUnsafe <int, byte[]>((value) => Slice.FromInt32(value).GetBytes()); RegisterUnsafe <int, string>((value) => value.ToString(CultureInfo.InvariantCulture)); //TODO: string table! RegisterUnsafe <int, bool>((value) => value != 0); RegisterUnsafe <int, sbyte>((value) => checked ((sbyte)value)); RegisterUnsafe <int, byte>((value) => checked ((byte)value)); RegisterUnsafe <int, short>((value) => checked ((short)value)); RegisterUnsafe <int, ushort>((value) => checked ((ushort)value)); RegisterUnsafe <int, uint>((value) => (uint)value); RegisterUnsafe <int, long>((value) => value); RegisterUnsafe <int, ulong>((value) => (ulong)value); RegisterUnsafe <int, double>((value) => value); RegisterUnsafe <int, float>((value) => checked ((float)value)); RegisterUnsafe <int, FdbTupleAlias>((value) => (FdbTupleAlias)value); RegisterUnsafe <uint, Slice>((value) => Slice.FromUInt64(value)); RegisterUnsafe <uint, byte[]>((value) => Slice.FromUInt64(value).GetBytes()); RegisterUnsafe <uint, string>((value) => value.ToString(CultureInfo.InvariantCulture)); //TODO: string table! RegisterUnsafe <uint, bool>((value) => value != 0); RegisterUnsafe <uint, sbyte>((value) => checked ((sbyte)value)); RegisterUnsafe <uint, byte>((value) => checked ((byte)value)); RegisterUnsafe <uint, short>((value) => checked ((short)value)); RegisterUnsafe <uint, ushort>((value) => checked ((ushort)value)); RegisterUnsafe <uint, int>((value) => (int)value); RegisterUnsafe <uint, long>((value) => value); RegisterUnsafe <uint, ulong>((value) => value); RegisterUnsafe <uint, double>((value) => value); RegisterUnsafe <uint, float>((value) => checked ((float)value)); RegisterUnsafe <long, Slice>((value) => Slice.FromInt64(value)); RegisterUnsafe <long, byte[]>((value) => Slice.FromInt64(value).GetBytes()); RegisterUnsafe <long, string>((value) => value.ToString(CultureInfo.InvariantCulture)); //TODO: string table! RegisterUnsafe <long, bool>((value) => value != 0); RegisterUnsafe <long, sbyte>((value) => checked ((sbyte)value)); RegisterUnsafe <long, byte>((value) => checked ((byte)value)); RegisterUnsafe <long, short>((value) => checked ((short)value)); RegisterUnsafe <long, ushort>((value) => checked ((ushort)value)); RegisterUnsafe <long, int>((value) => checked ((int)value)); RegisterUnsafe <long, uint>((value) => (uint)value); RegisterUnsafe <long, ulong>((value) => (ulong)value); RegisterUnsafe <long, double>((value) => checked ((double)value)); RegisterUnsafe <long, float>((value) => checked ((float)value)); RegisterUnsafe <long, TimeSpan>((value) => TimeSpan.FromTicks(value)); RegisterUnsafe <long, Uuid64>((value) => new Uuid64(value)); RegisterUnsafe <long, System.Net.IPAddress>((value) => new System.Net.IPAddress(value)); RegisterUnsafe <ulong, Slice>((value) => Slice.FromUInt64(value)); RegisterUnsafe <ulong, byte[]>((value) => Slice.FromUInt64(value).GetBytes()); RegisterUnsafe <ulong, string>((value) => value.ToString(CultureInfo.InvariantCulture)); //TODO: string table! RegisterUnsafe <ulong, bool>((value) => value != 0); RegisterUnsafe <ulong, sbyte>((value) => checked ((sbyte)value)); RegisterUnsafe <ulong, byte>((value) => checked ((byte)value)); RegisterUnsafe <ulong, short>((value) => checked ((short)value)); RegisterUnsafe <ulong, ushort>((value) => checked ((ushort)value)); RegisterUnsafe <ulong, int>((value) => checked ((int)value)); RegisterUnsafe <ulong, uint>((value) => checked ((uint)value)); RegisterUnsafe <ulong, long>((value) => checked ((long)value)); RegisterUnsafe <ulong, double>((value) => checked ((double)value)); RegisterUnsafe <ulong, float>((value) => checked ((float)value)); RegisterUnsafe <ulong, Uuid64>((value) => new Uuid64(value)); RegisterUnsafe <ulong, TimeSpan>((value) => TimeSpan.FromTicks(checked ((long)value))); RegisterUnsafe <short, Slice>((value) => Slice.FromInt32(value)); RegisterUnsafe <short, byte[]>((value) => Slice.FromInt32(value).GetBytes()); RegisterUnsafe <short, string>((value) => value.ToString(CultureInfo.InvariantCulture)); //TODO: string table! RegisterUnsafe <short, bool>((value) => value != 0); RegisterUnsafe <short, sbyte>((value) => checked ((sbyte)value)); RegisterUnsafe <short, byte>((value) => checked ((byte)value)); RegisterUnsafe <short, ushort>((value) => checked ((ushort)value)); RegisterUnsafe <short, int>((value) => value); RegisterUnsafe <short, uint>((value) => checked ((uint)value)); RegisterUnsafe <short, long>((value) => value); RegisterUnsafe <short, ulong>((value) => checked ((ulong)value)); RegisterUnsafe <short, double>((value) => value); RegisterUnsafe <short, float>((value) => value); RegisterUnsafe <short, FdbTupleAlias>((value) => (FdbTupleAlias)value); RegisterUnsafe <ushort, Slice>((value) => Slice.FromUInt64(value)); RegisterUnsafe <ushort, byte[]>((value) => Slice.FromUInt64(value).GetBytes()); RegisterUnsafe <ushort, string>((value) => value.ToString(CultureInfo.InvariantCulture)); //TODO: string table! RegisterUnsafe <ushort, bool>((value) => value != 0); RegisterUnsafe <ushort, byte>((value) => checked ((byte)value)); RegisterUnsafe <ushort, sbyte>((value) => checked ((sbyte)value)); RegisterUnsafe <ushort, short>((value) => checked ((short)value)); RegisterUnsafe <ushort, int>((value) => value); RegisterUnsafe <ushort, uint>((value) => value); RegisterUnsafe <ushort, long>((value) => value); RegisterUnsafe <ushort, ulong>((value) => value); RegisterUnsafe <ushort, double>((value) => value); RegisterUnsafe <ushort, float>((value) => value); RegisterUnsafe <byte, Slice>((value) => Slice.FromInt32(value)); RegisterUnsafe <byte, byte[]>((value) => Slice.FromInt32(value).GetBytes()); RegisterUnsafe <byte, string>((value) => value.ToString(CultureInfo.InvariantCulture)); //TODO: string table! RegisterUnsafe <byte, bool>((value) => value != 0); RegisterUnsafe <byte, sbyte>((value) => checked ((sbyte)value)); RegisterUnsafe <byte, short>((value) => value); RegisterUnsafe <byte, ushort>((value) => value); RegisterUnsafe <byte, int>((value) => value); RegisterUnsafe <byte, uint>((value) => value); RegisterUnsafe <byte, long>((value) => value); RegisterUnsafe <byte, ulong>((value) => value); RegisterUnsafe <byte, double>((value) => value); RegisterUnsafe <byte, float>((value) => value); RegisterUnsafe <byte, FdbTupleAlias>((value) => (FdbTupleAlias)value); RegisterUnsafe <sbyte, Slice>((value) => Slice.FromInt64(value)); RegisterUnsafe <sbyte, byte[]>((value) => Slice.FromInt64(value).GetBytes()); RegisterUnsafe <sbyte, string>((value) => value.ToString(CultureInfo.InvariantCulture)); //TODO: string table! RegisterUnsafe <sbyte, bool>((value) => value != 0); RegisterUnsafe <sbyte, byte>((value) => checked ((byte)value)); RegisterUnsafe <sbyte, short>((value) => value); RegisterUnsafe <sbyte, ushort>((value) => checked ((ushort)value)); RegisterUnsafe <sbyte, int>((value) => value); RegisterUnsafe <sbyte, uint>((value) => checked ((uint)value)); RegisterUnsafe <sbyte, long>((value) => value); RegisterUnsafe <sbyte, ulong>((value) => checked ((ulong)value)); RegisterUnsafe <sbyte, double>((value) => value); RegisterUnsafe <sbyte, float>((value) => value); RegisterUnsafe <float, Slice>((value) => Slice.FromSingle(value)); RegisterUnsafe <float, byte[]>((value) => Slice.FromSingle(value).GetBytes()); RegisterUnsafe <float, string>((value) => value.ToString("R", CultureInfo.InvariantCulture)); RegisterUnsafe <float, bool>((value) => !(value == 0f || float.IsNaN(value))); RegisterUnsafe <float, sbyte>((value) => checked ((sbyte)value)); RegisterUnsafe <float, byte>((value) => checked ((byte)value)); RegisterUnsafe <float, short>((value) => checked ((short)value)); RegisterUnsafe <float, ushort>((value) => checked ((ushort)value)); RegisterUnsafe <float, int>((value) => checked ((int)value)); RegisterUnsafe <float, uint>((value) => (uint)value); RegisterUnsafe <float, long>((value) => checked ((long)value)); RegisterUnsafe <float, ulong>((value) => (ulong)value); RegisterUnsafe <float, double>((value) => value); RegisterUnsafe <double, Slice>((value) => Slice.FromDouble(value)); RegisterUnsafe <double, byte[]>((value) => Slice.FromDouble(value).GetBytes()); RegisterUnsafe <double, string>((value) => value.ToString("R", CultureInfo.InvariantCulture)); RegisterUnsafe <double, bool>((value) => !(value == 0d || double.IsNaN(value))); RegisterUnsafe <double, sbyte>((value) => checked ((sbyte)value)); RegisterUnsafe <double, byte>((value) => checked ((byte)value)); RegisterUnsafe <double, short>((value) => checked ((short)value)); RegisterUnsafe <double, ushort>((value) => checked ((ushort)value)); RegisterUnsafe <double, int>((value) => checked ((int)value)); RegisterUnsafe <double, uint>((value) => (uint)value); RegisterUnsafe <double, long>((value) => checked ((long)value)); RegisterUnsafe <double, ulong>((value) => (ulong)value); RegisterUnsafe <double, float>((value) => checked ((float)value)); RegisterUnsafe <string, Slice>((value) => Slice.FromString(value)); RegisterUnsafe <string, byte[]>((value) => Slice.FromString(value).GetBytes()); RegisterUnsafe <string, bool>((value) => !string.IsNullOrEmpty(value)); RegisterUnsafe <string, sbyte>((value) => string.IsNullOrEmpty(value) ? default(sbyte) : SByte.Parse(value, CultureInfo.InvariantCulture)); RegisterUnsafe <string, byte>((value) => string.IsNullOrEmpty(value) ? default(byte) : Byte.Parse(value, CultureInfo.InvariantCulture)); RegisterUnsafe <string, short>((value) => string.IsNullOrEmpty(value) ? default(short) : Int16.Parse(value, CultureInfo.InvariantCulture)); RegisterUnsafe <string, ushort>((value) => string.IsNullOrEmpty(value) ? default(ushort) : UInt16.Parse(value, CultureInfo.InvariantCulture)); RegisterUnsafe <string, int>((value) => string.IsNullOrEmpty(value) ? default(int) : Int32.Parse(value, CultureInfo.InvariantCulture)); RegisterUnsafe <string, uint>((value) => string.IsNullOrEmpty(value) ? default(uint) : UInt32.Parse(value, CultureInfo.InvariantCulture)); RegisterUnsafe <string, long>((value) => string.IsNullOrEmpty(value) ? default(long) : Int64.Parse(value, CultureInfo.InvariantCulture)); RegisterUnsafe <string, ulong>((value) => string.IsNullOrEmpty(value) ? default(ulong) : UInt64.Parse(value, CultureInfo.InvariantCulture)); RegisterUnsafe <string, float>((value) => string.IsNullOrEmpty(value) ? default(float) : Single.Parse(value, NumberStyles.Float, CultureInfo.InvariantCulture)); RegisterUnsafe <string, double>((value) => string.IsNullOrEmpty(value) ? default(double) : Double.Parse(value, NumberStyles.Float, CultureInfo.InvariantCulture)); RegisterUnsafe <string, Guid>((value) => string.IsNullOrEmpty(value) ? default(Guid) : Guid.Parse(value)); RegisterUnsafe <string, Uuid128>((value) => string.IsNullOrEmpty(value) ? default(Uuid128) : Uuid128.Parse(value)); RegisterUnsafe <string, Uuid64>((value) => string.IsNullOrEmpty(value) ? default(Uuid64) : Uuid64.Parse(value)); RegisterUnsafe <string, System.Net.IPAddress>((value) => string.IsNullOrEmpty(value) ? default(System.Net.IPAddress) : System.Net.IPAddress.Parse(value)); RegisterUnsafe <byte[], Slice>((value) => Slice.Create(value)); RegisterUnsafe <byte[], string>((value) => value == null ? default(string) : value.Length == 0 ? String.Empty : System.Convert.ToBase64String(value)); RegisterUnsafe <byte[], bool>((value) => value != null && value.Length > 0); RegisterUnsafe <byte[], sbyte>((value) => value == null ? default(sbyte) : Slice.Create(value).ToSByte()); RegisterUnsafe <byte[], byte>((value) => value == null ? default(byte) : Slice.Create(value).ToByte()); RegisterUnsafe <byte[], short>((value) => value == null ? default(short) : Slice.Create(value).ToInt16()); RegisterUnsafe <byte[], ushort>((value) => value == null ? default(ushort) : Slice.Create(value).ToUInt16()); RegisterUnsafe <byte[], int>((value) => value == null ? 0 : Slice.Create(value).ToInt32()); RegisterUnsafe <byte[], uint>((value) => value == null ? 0U : Slice.Create(value).ToUInt32()); RegisterUnsafe <byte[], long>((value) => value == null ? 0L : Slice.Create(value).ToInt64()); RegisterUnsafe <byte[], ulong>((value) => value == null ? 0UL : Slice.Create(value).ToUInt64()); RegisterUnsafe <byte[], Guid>((value) => value == null || value.Length == 0 ? default(Guid) : new Uuid128(value).ToGuid()); RegisterUnsafe <byte[], Uuid128>((value) => value == null || value.Length == 0 ? default(Uuid128) : new Uuid128(value)); RegisterUnsafe <byte[], Uuid64>((value) => value == null || value.Length == 0 ? default(Uuid64) : new Uuid64(value)); RegisterUnsafe <byte[], TimeSpan>((value) => value == null ? TimeSpan.Zero : TimeSpan.FromTicks(Slice.Create(value).ToInt64())); RegisterUnsafe <byte[], System.Net.IPAddress>((value) => value == null || value.Length == 0 ? default(System.Net.IPAddress) : new System.Net.IPAddress(value)); RegisterUnsafe <Guid, Slice>((value) => Slice.FromGuid(value)); RegisterUnsafe <Guid, byte[]>((value) => Slice.FromGuid(value).GetBytes()); RegisterUnsafe <Guid, string>((value) => value.ToString("D", null)); RegisterUnsafe <Guid, Uuid128>((value) => new Uuid128(value)); RegisterUnsafe <Guid, bool>((value) => value != Guid.Empty); RegisterUnsafe <Guid, System.Net.IPAddress>((value) => new System.Net.IPAddress(new Uuid128(value).ToByteArray())); RegisterUnsafe <Uuid128, Slice>((value) => value.ToSlice()); RegisterUnsafe <Uuid128, byte[]>((value) => value.ToByteArray()); RegisterUnsafe <Uuid128, string>((value) => value.ToString("D", null)); RegisterUnsafe <Uuid128, Guid>((value) => value.ToGuid()); RegisterUnsafe <Uuid128, bool>((value) => value != Uuid128.Empty); RegisterUnsafe <Guid, System.Net.IPAddress>((value) => new System.Net.IPAddress(value.ToByteArray())); RegisterUnsafe <Uuid64, Slice>((value) => value.ToSlice()); RegisterUnsafe <Uuid64, byte[]>((value) => value.ToByteArray()); RegisterUnsafe <Uuid64, string>((value) => value.ToString("D", null)); RegisterUnsafe <Uuid64, long>((value) => value.ToInt64()); RegisterUnsafe <Uuid64, ulong>((value) => value.ToUInt64()); RegisterUnsafe <Uuid64, bool>((value) => value.ToInt64() != 0L); RegisterUnsafe <TimeSpan, Slice>((value) => Slice.FromInt64(value.Ticks)); RegisterUnsafe <TimeSpan, byte[]>((value) => Slice.FromInt64(value.Ticks).GetBytes()); RegisterUnsafe <TimeSpan, long>((value) => value.Ticks); RegisterUnsafe <TimeSpan, ulong>((value) => checked ((ulong)value.Ticks)); RegisterUnsafe <TimeSpan, double>((value) => value.TotalSeconds); RegisterUnsafe <TimeSpan, bool>((value) => value == TimeSpan.Zero); RegisterUnsafe <System.Net.IPAddress, Slice>((value) => value != null ? Slice.Create(value.GetAddressBytes()) : Slice.Nil); RegisterUnsafe <System.Net.IPAddress, byte[]>((value) => value != null ? value.GetAddressBytes() : null); RegisterUnsafe <System.Net.IPAddress, string>((value) => value != null ? value.ToString() : null); RegisterUnsafe <FdbTupleAlias, byte>((value) => (byte)value); RegisterUnsafe <FdbTupleAlias, int>((value) => (int)value); RegisterUnsafe <FdbTupleAlias, Slice>((value) => Slice.FromByte((byte)value)); //REVIEW: this should go in the Tuples layer ! RegisterUnsafe <Slice, byte[]>((value) => value.GetBytes()); RegisterUnsafe <Slice, string>((value) => value.ToUnicode()); RegisterUnsafe <Slice, bool>((value) => value.ToBool()); RegisterUnsafe <Slice, sbyte>((value) => value.ToSByte()); RegisterUnsafe <Slice, byte>((value) => value.ToByte()); RegisterUnsafe <Slice, short>((value) => value.ToInt16()); RegisterUnsafe <Slice, ushort>((value) => value.ToUInt16()); RegisterUnsafe <Slice, int>((value) => value.ToInt32()); RegisterUnsafe <Slice, uint>((value) => value.ToUInt32()); RegisterUnsafe <Slice, long>((value) => value.ToInt64()); RegisterUnsafe <Slice, ulong>((value) => value.ToUInt64()); RegisterUnsafe <Slice, Guid>((value) => value.ToGuid()); RegisterUnsafe <Slice, Uuid128>((value) => value.ToUuid128()); RegisterUnsafe <Slice, Uuid64>((value) => value.ToUuid64()); RegisterUnsafe <Slice, TimeSpan>((value) => TimeSpan.FromTicks(value.ToInt64())); RegisterUnsafe <Slice, FdbTupleAlias>((value) => (FdbTupleAlias)value.ToByte()); RegisterUnsafe <Slice, System.Net.IPAddress>((value) => !value.IsNullOrEmpty ? new System.Net.IPAddress(value.GetBytes()) : null); }
/// <summary>Register all the default converters</summary> private static void RegisterDefaultConverters() { //TODO: there is too much generic type combinations! need to refactor this ... RegisterUnsafe <bool, Slice>((value) => Slice.FromByte(value ? (byte)1 : default(byte))); RegisterUnsafe <bool, byte[]>((value) => Slice.FromByte(value ? (byte)1 : default(byte)).GetBytes()); RegisterUnsafe <bool, string>((value) => value ? "true" : "false"); RegisterUnsafe <bool, sbyte>((value) => value ? (sbyte)1 : default(sbyte)); RegisterUnsafe <bool, byte>((value) => value ? (byte)1 : default(byte)); RegisterUnsafe <bool, short>((value) => value ? (short)1 : default(short)); RegisterUnsafe <bool, ushort>((value) => value ? (ushort)1 : default(ushort)); RegisterUnsafe <bool, int>((value) => value ? 1 : default(int)); RegisterUnsafe <bool, uint>((value) => value ? 1U : default(uint)); RegisterUnsafe <bool, long>((value) => value ? 1L : default(long)); RegisterUnsafe <bool, ulong>((value) => value ? 1UL : default(ulong)); RegisterUnsafe <bool, double>((value) => value ? 1.0d : default(double)); RegisterUnsafe <bool, float>((value) => value ? 1.0f : default(float)); RegisterUnsafe <bool, decimal>((value) => value ? 1m : default(decimal)); RegisterUnsafe <int, Slice>(Slice.FromInt32); RegisterUnsafe <int, byte[]>((value) => Slice.FromInt32(value).GetBytes()); RegisterUnsafe <int, string>(StringConverters.ToString); RegisterUnsafe <int, bool>((value) => value != 0); RegisterUnsafe <int, sbyte>((value) => checked ((sbyte)value)); RegisterUnsafe <int, byte>((value) => checked ((byte)value)); RegisterUnsafe <int, short>((value) => checked ((short)value)); RegisterUnsafe <int, ushort>((value) => checked ((ushort)value)); RegisterUnsafe <int, uint>((value) => (uint)value); RegisterUnsafe <int, long>((value) => value); RegisterUnsafe <int, ulong>((value) => (ulong)value); RegisterUnsafe <int, double>((value) => value); RegisterUnsafe <int, float>((value) => value); // possible loss of precision RegisterUnsafe <int, decimal>((value) => value); RegisterUnsafe <uint, Slice>(Slice.FromUInt32); RegisterUnsafe <uint, byte[]>((value) => Slice.FromUInt32(value).GetBytes()); RegisterUnsafe <uint, string>(StringConverters.ToString); RegisterUnsafe <uint, bool>((value) => value != 0); RegisterUnsafe <uint, sbyte>((value) => checked ((sbyte)value)); RegisterUnsafe <uint, byte>((value) => checked ((byte)value)); RegisterUnsafe <uint, short>((value) => checked ((short)value)); RegisterUnsafe <uint, ushort>((value) => checked ((ushort)value)); RegisterUnsafe <uint, int>((value) => (int)value); RegisterUnsafe <uint, long>((value) => value); RegisterUnsafe <uint, ulong>((value) => value); RegisterUnsafe <uint, double>((value) => value); RegisterUnsafe <uint, float>((value) => value); // possible loss of precision RegisterUnsafe <uint, decimal>((value) => value); RegisterUnsafe <long, Slice>(Slice.FromInt64); RegisterUnsafe <long, byte[]>((value) => Slice.FromInt64(value).GetBytes()); RegisterUnsafe <long, string>(StringConverters.ToString); RegisterUnsafe <long, bool>((value) => value != 0); RegisterUnsafe <long, sbyte>((value) => checked ((sbyte)value)); RegisterUnsafe <long, byte>((value) => checked ((byte)value)); RegisterUnsafe <long, short>((value) => checked ((short)value)); RegisterUnsafe <long, ushort>((value) => checked ((ushort)value)); RegisterUnsafe <long, int>((value) => checked ((int)value)); RegisterUnsafe <long, uint>((value) => (uint)value); RegisterUnsafe <long, ulong>((value) => (ulong)value); RegisterUnsafe <long, double>((value) => value); // possible loss of precision RegisterUnsafe <long, float>((value) => value); // possible loss of precision RegisterUnsafe <long, TimeSpan>(TimeSpan.FromTicks); RegisterUnsafe <long, Uuid64>((value) => new Uuid64(value)); RegisterUnsafe <long, System.Net.IPAddress>((value) => new System.Net.IPAddress(value)); RegisterUnsafe <long, decimal>((value) => value); RegisterUnsafe <ulong, Slice>(Slice.FromUInt64); RegisterUnsafe <ulong, byte[]>((value) => Slice.FromUInt64(value).GetBytes()); RegisterUnsafe <ulong, string>(StringConverters.ToString); RegisterUnsafe <ulong, bool>((value) => value != 0); RegisterUnsafe <ulong, sbyte>((value) => checked ((sbyte)value)); RegisterUnsafe <ulong, byte>((value) => checked ((byte)value)); RegisterUnsafe <ulong, short>((value) => checked ((short)value)); RegisterUnsafe <ulong, ushort>((value) => checked ((ushort)value)); RegisterUnsafe <ulong, int>((value) => checked ((int)value)); RegisterUnsafe <ulong, uint>((value) => checked ((uint)value)); RegisterUnsafe <ulong, long>((value) => checked ((long)value)); RegisterUnsafe <ulong, double>((value) => value); // possible loss of precision RegisterUnsafe <ulong, float>((value) => value); // possible loss of precision RegisterUnsafe <ulong, Uuid64>((value) => new Uuid64(value)); RegisterUnsafe <ulong, TimeSpan>((value) => TimeSpan.FromTicks(checked ((long)value))); RegisterUnsafe <ulong, decimal>((value) => value); RegisterUnsafe <short, Slice>(Slice.FromInt16); RegisterUnsafe <short, byte[]>((value) => Slice.FromInt16(value).GetBytes()); RegisterUnsafe <short, string>((value) => StringConverters.ToString(value)); RegisterUnsafe <short, bool>((value) => value != 0); RegisterUnsafe <short, sbyte>((value) => checked ((sbyte)value)); RegisterUnsafe <short, byte>((value) => checked ((byte)value)); RegisterUnsafe <short, ushort>((value) => checked ((ushort)value)); RegisterUnsafe <short, int>((value) => value); RegisterUnsafe <short, uint>((value) => checked ((uint)value)); RegisterUnsafe <short, long>((value) => value); RegisterUnsafe <short, ulong>((value) => checked ((ulong)value)); RegisterUnsafe <short, double>((value) => value); RegisterUnsafe <short, float>((value) => value); RegisterUnsafe <short, decimal>((value) => value); RegisterUnsafe <ushort, Slice>(Slice.FromUInt16); RegisterUnsafe <ushort, byte[]>((value) => Slice.FromUInt16(value).GetBytes()); RegisterUnsafe <ushort, string>((value) => StringConverters.ToString(value)); RegisterUnsafe <ushort, bool>((value) => value != 0); RegisterUnsafe <ushort, byte>((value) => checked ((byte)value)); RegisterUnsafe <ushort, sbyte>((value) => checked ((sbyte)value)); RegisterUnsafe <ushort, short>((value) => checked ((short)value)); RegisterUnsafe <ushort, int>((value) => value); RegisterUnsafe <ushort, uint>((value) => value); RegisterUnsafe <ushort, long>((value) => value); RegisterUnsafe <ushort, ulong>((value) => value); RegisterUnsafe <ushort, double>((value) => value); RegisterUnsafe <ushort, float>((value) => value); RegisterUnsafe <ushort, decimal>((value) => value); RegisterUnsafe <byte, Slice>(Slice.FromByte); RegisterUnsafe <byte, byte[]>((value) => Slice.FromByte(value).GetBytes()); RegisterUnsafe <byte, string>((value) => StringConverters.ToString(value)); RegisterUnsafe <byte, bool>((value) => value != 0); RegisterUnsafe <byte, sbyte>((value) => checked ((sbyte)value)); RegisterUnsafe <byte, short>((value) => value); RegisterUnsafe <byte, ushort>((value) => value); RegisterUnsafe <byte, int>((value) => value); RegisterUnsafe <byte, uint>((value) => value); RegisterUnsafe <byte, long>((value) => value); RegisterUnsafe <byte, ulong>((value) => value); RegisterUnsafe <byte, double>((value) => value); RegisterUnsafe <byte, float>((value) => value); RegisterUnsafe <byte, decimal>((value) => value); RegisterUnsafe <sbyte, Slice>((value) => Slice.FromInt64(value)); RegisterUnsafe <sbyte, byte[]>((value) => Slice.FromInt64(value).GetBytes()); RegisterUnsafe <sbyte, string>((value) => value.ToString(CultureInfo.InvariantCulture)); //TODO: string table! RegisterUnsafe <sbyte, bool>((value) => value != 0); RegisterUnsafe <sbyte, byte>((value) => checked ((byte)value)); RegisterUnsafe <sbyte, short>((value) => value); RegisterUnsafe <sbyte, ushort>((value) => checked ((ushort)value)); RegisterUnsafe <sbyte, int>((value) => value); RegisterUnsafe <sbyte, uint>((value) => checked ((uint)value)); RegisterUnsafe <sbyte, long>((value) => value); RegisterUnsafe <sbyte, ulong>((value) => checked ((ulong)value)); RegisterUnsafe <sbyte, double>((value) => value); RegisterUnsafe <sbyte, float>((value) => value); RegisterUnsafe <sbyte, decimal>((value) => value); RegisterUnsafe <float, Slice>(Slice.FromSingle); RegisterUnsafe <float, byte[]>((value) => Slice.FromSingle(value).GetBytes()); RegisterUnsafe <float, string>((value) => value.ToString("R", CultureInfo.InvariantCulture)); RegisterUnsafe <float, bool>((value) => !(value == 0f || float.IsNaN(value))); RegisterUnsafe <float, sbyte>((value) => checked ((sbyte)value)); RegisterUnsafe <float, byte>((value) => checked ((byte)value)); RegisterUnsafe <float, short>((value) => checked ((short)value)); RegisterUnsafe <float, ushort>((value) => checked ((ushort)value)); RegisterUnsafe <float, int>((value) => checked ((int)value)); RegisterUnsafe <float, uint>((value) => (uint)value); RegisterUnsafe <float, long>((value) => checked ((long)value)); RegisterUnsafe <float, ulong>((value) => (ulong)value); RegisterUnsafe <float, double>((value) => value); RegisterUnsafe <float, decimal>((value) => (decimal)value); // possible loss of precision RegisterUnsafe <double, Slice>((value) => Slice.FromDouble(value)); RegisterUnsafe <double, byte[]>((value) => Slice.FromDouble(value).GetBytes()); RegisterUnsafe <double, string>((value) => value.ToString("R", CultureInfo.InvariantCulture)); RegisterUnsafe <double, bool>((value) => !(value == 0d || double.IsNaN(value))); RegisterUnsafe <double, sbyte>((value) => checked ((sbyte)value)); RegisterUnsafe <double, byte>((value) => checked ((byte)value)); RegisterUnsafe <double, short>((value) => checked ((short)value)); RegisterUnsafe <double, ushort>((value) => checked ((ushort)value)); RegisterUnsafe <double, int>((value) => checked ((int)value)); RegisterUnsafe <double, uint>((value) => (uint)value); RegisterUnsafe <double, long>((value) => checked ((long)value)); RegisterUnsafe <double, ulong>((value) => (ulong)value); RegisterUnsafe <double, float>((value) => (float)value); // possible loss of precision RegisterUnsafe <double, decimal>((value) => (decimal)value); // possible loss of precision RegisterUnsafe <string, Slice>((value) => Slice.FromString(value)); RegisterUnsafe <string, byte[]>((value) => Slice.FromString(value).GetBytes()); //REVIEW: string=>byte[] use UTF-8, but byte[]=>string uses Base64 ? RegisterUnsafe <string, bool>((value) => !string.IsNullOrEmpty(value)); RegisterUnsafe <string, sbyte>((value) => string.IsNullOrEmpty(value) ? default(sbyte) : sbyte.Parse(value, CultureInfo.InvariantCulture)); RegisterUnsafe <string, byte>((value) => string.IsNullOrEmpty(value) ? default(byte) : byte.Parse(value, CultureInfo.InvariantCulture)); RegisterUnsafe <string, short>((value) => string.IsNullOrEmpty(value) ? default(short) : short.Parse(value, CultureInfo.InvariantCulture)); RegisterUnsafe <string, ushort>((value) => string.IsNullOrEmpty(value) ? default(ushort) : ushort.Parse(value, CultureInfo.InvariantCulture)); RegisterUnsafe <string, int>((value) => string.IsNullOrEmpty(value) ? default(int) : int.Parse(value, CultureInfo.InvariantCulture)); RegisterUnsafe <string, uint>((value) => string.IsNullOrEmpty(value) ? default(uint) : uint.Parse(value, CultureInfo.InvariantCulture)); RegisterUnsafe <string, long>((value) => string.IsNullOrEmpty(value) ? default(long) : long.Parse(value, CultureInfo.InvariantCulture)); RegisterUnsafe <string, ulong>((value) => string.IsNullOrEmpty(value) ? default(ulong) : ulong.Parse(value, CultureInfo.InvariantCulture)); RegisterUnsafe <string, float>((value) => string.IsNullOrEmpty(value) ? default(float) : float.Parse(value, NumberStyles.Float, CultureInfo.InvariantCulture)); RegisterUnsafe <string, double>((value) => string.IsNullOrEmpty(value) ? default(double) : double.Parse(value, NumberStyles.Float, CultureInfo.InvariantCulture)); RegisterUnsafe <string, decimal>((value) => string.IsNullOrEmpty(value) ? default(decimal) : decimal.Parse(value, NumberStyles.Float, CultureInfo.InvariantCulture)); RegisterUnsafe <string, Guid>((value) => string.IsNullOrEmpty(value) ? default(Guid) : Guid.Parse(value)); RegisterUnsafe <string, Uuid128>((value) => string.IsNullOrEmpty(value) ? default(Uuid128) : Uuid128.Parse(value)); RegisterUnsafe <string, Uuid64>((value) => string.IsNullOrEmpty(value) ? default(Uuid64) : Uuid64.Parse(value)); RegisterUnsafe <string, System.Net.IPAddress>((value) => string.IsNullOrEmpty(value) ? default(System.Net.IPAddress) : System.Net.IPAddress.Parse(value)); RegisterUnsafe <byte[], Slice>((value) => value.AsSlice()); RegisterUnsafe <byte[], string>((value) => value == null ? default(string) : value.Length == 0 ? string.Empty : System.Convert.ToBase64String(value)); //REVIEW: string=>byte[] use UTF-8, but byte[]=>string uses Base64 ? RegisterUnsafe <byte[], bool>((value) => value != null && value.Length > 0); RegisterUnsafe <byte[], sbyte>((value) => value == null ? default(sbyte) : value.AsSlice().ToSByte()); RegisterUnsafe <byte[], byte>((value) => value == null ? default(byte) : value.AsSlice().ToByte()); RegisterUnsafe <byte[], short>((value) => value == null ? default(short) : value.AsSlice().ToInt16()); RegisterUnsafe <byte[], ushort>((value) => value == null ? default(ushort) : value.AsSlice().ToUInt16()); RegisterUnsafe <byte[], int>((value) => value == null ? 0 : value.AsSlice().ToInt32()); RegisterUnsafe <byte[], uint>((value) => value == null ? 0U : value.AsSlice().ToUInt32()); RegisterUnsafe <byte[], long>((value) => value == null ? 0L : value.AsSlice().ToInt64()); RegisterUnsafe <byte[], ulong>((value) => value == null ? 0UL : value.AsSlice().ToUInt64()); RegisterUnsafe <byte[], Guid>((value) => value == null || value.Length == 0 ? default(Guid) : new Uuid128(value).ToGuid()); RegisterUnsafe <byte[], Uuid128>((value) => value == null || value.Length == 0 ? default(Uuid128) : new Uuid128(value)); RegisterUnsafe <byte[], Uuid64>((value) => value != null ? Uuid64.Read(value) : default(Uuid64)); RegisterUnsafe <byte[], TimeSpan>((value) => value == null ? TimeSpan.Zero : TimeSpan.FromTicks(value.AsSlice().ToInt64())); RegisterUnsafe <byte[], System.Net.IPAddress>((value) => value == null || value.Length == 0 ? default(System.Net.IPAddress) : new System.Net.IPAddress(value)); RegisterUnsafe <Guid, Slice>((value) => Slice.FromGuid(value)); RegisterUnsafe <Guid, byte[]>((value) => Slice.FromGuid(value).GetBytes()); RegisterUnsafe <Guid, string>((value) => value.ToString("D", null)); RegisterUnsafe <Guid, Uuid128>((value) => new Uuid128(value)); RegisterUnsafe <Guid, bool>((value) => value != Guid.Empty); RegisterUnsafe <Guid, System.Net.IPAddress>((value) => new System.Net.IPAddress(new Uuid128(value).ToByteArray())); //REVIEW: custom converter for Guid=>IPv6? RegisterUnsafe <Uuid128, Slice>((value) => value.ToSlice()); RegisterUnsafe <Uuid128, byte[]>((value) => value.ToByteArray()); RegisterUnsafe <Uuid128, string>((value) => value.ToString("D", null)); RegisterUnsafe <Uuid128, Guid>((value) => value.ToGuid()); RegisterUnsafe <Uuid128, bool>((value) => value != Uuid128.Empty); RegisterUnsafe <Uuid128, System.Net.IPAddress>((value) => new System.Net.IPAddress(value.ToByteArray())); //REVIEW: custom converter for Guid=>IPv6? RegisterUnsafe <Uuid64, Slice>((value) => value.ToSlice()); RegisterUnsafe <Uuid64, byte[]>((value) => value.ToByteArray()); RegisterUnsafe <Uuid64, string>((value) => value.ToString("D", null)); RegisterUnsafe <Uuid64, long>((value) => value.ToInt64()); RegisterUnsafe <Uuid64, ulong>((value) => value.ToUInt64()); RegisterUnsafe <Uuid64, bool>((value) => value.ToInt64() != 0L); RegisterUnsafe <TimeSpan, Slice>((value) => Slice.FromInt64(value.Ticks)); RegisterUnsafe <TimeSpan, byte[]>((value) => Slice.FromInt64(value.Ticks).GetBytes()); RegisterUnsafe <TimeSpan, long>((value) => value.Ticks); RegisterUnsafe <TimeSpan, ulong>((value) => checked ((ulong)value.Ticks)); RegisterUnsafe <TimeSpan, double>((value) => value.TotalSeconds); RegisterUnsafe <TimeSpan, bool>((value) => value == TimeSpan.Zero); RegisterUnsafe <System.Net.IPAddress, Slice>((value) => (value?.GetAddressBytes()).AsSlice()); RegisterUnsafe <System.Net.IPAddress, byte[]>((value) => value?.GetAddressBytes()); RegisterUnsafe <System.Net.IPAddress, string>((value) => value?.ToString()); #pragma warning disable 618 RegisterUnsafe <System.Net.IPAddress, int>((value) => (int)(value?.Address ?? 0)); #pragma warning restore 618 RegisterUnsafe <Slice, byte[]>((value) => value.GetBytes()); RegisterUnsafe <Slice, string>((value) => value.ToUnicode()); RegisterUnsafe <Slice, bool>((value) => value.ToBool()); RegisterUnsafe <Slice, sbyte>((value) => value.ToSByte()); RegisterUnsafe <Slice, byte>((value) => value.ToByte()); RegisterUnsafe <Slice, short>((value) => value.ToInt16()); RegisterUnsafe <Slice, ushort>((value) => value.ToUInt16()); RegisterUnsafe <Slice, int>((value) => value.ToInt32()); RegisterUnsafe <Slice, uint>((value) => value.ToUInt32()); RegisterUnsafe <Slice, long>((value) => value.ToInt64()); RegisterUnsafe <Slice, ulong>((value) => value.ToUInt64()); RegisterUnsafe <Slice, float>((value) => value.ToSingle()); RegisterUnsafe <Slice, double>((value) => value.ToDouble()); RegisterUnsafe <Slice, decimal>((value) => value.ToDecimal()); RegisterUnsafe <Slice, Guid>((value) => value.ToGuid()); RegisterUnsafe <Slice, Uuid128>((value) => value.ToUuid128()); RegisterUnsafe <Slice, Uuid64>((value) => value.ToUuid64()); RegisterUnsafe <Slice, TimeSpan>((value) => TimeSpan.FromTicks(value.ToInt64())); RegisterUnsafe <Slice, System.Net.IPAddress>((value) => !value.IsNullOrEmpty ? new System.Net.IPAddress(value.GetBytesOrEmpty()) : null); }