Skip to content

Commit

Permalink
Support MemorStream
Browse files Browse the repository at this point in the history
  • Loading branch information
inputfalken committed Sep 30, 2023
1 parent 3486eb5 commit 62f1e12
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System.Text;
using Amazon.DynamoDBv2.Model;
using DynamoDBGenerator.Attributes;
namespace DynamoDBGenerator.SourceGenerator.Tests.DynamoDBDocumentTests.Deserialize.Types;

[DynamoDBMarshaller(typeof(WithStream))]
public partial class StreamTests
{

[Fact]
public void Unmarshall_Support_Stream()
{
var subject = WithStreamMarshaller
.Unmarshall(new Dictionary<string, AttributeValue>
{

{
nameof(WithStream.MyMemoryStream), new AttributeValue
{
B = new MemoryStream(Encoding.UTF8.GetBytes("Hello"))
}
}
})
.Should()
.BeOfType<WithStream>()
.Subject;

using var streamWriter = new StreamReader(subject.MyMemoryStream);

streamWriter.ReadToEnd().Should().Be("Hello");
}
}

public class WithStream
{
public MemoryStream MyMemoryStream { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.Text;
using Amazon.DynamoDBv2.Model;
using DynamoDBGenerator.Attributes;
namespace DynamoDBGenerator.SourceGenerator.Tests.DynamoDBDocumentTests.Serialize.Types;

[DynamoDBMarshaller(typeof(WithStream))]
public partial class StreamTests
{
[Fact]
public void Marshall_Support_Stream()
{
var subject = WithStreamMarshaller
.Marshall(new WithStream {MyMemoryStream = new MemoryStream(Encoding.UTF8.GetBytes("Hello"))})
.Should()
.BeOfType<Dictionary<string, AttributeValue>>()
.Subject;

using var streamWriter = new StreamReader(subject[nameof(WithStream.MyMemoryStream)].B);
streamWriter.ReadToEnd().Should().Be("Hello");
}

}

public class WithStream
{
public MemoryStream MyMemoryStream { get; set; }
}
2 changes: 2 additions & 0 deletions DynamoDBGenerator.SourceGenerator/DynamoDbMarshaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ or BaseType.SupportedType.Byte
BaseType.SupportedType.Char => typeSymbol.ToInlineAssignment($"S = {accessPattern}.ToString()"),
BaseType.SupportedType.DateOnly or BaseType.SupportedType.DateTimeOffset or BaseType.SupportedType.DateTime => typeSymbol.ToInlineAssignment($@"S = {accessPattern}.ToString(""O"")"),
BaseType.SupportedType.Enum => typeSymbol.ToInlineAssignment($"N = ((int){accessPattern}).ToString()"),
BaseType.SupportedType.MemoryStream => typeSymbol.ToInlineAssignment($"B = {accessPattern}"),
_ => throw new ArgumentOutOfRangeException(typeSymbol.ToDisplayString())
},
SingleGeneric singleGeneric => singleGeneric.Type switch
Expand Down Expand Up @@ -255,6 +256,7 @@ Assignment Execution(in ITypeSymbol typeSymbol, in string accessPattern, string
BaseType.SupportedType.DateTime => typeSymbol.ToInlineAssignment($"{accessPattern} switch {{ {{ S: {{ }} x }} => DateTime.Parse(x), {@default} }}"),
BaseType.SupportedType.DateTimeOffset => typeSymbol.ToInlineAssignment($"{accessPattern} switch {{ {{ S: {{ }} x }} => DateTimeOffset.Parse(x), {@default} }}"),
BaseType.SupportedType.DateOnly => typeSymbol.ToInlineAssignment($"{accessPattern} switch {{ {{ S: {{ }} x }} => DateOnly.Parse(x), {@default} }}"),
BaseType.SupportedType.MemoryStream => typeSymbol.ToInlineAssignment($"{accessPattern} switch {{ {{ B: {{ }} x }} => x, {@default} }}"),
_ => throw new ArgumentOutOfRangeException(typeSymbol.ToDisplayString())
},
SingleGeneric singleGeneric => singleGeneric.Type switch
Expand Down
4 changes: 3 additions & 1 deletion DynamoDBGenerator.SourceGenerator/Types/KnownType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ private BaseType(in ITypeSymbol typeSymbol, in SupportedType type)
{SpecialType: SpecialType.System_Single} => SupportedType.Single,
{SpecialType: SpecialType.System_Byte} => SupportedType.Byte,
{SpecialType: SpecialType.System_SByte} => SupportedType.SByte,
{Name: nameof(MemoryStream)} => SupportedType.MemoryStream,
{TypeKind: TypeKind.Enum} => SupportedType.Enum,
{SpecialType: SpecialType.System_DateTime} => SupportedType.DateTime,
{Name: nameof(DateTimeOffset)} => SupportedType.DateTimeOffset,
Expand Down Expand Up @@ -159,6 +160,7 @@ public enum SupportedType
Single = 15,
DateTime = 16,
DateTimeOffset = 17,
DateOnly = 18
DateOnly = 18,
MemoryStream = 19
}
}
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ This project has not been tested in any real scenario and currently serves as a
| `uint` | `N` |
| `ulong` | `N` |

### Binary

| Type | Field |
|----------------|-------|
| `MemoryStream` | `B` |


### Temporal Types

| Type | Field | Format |
Expand Down

0 comments on commit 62f1e12

Please sign in to comment.