Skip to content

Commit f05f1e6

Browse files
author
Michaël Catanzariti
committed
Improved CborArray.FromCollection
- Better peformance - Supports a mixed collection (IEnumerable<object>)
1 parent c34afd1 commit f05f1e6

File tree

3 files changed

+205
-14
lines changed

3 files changed

+205
-14
lines changed

src/Dahomey.Cbor.Tests/CborValueTests.cs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,53 @@ public void FromArray()
208208
Assert.Equal(new CborArray(1, 2, 3), actual);
209209
}
210210

211+
class Foo
212+
{
213+
public int Id { get; set; }
214+
public string Name { get; set; }
215+
}
216+
217+
[Fact]
218+
public void FromClassArray()
219+
{
220+
Foo[] array =
221+
{
222+
new Foo
223+
{
224+
Id = 1,
225+
Name = "foo"
226+
},
227+
new Foo
228+
{
229+
Id = 2,
230+
Name = "bar"
231+
}
232+
};
233+
234+
CborArray actual = CborArray.FromCollection(array);
235+
CborArray expected = new CborArray(
236+
new CborObject
237+
{
238+
["Id"] = 1,
239+
["Name"] = "foo"
240+
},
241+
new CborObject
242+
{
243+
["Id"] = 2,
244+
["Name"] = "bar"
245+
});
246+
Assert.Equal(expected, actual);
247+
}
248+
249+
[Fact]
250+
public void FromMixedArray()
251+
{
252+
object[] array = { true, "foo", 12, 12.12 };
253+
CborArray actual = CborArray.FromCollection(array);
254+
CborArray expected = new CborArray(true, "foo", 12, 12.12);
255+
Assert.Equal(expected, actual);
256+
}
257+
211258
[Fact]
212259
public void ToArray()
213260
{
@@ -224,6 +271,47 @@ public void FromList()
224271
Assert.Equal(new CborArray(1, 2, 3), actual);
225272
}
226273

274+
[Fact]
275+
public void FromClassList()
276+
{
277+
List<Foo> list = new List<Foo>
278+
{
279+
new Foo
280+
{
281+
Id = 1,
282+
Name = "foo"
283+
},
284+
new Foo
285+
{
286+
Id = 2,
287+
Name = "bar"
288+
}
289+
};
290+
291+
CborArray actual = CborArray.FromCollection(list);
292+
CborArray expected = new CborArray(
293+
new CborObject
294+
{
295+
["Id"] = 1,
296+
["Name"] = "foo"
297+
},
298+
new CborObject
299+
{
300+
["Id"] = 2,
301+
["Name"] = "bar"
302+
});
303+
Assert.Equal(expected, actual);
304+
}
305+
306+
[Fact]
307+
public void FromMixedList()
308+
{
309+
List<object> list = new List<object> { true, "foo", 12, 12.12 };
310+
CborArray actual = CborArray.FromCollection(list);
311+
CborArray expected = new CborArray(true, "foo", 12, 12.12);
312+
Assert.Equal(expected, actual);
313+
}
314+
227315
[Fact]
228316
public void ToList()
229317
{

src/Dahomey.Cbor/ObjectModel/CborArray.cs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -177,19 +177,12 @@ public override int GetHashCode()
177177
return hash;
178178
}
179179

180-
public static CborArray FromCollection<T>(T collection, CborOptions? options = null)
181-
where T : ICollection
180+
public static CborArray FromCollection<T>(IEnumerable<T> collection, CborOptions? options = null)
182181
{
183182
options ??= CborOptions.Default;
184183

185-
using ByteBufferWriter buffer = new ByteBufferWriter();
186-
ICborConverter<T> listConverter = options.Registry.ConverterRegistry.Lookup<T>();
187-
CborWriter writer = new CborWriter(buffer);
188-
listConverter.Write(ref writer, collection);
189-
190-
ICborConverter<CborArray> cborArrayConverter = options.Registry.ConverterRegistry.Lookup<CborArray>();
191-
CborReader reader = new CborReader(buffer.WrittenSpan);
192-
return cborArrayConverter.Read(ref reader);
184+
IEnumerable<CborValue> values = collection.Select(item => item == null ? CborValue.Null : CborValueConvert.ToValue(item, options));
185+
return new CborArray(values);
193186
}
194187

195188
public T ToCollection<T>(CborOptions? options = null)

src/Dahomey.Cbor/ObjectModel/CborValue.cs

