Skip to content

Commit

Permalink
Added support for indefinite length arrays and maps
Browse files Browse the repository at this point in the history
By options
By attributes
Support for objects, dictionaries, collections, CborObject, CborValue
  • Loading branch information
Michael Catanzariti committed Oct 29, 2019
1 parent f8c0ec2 commit d4c3b44
Show file tree
Hide file tree
Showing 19 changed files with 577 additions and 50 deletions.
90 changes: 90 additions & 0 deletions src/Dahomey.Cbor.Tests/ArrayInfiniteLengthTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using Dahomey.Cbor.Attributes;
using System;
using System.Collections.Generic;
using Xunit;

namespace Dahomey.Cbor.Tests
{
public class ArrayIndefiniteLengthTests
{
private class ObjectWithProperty
{
public List<int> List { get; set; }
}

private class ObjectWithPropertyWithDefiniteAttribute
{
[CborLengthMode(LengthMode = LengthMode.DefiniteLength)]
public List<int> List { get; set; }
}

private class ObjectWithPropertyWithIndefiniteAttribute
{
[CborLengthMode(LengthMode = LengthMode.IndefiniteLength)]
public List<int> List { get; set; }
}

[Fact]
public void Options()
{
CborOptions options = new CborOptions
{
ArrayLengthMode = LengthMode.IndefiniteLength
};

List<int> list = new List<int> { 1, 2, 3 };

const string hexBuffer = "9F010203FF";
Helper.TestWrite(list, hexBuffer, null, options);
}

[Fact]
public void PropertyWithIndefiniteLengthAttribute()
{
ObjectWithPropertyWithIndefiniteAttribute obj = new ObjectWithPropertyWithIndefiniteAttribute
{
List = new List<int> { 1, 2, 3}
};

const string hexBuffer = "A1644C6973749F010203FF";
Helper.TestWrite(obj, hexBuffer, null);
}

[Fact]
public void PropertyWithDefiniteLengthAttribute()
{
CborOptions options = new CborOptions
{
ArrayLengthMode = LengthMode.IndefiniteLength
};

ObjectWithPropertyWithDefiniteAttribute obj = new ObjectWithPropertyWithDefiniteAttribute
{
List = new List<int> { 1, 2, 3 }
};

const string hexBuffer = "A1644C69737483010203";
Helper.TestWrite(obj, hexBuffer, null, options);
}

[Fact]
public void PropertyIndefiniteLengthObjectMapping()
{
CborOptions options = new CborOptions();
options.Registry.ObjectMappingRegistry.Register<ObjectWithProperty>(om =>
{
om.AutoMap();
om.ClearMemberMappings();
om.MapMember(o => o.List).SetLengthMode(LengthMode.IndefiniteLength);
});

ObjectWithProperty obj = new ObjectWithProperty
{
List = new List<int> { 1, 2, 3 }
};

const string hexBuffer = "A1644C6973749F010203FF";
Helper.TestWrite(obj, hexBuffer, null, options);
}
}
}
105 changes: 105 additions & 0 deletions src/Dahomey.Cbor.Tests/DictionaryInfiniteLengthTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
using Dahomey.Cbor.Attributes;
using System.Collections.Generic;
using Xunit;

namespace Dahomey.Cbor.Tests
{
public class DictionaryIndefiniteLengthTests
{
private class ObjectWithProperty
{
public Dictionary<int, string> Dictionary { get; set; }
}

private class ObjectWithPropertyWithDefiniteAttribute
{
[CborLengthMode(LengthMode = LengthMode.DefiniteLength)]
public Dictionary<int, string> Dictionary { get; set; }
}

private class ObjectWithPropertyWithIndefiniteAttribute
{
[CborLengthMode(LengthMode = LengthMode.IndefiniteLength)]
public Dictionary<int, string> Dictionary { get; set; }
}

[Fact]
public void Options()
{
CborOptions options = new CborOptions
{
MapLengthMode = LengthMode.IndefiniteLength
};

Dictionary<int, string> dictionary = new Dictionary<int, string>
{
{ 1, "foo" },
{ 2, "bar" }
};

const string hexBuffer = "BF0163666F6F0263626172FF";
Helper.TestWrite(dictionary, hexBuffer, null, options);
}

[Fact]
public void PropertyWithIndefiniteLengthAttribute()
{
ObjectWithPropertyWithIndefiniteAttribute obj = new ObjectWithPropertyWithIndefiniteAttribute
{
Dictionary = new Dictionary<int, string>
{
{ 1, "foo" },
{ 2, "bar" }
}
};

const string hexBuffer = "A16A44696374696F6E617279BF0163666F6F0263626172FF";
Helper.TestWrite(obj, hexBuffer, null);
}

[Fact]
public void PropertyWithDefiniteLengthAttribute()
{
CborOptions options = new CborOptions
{
MapLengthMode = LengthMode.IndefiniteLength
};

ObjectWithPropertyWithDefiniteAttribute obj = new ObjectWithPropertyWithDefiniteAttribute
{
Dictionary = new Dictionary<int, string>
{
{ 1, "foo" },
{ 2, "bar" }
}
};

const string hexBuffer = "BF6A44696374696F6E617279A20163666F6F0263626172FF";
Helper.TestWrite(obj, hexBuffer, null, options);
}

[Fact]
public void PropertyIndefiniteLengthObjectMapping()
{
CborOptions options = new CborOptions();
options.Registry.ObjectMappingRegistry.Register<ObjectWithProperty>(om =>
{
om.AutoMap();
om.ClearMemberMappings();
om.MapMember(o => o.Dictionary).SetLengthMode(LengthMode.IndefiniteLength);
});

ObjectWithProperty obj = new ObjectWithProperty
{
Dictionary = new Dictionary<int, string>
{
{ 1, "foo" },
{ 2, "bar" }
}
};

const string hexBuffer = "A16A44696374696F6E617279BF0163666F6F0263626172FF";
Helper.TestWrite(obj, hexBuffer, null, options);
}
}
}
199 changes: 199 additions & 0 deletions src/Dahomey.Cbor.Tests/ObjectInfiniteLengthTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
using Dahomey.Cbor.Attributes;
using Xunit;

