Skip to content

Commit

Permalink
Merge pull request #706 from SteveDunn/bson-markers
Browse files Browse the repository at this point in the history
Generate BSON serialisers for types in other projects
  • Loading branch information
SteveDunn authored Nov 19, 2024
2 parents 2acd55f + 16eca85 commit ce60ef3
Show file tree
Hide file tree
Showing 5,544 changed files with 1,099,524 additions and 648,342 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
8 changes: 8 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ indent_style=tab
indent_size=tab
tab_width=4

[*.cs]
dotnet_diagnostic.IDE0058.severity = none
dotnet_diagnostic.IDE0303.severity = none
dotnet_diagnostic.CA1508.severity = none
dotnet_diagnostic.CA2000.severity = none
dotnet_diagnostic.RS1041.severity = none
dotnet_diagnostic.RS2008.severity = none

[*.{asax,ascx,aspx,cs,cshtml,css,htm,html,js,jsx,master,razor,skin,ts,tsx,vb,xaml,xamlx,xoml}]
indent_style=space
indent_size= 4
Expand Down
14 changes: 4 additions & 10 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ jobs:
- uses: actions/[email protected]
with:
dotnet-version: |
9.0.x
8.0.x
7.0.x
6.0.x
5.0.x
3.1.x
- name: Build and Test
run: ./Build.ps1
Expand All @@ -31,10 +29,8 @@ jobs:
- uses: actions/[email protected]
with:
dotnet-version: |
9.0.x
8.0.x
7.0.x
6.0.x
5.0.x
3.1.x
- name: Build and Test
run: ./Build.ps1
Expand All @@ -47,7 +43,7 @@ jobs:
- uses: actions/[email protected]
with:
dotnet-version: |
8.0.x
9.0.x
- name: Run Snapshots
run: ./RunSnapshots.ps1
shell: pwsh
Expand All @@ -59,10 +55,8 @@ jobs:
- uses: actions/[email protected]
with:
dotnet-version: |
9.0.x
8.0.x
7.0.x
6.0.x
5.0.x
3.1.x
- name: Build and Test
run: ./Build.ps1
Expand Down
15 changes: 4 additions & 11 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,14 @@ jobs:
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Setup dotnet 8.0
uses: actions/[email protected]
with:
dotnet-version: '8.0.x'
- name: Setup dotnet 7.0
- name: Setup dotnet 9.0
uses: actions/[email protected]
with:
dotnet-version: '7.0.x'
- name: Setup dotnet 6.0
dotnet-version: '9.0.x'
- name: Setup dotnet 8.0
uses: actions/[email protected]
with:
dotnet-version: '6.0.x'
- uses: actions/[email protected]
with:
dotnet-version: '5.0.x'
dotnet-version: '8.0.x'
- uses: actions/[email protected]
with:
dotnet-version: '3.1.x'
Expand Down
4 changes: 1 addition & 3 deletions .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ jobs:
- uses: actions/[email protected]
with:
dotnet-version: |
9.0.x
8.0.x
7.0.x
6.0.x
5.0.x
3.1.x
- name: Build and Test
run: ./Build.ps1
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ coverage*.info
*.coveragexml

# NCrunch
.NCrunch_*
_NCrunch_*
.*crunch*.local.xml
nCrunchTemp_*
Expand Down
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

</PropertyGroup>
<ItemGroup>
<PackageReference Include="DotNet.ReproducibleBuilds" Version="1.2.4">
<PackageReference Include="DotNet.ReproducibleBuilds" Version="1.2.25">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
Expand Down
14 changes: 7 additions & 7 deletions Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,14 @@

