Skip to content

Commit

Permalink
Support MemoryStream
Browse files Browse the repository at this point in the history
  • Loading branch information
inputfalken committed Sep 30, 2023
1 parent 3486eb5 commit 41270be
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; }

Check warning on line 26 in DynamoDBGenerator.SourceGenerator.Tests/DynamoDBDocumentTests/Serialize/Types/StreamTests.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'MyMemoryStream' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
}
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 41270be

Please sign in to comment.