Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 56 additions & 56 deletions src/HotChocolate/Core/src/Types/Types/Scalars/Scalars.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,88 +11,88 @@ public static class Scalars
{
private static readonly Dictionary<Type, Type> s_lookup = new()
{
{ typeof(string), typeof(StringType) },
{ typeof(bool), typeof(BooleanType) },
{ typeof(byte), typeof(UnsignedByteType) },
{ typeof(short), typeof(ShortType) },
{ typeof(byte[]), typeof(Base64StringType) },
{ typeof(DateOnly), typeof(LocalDateType) },
{ typeof(DateTime), typeof(DateTimeType) },
{ typeof(DateTimeOffset), typeof(DateTimeType) },
{ typeof(decimal), typeof(DecimalType) },
{ typeof(double), typeof(FloatType) },
{ typeof(float), typeof(FloatType) },
{ typeof(Guid), typeof(UuidType) },
{ typeof(int), typeof(IntType) },
{ typeof(JsonElement), typeof(AnyType) },
{ typeof(long), typeof(LongType) },
{ typeof(sbyte), typeof(ByteType) },

{ typeof(float), typeof(FloatType) },
{ typeof(double), typeof(FloatType) },
{ typeof(decimal), typeof(DecimalType) },

{ typeof(Uri), typeof(UriType) },
{ typeof(Guid), typeof(UuidType) },
{ typeof(DateTime), typeof(DateTimeType) },
{ typeof(DateTimeOffset), typeof(DateTimeType) },
{ typeof(byte[]), typeof(Base64StringType) },
{ typeof(TimeSpan), typeof(TimeSpanType) },

{ typeof(DateOnly), typeof(LocalDateType) },
{ typeof(short), typeof(ShortType) },
{ typeof(string), typeof(StringType) },
{ typeof(TimeOnly), typeof(LocalTimeType) },
{ typeof(JsonElement), typeof(AnyType) }
{ typeof(TimeSpan), typeof(TimeSpanType) },
{ typeof(uint), typeof(UnsignedIntType) },
{ typeof(ulong), typeof(UnsignedLongType) },
{ typeof(Uri), typeof(UriType) },
{ typeof(ushort), typeof(UnsignedShortType) }
};

private static readonly Dictionary<string, Type> s_nameLookup = new()
{
{ ScalarNames.String, typeof(StringType) },
{ ScalarNames.ID, typeof(IdType) },
{ ScalarNames.Any, typeof(AnyType) },
{ ScalarNames.Base64String, typeof(Base64StringType) },
{ ScalarNames.Boolean, typeof(BooleanType) },
{ ScalarNames.Byte, typeof(ByteType) },
{ ScalarNames.UnsignedByte, typeof(UnsignedByteType) },
{ ScalarNames.Short, typeof(ShortType) },
{ ScalarNames.Int, typeof(IntType) },
{ ScalarNames.Long, typeof(LongType) },

{ ScalarNames.Float, typeof(FloatType) },
{ ScalarNames.Decimal, typeof(DecimalType) },

{ ScalarNames.URI, typeof(UriType) },
{ ScalarNames.URL, typeof(UrlType) },
{ ScalarNames.UUID, typeof(UuidType) },
{ ScalarNames.DateTime, typeof(DateTimeType) },
#pragma warning disable CS0618 // Type or member is obsolete
{ ScalarNames.ByteArray, typeof(ByteArrayType) },
#pragma warning restore CS0618 // Type or member is obsolete
{ ScalarNames.Date, typeof(DateType) },
{ ScalarNames.TimeSpan, typeof(TimeSpanType) },
{ ScalarNames.Any, typeof(AnyType) },
{ ScalarNames.DateTime, typeof(DateTimeType) },
{ ScalarNames.Decimal, typeof(DecimalType) },
{ ScalarNames.Float, typeof(FloatType) },
{ ScalarNames.ID, typeof(IdType) },
{ ScalarNames.Int, typeof(IntType) },
{ ScalarNames.LocalDate, typeof(LocalDateType) },
{ ScalarNames.LocalDateTime, typeof(LocalDateTimeType) },
{ ScalarNames.LocalTime, typeof(LocalTimeType) },

{ ScalarNames.Base64String, typeof(Base64StringType) },
#pragma warning disable CS0618 // Type or member is obsolete
{ ScalarNames.ByteArray, typeof(ByteArrayType) }
#pragma warning restore CS0618 // Type or member is obsolete
{ ScalarNames.Long, typeof(LongType) },
{ ScalarNames.Short, typeof(ShortType) },
{ ScalarNames.String, typeof(StringType) },
{ ScalarNames.TimeSpan, typeof(TimeSpanType) },
{ ScalarNames.UnsignedByte, typeof(UnsignedByteType) },
{ ScalarNames.UnsignedInt, typeof(UnsignedIntType) },
{ ScalarNames.UnsignedLong, typeof(UnsignedLongType) },
{ ScalarNames.UnsignedShort, typeof(UnsignedShortType) },
{ ScalarNames.URI, typeof(UriType) },
{ ScalarNames.URL, typeof(UrlType) },
{ ScalarNames.UUID, typeof(UuidType) }
};

private static readonly Dictionary<Type, ValueKind> s_scalarKinds = new()
{
{ typeof(string), ValueKind.String },
{ typeof(long), ValueKind.Integer },
{ typeof(bool?), ValueKind.Boolean },
{ typeof(bool), ValueKind.Boolean },
{ typeof(byte?), ValueKind.Integer },
{ typeof(byte), ValueKind.Integer },
{ typeof(decimal?), ValueKind.Float },
{ typeof(decimal), ValueKind.Float },
{ typeof(double?), ValueKind.Float },
{ typeof(double), ValueKind.Float },
{ typeof(float?), ValueKind.Float },
{ typeof(float), ValueKind.Float },
{ typeof(int?), ValueKind.Integer },
{ typeof(int), ValueKind.Integer },
{ typeof(short), ValueKind.Integer },
{ typeof(long?), ValueKind.Integer },
{ typeof(int?), ValueKind.Integer },
{ typeof(long), ValueKind.Integer },
{ typeof(sbyte?), ValueKind.Integer },
{ typeof(sbyte), ValueKind.Integer },
{ typeof(short?), ValueKind.Integer },
{ typeof(ulong), ValueKind.Integer },
{ typeof(short), ValueKind.Integer },
{ typeof(string), ValueKind.String },
{ typeof(uint?), ValueKind.Integer },
{ typeof(uint), ValueKind.Integer },
{ typeof(ushort), ValueKind.Integer },
{ typeof(ulong?), ValueKind.Integer },
{ typeof(uint?), ValueKind.Integer },
{ typeof(ulong), ValueKind.Integer },
{ typeof(ushort?), ValueKind.Integer },
{ typeof(byte), ValueKind.Integer },
{ typeof(byte?), ValueKind.Integer },
{ typeof(sbyte), ValueKind.Integer },
{ typeof(sbyte?), ValueKind.Integer },
{ typeof(float), ValueKind.Float },
{ typeof(double), ValueKind.Float },
{ typeof(decimal), ValueKind.Float },
{ typeof(float?), ValueKind.Float },
{ typeof(double?), ValueKind.Float },
{ typeof(decimal?), ValueKind.Float },
{ typeof(bool), ValueKind.Float },
{ typeof(bool?), ValueKind.Float }
{ typeof(ushort), ValueKind.Integer }
};

private static readonly HashSet<string> s_specScalars =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ public static class BuiltInScalarNames
public const string LocalDateTime = nameof(LocalDateTime);
public const string LocalTime = nameof(LocalTime);
public const string Long = nameof(Long);
public const string Name = nameof(Name);
public const string Short = nameof(Short);
public const string String = nameof(String);
Comment thread
glen-84 marked this conversation as resolved.
public const string TimeSpan = nameof(TimeSpan);
public const string UnsignedByte = nameof(UnsignedByte);
public const string UnsignedInt = nameof(UnsignedInt);
public const string UnsignedLong = nameof(UnsignedLong);
public const string UnsignedShort = nameof(UnsignedShort);
public const string Upload = nameof(Upload);
public const string URI = nameof(URI);
public const string URL = nameof(URL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace StrawberryShake.Serialization;
/// <summary>
/// This serializer handles byte scalars.
Comment thread
glen-84 marked this conversation as resolved.
/// </summary>
public class ByteSerializer : ScalarSerializer<byte>
public class ByteSerializer : ScalarSerializer<sbyte>
{
public ByteSerializer(string typeName = BuiltInScalarNames.Byte)
: base(typeName)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace StrawberryShake.Serialization;

/// <summary>
/// This serializer handles unsigned byte scalars.
/// </summary>
public class UnsignedByteSerializer : ScalarSerializer<byte>
{
public UnsignedByteSerializer(string typeName = BuiltInScalarNames.UnsignedByte)
: base(typeName)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace StrawberryShake.Serialization;

/// <summary>
/// This serializer handles unsigned int scalars.
/// </summary>
public class UnsignedIntSerializer : ScalarSerializer<uint>
{
public UnsignedIntSerializer(string typeName = BuiltInScalarNames.UnsignedInt)
: base(typeName)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace StrawberryShake.Serialization;

/// <summary>
/// This serializer handles unsigned long scalars.
/// </summary>
public class UnsignedLongSerializer : ScalarSerializer<ulong>
{
public UnsignedLongSerializer(string typeName = BuiltInScalarNames.UnsignedLong)
: base(typeName)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace StrawberryShake.Serialization;

/// <summary>
/// This serializer handles unsigned short scalars.
/// </summary>
public class UnsignedShortSerializer : ScalarSerializer<ushort>
{
public UnsignedShortSerializer(string typeName = BuiltInScalarNames.UnsignedShort)
: base(typeName)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class ByteSerializerTests
public void Parse()
{
// arrange
const byte value = 1;
const sbyte value = 1;

// act
var result = Serializer.Parse(value);
Expand All @@ -35,7 +35,7 @@ public void Format_Null()
public void Format_Value()
{
// arrange
const byte value = 1;
const sbyte value = 1;

// act
var result = Serializer.Format(value);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
namespace StrawberryShake.Serialization;

public class UnsignedByteSerializerTests
{
private UnsignedByteSerializer Serializer { get; } = new();

private UnsignedByteSerializer CustomSerializer { get; } = new("Abc");

[Fact]
public void Parse()
{
// arrange
const byte value = 1;

// act
var result = Serializer.Parse(value);

// assert
Assert.Equal(value, result);
}

[Fact]
public void Format_Null()
{
// arrange

// act
var result = Serializer.Format(null);

// assert
Assert.Null(result);
}

[Fact]
public void Format_Value()
{
// arrange
const byte value = 1;

// act
var result = Serializer.Format(value);

// assert
Assert.Equal(value, result);
}

[Fact]
public void Format_Exception()
{
// arrange
const string value = "1";

// act
void Action() => Serializer.Format(value);

// assert
Assert.Equal(
"SS0007",
Assert.Throws<GraphQLClientException>(Action).Errors.Single().Code);
}

[Fact]
public void TypeName_Default()
{
// arrange

// act
var typeName = Serializer.TypeName;

// assert
Assert.Equal("UnsignedByte", typeName);
}

[Fact]
public void TypeName_Custom()
{
// arrange

// act
var typeName = CustomSerializer.TypeName;

// assert
Assert.Equal("Abc", typeName);
}
}
Loading
Loading