diff --git a/DynamoDBGenerator.SourceGenerator.Tests/DynamoDBDocumentTests/Deserialize/Types/StreamTests.cs b/DynamoDBGenerator.SourceGenerator.Tests/DynamoDBDocumentTests/Deserialize/Types/StreamTests.cs new file mode 100644 index 00000000..e16cd041 --- /dev/null +++ b/DynamoDBGenerator.SourceGenerator.Tests/DynamoDBDocumentTests/Deserialize/Types/StreamTests.cs @@ -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 + { + + { + nameof(WithStream.MyMemoryStream), new AttributeValue + { + B = new MemoryStream(Encoding.UTF8.GetBytes("Hello")) + } + } + }) + .Should() + .BeOfType() + .Subject; + + using var streamWriter = new StreamReader(subject.MyMemoryStream); + + streamWriter.ReadToEnd().Should().Be("Hello"); + } +} + +public class WithStream +{ + public MemoryStream MyMemoryStream { get; set; } +} \ No newline at end of file diff --git a/DynamoDBGenerator.SourceGenerator.Tests/DynamoDBDocumentTests/Serialize/Types/StreamTests.cs b/DynamoDBGenerator.SourceGenerator.Tests/DynamoDBDocumentTests/Serialize/Types/StreamTests.cs new file mode 100644 index 00000000..b563b969 --- /dev/null +++ b/DynamoDBGenerator.SourceGenerator.Tests/DynamoDBDocumentTests/Serialize/Types/StreamTests.cs @@ -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>() + .Subject; + + using var streamWriter = new StreamReader(subject[nameof(WithStream.MyMemoryStream)].B); + streamWriter.ReadToEnd().Should().Be("Hello"); + } + +} + +public class WithStream +{ + public MemoryStream MyMemoryStream { get; set; } +} \ No newline at end of file diff --git a/DynamoDBGenerator.SourceGenerator/DynamoDbMarshaller.cs b/DynamoDBGenerator.SourceGenerator/DynamoDbMarshaller.cs index 03d694ea..5c1fb0a4 100644 --- a/DynamoDBGenerator.SourceGenerator/DynamoDbMarshaller.cs +++ b/DynamoDBGenerator.SourceGenerator/DynamoDbMarshaller.cs @@ -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 @@ -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 diff --git a/DynamoDBGenerator.SourceGenerator/Types/KnownType.cs b/DynamoDBGenerator.SourceGenerator/Types/KnownType.cs index 1cd09a50..03418a33 100644 --- a/DynamoDBGenerator.SourceGenerator/Types/KnownType.cs +++ b/DynamoDBGenerator.SourceGenerator/Types/KnownType.cs @@ -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, @@ -159,6 +160,7 @@ public enum SupportedType Single = 15, DateTime = 16, DateTimeOffset = 17, - DateOnly = 18 + DateOnly = 18, + MemoryStream = 19 } } \ No newline at end of file diff --git a/README.md b/README.md index 4ecc1640..dc28bdad 100644 --- a/README.md +++ b/README.md @@ -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 |