Skip to content

Commit

Permalink
Merge pull request #126 from fauna/remove-type-overrides
Browse files Browse the repository at this point in the history
Remove FaunaType override attribute
  • Loading branch information
pnwpedro authored Jul 1, 2024
2 parents 1cf4c4f + cf2fd28 commit 2d42c55
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 282 deletions.
54 changes: 7 additions & 47 deletions Fauna.Test/Serialization/Serializer.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public void SerializeClassWithAttributes()
{
var test = new PersonWithAttributes();
var actual = Serialize(test);
Assert.AreEqual(@"{""first_name"":""Baz"",""last_name"":""Luhrmann"",""age"":{""@long"":""61""}}", actual);
Assert.AreEqual(@"{""first_name"":""Baz"",""last_name"":""Luhrmann"",""age"":{""@int"":""61""}}", actual);
}

[Test]
Expand All @@ -166,52 +166,6 @@ public void SerializeClassWithTagConflicts()
}
}

[Test]
public void SerializeClassWithTypeConversions()
{
var test = new PersonWithTypeOverrides();
var expectedWithWhitespace = @"
{
""short_to_long"": {""@long"": ""10""},
""ushort_to_long"": {""@long"": ""11""},
""byte_to_long"": {""@long"": ""12""},
""sbyte_to_long"": {""@long"": ""13""},
""int_to_long"": {""@long"": ""20""},
""uint_to_long"": {""@long"": ""21""},
""long_to_long"": {""@long"": ""30""},
""short_to_int"": {""@int"": ""40""},
""ushort_to_int"": {""@int"": ""41""},
""byte_to_int"": {""@int"": ""42""},
""sbyte_to_int"": {""@int"": ""43""},
""int_to_int"": {""@int"": ""50""},
""short_to_double"": {""@double"": ""60""},
""int_to_double"": {""@double"": ""70""},
""long_to_double"": {""@double"": ""80""},
""double_to_double"": {""@double"": ""10.1""},
""float_to_double"": {""@double"": ""1.344499945640564""},
""true_to_true"": true,
""false_to_false"": false,
""class_to_string"": ""TheThing"",
""string_to_string"": ""aString"",
""datetime_to_date"": {""@date"": ""2023-12-13""},
""dateonly_to_date"": {""@date"": ""2023-12-13""},
""datetimeoffset_to_date"": {""@date"": ""2023-12-13""},
""datetime_to_time"": {""@time"":""2023-12-13T12:12:12.0010010Z""},
""datetimeoffset_to_time"": {""@time"":""2023-12-13T12:12:12.0010010\u002B00:00""}
}
";
var expected = Regex.Replace(expectedWithWhitespace, @"\s", string.Empty);
var actual = Serialize(test);
Assert.AreEqual(expected, actual);
}

[Test]
public void SerializeObjectWithInvalidTypeHint()
{
var obj = new ClassWithInvalidPropertyTypeHint();
Assert.Throws<SerializationException>(() => Serialize(obj));
}

[Test]
public void SerializeObjectWithFieldAttributeAndWithoutObjectAttribute()
{
Expand Down Expand Up @@ -263,10 +217,16 @@ public void Serialize_ExtremeNumericValues()
{
{ short.MaxValue, @"{""@int"":""32767""}" },
{ short.MinValue, @"{""@int"":""-32768""}" },
{ ushort.MaxValue, @"{""@int"":""65535""}" },
{ ushort.MinValue, @"{""@int"":""0""}" },
{ int.MaxValue, @"{""@int"":""2147483647""}" },
{ int.MinValue, @"{""@int"":""-2147483648""}" },
{ uint.MaxValue, @"{""@long"":""4294967295""}" },
{ uint.MinValue, @"{""@long"":""0""}" },
{ long.MaxValue, @"{""@long"":""9223372036854775807""}" },
{ long.MinValue, @"{""@long"":""-9223372036854775808""}" },
{ float.MaxValue, @"{""@double"":""3.4028234663852886E\u002B38""}" },
{ float.MinValue, @"{""@double"":""-3.4028234663852886E\u002B38""}" },
{ double.MaxValue, @"{""@double"":""1.7976931348623157E\u002B308""}" },
{ double.MinValue, @"{""@double"":""-1.7976931348623157E\u002B308""}" }
};
Expand Down
60 changes: 1 addition & 59 deletions Fauna.Test/Serialization/TestClasses.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,10 @@ class PersonWithAttributes
{
[Field("first_name")] public string? FirstName { get; set; } = "Baz";
[Field("last_name")] public string? LastName { get; set; } = "Luhrmann";
[Field("age", FaunaType.Long)] public int Age { get; set; } = 61;
[Field("age")] public int Age { get; set; } = 61;
public string? Ignored { get; set; }
}


[Object]
class ClassWithInvalidPropertyTypeHint
{
[Field("first_name", FaunaType.Int)] public string FirstName { get; set; } = "NotANumber";
}

class ClassWithFieldAttributeAndWithoutObjectAttribute
{
[Field("first_name")] public string? FirstName { get; set; } = "Baz";
Expand All @@ -83,57 +76,6 @@ public override string ToString()
}
}

