-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Newstonsoft.Json converter support
This is the first template that truly showcases the dynamic nature of our approach: you only need to add a reference to the Newtonsoft.Json package, and you get the added support, without any additional configuration whatesoever.
- Loading branch information
Showing
16 changed files
with
196 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using System.Text; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Text; | ||
|
||
namespace StructId; | ||
|
||
[Generator(LanguageNames.CSharp)] | ||
public class NewtonsoftJsonGenerator() : TemplateGenerator( | ||
"Newtonsoft.Json.JsonConverter", | ||
ThisAssembly.Resources.Templates.NewtonsoftJsonConverter.Text, | ||
ThisAssembly.Resources.Templates.NewtonsoftJsonConverterT.Text, | ||
ReferenceCheck.TypeExists) | ||
{ | ||
public override void Initialize(IncrementalGeneratorInitializationContext context) | ||
{ | ||
base.Initialize(context); | ||
|
||
context.RegisterSourceOutput( | ||
context.CompilationProvider | ||
.Select((x, _) => x.GetTypeByMetadataName("Newtonsoft.Json.JsonConverter")), | ||
(context, source) => | ||
{ | ||
if (source == null) | ||
return; | ||
|
||
context.AddSource("NewtonsoftJsonConverter.cs", SourceText.From( | ||
ThisAssembly.Resources.Templates.NewtonsoftJsonConverter_1.Text, Encoding.UTF8)); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp.Testing; | ||
using Microsoft.CodeAnalysis.Testing; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
using Xunit.Sdk; | ||
|
||
namespace StructId; | ||
|
||
public class NewtonsoftJsonGeneratorTests | ||
{ | ||
[Fact] | ||
public async Task DoesNotGenerateIfNewtonsoftJsonNotPresent() | ||
{ | ||
var test = new CSharpSourceGeneratorTest<NewtonsoftJsonGenerator, DefaultVerifier> | ||
{ | ||
ReferenceAssemblies = ReferenceAssemblies.Net.Net80, | ||
TestState = | ||
{ | ||
Sources = | ||
{ | ||
""" | ||
using StructId; | ||
|
||
public readonly partial record struct UserId(int Value) : IStructId<int>; | ||
""", | ||
}, | ||
}, | ||
}.WithAnalyzerStructId(); | ||
|
||
await test.RunAsync(); | ||
} | ||
|
||
[Fact] | ||
public async Task GenerateIfNewtonsoftJsonPresent() | ||
{ | ||
var test = new StructIdSourceGeneratorTest<NewtonsoftJsonGenerator>("int") | ||
{ | ||
SolutionTransforms = | ||
{ | ||
(solution, projectId) => solution | ||
.GetProject(projectId)? | ||
.AddMetadataReference(MetadataReference.CreateFromFile(typeof(JsonConvert).Assembly.Location)) | ||
.Solution ?? solution | ||
}, | ||
ReferenceAssemblies = ReferenceAssemblies.Net.Net80, | ||
TestState = | ||
{ | ||
Sources = | ||
{ | ||
""" | ||
using StructId; | ||
|
||
public readonly partial record struct UserId(int Value) : IStructId<int>; | ||
""", | ||
}, | ||
GeneratedSources = | ||
{ | ||
(typeof(NewtonsoftJsonGenerator), "UserId.cs", | ||
ThisAssembly.Resources.StructId.Templates.NewtonsoftJsonConverterT.Text.Replace("TStruct", "UserId").Replace("TValue", "int"), | ||
Encoding.UTF8), | ||
(typeof(NewtonsoftJsonGenerator), "NewtonsoftJsonConverter.cs", | ||
ThisAssembly.Resources.StructId.Templates.NewtonsoftJsonConverter_1.Text, | ||
Encoding.UTF8) | ||
}, | ||
}, | ||
}.WithAnalyzerStructId(); | ||
|
||
await test.RunAsync(); | ||
} | ||
|
||
class StructIdSourceGeneratorTest<TGenerator> : CSharpSourceGeneratorTest<TGenerator, DefaultVerifier> | ||
where TGenerator : new() | ||
{ | ||
public StructIdSourceGeneratorTest(string? tvalue = null) | ||
{ | ||
ReferenceAssemblies = ReferenceAssemblies.Net.Net80; | ||
|
||
if (tvalue != null) | ||
{ | ||
TestState.GeneratedSources.Add( | ||
(typeof(NewableGenerator), "UserId.cs", | ||
ThisAssembly.Resources.StructId.Templates.NewableT.Text.Replace("TStruct", "UserId").Replace("TValue", tvalue), | ||
Encoding.UTF8)); | ||
TestState.GeneratedSources.Add( | ||
(typeof(ParsableGenerator), "UserId.cs", | ||
ThisAssembly.Resources.StructId.Templates.ParsableT.Text.Replace("TStruct", "UserId").Replace("TValue", "int"), | ||
Encoding.UTF8)); | ||
} | ||
else | ||
{ | ||
TestState.GeneratedSources.Add( | ||
(typeof(NewableGenerator), "UserId.cs", | ||
ThisAssembly.Resources.StructId.Templates.Newable.Text.Replace("SStruct", "UserId"), | ||
Encoding.UTF8)); | ||
TestState.GeneratedSources.Add( | ||
(typeof(ParsableGenerator), "UserId.cs", | ||
ThisAssembly.Resources.StructId.Templates.Parsable.Text.Replace("SStruct", "UserId"), | ||
Encoding.UTF8)); | ||
} | ||
} | ||
|
||
protected override IEnumerable<Type> GetSourceGenerators() | ||
{ | ||
yield return typeof(NewableGenerator); | ||
yield return typeof(ParsableGenerator); | ||
yield return typeof(TGenerator); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.