public override Dictionary <TKey, TValue> ConvertFromInternal(object value) { if (value is byte[]) { var components = new Dictionary <TKey, TValue>(); var keyTypeHint = typeof(TKey); var valueTypeHint = typeof(TValue); using (var bytes = new MemoryStream((byte[])value)) { // number of key / value pairs var numEntriesBytes = new byte[2]; if (bytes.Read(numEntriesBytes, 0, 2) <= 0) { return(components); } var nEntries = BitConverter.ToUInt16(numEntriesBytes, 0); for (var i = 0; i < nEntries; i++) { //get the length of the key var keyLengthBytes = new byte[2]; bytes.Read(keyLengthBytes, 0, 2); var keyLength = BitConverter.ToUInt16(keyLengthBytes, 0); //read the content of the key into a buffer var keyBuffer = new byte[keyLength]; bytes.Read(keyBuffer, 0, keyLength); var entryKey = CassandraObject.GetCassandraObjectFromDatabaseByteArray(keyBuffer, keyTypeHint); //get the length of the value var valueLengthBytes = new byte[2]; bytes.Read(valueLengthBytes, 0, 2); var valueLength = BitConverter.ToUInt16(valueLengthBytes, 0); //read the content of the key into a buffer var valueBuffer = new byte[valueLength]; bytes.Read(valueBuffer, 0, valueLength); var entryValue = CassandraObject.GetCassandraObjectFromDatabaseByteArray(valueBuffer, valueTypeHint); components.Add((TKey)entryKey, (TValue)entryValue); } } return(components); } if (value.GetType().GetInterfaces().Contains(typeof(IEnumerable <object>))) { return(new Dictionary <TKey, TValue>(((IEnumerable <object>)value).Cast <KeyValuePair <TKey, TValue> >().ToDictionary(k => k.Key, v => v.Value))); } if (value.GetType().GetInterfaces().Contains(typeof(IEnumerable <KeyValuePair <TKey, TValue> >))) { return(new Dictionary <TKey, TValue>(((IEnumerable <KeyValuePair <TKey, TValue> >)value).ToDictionary(k => k.Key, v => v.Value))); } return(null); }
private static T ConvertTo <T>(CassandraObject type) { if (type == null) { return(default(T)); } return(type.GetValue <T>()); }
public static ListType <T> From <TIn>(List <TIn> obj) { //Check to make sure we can convert if (ComponentType.CreateInstance().CanConvertFrom(typeof(TIn))) { return(new ListType <T>(obj.Select(o => (T)CassandraObject.GetCassandraObjectFromObject(o, typeof(T))))); } throw new ArgumentException(string.Format("can't convert list of type {0} to ListType of type {1}", typeof(TIn), typeof(T))); }
public static MapType <TKey, TValue> From <TKeyIn, TValueIn>(IDictionary <TKeyIn, TValueIn> obj) { //Check to make sure we can convert if (KeyType.CreateInstance().CanConvertFrom(typeof(TKeyIn)) && ValueType.CreateInstance().CanConvertFrom(typeof(TValueIn))) { return(new MapType <TKey, TValue>(obj.ToDictionary( k => (TKey)CassandraObject.GetCassandraObjectFromObject(k.Key, KeyType), v => (TValue)CassandraObject.GetCassandraObjectFromObject(v.Value, ValueType)))); } throw new ArgumentException(string.Format("can't convert IDictionary of type {0},{1} to MapType of type {2},{3}", typeof(TKeyIn), typeof(TValueIn), typeof(TKey), typeof(TValue))); }
public void CassandraType_Cast() { // arrange var expected = new CassandraObject[] { (AsciiType)"string1", (LongType)300 }; // act CompositeType actualType = expected; CassandraObject actual = actualType; // assert Assert.True(expected.SequenceEqual((CassandraObject[])actual)); }
public void CassandraType_Cast() { // arrange BigInteger expected = 100; IntegerType actualType = expected; // act CassandraObject actual = actualType; // assert Assert.Equal(expected, (BigInteger)actual); }
public void CassandraType_Cast() { // arrange Guid expected = guid; TimeUUIDType actualType = expected; // act CassandraObject actual = actualType; // assert Assert.Equal(expected, (Guid)actual); }
public void CassandraType_Cast() { // arranage string expected = "The quick brown fox jumps over the lazy dog."; UTF8Type actualType = expected; // act CassandraObject actual = actualType; // assert Assert.Equal(expected, (string)actual); }
public void CassandraType_Cast() { //arrange var expected = _setType; //act SetType <UTF8Type> actualType = expected; CassandraObject actual = actualType; //assert Assert.True(expected.SequenceEqual((CassandraObject[])actual)); }
public void CassandraType_Cast() { //arrange var expected = _mapType; //act MapType <LongType, UUIDType> actualType = expected; CassandraObject actual = actualType; //assert Assert.True(expected.SequenceEqual(actual.GetValue <Dictionary <LongType, UUIDType> >())); }
public void CassandraType_Cast() { // arranage long expected = 100L; IntegerType actualType = expected; // act CassandraObject actual = actualType; // assert Assert.Equal(expected, (long)actual); }
public void CassandraType_Cast() { // arrange BigDecimal expected = bigDecimal; DecimalType actualType = expected; // act CassandraObject actual = actualType; // assert Assert.Equal(expected, (BigDecimal)actual); }
public void JavaBytes_To_CompositeType() { // arrange var expected = new CassandraObject[] { (BytesType)_compositeType[0].GetValue <string>(), (BytesType)_compositeType[1].GetValue <long>() }; // act var actual = new CompositeType(); actual.SetValueFromBigEndian(_javaByteOrder); // assert Assert.True(expected.SequenceEqual((CassandraObject[])actual)); }
public void Implicit_ByteArray_Cast() { // arrange var expected = new CassandraObject[] { (AsciiType)"string1", (LongType)300 }; byte[] bytes = GetBytes(expected); // act CompositeType actual = bytes; // assert Assert.True(expected.SequenceEqual(actual)); }
public void CassandraType_Cast() { // arrange byte[] expected = new byte[] { 0, 32, 0, 16, 0, 0, 64, 128 }; BytesType actualType = expected; // act CassandraObject actualCassandraType = actualType; byte[] actual = actualCassandraType; // assert Assert.True(expected.SequenceEqual(actual)); }
public override Dictionary <TKey, TValue> FromBigEndian(byte[] value) { var components = new Dictionary <TKey, TValue>(); var keyTypeHint = typeof(TKey); var valueTypeHint = typeof(TValue); if (value == null) { return(components); } using (var bytes = new MemoryStream((byte[])value)) { // number of key / value pairs var numEntriesBytes = new byte[2]; if (bytes.Read(numEntriesBytes, 0, 2) <= 0) { return(components); } var nEntries = BitConverter.ToUInt16(ConvertEndian(numEntriesBytes), 0); for (var i = 0; i < nEntries; i++) { //get the length of the key var keyLengthBytes = new byte[2]; bytes.Read(keyLengthBytes, 0, 2); var keyLength = BitConverter.ToUInt16(ConvertEndian(keyLengthBytes), 0); //read the content of the key into a buffer var keyBuffer = new byte[keyLength]; bytes.Read(keyBuffer, 0, keyLength); var entryKey = CassandraObject.GetCassandraObjectFromDatabaseByteArray(keyBuffer, keyTypeHint); //get the length of the value var valueLengthBytes = new byte[2]; bytes.Read(valueLengthBytes, 0, 2); var valueLength = BitConverter.ToUInt16(ConvertEndian(valueLengthBytes), 0); //read the content of the key into a buffer var valueBuffer = new byte[valueLength]; bytes.Read(valueBuffer, 0, valueLength); var entryValue = CassandraObject.GetCassandraObjectFromDatabaseByteArray(valueBuffer, valueTypeHint); components.Add((TKey)entryKey, (TValue)entryValue); } } return(components); }
public static CassandraType GetCassandraType(CassandraObject obj) { var typeName = obj.GetType().Name; var cassandraType = (CassandraType)null; switch (typeName.ToLower()) { case "asciitype": cassandraType = AsciiType; break; case "booleantype": cassandraType = BooleanType; break; case "bytestype": cassandraType = BytesType; break; case "datetype": cassandraType = DateType; break; case "decimaltype": cassandraType = DecimalType; break; case "doubletype": cassandraType = DoubleType; break; case "floattype": cassandraType = FloatType; break; case "int32type": cassandraType = Int32Type; break; case "integertype": cassandraType = IntegerType; break; case "lexicaluuidtype": cassandraType = LexicalUUIDType; break; case "longtype": cassandraType = LongType; break; case "timeuuidtype": cassandraType = TimeUUIDType; break; case "utf8type": cassandraType = UTF8Type; break; case "uuidtype": cassandraType = UUIDType; break; case "emptytype": cassandraType = EmptyType; break; case "inetaddresstype": cassandraType = InetAddressType; break; // these need work //case "compositetype": cassandraType = CompositeType; break; //case "dynamiccompositetype": cassandraType = DynamicCompositeType; break; //case "countercolumntype": cassandraType = CounterColumnType; break; //case "reversedtype": cassandraType = ReversedType; break; default: throw new CassandraException("Type '" + typeName + "' not found."); } return(cassandraType); }
public override List <T> ConvertFromInternal(object value) { if (value is byte[]) { var components = new List <T>(); var typeHint = typeof(T); using (var bytes = new MemoryStream((byte[])value)) { // number of elements var numElementsBytes = new byte[2]; if (bytes.Read(numElementsBytes, 0, 2) <= 0) { return(components); } var nElements = BitConverter.ToUInt16(numElementsBytes, 0); for (var i = 0; i < nElements; i++) { //get the length of this element var elementLengthBytes = new byte[2]; bytes.Read(elementLengthBytes, 0, 2); var elementLength = BitConverter.ToUInt16(elementLengthBytes, 0); //read the content of the element into a buffer var buffer = new byte[elementLength]; bytes.Read(buffer, 0, elementLength); var component = CassandraObject.GetCassandraObjectFromDatabaseByteArray(buffer, typeHint); components.Add((T)component); } } return(components); } if (value.GetType().GetInterfaces().Contains(typeof(IEnumerable <object>))) { return(new List <T>(((IEnumerable <object>)value).Cast <T>())); } if (value.GetType().GetInterfaces().Contains(typeof(IEnumerable <T>))) { return(new List <T>((IEnumerable <T>)value)); } return(null); }
public void Explicit_Dictionary_Cast() { //arrange Dictionary <string, int> expected = new Dictionary <string, int>() { { "item1", 1 }, { "item2", 2 }, { "item3", 3 } }; //act MapType <UTF8Type, IntegerType> actualType = MapType <UTF8Type, IntegerType> .From(expected); CassandraObject actual = actualType; //assert Assert.True(expected.SequenceEqual(actual.GetValue <Dictionary <string, int> >())); }
public void Explicit_List_Cast() { //arrange List <int> expected = new List <int>() { 1, 2, 3 }; SetType <IntegerType> actualType = SetType <IntegerType> .From(expected); //act CassandraObject actual = actualType; var actualValues = actual.GetValue <List <object> >(); //assert Assert.True(expected.SequenceEqual(actualValues.Select(Convert.ToInt32))); }
public override List <T> FromBigEndian(byte[] value) { var components = new List <T>(); var typeHint = typeof(T); if (value == null) { return(components); } using (var bytes = new MemoryStream(value)) { // number of elements var numElementsBytes = new byte[2]; if (bytes.Read(numElementsBytes, 0, 2) <= 0) { return(components); } var nElements = BitConverter.ToUInt16(ConvertEndian(numElementsBytes), 0); for (var i = 0; i < nElements; i++) { //get the length of this element var elementLengthBytes = new byte[2]; bytes.Read(elementLengthBytes, 0, 2); var elementLength = BitConverter.ToUInt16(ConvertEndian(elementLengthBytes), 0); //read the content of the element into a buffer var buffer = new byte[elementLength]; bytes.Read(buffer, 0, elementLength); var component = CassandraObject.GetCassandraObjectFromDatabaseByteArray(buffer, typeHint); components.Add((T)component); } } return(components); }