[Object]
class PersonWithTypeOverrides
{
// Long Conversions
[Field("short_to_long", FaunaType.Long)] public short? ShortToLong { get; set; } = 10;
[Field("ushort_to_long", FaunaType.Long)] public ushort? UShortToLong { get; set; } = 11;
[Field("byte_to_long", FaunaType.Long)] public byte? ByteToLong { get; set; } = 12;
[Field("sbyte_to_long", FaunaType.Long)] public sbyte? SByteToLong { get; set; } = 13;
[Field("int_to_long", FaunaType.Long)] public int? IntToLong { get; set; } = 20;
[Field("uint_to_long", FaunaType.Long)] public uint? UIntToLong { get; set; } = 21;
[Field("long_to_long", FaunaType.Long)] public long? LongToLong { get; set; } = 30L;

// Int Conversions
[Field("short_to_int", FaunaType.Int)] public short? ShortToInt { get; set; } = 40;
[Field("ushort_to_int", FaunaType.Int)] public short? UShortToInt { get; set; } = 41;
[Field("byte_to_int", FaunaType.Int)] public byte? ByteToInt { get; set; } = 42;
[Field("sbyte_to_int", FaunaType.Int)] public sbyte? SByteToInt { get; set; } = 43;
[Field("int_to_int", FaunaType.Int)] public int? IntToInt { get; set; } = 50;

// Double Conversions
[Field("short_to_double", FaunaType.Double)] public short? ShortToDouble { get; set; } = 60;
[Field("int_to_double", FaunaType.Double)] public int? IntToDouble { get; set; } = 70;
[Field("long_to_double", FaunaType.Double)] public long? LongToDouble { get; set; } = 80L;
[Field("double_to_double", FaunaType.Double)] public double? DoubleToDouble { get; set; } = 10.1d;
[Field("float_to_double", FaunaType.Double)] public float? FloatToDouble { get; set; } = 1.3445f;

// Bool conversions
[Field("true_to_true", FaunaType.Boolean)] public bool? TrueToTrue { get; set; } = true;
[Field("false_to_false", FaunaType.Boolean)] public bool? FalseToFalse { get; set; } = false;

// String conversions
[Field("class_to_string", FaunaType.String)]
public ThingWithStringOverride? ThingToString { get; set; } = new();
[Field("string_to_string", FaunaType.String)] public string? StringToString { get; set; } = "aString";

// Date conversions
[Field("datetime_to_date", FaunaType.Date)]
public DateTime? DateTimeToDate { get; set; } = DateTime.Parse("2023-12-13T12:12:12.001001Z");
[Field("dateonly_to_date", FaunaType.Date)]
public DateOnly? DateOnlyToDate { get; set; } = new DateOnly(2023, 12, 13);
[Field("datetimeoffset_to_date", FaunaType.Date)]
public DateTimeOffset? DateTimeOffsetToDate { get; set; } = DateTimeOffset.Parse("2023-12-13T12:12:12.001001Z");

// Time conversions
[Field("datetime_to_time", FaunaType.Time)]
public DateTime? DateTimeToTime { get; set; } = DateTime.Parse("2023-12-13T12:12:12.001001Z");
[Field("datetimeoffset_to_time", FaunaType.Time)]
public DateTimeOffset? DateTimeOffsetToTime { get; set; } = new DateTimeOffset(DateTime.Parse("2023-12-13T12:12:12.001001Z"));
}


[Object]
class PersonWithIntConflict
{
Expand Down
25 changes: 0 additions & 25 deletions Fauna/Mapping/Attributes.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
namespace Fauna.Mapping.Attributes;

/// <summary>
/// Enumerates the different types of data that can be stored in Fauna.
/// </summary>
public enum FaunaType
{
Int,
Long,
Double,
String,
Date,
Time,
Boolean,
}

/// <summary>
/// Attribute used to indicate that a class represents a Fauna document or struct.
Expand All @@ -29,23 +16,11 @@ public class ObjectAttribute : Attribute
public class FieldAttribute : Attribute
{
internal readonly string? Name;
internal readonly FaunaType? Type;

public FieldAttribute() { }

public FieldAttribute(string name)
{
Name = name;
}

public FieldAttribute(FaunaType type)
{
Type = type;
}

public FieldAttribute(string name, FaunaType type)
{
Name = name;
Type = type;
}
}
5 changes: 0 additions & 5 deletions Fauna/Mapping/FieldInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ public sealed class FieldInfo
/// </summary>
public PropertyInfo Property { get; }
/// <summary>
/// Indicates which fauna type the value should serialize into.
/// </summary>
public FaunaType? FaunaTypeHint { get; }
/// <summary>
/// The <see cref="Type"/> that the field should deserialize into.
/// </summary>
public Type Type { get; }
Expand All @@ -39,7 +35,6 @@ internal FieldInfo(MappingContext ctx, FieldAttribute attr, PropertyInfo prop)
var nullInfo = nullCtx.Create(prop);

Name = attr.Name ?? FieldName.Canonical(prop.Name);
FaunaTypeHint = attr.Type;
Property = prop;
Type = prop.PropertyType;
IsNullable = nullInfo.WriteState is NullabilityState.Nullable;
Expand Down
Loading

0 comments on commit 2d42c55

Please sign in to comment.