namespace Dahomey.Cbor.Tests
{
public class ObjectIndefiniteLengthTests
{
private class Object
{
public int Id { get; set; }
public string Name { get; set; }
}

[CborLengthMode(LengthMode = LengthMode.IndefiniteLength)]
private class ObjectWithIndefiniteAttribute
{
public int Id { get; set; }
public string Name { get; set; }
}

[CborLengthMode(LengthMode = LengthMode.DefiniteLength)]
private class ObjectWithDefiniteAttribute
{
public int Id { get; set; }
public string Name { get; set; }
}

private class ObjectWithProperty
{
public Object Object { get; set; }
}

private class ObjectWithPropertyWithDefiniteAttribute
{
[CborLengthMode(LengthMode = LengthMode.DefiniteLength)]
public Object Object { get; set; }
}

private class ObjectWithPropertyWithIndefiniteAttribute
{
[CborLengthMode(LengthMode = LengthMode.IndefiniteLength)]
public Object Object { get; set; }
}

[Fact]
public void Options()
{
CborOptions options = new CborOptions
{
MapLengthMode = LengthMode.IndefiniteLength
};

Object obj = new Object
{
Id = 12,
Name = "foo"
};

const string hexBuffer = "BF6249640C644E616D6563666F6FFF";
Helper.TestWrite(obj, hexBuffer, null, options);
}

[Fact]
public void IndefiniteLengthAttribute()
{
ObjectWithIndefiniteAttribute obj = new ObjectWithIndefiniteAttribute
{
Id = 12,
Name = "foo"
};

const string hexBuffer = "BF6249640C644E616D6563666F6FFF";
Helper.TestWrite(obj, hexBuffer, null);
}

[Fact]
public void DefiniteLengthAttribute()
{
CborOptions options = new CborOptions
{
MapLengthMode = LengthMode.IndefiniteLength
};

ObjectWithDefiniteAttribute obj = new ObjectWithDefiniteAttribute
{
Id = 12,
Name = "foo"
};

const string hexBuffer = "A26249640C644E616D6563666F6F";
Helper.TestWrite(obj, hexBuffer, null, options);
}

[Fact]
public void IndefiniteLengthObjectMapping()
{
CborOptions options = new CborOptions();
options.Registry.ObjectMappingRegistry.Register<Object>(objectMapping =>
objectMapping
.AutoMap()
.SetLengthMode(LengthMode.IndefiniteLength)
);

Object obj = new Object
{
Id = 12,
Name = "foo"
};

const string hexBuffer = "BF6249640C644E616D6563666F6FFF";
Helper.TestWrite(obj, hexBuffer, null, options);
}

[Fact]
public void DefiniteLengthObjectMapping()
{
CborOptions options = new CborOptions
{
MapLengthMode = LengthMode.IndefiniteLength
};

options.Registry.ObjectMappingRegistry.Register<Object>(objectMapping =>
objectMapping
.AutoMap()
.SetLengthMode(LengthMode.DefiniteLength)
);

Object obj = new Object
{
Id = 12,
Name = "foo"
};

const string hexBuffer = "A26249640C644E616D6563666F6F";
Helper.TestWrite(obj, hexBuffer, null, options);
}

[Fact]
public void PropertyWithIndefiniteLengthAttribute()
{
ObjectWithPropertyWithIndefiniteAttribute obj = new ObjectWithPropertyWithIndefiniteAttribute
{
Object = new Object
{
Id = 12,
Name = "foo"
}
};

const string hexBuffer = "A1664F626A656374BF6249640C644E616D6563666F6FFF";
Helper.TestWrite(obj, hexBuffer, null);
}

[Fact]
public void PropertyWithDefiniteLengthAttribute()
{
CborOptions options = new CborOptions
{
MapLengthMode = LengthMode.IndefiniteLength
};

ObjectWithPropertyWithDefiniteAttribute obj = new ObjectWithPropertyWithDefiniteAttribute
{
Object = new Object
{
Id = 12,
Name = "foo"
}
};

const string hexBuffer = "BF664F626A656374A26249640C644E616D6563666F6FFF";
Helper.TestWrite(obj, hexBuffer, null, options);
}

[Fact]
public void PropertyIndefiniteLengthObjectMapping()
{
CborOptions options = new CborOptions();
options.Registry.ObjectMappingRegistry.Register<ObjectWithProperty>(om =>
{
om.AutoMap();
om.ClearMemberMappings();
om.MapMember(o => o.Object).SetLengthMode(LengthMode.IndefiniteLength);
});

ObjectWithProperty obj = new ObjectWithProperty
{
Object = new Object
{
Id = 12,
Name = "foo"
}
};

const string hexBuffer = "A1664F626A656374BF6249640C644E616D6563666F6FFF";
Helper.TestWrite(obj, hexBuffer, null, options);
}
}
}

0 comments on commit d4c3b44

Please sign in to comment.