Skip to content

Commit

Permalink
Merge pull request #701 from SteveDunn/memorypack-support
Browse files Browse the repository at this point in the history
Implement MessagePack support
  • Loading branch information
SteveDunn authored Nov 14, 2024
2 parents a4eb561 + 95b2d38 commit 2acd55f
Show file tree
Hide file tree
Showing 80 changed files with 5,119 additions and 821 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<LangVersion>preview</LangVersion>
<Features>strict</Features>
<SuppressNETCoreSdkPreviewMessage>true</SuppressNETCoreSdkPreviewMessage>
<NoWarn>$(NoWarn);CS1701;CS1702;CS1591;CA1014;CS9057;CA2255</NoWarn>
<NoWarn>$(NoWarn);CS1701;CS1702;CS1591;CA1014;CS9057;CA2255;NU1904</NoWarn>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<CheckEolTargetFramework>false</CheckEolTargetFramework>
<AnalysisLevel>latest</AnalysisLevel>
Expand Down
2 changes: 2 additions & 0 deletions Vogen.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_AFTER_INVOCATION_LPAR/@EntryValue">True</s:Boolean>
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_ARGUMENTS_STYLE/@EntryValue">CHOP_IF_LONG</s:String>
<s:String x:Key="/Default/CodeStyle/CSharpVarKeywordUsage/ForOtherTypes/@EntryValue">UseVarWhenEvident</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=EF/@EntryIndexedValue">EF</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateConstants/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="_" Suffix="" Style="aaBb" /&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateStaticReadonly/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="_" Suffix="" Style="aaBb" /&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/UserRules/=15b5b1f1_002D457c_002D4ca6_002Db278_002D5615aedc07d3/@EntryIndexedValue">&lt;Policy&gt;&lt;Descriptor Staticness="Static" AccessRightKinds="Private" Description="Static readonly fields (private)"&gt;&lt;ElementKinds&gt;&lt;Kind Name="READONLY_FIELD" /&gt;&lt;/ElementKinds&gt;&lt;/Descriptor&gt;&lt;Policy Inspect="True" Prefix="_" Suffix="" Style="aaBb" /&gt;&lt;/Policy&gt;</s:String>
Expand All @@ -57,6 +58,7 @@
<s:Boolean x:Key="/Default/UserDictionary/Words/=Daves/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Diag/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Diags/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=efcore/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Errored/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Flintstone/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=formattable/@EntryIndexedValue">True</s:Boolean>
Expand Down
16 changes: 16 additions & 0 deletions docs/site/Writerside/topics/reference/Integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,22 @@ public enum Conversions
/// Creates a BSON serializer for each value object.
/// </summary>
Bson = 1 << 8,

/// <summary>
/// Creates and registers a codec and copier for Microsoft Orleans.
/// This feature requires .NET 8 and C#12 and cannot be polly-filled.
/// </summary>
Orleans = 1 << 9,

/// <summary>
/// Generates implementation of IXmlSerializable.
/// </summary>
XmlSerializable = 1 << 10,

