From 8eb3c6f78cda3c1828c52d55ebd09514855402e0 Mon Sep 17 00:00:00 2001 From: "o.y.safonov" Date: Mon, 24 Jul 2023 09:25:05 +0300 Subject: [PATCH 1/6] add generic job support for assemblyQualifiedNameWithoutVersion method --- ...ssemblyQualifiedNameWithoutVersionTests.cs | 75 +++++++++++++++++++ .../TypeName/FirstNonGenericJob.cs | 4 + src/Horarium.Test/TypeName/GenericJob.cs | 4 + .../TypeName/GenericJobWithTwoArguments.cs | 4 + .../TypeName/SecondNonGenericJob.cs | 4 + src/Horarium/Utils.cs | 29 ++++++- 6 files changed, 116 insertions(+), 4 deletions(-) create mode 100644 src/Horarium.Test/TypeName/AssemblyQualifiedNameWithoutVersionTests.cs create mode 100644 src/Horarium.Test/TypeName/FirstNonGenericJob.cs create mode 100644 src/Horarium.Test/TypeName/GenericJob.cs create mode 100644 src/Horarium.Test/TypeName/GenericJobWithTwoArguments.cs create mode 100644 src/Horarium.Test/TypeName/SecondNonGenericJob.cs diff --git a/src/Horarium.Test/TypeName/AssemblyQualifiedNameWithoutVersionTests.cs b/src/Horarium.Test/TypeName/AssemblyQualifiedNameWithoutVersionTests.cs new file mode 100644 index 0000000..36a98eb --- /dev/null +++ b/src/Horarium.Test/TypeName/AssemblyQualifiedNameWithoutVersionTests.cs @@ -0,0 +1,75 @@ +using System; +using Xunit; + +namespace Horarium.Test +{ + public class AssemblyQualifiedNameWithoutVersionTests + { + [Fact] + public void GetName_ForNonGenericJob_ShouldRemoveVersion() + { + var jobName = new SecondNonGenericJob().GetType().AssemblyQualifiedNameWithoutVersion(); + + var type = Type.GetType(jobName, true); + var instance = Activator.CreateInstance(type); + + Assert.NotNull(instance); + Assert.IsType(instance); + Assert.Equal("Horarium.Test.SecondNonGenericJob, Horarium.Test", jobName); + } + + [Fact] + public void GetName_ForGenericJobWithTwoArguments_ShouldRemoveVersionForAllTypes() + { + var jobName = new GenericJobWithTwoArguments().GetType().AssemblyQualifiedNameWithoutVersion(); + + var type = Type.GetType(jobName, true); + var instance = Activator.CreateInstance(type); + + Assert.NotNull(instance); + Assert.IsType>(instance); + Assert.Equal("Horarium.Test.GenericJobWithTwoArguments`2[[Horarium.Test.SecondNonGenericJob, Horarium.Test], [Horarium.Test.FirstNonGenericJob, Horarium.Test]], Horarium.Test", jobName); + } + + [Fact] + public void GetName_ForGenericJobWithTwoNestedGenericArguments_ShouldRemoveVersionForAllTypes() + { + var jobName = new GenericJobWithTwoArguments, GenericJob>() + .GetType() + .AssemblyQualifiedNameWithoutVersion(); + + var type = Type.GetType(jobName, true); + var instance = Activator.CreateInstance(type); + + Assert.NotNull(instance); + Assert.IsType, GenericJob>>(instance); + // Assert.Equal("Horarium.Test.GenericJobWithTwoArguments`2[[Horarium.Test.GenericJob`1[[Horarium.Test.FirstNonGenericJob, Horarium.Test]], Horarium.Test], [Horarium.Test.GenericJob`1[[Horarium.Test.SecondNonGenericJob, Horarium.Test]], Horarium.Test]], Horarium.Test", jobName); + } + + [Fact] + public void GetName_ForGenericJobWithSingleGenericArgument_ShouldRemoveVersionForAllTypes() + { + var jobName = new GenericJob().GetType().AssemblyQualifiedNameWithoutVersion(); + + var type = Type.GetType(jobName, true); + var instance = Activator.CreateInstance(type); + + Assert.NotNull(instance); + Assert.IsType>(instance); + Assert.Equal("Horarium.Test.GenericJob`1[[Horarium.Test.SecondNonGenericJob, Horarium.Test]], Horarium.Test", jobName); + } + + [Fact] + public void GetName_ForGenericJobWithSingleNestedGenericArgument_ShouldRemoveVersionForAllTypes() + { + var jobName = new GenericJob>().GetType().AssemblyQualifiedNameWithoutVersion(); + + var type = Type.GetType(jobName, true); + var instance = Activator.CreateInstance(type); + + Assert.NotNull(instance); + Assert.IsType>>(instance); + Assert.Equal("Horarium.Test.GenericJob`1[[Horarium.Test.GenericJob`1[[Horarium.Test.SecondNonGenericJob, Horarium.Test]], Horarium.Test]], Horarium.Test", jobName); + } + } +} \ No newline at end of file diff --git a/src/Horarium.Test/TypeName/FirstNonGenericJob.cs b/src/Horarium.Test/TypeName/FirstNonGenericJob.cs new file mode 100644 index 0000000..52f414b --- /dev/null +++ b/src/Horarium.Test/TypeName/FirstNonGenericJob.cs @@ -0,0 +1,4 @@ +namespace Horarium.Test +{ + public class FirstNonGenericJob { } +} \ No newline at end of file diff --git a/src/Horarium.Test/TypeName/GenericJob.cs b/src/Horarium.Test/TypeName/GenericJob.cs new file mode 100644 index 0000000..bd73030 --- /dev/null +++ b/src/Horarium.Test/TypeName/GenericJob.cs @@ -0,0 +1,4 @@ +namespace Horarium.Test +{ + public class GenericJob { } +} \ No newline at end of file diff --git a/src/Horarium.Test/TypeName/GenericJobWithTwoArguments.cs b/src/Horarium.Test/TypeName/GenericJobWithTwoArguments.cs new file mode 100644 index 0000000..893de24 --- /dev/null +++ b/src/Horarium.Test/TypeName/GenericJobWithTwoArguments.cs @@ -0,0 +1,4 @@ +namespace Horarium.Test +{ + public class GenericJobWithTwoArguments { } +} \ No newline at end of file diff --git a/src/Horarium.Test/TypeName/SecondNonGenericJob.cs b/src/Horarium.Test/TypeName/SecondNonGenericJob.cs new file mode 100644 index 0000000..a32c675 --- /dev/null +++ b/src/Horarium.Test/TypeName/SecondNonGenericJob.cs @@ -0,0 +1,4 @@ +namespace Horarium.Test +{ + public class SecondNonGenericJob { } +} \ No newline at end of file diff --git a/src/Horarium/Utils.cs b/src/Horarium/Utils.cs index f376351..a295d20 100644 --- a/src/Horarium/Utils.cs +++ b/src/Horarium/Utils.cs @@ -1,8 +1,11 @@ using System; +using System.Linq; using System.Reflection; +using System.Runtime.CompilerServices; using Cronos; using Newtonsoft.Json; +[assembly:InternalsVisibleTo("Horarium.Test")] namespace Horarium { internal static class Utils @@ -19,14 +22,32 @@ public static object FromJson(this string json, Type type, JsonSerializerSetting public static string AssemblyQualifiedNameWithoutVersion(this Type type) { - string retValue = type.FullName + ", " + type.GetTypeInfo().Assembly.GetName().Name; - return retValue; + if (!type.IsGenericType) + { + return $"{type.FullName}, {type.GetTypeInfo().Assembly.GetName().Name}"; + } + + if (string.IsNullOrWhiteSpace(type.FullName)) + { + throw new ArgumentException($"Не удалось получить имя тип {type}"); + } + + var genericArguments = type + .GetGenericArguments() + .Select(typeArgument => $"[{typeArgument.AssemblyQualifiedNameWithoutVersion()}]") + .ToArray(); + + var genericPart = string.Join(", ", genericArguments); + + var bracketIndex = type.FullName.IndexOf("[", StringComparison.Ordinal); + + return $"{type.FullName.Substring(0, bracketIndex)}[{genericPart}], {type.GetTypeInfo().Assembly.GetName().Name}"; } - + public static DateTime? ParseAndGetNextOccurrence(string cron) { var expression = CronExpression.Parse(cron, CronFormat.IncludeSeconds); - + return expression.GetNextOccurrence(DateTime.UtcNow, TimeZoneInfo.Local); } } From c4b6890b7463f8e5dfbd23c8bc5aebca0124a1c6 Mon Sep 17 00:00:00 2001 From: "o.y.safonov" Date: Mon, 24 Jul 2023 14:58:18 +0300 Subject: [PATCH 2/6] review fixes: uncomment code, change exception language, check for null before usage --- .../TypeName/AssemblyQualifiedNameWithoutVersionTests.cs | 2 +- src/Horarium/Utils.cs | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Horarium.Test/TypeName/AssemblyQualifiedNameWithoutVersionTests.cs b/src/Horarium.Test/TypeName/AssemblyQualifiedNameWithoutVersionTests.cs index 36a98eb..aff96a4 100644 --- a/src/Horarium.Test/TypeName/AssemblyQualifiedNameWithoutVersionTests.cs +++ b/src/Horarium.Test/TypeName/AssemblyQualifiedNameWithoutVersionTests.cs @@ -43,7 +43,7 @@ public void GetName_ForGenericJobWithTwoNestedGenericArguments_ShouldRemoveVersi Assert.NotNull(instance); Assert.IsType, GenericJob>>(instance); - // Assert.Equal("Horarium.Test.GenericJobWithTwoArguments`2[[Horarium.Test.GenericJob`1[[Horarium.Test.FirstNonGenericJob, Horarium.Test]], Horarium.Test], [Horarium.Test.GenericJob`1[[Horarium.Test.SecondNonGenericJob, Horarium.Test]], Horarium.Test]], Horarium.Test", jobName); + Assert.Equal("Horarium.Test.GenericJobWithTwoArguments`2[[Horarium.Test.GenericJob`1[[Horarium.Test.FirstNonGenericJob, Horarium.Test]], Horarium.Test], [Horarium.Test.GenericJob`1[[Horarium.Test.SecondNonGenericJob, Horarium.Test]], Horarium.Test]], Horarium.Test", jobName); } [Fact] diff --git a/src/Horarium/Utils.cs b/src/Horarium/Utils.cs index a295d20..55772f9 100644 --- a/src/Horarium/Utils.cs +++ b/src/Horarium/Utils.cs @@ -22,14 +22,14 @@ public static object FromJson(this string json, Type type, JsonSerializerSetting public static string AssemblyQualifiedNameWithoutVersion(this Type type) { - if (!type.IsGenericType) + if (string.IsNullOrWhiteSpace(type.FullName)) { - return $"{type.FullName}, {type.GetTypeInfo().Assembly.GetName().Name}"; + throw new ArgumentException($"Unable to receive FullName for type {type}"); } - if (string.IsNullOrWhiteSpace(type.FullName)) + if (!type.IsGenericType) { - throw new ArgumentException($"Не удалось получить имя тип {type}"); + return $"{type.FullName}, {type.GetTypeInfo().Assembly.GetName().Name}"; } var genericArguments = type From f47deb7b27799818227d5d0dc0135ae372bd3ed0 Mon Sep 17 00:00:00 2001 From: "o.y.safonov" Date: Mon, 14 Aug 2023 21:27:22 +0300 Subject: [PATCH 3/6] add generic job support for assemblyQualifiedNameWithoutVersion method. Use custom GenericTypeAttribute for avoid unexpected changes in working code and for backward compatibility --- ...ssemblyQualifiedNameWithoutVersionTests.cs | 101 +++++++++++++++--- .../TypeName/FirstNonGenericJob.cs | 7 +- src/Horarium.Test/TypeName/GenericJob.cs | 7 +- .../TypeName/GenericJobWithTwoArguments.cs | 7 +- .../TypeName/SecondNonGenericJob.cs | 7 +- src/Horarium/Attributes/GenericJob.cs | 7 ++ src/Horarium/Utils.cs | 17 ++- 7 files changed, 133 insertions(+), 20 deletions(-) create mode 100644 src/Horarium/Attributes/GenericJob.cs diff --git a/src/Horarium.Test/TypeName/AssemblyQualifiedNameWithoutVersionTests.cs b/src/Horarium.Test/TypeName/AssemblyQualifiedNameWithoutVersionTests.cs index aff96a4..a771fce 100644 --- a/src/Horarium.Test/TypeName/AssemblyQualifiedNameWithoutVersionTests.cs +++ b/src/Horarium.Test/TypeName/AssemblyQualifiedNameWithoutVersionTests.cs @@ -1,38 +1,68 @@ using System; +using System.ComponentModel; using Xunit; -namespace Horarium.Test +namespace Horarium.Test.TypeName { public class AssemblyQualifiedNameWithoutVersionTests { [Fact] + [Category("OldBehavior")] public void GetName_ForNonGenericJob_ShouldRemoveVersion() { - var jobName = new SecondNonGenericJob().GetType().AssemblyQualifiedNameWithoutVersion(); + var jobName = new FirstNonGenericJob().GetType().AssemblyQualifiedNameWithoutVersion(); var type = Type.GetType(jobName, true); var instance = Activator.CreateInstance(type); Assert.NotNull(instance); - Assert.IsType(instance); - Assert.Equal("Horarium.Test.SecondNonGenericJob, Horarium.Test", jobName); + Assert.IsType(instance); + Assert.Equal("Horarium.Test.TypeName.FirstNonGenericJob, Horarium.Test", jobName); } [Fact] - public void GetName_ForGenericJobWithTwoArguments_ShouldRemoveVersionForAllTypes() + public void GetName_ForNonGenericJobWithAttribute_ShouldRemoveVersion() { - var jobName = new GenericJobWithTwoArguments().GetType().AssemblyQualifiedNameWithoutVersion(); + var jobName = new FirstNonGenericJobWithAttribute().GetType().AssemblyQualifiedNameWithoutVersion(); var type = Type.GetType(jobName, true); var instance = Activator.CreateInstance(type); Assert.NotNull(instance); - Assert.IsType>(instance); - Assert.Equal("Horarium.Test.GenericJobWithTwoArguments`2[[Horarium.Test.SecondNonGenericJob, Horarium.Test], [Horarium.Test.FirstNonGenericJob, Horarium.Test]], Horarium.Test", jobName); + Assert.IsType(instance); + Assert.Equal("Horarium.Test.TypeName.FirstNonGenericJobWithAttribute, Horarium.Test", jobName); } [Fact] - public void GetName_ForGenericJobWithTwoNestedGenericArguments_ShouldRemoveVersionForAllTypes() + [Category("OldBehavior")] + public void GetName_ForGenericJobWithTwoArguments_ShouldNotRemoveVersionForAllTypes() + { + var jobName = new GenericJobWithTwoArguments().GetType().AssemblyQualifiedNameWithoutVersion(); + + var type = Type.GetType(jobName, true); + var instance = Activator.CreateInstance(type); + + Assert.NotNull(instance); + Assert.IsType>(instance); + Assert.Equal("Horarium.Test.TypeName.GenericJobWithTwoArguments`2[[Horarium.Test.TypeName.FirstNonGenericJob, Horarium.Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Horarium.Test.TypeName.SecondNonGenericJob, Horarium.Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], Horarium.Test", jobName); + } + + [Fact] + public void GetName_ForGenericJobWithTwoArgumentsWithAttribute_ShouldRemoveVersionForAllTypes() + { + var jobName = new GenericJobWithTwoArgumentsWithAttribute().GetType().AssemblyQualifiedNameWithoutVersion(); + + var type = Type.GetType(jobName, true); + var instance = Activator.CreateInstance(type); + + Assert.NotNull(instance); + Assert.IsType>(instance); + Assert.Equal("Horarium.Test.TypeName.GenericJobWithTwoArgumentsWithAttribute`2[[Horarium.Test.TypeName.FirstNonGenericJob, Horarium.Test], [Horarium.Test.TypeName.SecondNonGenericJob, Horarium.Test]], Horarium.Test", jobName); + } + + [Fact] + [Category("OldBehavior")] + public void GetName_ForGenericJobWithTwoNestedGenericArguments_ShouldNotRemoveVersionForAllTypes() { var jobName = new GenericJobWithTwoArguments, GenericJob>() .GetType() @@ -43,11 +73,27 @@ public void GetName_ForGenericJobWithTwoNestedGenericArguments_ShouldRemoveVersi Assert.NotNull(instance); Assert.IsType, GenericJob>>(instance); - Assert.Equal("Horarium.Test.GenericJobWithTwoArguments`2[[Horarium.Test.GenericJob`1[[Horarium.Test.FirstNonGenericJob, Horarium.Test]], Horarium.Test], [Horarium.Test.GenericJob`1[[Horarium.Test.SecondNonGenericJob, Horarium.Test]], Horarium.Test]], Horarium.Test", jobName); + Assert.Equal("Horarium.Test.TypeName.GenericJobWithTwoArguments`2[[Horarium.Test.TypeName.GenericJob`1[[Horarium.Test.TypeName.FirstNonGenericJob, Horarium.Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], Horarium.Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Horarium.Test.TypeName.GenericJob`1[[Horarium.Test.TypeName.SecondNonGenericJob, Horarium.Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], Horarium.Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], Horarium.Test", jobName); } [Fact] - public void GetName_ForGenericJobWithSingleGenericArgument_ShouldRemoveVersionForAllTypes() + public void GetName_ForGenericJobWithTwoNestedGenericArgumentsWithAttribute_ShouldRemoveVersionForAllTypes() + { + var jobName = new GenericJobWithTwoArgumentsWithAttribute, GenericJob>() + .GetType() + .AssemblyQualifiedNameWithoutVersion(); + + var type = Type.GetType(jobName, true); + var instance = Activator.CreateInstance(type); + + Assert.NotNull(instance); + Assert.IsType, GenericJob>>(instance); + Assert.Equal("Horarium.Test.TypeName.GenericJobWithTwoArgumentsWithAttribute`2[[Horarium.Test.TypeName.GenericJob`1[[Horarium.Test.TypeName.FirstNonGenericJob, Horarium.Test]], Horarium.Test], [Horarium.Test.TypeName.GenericJob`1[[Horarium.Test.TypeName.SecondNonGenericJob, Horarium.Test]], Horarium.Test]], Horarium.Test", jobName); + } + + [Fact] + [Category("OldBehavior")] + public void GetName_ForGenericJobWithSingleGenericArgument_ShouldNotRemoveVersionForAllTypes() { var jobName = new GenericJob().GetType().AssemblyQualifiedNameWithoutVersion(); @@ -56,11 +102,25 @@ public void GetName_ForGenericJobWithSingleGenericArgument_ShouldRemoveVersionFo Assert.NotNull(instance); Assert.IsType>(instance); - Assert.Equal("Horarium.Test.GenericJob`1[[Horarium.Test.SecondNonGenericJob, Horarium.Test]], Horarium.Test", jobName); + Assert.Equal("Horarium.Test.TypeName.GenericJob`1[[Horarium.Test.TypeName.SecondNonGenericJob, Horarium.Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], Horarium.Test", jobName); } [Fact] - public void GetName_ForGenericJobWithSingleNestedGenericArgument_ShouldRemoveVersionForAllTypes() + public void GetName_ForGenericJobWithSingleGenericArgumentWithAttribute_ShouldRemoveVersionForAllTypes() + { + var jobName = new GenericJobWithAttribute().GetType().AssemblyQualifiedNameWithoutVersion(); + + var type = Type.GetType(jobName, true); + var instance = Activator.CreateInstance(type); + + Assert.NotNull(instance); + Assert.IsType>(instance); + Assert.Equal("Horarium.Test.TypeName.GenericJobWithAttribute`1[[Horarium.Test.TypeName.SecondNonGenericJob, Horarium.Test]], Horarium.Test", jobName); + } + + [Fact] + [Category("OldBehavior")] + public void GetName_ForGenericJobWithSingleNestedGenericArgument_ShouldNotRemoveVersionForAllTypes() { var jobName = new GenericJob>().GetType().AssemblyQualifiedNameWithoutVersion(); @@ -69,7 +129,20 @@ public void GetName_ForGenericJobWithSingleNestedGenericArgument_ShouldRemoveVer Assert.NotNull(instance); Assert.IsType>>(instance); - Assert.Equal("Horarium.Test.GenericJob`1[[Horarium.Test.GenericJob`1[[Horarium.Test.SecondNonGenericJob, Horarium.Test]], Horarium.Test]], Horarium.Test", jobName); + Assert.Equal("Horarium.Test.TypeName.GenericJob`1[[Horarium.Test.TypeName.GenericJob`1[[Horarium.Test.TypeName.SecondNonGenericJob, Horarium.Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], Horarium.Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], Horarium.Test", jobName); + } + + [Fact] + public void GetName_ForGenericJobWithSingleNestedGenericArgumentWithAttribute_ShouldRemoveVersionForAllTypes() + { + var jobName = new GenericJobWithAttribute>().GetType().AssemblyQualifiedNameWithoutVersion(); + + var type = Type.GetType(jobName, true); + var instance = Activator.CreateInstance(type); + + Assert.NotNull(instance); + Assert.IsType>>(instance); + Assert.Equal("Horarium.Test.TypeName.GenericJobWithAttribute`1[[Horarium.Test.TypeName.GenericJob`1[[Horarium.Test.TypeName.SecondNonGenericJob, Horarium.Test]], Horarium.Test]], Horarium.Test", jobName); } } } \ No newline at end of file diff --git a/src/Horarium.Test/TypeName/FirstNonGenericJob.cs b/src/Horarium.Test/TypeName/FirstNonGenericJob.cs index 52f414b..d440568 100644 --- a/src/Horarium.Test/TypeName/FirstNonGenericJob.cs +++ b/src/Horarium.Test/TypeName/FirstNonGenericJob.cs @@ -1,4 +1,9 @@ -namespace Horarium.Test +using Horarium.Attributes; + +namespace Horarium.Test.TypeName { public class FirstNonGenericJob { } + + [GenericJob] + public class FirstNonGenericJobWithAttribute {} } \ No newline at end of file diff --git a/src/Horarium.Test/TypeName/GenericJob.cs b/src/Horarium.Test/TypeName/GenericJob.cs index bd73030..6ed60e8 100644 --- a/src/Horarium.Test/TypeName/GenericJob.cs +++ b/src/Horarium.Test/TypeName/GenericJob.cs @@ -1,4 +1,9 @@ -namespace Horarium.Test +using Horarium.Attributes; + +namespace Horarium.Test.TypeName { public class GenericJob { } + + [GenericJob] + public class GenericJobWithAttribute { } } \ No newline at end of file diff --git a/src/Horarium.Test/TypeName/GenericJobWithTwoArguments.cs b/src/Horarium.Test/TypeName/GenericJobWithTwoArguments.cs index 893de24..a71e96e 100644 --- a/src/Horarium.Test/TypeName/GenericJobWithTwoArguments.cs +++ b/src/Horarium.Test/TypeName/GenericJobWithTwoArguments.cs @@ -1,4 +1,9 @@ -namespace Horarium.Test +using Horarium.Attributes; + +namespace Horarium.Test.TypeName { public class GenericJobWithTwoArguments { } + + [GenericJob] + public class GenericJobWithTwoArgumentsWithAttribute { } } \ No newline at end of file diff --git a/src/Horarium.Test/TypeName/SecondNonGenericJob.cs b/src/Horarium.Test/TypeName/SecondNonGenericJob.cs index a32c675..ca26740 100644 --- a/src/Horarium.Test/TypeName/SecondNonGenericJob.cs +++ b/src/Horarium.Test/TypeName/SecondNonGenericJob.cs @@ -1,4 +1,9 @@ -namespace Horarium.Test +using Horarium.Attributes; + +namespace Horarium.Test.TypeName { public class SecondNonGenericJob { } + + [GenericJob] + public class SecondNonGenericJobWithAttribute { } } \ No newline at end of file diff --git a/src/Horarium/Attributes/GenericJob.cs b/src/Horarium/Attributes/GenericJob.cs new file mode 100644 index 0000000..434e993 --- /dev/null +++ b/src/Horarium/Attributes/GenericJob.cs @@ -0,0 +1,7 @@ +using System; + +namespace Horarium.Attributes +{ + [AttributeUsage(AttributeTargets.Class)] + public class GenericJob : Attribute { } +} \ No newline at end of file diff --git a/src/Horarium/Utils.cs b/src/Horarium/Utils.cs index 55772f9..5c1ffb0 100644 --- a/src/Horarium/Utils.cs +++ b/src/Horarium/Utils.cs @@ -3,6 +3,7 @@ using System.Reflection; using System.Runtime.CompilerServices; using Cronos; +using Horarium.Attributes; using Newtonsoft.Json; [assembly:InternalsVisibleTo("Horarium.Test")] @@ -20,7 +21,7 @@ public static object FromJson(this string json, Type type, JsonSerializerSetting return JsonConvert.DeserializeObject(json, type, jsonSerializerSettings); } - public static string AssemblyQualifiedNameWithoutVersion(this Type type) + private static string AssemblyQualifiedNameWithoutVersionForGenericType(this Type type) { if (string.IsNullOrWhiteSpace(type.FullName)) { @@ -34,7 +35,7 @@ public static string AssemblyQualifiedNameWithoutVersion(this Type type) var genericArguments = type .GetGenericArguments() - .Select(typeArgument => $"[{typeArgument.AssemblyQualifiedNameWithoutVersion()}]") + .Select(typeArgument => $"[{typeArgument.AssemblyQualifiedNameWithoutVersionForGenericType()}]") .ToArray(); var genericPart = string.Join(", ", genericArguments); @@ -44,6 +45,18 @@ public static string AssemblyQualifiedNameWithoutVersion(this Type type) return $"{type.FullName.Substring(0, bracketIndex)}[{genericPart}], {type.GetTypeInfo().Assembly.GetName().Name}"; } + public static string AssemblyQualifiedNameWithoutVersion(this Type type, bool genericJob = false) + { + var hasGenericJobAttribute = type.GetCustomAttributes().Any(); + + if (!hasGenericJobAttribute && !genericJob) + { + return $"{type.FullName}, {type.GetTypeInfo().Assembly.GetName().Name}"; + } + + return type.AssemblyQualifiedNameWithoutVersionForGenericType(); + } + public static DateTime? ParseAndGetNextOccurrence(string cron) { var expression = CronExpression.Parse(cron, CronFormat.IncludeSeconds); From 1dac3f35c67c52e04d2ac3b74b7290dc59e14047 Mon Sep 17 00:00:00 2001 From: "o.y.safonov" Date: Mon, 14 Aug 2023 21:28:59 +0300 Subject: [PATCH 4/6] Keep single file - single class --- src/Horarium.Test/TypeName/FirstNonGenericJob.cs | 7 +------ .../TypeName/FirstNonGenericJobWithAttribute.cs | 7 +++++++ src/Horarium.Test/TypeName/GenericJob.cs | 5 ----- src/Horarium.Test/TypeName/GenericJobWithAttribute.cs | 7 +++++++ src/Horarium.Test/TypeName/GenericJobWithTwoArguments.cs | 5 ----- .../TypeName/GenericJobWithTwoArgumentsWithAttribute.cs | 7 +++++++ src/Horarium.Test/TypeName/SecondNonGenericJob.cs | 5 ----- .../TypeName/SecondNonGenericJobWithAttribute.cs | 7 +++++++ 8 files changed, 29 insertions(+), 21 deletions(-) create mode 100644 src/Horarium.Test/TypeName/FirstNonGenericJobWithAttribute.cs create mode 100644 src/Horarium.Test/TypeName/GenericJobWithAttribute.cs create mode 100644 src/Horarium.Test/TypeName/GenericJobWithTwoArgumentsWithAttribute.cs create mode 100644 src/Horarium.Test/TypeName/SecondNonGenericJobWithAttribute.cs diff --git a/src/Horarium.Test/TypeName/FirstNonGenericJob.cs b/src/Horarium.Test/TypeName/FirstNonGenericJob.cs index d440568..3faccdf 100644 --- a/src/Horarium.Test/TypeName/FirstNonGenericJob.cs +++ b/src/Horarium.Test/TypeName/FirstNonGenericJob.cs @@ -1,9 +1,4 @@ -using Horarium.Attributes; - -namespace Horarium.Test.TypeName +namespace Horarium.Test.TypeName { public class FirstNonGenericJob { } - - [GenericJob] - public class FirstNonGenericJobWithAttribute {} } \ No newline at end of file diff --git a/src/Horarium.Test/TypeName/FirstNonGenericJobWithAttribute.cs b/src/Horarium.Test/TypeName/FirstNonGenericJobWithAttribute.cs new file mode 100644 index 0000000..896001f --- /dev/null +++ b/src/Horarium.Test/TypeName/FirstNonGenericJobWithAttribute.cs @@ -0,0 +1,7 @@ +using Horarium.Attributes; + +namespace Horarium.Test.TypeName +{ + [GenericJob] + public class FirstNonGenericJobWithAttribute {} +} \ No newline at end of file diff --git a/src/Horarium.Test/TypeName/GenericJob.cs b/src/Horarium.Test/TypeName/GenericJob.cs index 6ed60e8..b32004a 100644 --- a/src/Horarium.Test/TypeName/GenericJob.cs +++ b/src/Horarium.Test/TypeName/GenericJob.cs @@ -1,9 +1,4 @@ -using Horarium.Attributes; - namespace Horarium.Test.TypeName { public class GenericJob { } - - [GenericJob] - public class GenericJobWithAttribute { } } \ No newline at end of file diff --git a/src/Horarium.Test/TypeName/GenericJobWithAttribute.cs b/src/Horarium.Test/TypeName/GenericJobWithAttribute.cs new file mode 100644 index 0000000..29a0472 --- /dev/null +++ b/src/Horarium.Test/TypeName/GenericJobWithAttribute.cs @@ -0,0 +1,7 @@ +using Horarium.Attributes; + +namespace Horarium.Test.TypeName +{ + [GenericJob] + public class GenericJobWithAttribute { } +} \ No newline at end of file diff --git a/src/Horarium.Test/TypeName/GenericJobWithTwoArguments.cs b/src/Horarium.Test/TypeName/GenericJobWithTwoArguments.cs index a71e96e..c33e954 100644 --- a/src/Horarium.Test/TypeName/GenericJobWithTwoArguments.cs +++ b/src/Horarium.Test/TypeName/GenericJobWithTwoArguments.cs @@ -1,9 +1,4 @@ -using Horarium.Attributes; - namespace Horarium.Test.TypeName { public class GenericJobWithTwoArguments { } - - [GenericJob] - public class GenericJobWithTwoArgumentsWithAttribute { } } \ No newline at end of file diff --git a/src/Horarium.Test/TypeName/GenericJobWithTwoArgumentsWithAttribute.cs b/src/Horarium.Test/TypeName/GenericJobWithTwoArgumentsWithAttribute.cs new file mode 100644 index 0000000..2023d20 --- /dev/null +++ b/src/Horarium.Test/TypeName/GenericJobWithTwoArgumentsWithAttribute.cs @@ -0,0 +1,7 @@ +using Horarium.Attributes; + +namespace Horarium.Test.TypeName +{ + [GenericJob] + public class GenericJobWithTwoArgumentsWithAttribute { } +} \ No newline at end of file diff --git a/src/Horarium.Test/TypeName/SecondNonGenericJob.cs b/src/Horarium.Test/TypeName/SecondNonGenericJob.cs index ca26740..a621e9f 100644 --- a/src/Horarium.Test/TypeName/SecondNonGenericJob.cs +++ b/src/Horarium.Test/TypeName/SecondNonGenericJob.cs @@ -1,9 +1,4 @@ -using Horarium.Attributes; - namespace Horarium.Test.TypeName { public class SecondNonGenericJob { } - - [GenericJob] - public class SecondNonGenericJobWithAttribute { } } \ No newline at end of file diff --git a/src/Horarium.Test/TypeName/SecondNonGenericJobWithAttribute.cs b/src/Horarium.Test/TypeName/SecondNonGenericJobWithAttribute.cs new file mode 100644 index 0000000..8d69732 --- /dev/null +++ b/src/Horarium.Test/TypeName/SecondNonGenericJobWithAttribute.cs @@ -0,0 +1,7 @@ +using Horarium.Attributes; + +namespace Horarium.Test.TypeName +{ + [GenericJob] + public class SecondNonGenericJobWithAttribute { } +} \ No newline at end of file From db76bb1629fcaadbb0c5517a18ac341beee35048 Mon Sep 17 00:00:00 2001 From: "o.y.safonov" Date: Mon, 14 Aug 2023 21:30:17 +0300 Subject: [PATCH 5/6] Remove unused code --- src/Horarium/Utils.cs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/Horarium/Utils.cs b/src/Horarium/Utils.cs index 5c1ffb0..725593c 100644 --- a/src/Horarium/Utils.cs +++ b/src/Horarium/Utils.cs @@ -21,6 +21,18 @@ public static object FromJson(this string json, Type type, JsonSerializerSetting return JsonConvert.DeserializeObject(json, type, jsonSerializerSettings); } + public static string AssemblyQualifiedNameWithoutVersion(this Type type) + { + var hasGenericJobAttribute = type.GetCustomAttributes().Any(); + + if (!hasGenericJobAttribute) + { + return $"{type.FullName}, {type.GetTypeInfo().Assembly.GetName().Name}"; + } + + return type.AssemblyQualifiedNameWithoutVersionForGenericType(); + } + private static string AssemblyQualifiedNameWithoutVersionForGenericType(this Type type) { if (string.IsNullOrWhiteSpace(type.FullName)) @@ -45,18 +57,6 @@ private static string AssemblyQualifiedNameWithoutVersionForGenericType(this Typ return $"{type.FullName.Substring(0, bracketIndex)}[{genericPart}], {type.GetTypeInfo().Assembly.GetName().Name}"; } - public static string AssemblyQualifiedNameWithoutVersion(this Type type, bool genericJob = false) - { - var hasGenericJobAttribute = type.GetCustomAttributes().Any(); - - if (!hasGenericJobAttribute && !genericJob) - { - return $"{type.FullName}, {type.GetTypeInfo().Assembly.GetName().Name}"; - } - - return type.AssemblyQualifiedNameWithoutVersionForGenericType(); - } - public static DateTime? ParseAndGetNextOccurrence(string cron) { var expression = CronExpression.Parse(cron, CronFormat.IncludeSeconds); From 1dacac1e64bfa35714555a5742a9eda0ceb793ee Mon Sep 17 00:00:00 2001 From: "o.y.safonov" Date: Mon, 14 Aug 2023 21:31:47 +0300 Subject: [PATCH 6/6] Remove unused code --- .../TypeName/AssemblyQualifiedNameWithoutVersionTests.cs | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/Horarium.Test/TypeName/AssemblyQualifiedNameWithoutVersionTests.cs b/src/Horarium.Test/TypeName/AssemblyQualifiedNameWithoutVersionTests.cs index a771fce..25ae92e 100644 --- a/src/Horarium.Test/TypeName/AssemblyQualifiedNameWithoutVersionTests.cs +++ b/src/Horarium.Test/TypeName/AssemblyQualifiedNameWithoutVersionTests.cs @@ -1,5 +1,4 @@ using System; -using System.ComponentModel; using Xunit; namespace Horarium.Test.TypeName @@ -7,7 +6,6 @@ namespace Horarium.Test.TypeName public class AssemblyQualifiedNameWithoutVersionTests { [Fact] - [Category("OldBehavior")] public void GetName_ForNonGenericJob_ShouldRemoveVersion() { var jobName = new FirstNonGenericJob().GetType().AssemblyQualifiedNameWithoutVersion(); @@ -34,7 +32,6 @@ public void GetName_ForNonGenericJobWithAttribute_ShouldRemoveVersion() } [Fact] - [Category("OldBehavior")] public void GetName_ForGenericJobWithTwoArguments_ShouldNotRemoveVersionForAllTypes() { var jobName = new GenericJobWithTwoArguments().GetType().AssemblyQualifiedNameWithoutVersion(); @@ -61,7 +58,6 @@ public void GetName_ForGenericJobWithTwoArgumentsWithAttribute_ShouldRemoveVersi } [Fact] - [Category("OldBehavior")] public void GetName_ForGenericJobWithTwoNestedGenericArguments_ShouldNotRemoveVersionForAllTypes() { var jobName = new GenericJobWithTwoArguments, GenericJob>() @@ -92,7 +88,6 @@ public void GetName_ForGenericJobWithTwoNestedGenericArgumentsWithAttribute_Shou } [Fact] - [Category("OldBehavior")] public void GetName_ForGenericJobWithSingleGenericArgument_ShouldNotRemoveVersionForAllTypes() { var jobName = new GenericJob().GetType().AssemblyQualifiedNameWithoutVersion(); @@ -119,7 +114,6 @@ public void GetName_ForGenericJobWithSingleGenericArgumentWithAttribute_ShouldRe } [Fact] - [Category("OldBehavior")] public void GetName_ForGenericJobWithSingleNestedGenericArgument_ShouldNotRemoveVersionForAllTypes() { var jobName = new GenericJob>().GetType().AssemblyQualifiedNameWithoutVersion();