Skip to content

Commit

Permalink
DefaultNodeIdSerializer with defaults in constructor for better testing.
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib committed Jul 4, 2024
1 parent 27bcec2 commit a392223
Showing 1 changed file with 12 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,22 @@

namespace HotChocolate.Types.Relay;

internal sealed class DefaultNodeIdSerializer(INodeIdValueSerializer[] serializers, int maxIdLength) : INodeIdSerializer
internal sealed class DefaultNodeIdSerializer(INodeIdValueSerializer[]? serializers = null, int maxIdLength = 1024)
: INodeIdSerializer
{
private const byte _delimiter = (byte)':';
private const byte _legacyDelimiter = (byte)'\n';
private const int _stackallocThreshold = 256;
private static readonly Encoding _utf8 = Encoding.UTF8;
private readonly ConcurrentDictionary<string, byte[]> _names = new();
private readonly INodeIdValueSerializer[] _serializers = serializers ??
[
new StringNodeIdValueSerializer(),
new Int16NodeIdValueSerializer(),
new Int32NodeIdValueSerializer(),
new Int64NodeIdValueSerializer(),
new GuidNodeIdValueSerializer()
];

public string Format(string typeName, object internalId)
{
Expand Down Expand Up @@ -227,8 +236,8 @@ public NodeId Parse(string formattedId, Type runtimeType)

private INodeIdValueSerializer? TryResolveSerializer(Type type)
{
ref var serializer = ref MemoryMarshal.GetReference(serializers.AsSpan());
ref var end = ref Unsafe.Add(ref serializer, serializers.Length);
ref var serializer = ref MemoryMarshal.GetReference(_serializers.AsSpan());
ref var end = ref Unsafe.Add(ref serializer, _serializers.Length);

while (Unsafe.IsAddressLessThan(ref serializer, ref end))
{
Expand Down

0 comments on commit a392223

Please sign in to comment.