Lines changed: 114 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
using System;
1+
using Dahomey.Cbor.Serialization;
2+
using Dahomey.Cbor.Serialization.Converters;
3+
using Dahomey.Cbor.Util;
4+
using System;
25
using System.Collections.Generic;
36
using System.Dynamic;
47

@@ -8,6 +11,9 @@ public abstract class CborValue : DynamicObject, IComparable<CborValue>, IEquata
811
{
912
public abstract CborValueType Type { get; }
1013

14+
private static readonly CborNull s_Null = new CborNull();
15+
public static CborNull Null { get; } = s_Null;
16+
1117
public virtual T Value<T>()
1218
{
1319
throw new NotSupportedException(
@@ -84,9 +90,6 @@ public static implicit operator CborValue(bool value)
8490
return (CborBoolean)value;
8591
}
8692

87-
private static readonly CborNull s_Null = new CborNull();
88-
public static CborNull Null { get; } = s_Null;
89-
9093
public static implicit operator CborValue(CborValue[] values)
9194
{
9295
return new CborArray(values);
@@ -121,4 +124,111 @@ public int CompareTypeTo(CborValue other)
121124
public abstract override bool Equals(object? obj);
122125
public abstract override int GetHashCode();
123126
}
127+
128+
public static class CborValueConvert
129+
{
130+
public static CborValue ToValue(string? value, CborOptions? options = null)
131+
{
132+
return value;
133+
}
134+
135+
public static CborValue ToValue(char value, CborOptions? options = null)
136+
{
137+
return value;
138+
}
139+
140+
public static CborValue ToValue(double value, CborOptions? options = null)
141+
{
142+
return value;
143+
}
144+
145+
public static CborValue ToValue(float value, CborOptions? options = null)
146+
{
147+
return value;
148+
}
149+
150+
public static CborValue ToValue(decimal value, CborOptions? options = null)
151+
{
152+
return value;
153+
}
154+
155+
public static CborValue ToValue(sbyte value, CborOptions? options = null)
156+
{
157+
return value;
158+
}
159+
160+
public static CborValue ToValue(byte value, CborOptions? options = null)
161+
{
162+
return value;
163+
}
164+
165+
public static CborValue ToValue(int value, CborOptions? options = null)
166+
{
167+
return value;
168+
}
169+
170+
public static CborValue ToValue(uint value, CborOptions? options = null)
171+
{
172+
return value;
173+
}
174+
175+
public static CborValue ToValue(short value, CborOptions? options = null)
176+
{
177+
return value;
178+
}
179+
180+
public static CborValue ToValue(ushort value, CborOptions? options = null)
181+
{
182+
return value;
183+
}
184+
185+
public static CborValue ToValue(long value, CborOptions? options = null)
186+
{
187+
return value;
188+
}
189+
190+
public static CborValue ToValue(ulong value, CborOptions? options = null)
191+
{
192+
return value;
193+
}
194+
195+
public static CborValue ToValue(bool value, CborOptions? options = null)
196+
{
197+
return value;
198+
}
199+
200+
public static CborValue ToValue(CborValue[] values, CborOptions? options = null)
201+
{
202+
return values;
203+
}
204+
205+
public static CborValue ToValue(Dictionary<CborValue, CborValue> pairs, CborOptions? options = null)
206+
{
207+
return pairs;
208+
}
209+
210+
public static CborValue ToValue(ReadOnlyMemory<byte> value, CborOptions? options = null)
211+
{
212+
return value;
213+
}
214+
215+
public static CborValue ToValue(ReadOnlySpan<byte> value, CborOptions? options = null)
216+
{
217+
return value;
218+
}
219+
220+
public static CborValue ToValue(object value, CborOptions? options = null)
221+
{
222+
options ??= CborOptions.Default;
223+
224+
using ByteBufferWriter buffer = new ByteBufferWriter();
225+
ICborConverter converter = options.Registry.ConverterRegistry.Lookup(value.GetType());
226+
CborWriter writer = new CborWriter(buffer);
227+
converter.Write(ref writer, value);
228+
229+
ICborConverter<CborValue> cborValueConverter = options.Registry.ConverterRegistry.Lookup<CborValue>();
230+
CborReader reader = new CborReader(buffer.WrittenSpan);
231+
return cborValueConverter.Read(ref reader);
232+
}
233+
}
124234
}

0 commit comments

Comments
 (0)