This repository has been archived by the owner on May 25, 2021. It is now read-only.
/
ListTypeConverter.cs
183 lines (144 loc) · 6.5 KB
/
ListTypeConverter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace FluentCassandra.Types
{
internal class ListTypeConverter<T> : CassandraObjectConverter<List<T>> where T : CassandraObject
{
protected virtual string CollectionStringBegin { get { return "["; } }
protected virtual string CollectionStringEnd { get { return "]"; } }
public override bool CanConvertFrom(Type sourceType)
{
return sourceType == typeof(byte[]) ||
sourceType.GetInterfaces().Contains(typeof(IEnumerable<T>)) ||
sourceType.GetInterfaces().Contains(typeof(IEnumerable<object>));
}
public override bool CanConvertTo(Type destinationType)
{
return destinationType == typeof(byte[]) ||
destinationType == typeof(List<T>) ||
destinationType == typeof(List<CassandraObject>) ||
destinationType == typeof(CassandraObject[]) ||
destinationType == typeof(List<object>) ||
destinationType == typeof(object[]) ||
destinationType == typeof(string);
}
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 override object ConvertToInternal(List<T> value, Type destinationType)
{
if (destinationType == typeof(string))
return CollectionStringBegin + String.Join(",", value) + CollectionStringEnd; //should format the list into a JSON-esque object
if (destinationType == typeof(byte[]))
{
var components = value;
using (var bytes = new MemoryStream())
{
//write the number of lengths
var elements = (ushort)components.Count;
bytes.Write(BitConverter.GetBytes(elements), 0, 2);
foreach (var c in components)
{
var b = c.ToBigEndian();
var length = (ushort)b.Length;
// value length
bytes.Write(BitConverter.GetBytes(length), 0, 2);
// value
bytes.Write(b, 0, length);
}
return bytes.ToArray();
}
}
if (destinationType == typeof(CassandraObject[]))
return value.ToArray();
if (destinationType == typeof(object[]))
return value.Cast<object>().ToArray();
if (destinationType == typeof(List<T>))
return value;
if (destinationType == typeof(List<CassandraObject>))
return value;
if (destinationType == typeof(List<object>))
return value.Cast<object>().ToList();
return null;
}
public override byte[] ToBigEndian(List<T> value)
{
var components = value;
using (var bytes = new MemoryStream())
{
//write the number of lengths
var elements = (ushort)components.Count;
bytes.Write(ConvertEndian(BitConverter.GetBytes(elements)), 0, 2);
foreach (var c in components)
{
var b = c.ToBigEndian();
var length = (ushort)b.Length;
// value length
bytes.Write(ConvertEndian(BitConverter.GetBytes(length)), 0, 2);
// value
bytes.Write(b, 0, length);
}
return bytes.ToArray();
}
}
public override List<T> FromBigEndian(byte[] value)
{
var components = new List<T>();
var typeHint = typeof(T);
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;
}
}
}