/// <summary>
/// Generates implementation of IMessagePackFormatter.
/// </summary>
MessagePack = 1 << 11
}
```

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#if NULLABLE_DISABLED_BUILD
#pragma warning disable CS8632 // The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
#endif

using System;
using System.Threading.Tasks;
using JetBrains.Annotations;
using MessagePack;
using MessagePack.Formatters;

namespace Vogen.Examples.SerializationAndConversion.MessagePackScenario.UsingConversionAttributes;

[ValueObject<int>(conversions: Conversions.MessagePack)]
public readonly partial struct Age;

[ValueObject<int>(conversions: Conversions.MessagePack)]
public readonly partial struct PersonId;

[ValueObject<string>(conversions: Conversions.MessagePack)]
public readonly partial struct Name;

[UsedImplicitly]
[MessagePackObject]
public class Person
{
[Key(0)]
public PersonId Id { get; set; }

[Key(1)]
public Name Name { get; set; }

[Key(2)]
public Age Age { get; set; }
}

[UsedImplicitly]
public class MessagePackScenario_using_conversion_attributes : IScenario
{
public Task Run()
{
var customResolver = MessagePack.Resolvers.CompositeResolver.Create(
[new PersonIdMessagePackFormatter(), new NameMessagePackFormatter(), new AgeMessagePackFormatter()],
[MessagePack.Resolvers.StandardResolver.Instance]
);

var options = MessagePackSerializerOptions.Standard.WithResolver(customResolver);

var originalObject = new Person
{
Id = PersonId.From(123),
Name = Name.From("Test"),
Age = Age.From(42)
};

byte[] serializedObject = MessagePackSerializer.Serialize(originalObject, options);

var deserializedObject = MessagePackSerializer.Deserialize<Person>(serializedObject, options);

Console.WriteLine($"Id: {deserializedObject.Id}, Name: {deserializedObject.Name}, Active: {deserializedObject.Age}");

return Task.CompletedTask;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#if NULLABLE_DISABLED_BUILD
#pragma warning disable CS8632 // The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
#endif

using System;
using System.Threading.Tasks;
using JetBrains.Annotations;
using MessagePack;
using MessagePack.Formatters;

namespace Vogen.Examples.SerializationAndConversion.MessagePackScenario.UsingMarkers;

[ValueObject<int>]
public readonly partial struct Age;

[ValueObject<int>]
public readonly partial struct PersonId;

[ValueObject<string>]
public readonly partial struct Name;

[UsedImplicitly]
[MessagePackObject]
public class Person
{
[Key(0)]
public PersonId Id { get; set; }

[Key(1)]
public Name Name { get; set; }

[Key(2)]
public Age Age { get; set; }
}

[MessagePack<PersonId>]
[MessagePack<Name>]
[MessagePack<Age>]
public partial class Markers;

[UsedImplicitly]
public class MessagePackScenario_using_markers : IScenario
{
public Task Run()
{
var customResolver = MessagePack.Resolvers.CompositeResolver.Create(
Markers.MessagePackFormatters,
[MessagePack.Resolvers.StandardResolver.Instance]
);

var options = MessagePackSerializerOptions.Standard.WithResolver(customResolver);

var originalObject = new Person
{
Id = PersonId.From(123),
Name = Name.From("Test"),
Age = Age.From(42)
};

byte[] serializedObject = MessagePackSerializer.Serialize(originalObject, options);

var deserializedObject = MessagePackSerializer.Deserialize<Person>(serializedObject, options);

Console.WriteLine($"Id: {deserializedObject.Id}, Name: {deserializedObject.Name}, Active: {deserializedObject.Age}");

return Task.CompletedTask;
}
}
1 change: 1 addition & 0 deletions samples/Vogen.Examples/Vogen.Examples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="JetBrains.Annotations" Version="2022.3.1" />
<PackageReference Include="MessagePack" Version="2.5.187" />

<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.8" />
Expand Down
2 changes: 1 addition & 1 deletion samples/WebApplication/WebApplication.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Refit" Version="7.0.0"/>
<PackageReference Include="Refit" Version="8.0.0"/>
<PackageReference Include="System.Text.Json" Version="8.0.5" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<ItemGroup>
<PackageReference Include="System.Text.Json" Version="8.0.5" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Refit" Version="7.0.0" />
<PackageReference Include="Refit" Version="8.0.0" />
<PackageReference Include="ServiceStack.Text" Version="8.2.2" />
</ItemGroup>

Expand Down
7 changes: 6 additions & 1 deletion src/Vogen.SharedTypes/Conversions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,10 @@ public enum Conversions
/// <summary>
/// Generates implementation of IXmlSerializable.
/// </summary>
XmlSerializable = 1 << 10
XmlSerializable = 1 << 10,

/// <summary>
/// Generates implementation of IMessagePackFormatter.
/// </summary>
MessagePack = 1 << 11
}
8 changes: 7 additions & 1 deletion src/Vogen.SharedTypes/EfCoreConverterAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@

namespace Vogen;



[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
public class EfCoreConverterAttribute<T> : Attribute
public class EfCoreConverterAttribute<T> : ConversionMarkerAttribute
{
}

public class ConversionMarkerAttribute : Attribute
{
}
12 changes: 12 additions & 0 deletions src/Vogen.SharedTypes/MessagePackAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace Vogen;

/// <summary>
/// Represent a marker for MessagePack generation.
/// </summary>
/// <typeparam name="T">The type of the value object.</typeparam>
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
public class MessagePackAttribute<T> : ConversionMarkerAttribute
{
}
3 changes: 2 additions & 1 deletion src/Vogen/AnalyzerReleases.Shipped.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,5 @@ VOG032 | Usage | Warning | DoNotThrowFromUserCodeAnalyzer
Rule ID | Category | Severity | Notes
--------|----------|----------|-------
VOG033 | Usage | Info | UseReadonlyStructInsteadOfStructAnalyzer
VOG034 | Usage | Error | DoNotCompareWithPrimitivesInEfCoreAnalyzer
VOG034 | Usage | Error | DoNotCompareWithPrimitivesInEfCoreAnalyzer

3 changes: 1 addition & 2 deletions src/Vogen/AnalyzerReleases.Unshipped.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
; Unshipped analyzer release
; https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md


### New Rules

Rule ID | Category | Severity | Notes
--------|----------|----------|-------


85 changes: 0 additions & 85 deletions src/Vogen/BuildEfCoreConverterSpecsFromAttributes.cs

This file was deleted.

16 changes: 16 additions & 0 deletions src/Vogen/ConversionMarker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Microsoft.CodeAnalysis;

namespace Vogen;

/// <summary>
/// Represents an instance of a marker attribute, e.g. `EfCoreConverterAttribute` or a `MemoryPackageConverterAttribute`, and its
/// containing marker class.
/// </summary>
/// <param name="VoSymbol">The symbol for the value object being referenced. In effect, the generic value of the attribute.</param>
/// <param name="UnderlyingTypeSymbol">The symbol for the underlying type that is represented by the value object.</param>
/// <param name="MarkerClassSymbol">The symbol for the marker class that has the marker attribute(s).</param>
internal record ConversionMarker(
ConversionMarkerKind Kind,
INamedTypeSymbol VoSymbol,
INamedTypeSymbol UnderlyingTypeSymbol,
INamedTypeSymbol MarkerClassSymbol);
8 changes: 8 additions & 0 deletions src/Vogen/ConversionMarkerKind.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Vogen;

public enum ConversionMarkerKind
{
Unrecognized,
EFCore,
MessagePack,
}
Loading

0 comments on commit 2acd55f

Please sign in to comment.