<Otherwise>
<ItemGroup>
<PackageReference Update="Microsoft.CodeAnalysis.Analyzers" Version="3.11.0-beta1.23525.2" />
<PackageReference Update="Microsoft.CodeAnalysis.CSharp" Version="4.8.0" />
<PackageReference Update="Microsoft.CodeAnalysis.Workspaces.Common" Version="4.8.0" />
<PackageReference Update="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.8.0" />
<PackageReference Update="Microsoft.CodeAnalysis.Analyzers" Version="3.11.0" />
<PackageReference Update="Microsoft.CodeAnalysis.CSharp" Version="4.11.0" />
<PackageReference Update="Microsoft.CodeAnalysis.Workspaces.Common" Version="4.11.0" />
<PackageReference Update="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.11.0" />
</ItemGroup>
<PropertyGroup>
<DefineConstants>$(DefineConstants);ROSLYN4_8;ROSLYN_4_2_OR_GREATER;ROSLYN_4_4_OR_GREATER;ROSLYN_4_5_OR_GREATER;ROSLYN_4_6_OR_GREATER;ROSLYN_4_8_OR_GREATER</DefineConstants>
<DefineConstants>$(DefineConstants);CSHARP9_OR_GREATER;CSHARP10_OR_GREATER;CSHARP11_OR_GREATER;CSHARP12_OR_GREATER</DefineConstants>
<DefineConstants>$(DefineConstants);ROSLYN4_8;ROSLYN_4_2_OR_GREATER;ROSLYN_4_4_OR_GREATER;ROSLYN_4_5_OR_GREATER;ROSLYN_4_6_OR_GREATER;ROSLYN_4_8_OR_GREATER;ROSLYN_4_10_OR_GREATER</DefineConstants>
<DefineConstants>$(DefineConstants);CSHARP9_OR_GREATER;CSHARP10_OR_GREATER;CSHARP11_OR_GREATER;CSHARP12_OR_GREATER;CSHARP13_OR_GREATER</DefineConstants>
<NoWarn>$(NoWarn);CS0618</NoWarn>
</PropertyGroup>
</Otherwise>
Expand All @@ -107,7 +107,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MinVer" Version="5.0.0">
<PackageReference Include="MinVer" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ There are several code analysis warnings for invalid configuration, including:

## Performance

(to run these yourself: `dotnet run -c Release --framework net8.0 -- --job short --filter *` in the `Vogen.Benchmarks` folder)
(to run these yourself: `dotnet run -c Release --framework net9.0 -- --job short --filter *` in the `Vogen.Benchmarks` folder)

As mentioned previously, the goal of Vogen is to achieve very similar performance compare to using primitives themselves.
Here's a benchmark comparing the use of a validated value object with underlying type of int vs using an int natively (*primitively* 🤓)
Expand Down
2 changes: 2 additions & 0 deletions Vogen.v3.ncrunchsolution
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<SolutionConfiguration>
<Settings>
<AllowParallelTestExecution>True</AllowParallelTestExecution>
<EnableRDI>True</EnableRDI>
<RdiConfigured>True</RdiConfigured>
<SolutionConfigured>True</SolutionConfigured>
</Settings>
</SolutionConfiguration>
2 changes: 1 addition & 1 deletion docs/nuget-readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ This adds a `<PackageReference>` to your project. You can additionally mark the

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
</PropertyGroup>

<!-- Add the package -->
Expand Down
2 changes: 1 addition & 1 deletion src/Vogen.CodeFixers/Vogen.CodeFixers.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<!-- Version and PackageVersion are handled in Directory.Build.targets -->
<TargetFrameworks>net8.0;netstandard2.0</TargetFrameworks>
<TargetFrameworks>net9.0;netstandard2.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<IncludeBuildOutput>false</IncludeBuildOutput>
<Nullable>enable</Nullable>
Expand Down
4 changes: 2 additions & 2 deletions src/Vogen.Pack.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@
<None Include="$(MSBuildThisFileDirectory)\Vogen\bin\$(Configuration)\netstandard2.0\Vogen.SharedTypes.dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />

