-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
4 changed files
with
656 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using Xunit; | ||
|
||
namespace Dahomey.Cbor.Tests | ||
{ | ||
public class TupleTests | ||
{ | ||
[Fact] | ||
public void ReadTuple() | ||
{ | ||
const string hexBuffer = "82664C6F6E646F6E1907E7"; //["London", 2023] | ||
(string stringValue, int intValue) = Helper.Read<(string, int)>(hexBuffer); | ||
|
||
Assert.Equal("London", stringValue); | ||
Assert.Equal(2023, intValue); | ||
} | ||
|
||
[Fact] | ||
public void WriteTuple() | ||
{ | ||
const string hexBuffer = "82664C6F6E646F6E1907E7"; //["London", 2023] | ||
string hexResult = Helper.Write(("London", 2023)); | ||
|
||
Assert.Equal(hexBuffer, hexResult); | ||
} | ||
|
||
public class TupleObject | ||
{ | ||
public int Int { get; set; } | ||
public (int, string) Tuple { get; set; } | ||
public string String { get; set; } | ||
} | ||
|
||
[Fact] | ||
public void ReadTupleObject() | ||
{ | ||
// {"Int":12, "Tuple":[12, "foo"], "String": "foo"} | ||
const string hexBuffer = "A363496E740C655475706C65820C63666F6F66537472696E6763666F6F"; | ||
|
||
TupleObject obj = Helper.Read<TupleObject>(hexBuffer); | ||
Assert.NotNull(obj); | ||
Assert.Equal(12, obj.Int); | ||
Assert.Equal(12, obj.Tuple.Item1); | ||
Assert.Equal("foo", obj.Tuple.Item2); | ||
Assert.Equal("foo", obj.String); | ||
} | ||
|
||
[Fact] | ||
public void WriteTupleObject() | ||
{ | ||
// {"Int":12, "Tuple":[12, "foo"], "String": "foo"} | ||
const string hexBuffer = "A363496E740C655475706C65820C63666F6F66537472696E6763666F6F"; | ||
|
||
TupleObject obj = new TupleObject | ||
{ | ||
Int = 12, | ||
Tuple = (12, "foo"), | ||
String = "foo", | ||
}; | ||
|
||
Helper.TestWrite(obj, hexBuffer); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/Dahomey.Cbor/Serialization/Converters/Providers/TupleConverterProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
|
||
namespace Dahomey.Cbor.Serialization.Converters.Providers | ||
{ | ||
public class TupleConverterProvider : CborConverterProviderBase | ||
{ | ||
private static readonly HashSet<Type> ValueTupleTypes = new HashSet<Type>( | ||
[ | ||
typeof(ValueTuple<>), | ||
typeof(ValueTuple<,>), | ||
typeof(ValueTuple<,,>), | ||
typeof(ValueTuple<,,,>), | ||
typeof(ValueTuple<,,,,>), | ||
typeof(ValueTuple<,,,,,>), | ||
typeof(ValueTuple<,,,,,,>), | ||
typeof(ValueTuple<,,,,,,,>) | ||
]); | ||
|
||
public override ICborConverter? GetConverter(Type type, CborOptions options) | ||
{ | ||
if (type.IsGenericType && ValueTupleTypes.Contains(type.GetGenericTypeDefinition())) | ||
{ | ||
FieldInfo[] fields = type.GetFields(); | ||
switch (fields.Length) | ||
{ | ||
case 2: | ||
return CreateGenericConverter(options, typeof(Tuple2Converter<,>), fields.Select(field => field.FieldType).ToArray()); | ||
|
||
case 3: | ||
return CreateGenericConverter(options, typeof(Tuple3Converter<,,>), fields.Select(field => field.FieldType).ToArray()); | ||
|
||
case 4: | ||
return CreateGenericConverter(options, typeof(Tuple4Converter<,,,>), fields.Select(field => field.FieldType).ToArray()); | ||
|
||
case 5: | ||
return CreateGenericConverter(options, typeof(Tuple5Converter<,,,,>), fields.Select(field => field.FieldType).ToArray()); | ||
|
||
case 6: | ||
return CreateGenericConverter(options, typeof(Tuple6Converter<,,,,,>), fields.Select(field => field.FieldType).ToArray()); | ||
|
||
case 7: | ||
return CreateGenericConverter(options, typeof(Tuple7Converter<,,,,,,>), fields.Select(field => field.FieldType).ToArray()); | ||
|
||
case 8: | ||
return CreateGenericConverter(options, typeof(Tuple8Converter<,,,,,,,>), fields.Select(field => field.FieldType).ToArray()); | ||
|
||
default: | ||
throw new CborException($"Tuples of length {fields.Length} are not supported"); | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
} |
Oops, something went wrong.