-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e7955dd
commit 367d83c
Showing
61 changed files
with
1,468 additions
and
2,295 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
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
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
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
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
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
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
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
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
35 changes: 35 additions & 0 deletions
35
src/RabbitMQ.Next.Serialization.PlainText/Converters/NullableConverterWrapper.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,35 @@ | ||
using System.Buffers; | ||
|
||
namespace RabbitMQ.Next.Serialization.PlainText.Converters; | ||
|
||
internal class NullableConverterWrapper<T> : IConverter<T?> | ||
where T : struct | ||
|
||
{ | ||
private readonly IConverter<T> wrappedConverter; | ||
|
||
public NullableConverterWrapper(IConverter<T> wrappedConverter) | ||
{ | ||
this.wrappedConverter = wrappedConverter; | ||
} | ||
|
||
public void Format(T? content, IBufferWriter<byte> writer) | ||
{ | ||
if (!content.HasValue) | ||
{ | ||
return; | ||
} | ||
|
||
this.wrappedConverter.Format(content.Value, writer); | ||
} | ||
|
||
public T? Parse(ReadOnlySequence<byte> bytes) | ||
{ | ||
if (bytes.IsEmpty) | ||
{ | ||
return null; | ||
} | ||
|
||
return this.wrappedConverter.Parse(bytes); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
src/RabbitMQ.Next.Serialization.PlainText/Converters/PrimitiveTypeConverterBase.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,61 @@ | ||
using System; | ||
using System.Buffers; | ||
|
||
namespace RabbitMQ.Next.Serialization.PlainText.Converters; | ||
|
||
public abstract class PrimitiveTypeConverterBase<T> : IConverter<T> | ||
where T : struct | ||
{ | ||
public void Format(T content, IBufferWriter<byte> writer) | ||
{ | ||
var target = writer.GetSpan(); | ||
if (this.TryFormat(content, target, out int bytesWritten)) | ||
{ | ||
writer.Advance(bytesWritten); | ||
return; | ||
} | ||
|
||
// should never be here. | ||
throw new OutOfMemoryException(); | ||
} | ||
|
||
public T Parse(ReadOnlySequence<byte> bytes) | ||
{ | ||
T ParseSource(ReadOnlySpan<byte> data) | ||
{ | ||
if(this.TryParse(data, out T result, out var consumed)) | ||
{ | ||
if (consumed != data.Length) | ||
{ | ||
throw new FormatException("Found some extra bytes after the content parsed."); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
throw new FormatException($"Cannot read the payload as {typeof(T)}."); | ||
} | ||
|
||
if (bytes.IsEmpty) | ||
{ | ||
throw new FormatException($"Cannot read the payload as {typeof(T)}."); | ||
} | ||
|
||
if (bytes.IsSingleSegment) | ||
{ | ||
return ParseSource(bytes.FirstSpan); | ||
} | ||
|
||
// It's unlikely to get here, but it's safer to have fallback | ||
Span<byte> buffer = stackalloc byte[(int)bytes.Length]; | ||
bytes.CopyTo(buffer); | ||
|
||
return ParseSource(buffer); | ||
} | ||
|
||
|
||
protected abstract bool TryFormat(T content, Span<byte> target, out int bytesWritten); | ||
|
||
protected abstract bool TryParse(ReadOnlySpan<byte> data, out T value, out int bytesConsumed); | ||
|
||
} |
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
95 changes: 0 additions & 95 deletions
95
src/RabbitMQ.Next.Serialization.PlainText/Converters/SimpleConverterBase.cs
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.