Skip to content

Commit e98ca6b

Browse files
committed
Test and fix custom StructId namespace support
1 parent 525334b commit e98ca6b

8 files changed

Lines changed: 21 additions & 9 deletions

File tree

src/StructId.Analyzer/BaseGenerator.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public enum ReferenceCheck
2222

2323
public abstract class BaseGenerator(string referenceType, string stringTemplate, string typeTemplate, ReferenceCheck referenceCheck = ReferenceCheck.ValueIsType) : IIncrementalGenerator
2424
{
25-
protected record struct TemplateArgs(string TargetNamespace, INamedTypeSymbol StructId, INamedTypeSymbol ValueType, INamedTypeSymbol ReferenceType, INamedTypeSymbol StringType);
25+
protected record struct TemplateArgs(string StructIdNamespace, INamedTypeSymbol StructId, INamedTypeSymbol ValueType, INamedTypeSymbol ReferenceType, INamedTypeSymbol StringType);
2626

2727
public virtual void Initialize(IncrementalGeneratorInitializationContext context)
2828
{
@@ -79,8 +79,8 @@ protected static void AddFromTemplate(SourceProductionContext context, TemplateA
7979
// replace tokens in the template
8080
var replaced = template
8181
// Adjust to current target namespace
82-
.Replace("namespace StructId;", $"namespace {args.TargetNamespace};")
83-
.Replace("using StructId;", $"using {args.TargetNamespace};")
82+
.Replace("namespace StructId;", $"namespace {args.StructIdNamespace};")
83+
.Replace("using StructId;", $"using {args.StructIdNamespace};")
8484
// Simple names suffices since we emit a partial in the same namespace
8585
.Replace("TSelf", args.StructId.Name)
8686
.Replace("Self", args.StructId.Name)

src/StructId.Analyzer/DapperExtensions.sbn

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ using System.ComponentModel;
66
using System.Data;
77
using Dapper;
88

9-
namespace StructId;
9+
namespace {{ Namespace }};
1010

1111
/// <summary>
1212
/// Provides extensions for Dapper.

src/StructId.Analyzer/DapperGenerator.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,16 @@ void GenerateHandlers(SourceProductionContext context, ImmutableArray<TemplateAr
3333
{
3434
if (args.Length == 0)
3535
return;
36+
37+
var model = new SelectorModel(
38+
args.First().StructIdNamespace,
39+
args.Select(x => new StructIdModel(x.StructId.ToFullName(), x.ValueType.Name)));
3640

37-
var model = new SelectorModel(args.Select(x => new StructIdModel(x.StructId.ToFullName(), x.ValueType.Name)));
3841
var output = template.Render(model, member => member.Name);
3942
context.AddSource($"DapperExtensions.cs", output);
4043
}
4144

4245
record StructIdModel(string TSelf, string TId);
4346

44-
record SelectorModel(IEnumerable<StructIdModel> Ids);
47+
record SelectorModel(string Namespace, IEnumerable<StructIdModel> Ids);
4548
}

src/StructId.Analyzer/NewtonsoftJsonGenerator.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,19 @@ public override void Initialize(IncrementalGeneratorInitializationContext contex
1717

1818
context.RegisterSourceOutput(
1919
context.CompilationProvider
20-
.Select((x, _) => x.GetTypeByMetadataName("Newtonsoft.Json.JsonConverter`1")),
20+
.Select((x, _) => x.GetTypeByMetadataName("Newtonsoft.Json.JsonConverter`1"))
21+
.Combine(context.AnalyzerConfigOptionsProvider
22+
.Select((x, _) => x.GlobalOptions.TryGetValue("build_property.StructIdNamespace", out var ns) ? ns : "StructId")),
2123
(context, source) =>
2224
{
23-
if (source == null)
25+
if (source.Left == null)
2426
return;
2527

2628
context.AddSource("NewtonsoftJsonConverter.cs", SourceText.From(
27-
ThisAssembly.Resources.Templates.NewtonsoftJsonConverter_1.Text, Encoding.UTF8));
29+
ThisAssembly.Resources.Templates.NewtonsoftJsonConverter_1.Text
30+
.Replace("namespace StructId;", $"namespace {source.Right};")
31+
.Replace("using StructId;", $"using {source.Right};"),
32+
Encoding.UTF8));
2833
});
2934
}
3035
}

src/StructId.FunctionalTests/IIdTemplate.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Text;
55
using System.Threading.Tasks;
66
using StructId;
7+
using StructId.Functional;
78

89
[TStructId]
910
file partial record struct IIdTemplate(Guid Value) : IId

src/StructId.FunctionalTests/ObjectTemplate.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using StructId;
2+
using StructId.Functional;
23

34
[TStructId]
45
file partial record struct ObjectTemplate(object Value)

src/StructId.FunctionalTests/StructId.FunctionalTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<TargetFramework>net8.0</TargetFramework>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
8+
<RootNamespace>StructId.Functional</RootNamespace>
89
</PropertyGroup>
910

1011
<ItemGroup>

src/StructId.FunctionalTests/StructTemplate.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using StructId;
2+
using StructId.Functional;
23

34
[TStructId]
45
file partial record struct StructTemplate(ValueType Value)

0 commit comments

Comments
 (0)