<None Include="$(MSBuildThisFileDirectory)\Vogen\bin\$(Configuration)\netstandard2.0\Vogen.SharedTypes.dll" Pack="true" PackagePath="lib\netstandard2.0" Visible="true" />
<None Include="$(MSBuildThisFileDirectory)\Vogen\bin\$(Configuration)\net8.0\Vogen.SharedTypes.dll" Pack="true" PackagePath="lib\net8.0" Visible="true" />
<None Include="$(MSBuildThisFileDirectory)\Vogen\bin\$(Configuration)\net8.0\Vogen.SharedTypes.xml" Pack="true" PackagePath="lib\net8.0" Visible="true" />
<None Include="$(MSBuildThisFileDirectory)\Vogen\bin\$(Configuration)\net9.0\Vogen.SharedTypes.dll" Pack="true" PackagePath="lib\net9.0" Visible="true" />
<None Include="$(MSBuildThisFileDirectory)\Vogen\bin\$(Configuration)\net9.0\Vogen.SharedTypes.xml" Pack="true" PackagePath="lib\net9.0" Visible="true" />
</ItemGroup>

</Project>
5 changes: 5 additions & 0 deletions src/Vogen.Pack.v3.ncrunchproject
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<ProjectConfiguration>
<Settings>
<IgnoreThisComponentCompletely>True</IgnoreThisComponentCompletely>
</Settings>
</ProjectConfiguration>
12 changes: 12 additions & 0 deletions src/Vogen.SharedTypes/BsonSerializerAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace Vogen;

/// <summary>
/// Represent a marker for BSON generation.
/// </summary>
/// <typeparam name="T">The type of the value object.</typeparam>
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
public class BsonSerializerAttribute<T> : ConversionMarkerAttribute
{
}
2 changes: 1 addition & 1 deletion src/Vogen/BuildConfigurationFromAttributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ private void PopulateFromGenericValueObjectAttribute(AttributeData attributeData
}

var isDerivedFromGenericAttribute =
attrClassSymbol.BaseType!.FullName()!.StartsWith("Vogen.ValueObjectAttribute<");
attrClassSymbol.BaseType!.EscapedFullName().StartsWith("Vogen.ValueObjectAttribute<");

// Extracts the generic argument from the base type when the derived type isn't generic
// e.g. MyCustomVoAttribute : ValueObjectAttribute<long>
Expand Down
2 changes: 1 addition & 1 deletion src/Vogen/BuildInstanceProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ internal static class BuildInstanceProperties

var r = InstanceGeneration.TryBuildInstanceValueAsText(
(string)nameConstant.Value!,
valueConstant.Value!, underlyingType.FullName());
valueConstant.Value!, underlyingType.EscapedFullName());

if (!r.Success)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Vogen/BuildWorkItems.cs
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ private static bool TryHandleValidateMethod(string? methodName,
return false;
}

