Skip to content

Commit

Permalink
Add GeneratedCode attribute (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewlock authored Dec 2, 2023
1 parent e53602c commit abc8c7b
Show file tree
Hide file tree
Showing 16 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/StronglyTypedIds/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ namespace StronglyTypedIds
{
internal static class Constants
{
public const string StronglyTypedIdsVersion = "1.0.0-beta6";
public const string Usage = nameof(Usage);
}
}
17 changes: 17 additions & 0 deletions src/StronglyTypedIds/SourceGenerationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public static string CreateId(
ParentClass? parentClass,
string template,
bool addDefaultAttributes,
bool addGeneratedCodeAttribute,
StringBuilder? sb)
{
if (string.IsNullOrEmpty(idName))
Expand Down Expand Up @@ -43,6 +44,12 @@ public static string CreateId(
}

var hasGenericParent = false;
if (addGeneratedCodeAttribute && parentClass is not null)
{
addGeneratedCodeAttribute = false;
AddGeneratedCodeAttribute(sb);
}

while (parentClass is { } parent)
{
sb.Append(" ");
Expand Down Expand Up @@ -76,6 +83,11 @@ public static string CreateId(
sb.AppendLine(" [global::System.Text.Json.Serialization.JsonConverter(typeof(PLACEHOLDERIDSystemTextJsonConverter))]");
}

if (addGeneratedCodeAttribute)
{
AddGeneratedCodeAttribute(sb);
}

sb.AppendLine(template);

sb.Replace("PLACEHOLDERID", idName);
Expand All @@ -91,6 +103,11 @@ public static string CreateId(
}

return sb.ToString();

static void AddGeneratedCodeAttribute(StringBuilder sb)
{
sb.AppendLine($""" [global::System.CodeDom.Compiler.GeneratedCodeAttribute("StronglyTypedId", "{Constants.StronglyTypedIdsVersion}")]""");
}
}

internal static string CreateSourceName(StringBuilder sb, string nameSpace, ParentClass? parent, string name, string template)
Expand Down
4 changes: 4 additions & 0 deletions src/StronglyTypedIds/StronglyTypedIdGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ private static void Execute(
return;
}

var addGeneratedCodeAttribute = true;
var sb = new StringBuilder();
foreach (var (name, content) in templateContents.Distinct())
{
Expand All @@ -106,8 +107,11 @@ private static void Execute(
idToGenerate.Parent,
content,
addDefaultAttributes: string.IsNullOrEmpty(name),
addGeneratedCodeAttribute: addGeneratedCodeAttribute,
sb);

addGeneratedCodeAttribute = false; // We can only add it once, so just add to the first rendering

var fileName = SourceGenerationHelper.CreateSourceName(
sb,
idToGenerate.NameSpace,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#nullable enable
[global::System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))]
[global::System.Text.Json.Serialization.JsonConverter(typeof(MyIdSystemTextJsonConverter))]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("StronglyTypedId", "1.0.0-beta6")]
partial struct MyId :
#if NET6_0_OR_GREATER
global::System.ISpanFormattable,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#nullable enable
namespace SomeNamespace
{
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("StronglyTypedId", "1.0.0-beta6")]
[global::System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))]
[global::System.Text.Json.Serialization.JsonConverter(typeof(MyIdSystemTextJsonConverter))]
[global::Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#nullable enable
namespace SomeNamespace
{
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("StronglyTypedId", "1.0.0-beta6")]
public partial class ParentClass<T> where T: new()
{
internal partial record InnerClass<TKey, TValue>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace SomeNamespace
{
[global::System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))]
[global::System.Text.Json.Serialization.JsonConverter(typeof(MyIdSystemTextJsonConverter))]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("StronglyTypedId", "1.0.0-beta6")]
partial struct MyId :
#if NET6_0_OR_GREATER
global::System.ISpanFormattable,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace SomeNamespace
{
[global::System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))]
[global::System.Text.Json.Serialization.JsonConverter(typeof(MyIdSystemTextJsonConverter))]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("StronglyTypedId", "1.0.0-beta6")]
partial struct MyId :
#if NET6_0_OR_GREATER
global::System.ISpanFormattable,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace MyContracts.V1
{
[global::System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))]
[global::System.Text.Json.Serialization.JsonConverter(typeof(MyIdSystemTextJsonConverter))]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("StronglyTypedId", "1.0.0-beta6")]
partial struct MyId :
#if NET6_0_OR_GREATER
global::System.ISpanFormattable,
Expand Down Expand Up @@ -248,6 +249,7 @@ namespace MyContracts.V2
{
[global::System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))]
[global::System.Text.Json.Serialization.JsonConverter(typeof(MyIdSystemTextJsonConverter))]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("StronglyTypedId", "1.0.0-beta6")]
partial struct MyId :
#if NET6_0_OR_GREATER
global::System.ISpanFormattable,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#pragma warning disable 1591 // publicly visible type or member must be documented

#nullable enable
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("StronglyTypedId", "1.0.0-beta6")]
partial struct MyId
{
public partial class EfCoreValueConverter : global::Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter<MyId, global::System.Guid>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#pragma warning disable 1591 // publicly visible type or member must be documented

#nullable enable
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("StronglyTypedId", "1.0.0-beta6")]
partial struct MyId
{
public partial class EfCoreValueConverter : global::Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter<MyId, global::System.Guid>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#nullable enable
namespace SomeNamespace
{
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("StronglyTypedId", "1.0.0-beta6")]
public partial class ParentClass
{
[global::System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace SomeNamespace
{
[global::System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))]
[global::System.Text.Json.Serialization.JsonConverter(typeof(MyIdSystemTextJsonConverter))]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("StronglyTypedId", "1.0.0-beta6")]
partial struct MyId :
#if NET6_0_OR_GREATER
global::System.ISpanFormattable,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#nullable enable
namespace SomeNamespace
{
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("StronglyTypedId", "1.0.0-beta6")]
public partial class ParentClass
{
internal partial record InnerClass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#pragma warning disable 1591 // publicly visible type or member must be documented

#nullable enable
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("StronglyTypedId", "1.0.0-beta6")]
[global::System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))]
[global::System.Text.Json.Serialization.JsonConverter(typeof(MyIdSystemTextJsonConverter))]
[global::Newtonsoft.Json.JsonConverter(typeof(MyIdNewtonsoftJsonConverter))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#nullable enable
[global::System.ComponentModel.TypeConverter(typeof(MyIdTypeConverter))]
[global::System.Text.Json.Serialization.JsonConverter(typeof(MyIdSystemTextJsonConverter))]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("StronglyTypedId", "1.0.0-beta6")]
partial struct MyId :
#if NET6_0_OR_GREATER
global::System.ISpanFormattable,
Expand Down

0 comments on commit abc8c7b

Please sign in to comment.