if(method.ReturnType is INamedTypeSymbol s && s.FullName() != "Vogen.Validation")
if(method.ReturnType is INamedTypeSymbol s && s.EscapedFullName() != "Vogen.Validation")
{
context.ReportDiagnostic(DiagnosticsCatalogue.ValidationMustReturnValidationType(mds));
return false;
Expand All @@ -381,7 +381,7 @@ private static IEnumerable<InstanceProperties> TryBuildInstanceProperties(
INamedTypeSymbol underlyingType)
{
IEnumerable<AttributeData> matchingAttributes =
attributes.Where(a => a.AttributeClass?.FullName() is "Vogen.InstanceAttribute");
attributes.Where(a => a.AttributeClass?.EscapedFullName() is "Vogen.InstanceAttribute");

var props = BuildInstanceProperties.Build(matchingAttributes, context, voClass, underlyingType);

Expand Down
1 change: 1 addition & 0 deletions src/Vogen/ConversionMarkerKind.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ public enum ConversionMarkerKind
Unrecognized,
EFCore,
MessagePack,
Bson,
}
37 changes: 18 additions & 19 deletions src/Vogen/ConversionMarkers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,36 @@ internal static class ConversionMarkers
private static readonly Dictionary<string, ConversionMarkerKind> _knownMarkerAttributes = new()
{
{ "EfCoreConverterAttribute`1", ConversionMarkerKind.EFCore },
{ "MessagePackAttribute`1", ConversionMarkerKind.MessagePack }
{ "MessagePackAttribute`1", ConversionMarkerKind.MessagePack },
{ "BsonSerializerAttribute`1", ConversionMarkerKind.Bson }
};

/// <summary>
/// Tries to get any 'Market Object' attributes on a class.
/// </summary>
/// <param name="markerClass"></param>
/// <returns></returns>
public static IEnumerable<AttributeData> TryGetMarkerAttributes(INamedTypeSymbol markerClass)
{
var attrs = markerClass.GetAttributes();

return attrs.Where(
a => _knownMarkerAttributes.ContainsKey(a.AttributeClass?.MetadataName ?? ""));
}

public static MarkerClassDefinition? GetConversionMarkerClassesFromAttribute(GeneratorSyntaxContext context)
public static MarkerClassDefinition? GetMarkerClassFromAttribute(GeneratorSyntaxContext context)
{
var semanticModel = context.SemanticModel;

ISymbol declaredSymbol = semanticModel.GetDeclaredSymbol(context.Node)!;

var markerClassSymbol = (INamedTypeSymbol) declaredSymbol;

ImmutableArray<AttributeData> attributeData = TryGetMarkerAttributes(markerClassSymbol).ToImmutableArray();
ImmutableArray<AttributeData> attributeData = GetAnyMarkerAttributes(markerClassSymbol).ToImmutableArray();

if (attributeData.Length == 0) return null;
if (attributeData.Length == 0)
{
return null;
}

return new MarkerClassDefinition(
markerClassSymbol,
attributeData.Select(a => TryBuild(a, markerClassSymbol)));
markerClassSymbol,
attributeData.Select(a => TryBuild(a, markerClassSymbol)).Where(a => a is not null)!);
}

private static IEnumerable<AttributeData> GetAnyMarkerAttributes(INamedTypeSymbol markerClass)
{
var attrs = markerClass.GetAttributes();

return attrs.Where(
a => _knownMarkerAttributes.ContainsKey(a.AttributeClass?.MetadataName ?? ""));
}

public static bool IsTarget(SyntaxNode node) =>
Expand Down
30 changes: 30 additions & 0 deletions src/Vogen/DisplayFormats.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Microsoft.CodeAnalysis;

namespace Vogen;

public static class DisplayFormats
{
private static readonly SymbolDisplayMiscellaneousOptions _optionsWheNullabilityIsOn =
SymbolDisplayMiscellaneousOptions.EscapeKeywordIdentifiers |
SymbolDisplayMiscellaneousOptions.UseSpecialTypes |
SymbolDisplayMiscellaneousOptions.IncludeNullableReferenceTypeModifier;

private static readonly SymbolDisplayMiscellaneousOptions _optionsWheNullabilityIsOff =
SymbolDisplayMiscellaneousOptions.EscapeKeywordIdentifiers |
SymbolDisplayMiscellaneousOptions.UseSpecialTypes;

public static readonly SymbolDisplayFormat SymbolFormatWhenNullabilityIsOn =
new(
globalNamespaceStyle: SymbolDisplayGlobalNamespaceStyle.Included,
typeQualificationStyle: SymbolDisplayTypeQualificationStyle.NameAndContainingTypesAndNamespaces,
genericsOptions: SymbolDisplayGenericsOptions.IncludeTypeParameters,
miscellaneousOptions: _optionsWheNullabilityIsOn);

public static readonly SymbolDisplayFormat SymbolFormatWhenNullabilityIsOff =
new(
globalNamespaceStyle: SymbolDisplayGlobalNamespaceStyle.Included,
typeQualificationStyle: SymbolDisplayTypeQualificationStyle.NameAndContainingTypesAndNamespaces,
genericsOptions: SymbolDisplayGenericsOptions.IncludeTypeParameters,
miscellaneousOptions: _optionsWheNullabilityIsOff);

}
Loading

0 comments on commit ce60ef3

Please sign in to comment.