From a429eb69388b770a005345fb499773607cb6d326 Mon Sep 17 00:00:00 2001 From: Scott DePouw Date: Thu, 18 Jan 2024 17:20:46 -0500 Subject: [PATCH 1/2] Working on XML comments and other errors (#489) Co-authored-by: Steve Smith --- .../SmartEnumCustomization.cs | 7 + .../SmartEnumSpecimenBuilder.cs | 9 + .../SmartEnumConverterExtensions.cs | 221 +++++++++--------- .../SmartEnumNameConverter.cs | 27 +++ .../SmartEnumValueConverter.cs | 27 +++ .../SmartFlagEnumNameConverter.cs | 108 ++++----- .../SmartFlagEnumValueConverter.cs | 99 ++++---- .../SmartEnumNameFormatter.cs | 17 ++ .../SmartEnumNameResolver.cs | 11 + .../SmartEnumValueFormatter.cs | 5 + .../SmartEnumValueResolver.cs | 11 + .../SmartFlagEnumNameFormatter.cs | 18 +- .../SmartFlagEnumNameResolver.cs | 50 ++-- .../SmartEnumValueSurrogate.cs | 13 ++ .../SmartFlagEnumNameSurrogate.cs | 55 +++-- .../SmartFlagEnumValueSurrogate.cs | 15 +- .../SmartEnumNameConverter.cs | 36 ++- .../SmartEnumValueConverter.cs | 32 +++ .../SmartFlagEnumNameConverter.cs | 22 ++ .../SmartFlagEnumValueConverter.cs | 185 ++++++++------- .../SmartEnumNameFormatter.cs | 17 ++ .../SmartEnumNameResolver.cs | 11 + .../SmartEnumValueFormatter.cs | 18 ++ .../SmartEnumValueResolver.cs | 6 + .../SmartFlagEnumNameFormatter.cs | 12 + .../SmartFlagEnumNameResolver.cs | 54 +++-- .../SmartFlagEnumValueFormatter.cs | 18 ++ .../SmartFlagEnumValueResolver.cs | 5 +- .../AllowNegativeInputValuesAttribute.cs | 7 +- .../AllowUnsafeFlagEnumValuesAttribute.cs | 7 +- src/SmartEnum/Core/SmartEnumThen.cs | 7 +- src/SmartEnum/Core/SmartEnumWhen.cs | 7 +- .../InvalidFlagEnumValueParseException.cs | 5 +- ...tFlagEnumContainsNegativeValueException.cs | 5 +- src/SmartEnum/ISmartEnum.cs | 7 +- src/SmartEnum/SmartEnum.cs | 68 +++++- src/SmartEnum/SmartEnumComparerAttribute.cs | 86 ++++--- src/SmartEnum/SmartEnumExtensions.cs | 23 +- src/SmartEnum/SmartEnumNameAttribute.cs | 9 +- src/SmartEnum/SmartFlagEngine.cs | 12 +- src/SmartEnum/SmartFlagEnum.cs | 75 +++++- src/SmartEnum/SmartFlagEnumExtensions.cs | 23 +- src/SmartEnum/TypeExtensions.cs | 19 +- 43 files changed, 1025 insertions(+), 444 deletions(-) diff --git a/src/SmartEnum.AutoFixture/SmartEnumCustomization.cs b/src/SmartEnum.AutoFixture/SmartEnumCustomization.cs index bc8a096f..b447bc2e 100644 --- a/src/SmartEnum.AutoFixture/SmartEnumCustomization.cs +++ b/src/SmartEnum.AutoFixture/SmartEnumCustomization.cs @@ -2,8 +2,15 @@ namespace Ardalis.SmartEnum.AutoFixture { using global::AutoFixture; + /// + /// + /// public class SmartEnumCustomization : ICustomization { + /// + /// + /// + /// public void Customize(IFixture fixture) { fixture.Customizations.Add(new SmartEnumSpecimenBuilder()); diff --git a/src/SmartEnum.AutoFixture/SmartEnumSpecimenBuilder.cs b/src/SmartEnum.AutoFixture/SmartEnumSpecimenBuilder.cs index 2d1cd0d4..f8dabaaa 100644 --- a/src/SmartEnum.AutoFixture/SmartEnumSpecimenBuilder.cs +++ b/src/SmartEnum.AutoFixture/SmartEnumSpecimenBuilder.cs @@ -3,9 +3,18 @@ namespace Ardalis.SmartEnum.AutoFixture using System; using global::AutoFixture.Kernel; + /// + /// + /// public class SmartEnumSpecimenBuilder : ISpecimenBuilder { + /// + /// + /// + /// + /// + /// public object Create(object request, ISpecimenContext context) { if (request is Type type && type.TryGetValues(out var enums)) diff --git a/src/SmartEnum.EFCore/SmartEnumConverterExtensions.cs b/src/SmartEnum.EFCore/SmartEnumConverterExtensions.cs index 4b267b81..c3ad9955 100644 --- a/src/SmartEnum.EFCore/SmartEnumConverterExtensions.cs +++ b/src/SmartEnum.EFCore/SmartEnumConverterExtensions.cs @@ -9,155 +9,156 @@ using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Reflection; -using System.Text; -namespace SmartEnum.EFCore +namespace SmartEnum.EFCore; + +/// +/// +/// +public static class SmartEnumConverterExtensions { - public static class SmartEnumConverterExtensions + /// + /// Adds a converter for all properties derived from + /// so that entity framework core can work with it. + /// + public static void ConfigureSmartEnum(this ModelConfigurationBuilder configurationBuilder) + { + var modelBuilder = configurationBuilder.CreateModelBuilder(null); + var propertyTypes = modelBuilder.Model.GetEntityTypes() + .SelectMany(e => e.ClrType.GetProperties()) + .Where(p => TypeUtil.IsDerived(p.PropertyType, typeof(SmartEnum<,>))) + .Where(p => p.GetCustomAttribute() == null) + .Select(p => p.PropertyType) + .Distinct(); + + foreach (var propertyType in propertyTypes) + { + var (enumType, keyType) = TypeUtil.GetEnumAndValueTypes(propertyType, typeof(SmartEnum<,>)); + if (enumType != propertyType) + { + // Only enum types 'TEnum' which extend SmartEnum are currently supported. + continue; + } + + var converterType = typeof(SmartEnumConverter<,>).MakeGenericType(propertyType, keyType); + + configurationBuilder.Properties(propertyType) + .HaveConversion(converterType); + } + } + + /// + /// Adds a converter for all properties derived from + /// so that entity framework core can work with it. + /// + /// + public static void ConfigureSmartEnum(this ModelBuilder modelBuilder) { - /// - /// Adds a converter for all properties derived from - /// so that entity framework core can work with it. - /// - public static void ConfigureSmartEnum(this ModelConfigurationBuilder configurationBuilder) + foreach (var entityType in modelBuilder.Model.GetEntityTypes()) { - var modelBuilder = configurationBuilder.CreateModelBuilder(null); - var propertyTypes = modelBuilder.Model.GetEntityTypes() - .SelectMany(e => e.ClrType.GetProperties()) + var properties = entityType.ClrType.GetProperties() .Where(p => TypeUtil.IsDerived(p.PropertyType, typeof(SmartEnum<,>))) - .Where(p => p.GetCustomAttribute() == null) - .Select(p => p.PropertyType) - .Distinct(); + .Where(p => p.GetCustomAttribute() == null); - foreach (var propertyType in propertyTypes) + foreach (var property in properties) { - var (enumType, keyType) = TypeUtil.GetEnumAndValueTypes(propertyType, typeof(SmartEnum<,>)); - if (enumType != propertyType) + var (enumType, keyType) = TypeUtil.GetEnumAndValueTypes(property.PropertyType, typeof(SmartEnum<,>)); + if (enumType != property.PropertyType) { // Only enum types 'TEnum' which extend SmartEnum are currently supported. continue; } - var converterType = typeof(SmartEnumConverter<,>).MakeGenericType(propertyType, keyType); + var converterType = typeof(SmartEnumConverter<,>).MakeGenericType(property.PropertyType, keyType); - configurationBuilder.Properties(propertyType) - .HaveConversion(converterType); - } - } + var converter = (ValueConverter)Activator.CreateInstance(converterType); - /// - /// Adds a converter for all properties derived from - /// so that entity framework core can work with it. - /// - /// - public static void ConfigureSmartEnum(this ModelBuilder modelBuilder) - { - foreach (var entityType in modelBuilder.Model.GetEntityTypes()) - { - var properties = entityType.ClrType.GetProperties() - .Where(p => TypeUtil.IsDerived(p.PropertyType, typeof(SmartEnum<,>))) - .Where(p => p.GetCustomAttribute() == null); - - foreach (var property in properties) + var propertyBuilder = GetPropertyBuilder(modelBuilder, entityType, property.Name); + if (propertyBuilder == null) { - var (enumType, keyType) = TypeUtil.GetEnumAndValueTypes(property.PropertyType, typeof(SmartEnum<,>)); - if (enumType != property.PropertyType) - { - // Only enum types 'TEnum' which extend SmartEnum are currently supported. - continue; - } - - var converterType = typeof(SmartEnumConverter<,>).MakeGenericType(property.PropertyType, keyType); + continue; + } - var converter = (ValueConverter)Activator.CreateInstance(converterType); + propertyBuilder.HasConversion(converter); + } + } + } - var propertyBuilder = GetPropertyBuilder(modelBuilder, entityType, property.Name); - if (propertyBuilder == null) - { - continue; - } + private static PropertyBuilder GetPropertyBuilder( + ModelBuilder modelBuilder, + IMutableEntityType entityType, + string propertyName) + { + var ownershipPath = new List(); - propertyBuilder.HasConversion(converter); - } + var currEntityType = entityType; + while (currEntityType.IsOwned()) + { + var ownership = currEntityType.FindOwnership(); + if (ownership == null) + { + return null; } + + ownershipPath.Add(ownership); + currEntityType = ownership.PrincipalEntityType; } - private static PropertyBuilder GetPropertyBuilder( - ModelBuilder modelBuilder, - IMutableEntityType entityType, - string propertyName) + var entityTypeBuilder = modelBuilder.Entity(currEntityType.Name); + if (ownershipPath.Count == 0) { - var ownershipPath = new List(); + return entityTypeBuilder.Property(propertyName); + } - var currEntityType = entityType; - while (currEntityType.IsOwned()) - { - var ownership = currEntityType.FindOwnership(); - if (ownership == null) - { - return null; - } + var ownedNavigationBuilder = GetOwnedNavigationBuilder(entityTypeBuilder, ownershipPath); + if (ownedNavigationBuilder == null) + { + return null; + } - ownershipPath.Add(ownership); - currEntityType = ownership.PrincipalEntityType; - } + return ownedNavigationBuilder.Property(propertyName); + } - var entityTypeBuilder = modelBuilder.Entity(currEntityType.Name); - if (ownershipPath.Count == 0) - { - return entityTypeBuilder.Property(propertyName); - } + private static OwnedNavigationBuilder GetOwnedNavigationBuilder( + EntityTypeBuilder entityTypeBuilder, + List ownershipPath) + { + OwnedNavigationBuilder ownedNavigationBuilder = null; + for (int i = ownershipPath.Count - 1; i >= 0; i--) + { + var ownership = ownershipPath[i]; - var ownedNavigationBuilder = GetOwnedNavigationBuilder(entityTypeBuilder, ownershipPath); - if (ownedNavigationBuilder == null) + var navigation = ownership.GetNavigation(pointsToPrincipal: false); + if (navigation == null) { return null; } - return ownedNavigationBuilder.Property(propertyName); - } - - private static OwnedNavigationBuilder GetOwnedNavigationBuilder( - EntityTypeBuilder entityTypeBuilder, - List ownershipPath) - { - OwnedNavigationBuilder ownedNavigationBuilder = null; - for (int i = ownershipPath.Count - 1; i >= 0; i--) + if (ownedNavigationBuilder == null) { - var ownership = ownershipPath[i]; - - var navigation = ownership.GetNavigation(pointsToPrincipal: false); - if (navigation == null) + if (ownership.IsUnique) { - return null; + ownedNavigationBuilder = entityTypeBuilder.OwnsOne(ownership.DeclaringEntityType.Name, navigation.Name); } - - if (ownedNavigationBuilder == null) + else { - if (ownership.IsUnique) - { - ownedNavigationBuilder = entityTypeBuilder.OwnsOne(ownership.DeclaringEntityType.Name, navigation.Name); - } - else - { - ownedNavigationBuilder = entityTypeBuilder.OwnsMany(ownership.DeclaringEntityType.Name, navigation.Name); - } + ownedNavigationBuilder = entityTypeBuilder.OwnsMany(ownership.DeclaringEntityType.Name, navigation.Name); + } + } + else + { + if (ownership.IsUnique) + { + ownedNavigationBuilder = ownedNavigationBuilder.OwnsOne(ownership.DeclaringEntityType.Name, navigation.Name); } else { - if (ownership.IsUnique) - { - ownedNavigationBuilder = ownedNavigationBuilder.OwnsOne(ownership.DeclaringEntityType.Name, navigation.Name); - } - else - { - ownedNavigationBuilder = ownedNavigationBuilder.OwnsMany(ownership.DeclaringEntityType.Name, navigation.Name); - } + ownedNavigationBuilder = ownedNavigationBuilder.OwnsMany(ownership.DeclaringEntityType.Name, navigation.Name); } - } - - return ownedNavigationBuilder; + } + + return ownedNavigationBuilder; } } \ No newline at end of file diff --git a/src/SmartEnum.JsonNet/SmartEnumNameConverter.cs b/src/SmartEnum.JsonNet/SmartEnumNameConverter.cs index 24cb4060..c7809ada 100644 --- a/src/SmartEnum.JsonNet/SmartEnumNameConverter.cs +++ b/src/SmartEnum.JsonNet/SmartEnumNameConverter.cs @@ -5,14 +5,35 @@ namespace Ardalis.SmartEnum.JsonNet using Newtonsoft.Json; using System; + /// + /// + /// + /// + /// public class SmartEnumNameConverter : JsonConverter where TEnum : SmartEnum where TValue : IEquatable, IComparable { + /// + /// Defaults to true. + /// public override bool CanRead => true; + /// + /// Defaults to true. + /// public override bool CanWrite => true; + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// public override TEnum ReadJson(JsonReader reader, Type objectType, TEnum existingValue, bool hasExistingValue, JsonSerializer serializer) { switch (reader.TokenType) @@ -37,6 +58,12 @@ TEnum GetFromName(string name) } } + /// + /// + /// + /// + /// + /// public override void WriteJson(JsonWriter writer, TEnum value, JsonSerializer serializer) { if (value is null) diff --git a/src/SmartEnum.JsonNet/SmartEnumValueConverter.cs b/src/SmartEnum.JsonNet/SmartEnumValueConverter.cs index 88af5503..57c781c0 100644 --- a/src/SmartEnum.JsonNet/SmartEnumValueConverter.cs +++ b/src/SmartEnum.JsonNet/SmartEnumValueConverter.cs @@ -3,13 +3,34 @@ namespace Ardalis.SmartEnum.JsonNet using Newtonsoft.Json; using System; + /// + /// + /// + /// + /// public class SmartEnumValueConverter : JsonConverter where TEnum : SmartEnum where TValue : IEquatable, IComparable { + /// + /// Defaults to true. + /// public override bool CanRead => true; + /// + /// Defaults to true. + /// public override bool CanWrite => true; + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// public override TEnum ReadJson(JsonReader reader, Type objectType, TEnum existingValue, bool hasExistingValue, JsonSerializer serializer) { try @@ -32,6 +53,12 @@ public override TEnum ReadJson(JsonReader reader, Type objectType, TEnum existin } } + /// + /// + /// + /// + /// + /// public override void WriteJson(JsonWriter writer, TEnum value, JsonSerializer serializer) { if (value is null) diff --git a/src/SmartEnum.JsonNet/SmartFlagEnumNameConverter.cs b/src/SmartEnum.JsonNet/SmartFlagEnumNameConverter.cs index b67a5ea4..1e9cfb77 100644 --- a/src/SmartEnum.JsonNet/SmartFlagEnumNameConverter.cs +++ b/src/SmartEnum.JsonNet/SmartFlagEnumNameConverter.cs @@ -2,68 +2,72 @@ using System.Linq; using Newtonsoft.Json; -namespace Ardalis.SmartEnum.JsonNet +namespace Ardalis.SmartEnum.JsonNet; + +/// +/// +/// +/// +/// +public class SmartFlagEnumNameConverter : JsonConverter +where TEnum : SmartFlagEnum +where TValue : struct, IComparable, IEquatable { - public class SmartFlagEnumNameConverter : JsonConverter - where TEnum : SmartFlagEnum - where TValue : struct, IComparable, IEquatable + /// + /// Default to true. + /// + public override bool CanRead => true; + + /// + /// Defaults to true + /// + public override bool CanWrite => true; + + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + public override TEnum ReadJson(JsonReader reader, Type objectType, TEnum existingValue, bool hasExistingValue, JsonSerializer serializer) { - /// - /// Default to true. - /// - public override bool CanRead => true; + switch (reader.TokenType) + { + case JsonToken.String: + return GetFromName((string)reader.Value); - /// - /// Defaults to true - /// - public override bool CanWrite => true; + default: + throw new JsonSerializationException($"Unexpected token {reader.TokenType} when parsing a smart flag enum."); + } - /// - /// - /// - /// - /// - /// - /// - /// - /// - /// - public override TEnum ReadJson(JsonReader reader, Type objectType, TEnum existingValue, bool hasExistingValue, JsonSerializer serializer) + TEnum GetFromName(string name) { - switch (reader.TokenType) + try { - case JsonToken.String: - return GetFromName((string)reader.Value); - - default: - throw new JsonSerializationException($"Unexpected token {reader.TokenType} when parsing a smart flag enum."); + return SmartFlagEnum.FromName(name, false).FirstOrDefault(); } - - TEnum GetFromName(string name) + catch (Exception ex) { - try - { - return SmartFlagEnum.FromName(name, false).FirstOrDefault(); - } - catch (Exception ex) - { - throw new JsonSerializationException($"Error converting value '{name}' to a smart flag enum.", ex); - } + throw new JsonSerializationException($"Error converting value '{name}' to a smart flag enum.", ex); } } + } - /// - /// - /// - /// - /// - /// - public override void WriteJson(JsonWriter writer, TEnum value, JsonSerializer serializer) - { - if (value is null) - writer.WriteNull(); - else - writer.WriteValue(value.Name); - } + /// + /// + /// + /// + /// + /// + public override void WriteJson(JsonWriter writer, TEnum value, JsonSerializer serializer) + { + if (value is null) + writer.WriteNull(); + else + writer.WriteValue(value.Name); } } diff --git a/src/SmartEnum.JsonNet/SmartFlagEnumValueConverter.cs b/src/SmartEnum.JsonNet/SmartFlagEnumValueConverter.cs index ca0eb3af..9efb6e74 100644 --- a/src/SmartEnum.JsonNet/SmartFlagEnumValueConverter.cs +++ b/src/SmartEnum.JsonNet/SmartFlagEnumValueConverter.cs @@ -1,63 +1,70 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; using Newtonsoft.Json; -namespace Ardalis.SmartEnum.JsonNet +namespace Ardalis.SmartEnum.JsonNet; + +/// +/// +/// +/// +/// +public class SmartFlagEnumValueConverter : JsonConverter +where TEnum : SmartFlagEnum +where TValue: struct, IEquatable, IComparable { - public class SmartFlagEnumValueConverter : JsonConverter - where TEnum : SmartFlagEnum - where TValue: struct, IEquatable, IComparable - { - public override bool CanRead => true; + /// + /// Defaults to true. + /// + public override bool CanRead => true; - public override bool CanWrite => true; + /// + /// Defaults to true. + /// + public override bool CanWrite => true; - /// - /// - /// - /// - /// - /// - /// - /// - /// - public override TEnum ReadJson(JsonReader reader, Type objectType, TEnum existingValue, bool hasExistingValue, JsonSerializer serializer) + /// + /// + /// + /// + /// + /// + /// + /// + /// + public override TEnum ReadJson(JsonReader reader, Type objectType, TEnum existingValue, bool hasExistingValue, JsonSerializer serializer) + { + try { - try + TValue value; + if (reader.TokenType == JsonToken.Integer && typeof(TValue) != typeof(long) && typeof(TValue) != typeof(bool)) { - TValue value; - if (reader.TokenType == JsonToken.Integer && typeof(TValue) != typeof(long) && typeof(TValue) != typeof(bool)) - { - value = (TValue)Convert.ChangeType(reader.Value, typeof(TValue)); - } - else - { - value = (TValue)reader.Value; - } - - return SmartFlagEnum.DeserializeValue(value); + value = (TValue)Convert.ChangeType(reader.Value, typeof(TValue)); } - catch (Exception ex) + else { - throw new JsonSerializationException($"Error converting {reader.Value ?? "Null"} to {objectType.Name}.", ex); + value = (TValue)reader.Value; } - } - /// - /// - /// - /// - /// - /// - public override void WriteJson(JsonWriter writer, TEnum value, JsonSerializer serializer) + return SmartFlagEnum.DeserializeValue(value); + } + catch (Exception ex) { - if (value is null) - writer.WriteNull(); - else - writer.WriteValue(value.Value); + throw new JsonSerializationException($"Error converting {reader.Value ?? "Null"} to {objectType.Name}.", ex); } + } + /// + /// + /// + /// + /// + /// + public override void WriteJson(JsonWriter writer, TEnum value, JsonSerializer serializer) + { + if (value is null) + writer.WriteNull(); + else + writer.WriteValue(value.Value); } + } diff --git a/src/SmartEnum.MessagePack/SmartEnumNameFormatter.cs b/src/SmartEnum.MessagePack/SmartEnumNameFormatter.cs index 380539ae..3575df38 100644 --- a/src/SmartEnum.MessagePack/SmartEnumNameFormatter.cs +++ b/src/SmartEnum.MessagePack/SmartEnumNameFormatter.cs @@ -4,10 +4,21 @@ namespace Ardalis.SmartEnum.MessagePack using global::MessagePack; using global::MessagePack.Formatters; + /// + /// + /// + /// + /// public sealed class SmartEnumNameFormatter : IMessagePackFormatter where TEnum : SmartEnum where TValue : struct, IEquatable, IComparable { + /// + /// + /// + /// + /// + /// public void Serialize(ref MessagePackWriter writer, TEnum value, MessagePackSerializerOptions options) { if (value is null) return; @@ -15,6 +26,12 @@ public void Serialize(ref MessagePackWriter writer, TEnum value, MessagePackSeri writer.Write(value.Name); } + /// + /// + /// + /// + /// + /// public TEnum Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options) { var name = reader.ReadString(); diff --git a/src/SmartEnum.MessagePack/SmartEnumNameResolver.cs b/src/SmartEnum.MessagePack/SmartEnumNameResolver.cs index 0294d8ba..b177eca2 100644 --- a/src/SmartEnum.MessagePack/SmartEnumNameResolver.cs +++ b/src/SmartEnum.MessagePack/SmartEnumNameResolver.cs @@ -4,14 +4,25 @@ namespace Ardalis.SmartEnum.MessagePack using global::MessagePack; using global::MessagePack.Formatters; + /// + /// + /// public class SmartEnumNameResolver : IFormatterResolver { + /// + /// + /// public static readonly SmartEnumNameResolver Instance = new SmartEnumNameResolver(); private SmartEnumNameResolver() { } + /// + /// + /// + /// + /// public IMessagePackFormatter GetFormatter() => FormatterCache.Formatter; diff --git a/src/SmartEnum.MessagePack/SmartEnumValueFormatter.cs b/src/SmartEnum.MessagePack/SmartEnumValueFormatter.cs index 66de2131..403ac3a8 100644 --- a/src/SmartEnum.MessagePack/SmartEnumValueFormatter.cs +++ b/src/SmartEnum.MessagePack/SmartEnumValueFormatter.cs @@ -4,6 +4,11 @@ namespace Ardalis.SmartEnum.MessagePack using global::MessagePack; using global::MessagePack.Formatters; + /// + /// + /// + /// + /// public class SmartEnumValueFormatter : IMessagePackFormatter where TEnum : SmartEnum where TValue : struct, IEquatable, IComparable diff --git a/src/SmartEnum.MessagePack/SmartEnumValueResolver.cs b/src/SmartEnum.MessagePack/SmartEnumValueResolver.cs index 2e42a72f..994700a4 100644 --- a/src/SmartEnum.MessagePack/SmartEnumValueResolver.cs +++ b/src/SmartEnum.MessagePack/SmartEnumValueResolver.cs @@ -4,14 +4,25 @@ namespace Ardalis.SmartEnum.MessagePack using global::MessagePack; using global::MessagePack.Formatters; + /// + /// + /// public class SmartEnumValueResolver : IFormatterResolver { + /// + /// + /// public static readonly SmartEnumValueResolver Instance = new SmartEnumValueResolver(); private SmartEnumValueResolver() { } + /// + /// + /// + /// + /// public IMessagePackFormatter GetFormatter() => FormatterCache.Formatter; diff --git a/src/SmartEnum.MessagePack/SmartFlagEnumNameFormatter.cs b/src/SmartEnum.MessagePack/SmartFlagEnumNameFormatter.cs index c4c5038f..be4a2d40 100644 --- a/src/SmartEnum.MessagePack/SmartFlagEnumNameFormatter.cs +++ b/src/SmartEnum.MessagePack/SmartFlagEnumNameFormatter.cs @@ -6,11 +6,21 @@ namespace Ardalis.SmartEnum.MessagePack using global::MessagePack; using global::MessagePack.Formatters; - + /// + /// + /// + /// + /// public sealed class SmartFlagEnumNameFormatter : IMessagePackFormatter where TEnum : SmartFlagEnum where TValue : struct, IEquatable, IComparable { + /// + /// + /// + /// + /// + /// public void Serialize(ref MessagePackWriter writer, TEnum value, MessagePackSerializerOptions options) { if (value is null) return; @@ -18,6 +28,12 @@ public void Serialize(ref MessagePackWriter writer, TEnum value, MessagePackSeri writer.Write(value.Name); } + /// + /// + /// + /// + /// + /// public TEnum Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options) { var name = reader.ReadString(); diff --git a/src/SmartEnum.MessagePack/SmartFlagEnumNameResolver.cs b/src/SmartEnum.MessagePack/SmartFlagEnumNameResolver.cs index 32ac16b5..1fcfa865 100644 --- a/src/SmartEnum.MessagePack/SmartFlagEnumNameResolver.cs +++ b/src/SmartEnum.MessagePack/SmartFlagEnumNameResolver.cs @@ -1,34 +1,44 @@ -using global::MessagePack; +using global::MessagePack; using global::MessagePack.Formatters; using System; -namespace Ardalis.SmartEnum.MessagePack +namespace Ardalis.SmartEnum.MessagePack; + +/// +/// +/// +public class SmartFlagEnumNameResolver : IFormatterResolver { - public class SmartFlagEnumNameResolver : IFormatterResolver - { - public static readonly SmartFlagEnumNameResolver Instance = new SmartFlagEnumNameResolver(); + /// + /// + /// + public static readonly SmartFlagEnumNameResolver Instance = new SmartFlagEnumNameResolver(); - private SmartFlagEnumNameResolver() - { - } + private SmartFlagEnumNameResolver() + { + } - public IMessagePackFormatter GetFormatter() => - FormatterCache.Formatter; + /// + /// + /// + /// + /// + public IMessagePackFormatter GetFormatter() => + FormatterCache.Formatter; - private static class FormatterCache - { - public static readonly IMessagePackFormatter Formatter; + private static class FormatterCache + { + public static readonly IMessagePackFormatter Formatter; #pragma warning disable S3963 // "static" fields should be initialized inline - static FormatterCache() + static FormatterCache() + { + if (typeof(T).IsSmartFlagEnum(out var genericArguments)) { - if (typeof(T).IsSmartFlagEnum(out var genericArguments)) - { - var formatterType = typeof(SmartFlagEnumNameFormatter<,>).MakeGenericType(genericArguments); - Formatter = (IMessagePackFormatter)Activator.CreateInstance(formatterType); - } + var formatterType = typeof(SmartFlagEnumNameFormatter<,>).MakeGenericType(genericArguments); + Formatter = (IMessagePackFormatter)Activator.CreateInstance(formatterType); } -#pragma warning restore S3963 // "static" fields should be initialized inline } +#pragma warning restore S3963 // "static" fields should be initialized inline } } diff --git a/src/SmartEnum.ProtoBufNet/SmartEnumValueSurrogate.cs b/src/SmartEnum.ProtoBufNet/SmartEnumValueSurrogate.cs index 1a883652..fa305ff9 100644 --- a/src/SmartEnum.ProtoBufNet/SmartEnumValueSurrogate.cs +++ b/src/SmartEnum.ProtoBufNet/SmartEnumValueSurrogate.cs @@ -3,6 +3,11 @@ namespace Ardalis.SmartEnum.ProtoBufNet using System; using ProtoBuf; + /// + /// + /// + /// + /// [ProtoContract] public class SmartEnumValueSurrogate where TEnum : SmartEnum @@ -11,6 +16,10 @@ public class SmartEnumValueSurrogate [ProtoMember(1, IsRequired = true)] TValue Value { get; set; } + /// + /// + /// + /// public static implicit operator SmartEnumValueSurrogate(TEnum smartEnum) { if (smartEnum is null) @@ -19,6 +28,10 @@ public static implicit operator SmartEnumValueSurrogate(TEnum sma return new SmartEnumValueSurrogate { Value = smartEnum.Value }; } + /// + /// + /// + /// public static implicit operator TEnum(SmartEnumValueSurrogate surrogate) { if (surrogate is null) diff --git a/src/SmartEnum.ProtoBufNet/SmartFlagEnumNameSurrogate.cs b/src/SmartEnum.ProtoBufNet/SmartFlagEnumNameSurrogate.cs index f3822764..9aacd0d7 100644 --- a/src/SmartEnum.ProtoBufNet/SmartFlagEnumNameSurrogate.cs +++ b/src/SmartEnum.ProtoBufNet/SmartFlagEnumNameSurrogate.cs @@ -2,30 +2,45 @@ using System.Linq; using ProtoBuf; -namespace Ardalis.SmartEnum.ProtoBufNet +namespace Ardalis.SmartEnum.ProtoBufNet; + +/// +/// +/// +/// +/// +[ProtoContract] +public class SmartFlagEnumNameSurrogate +where TEnum : SmartFlagEnum +where TValue : struct, IEquatable, IComparable { - [ProtoContract] - public class SmartFlagEnumNameSurrogate - where TEnum : SmartFlagEnum - where TValue : struct, IEquatable, IComparable - { - [ProtoMember(1, IsRequired = true)] - public string Name { get; set; } + /// + /// + /// + [ProtoMember(1, IsRequired = true)] + public string Name { get; set; } - public static implicit operator SmartFlagEnumNameSurrogate(TEnum smartFlagEnum) - { - if (smartFlagEnum is null) - return null; + /// + /// + /// + /// + public static implicit operator SmartFlagEnumNameSurrogate(TEnum smartFlagEnum) + { + if (smartFlagEnum is null) + return null; - return new SmartFlagEnumNameSurrogate { Name = smartFlagEnum.Name }; - } + return new SmartFlagEnumNameSurrogate { Name = smartFlagEnum.Name }; + } - public static implicit operator TEnum(SmartFlagEnumNameSurrogate surrogate) - { - if (surrogate is null) - return null; + /// + /// + /// + /// + public static implicit operator TEnum(SmartFlagEnumNameSurrogate surrogate) + { + if (surrogate is null) + return null; - return SmartFlagEnum.FromName(surrogate.Name).FirstOrDefault(); - } + return SmartFlagEnum.FromName(surrogate.Name).FirstOrDefault(); } } diff --git a/src/SmartEnum.ProtoBufNet/SmartFlagEnumValueSurrogate.cs b/src/SmartEnum.ProtoBufNet/SmartFlagEnumValueSurrogate.cs index 2fd4b28f..6abe4640 100644 --- a/src/SmartEnum.ProtoBufNet/SmartFlagEnumValueSurrogate.cs +++ b/src/SmartEnum.ProtoBufNet/SmartFlagEnumValueSurrogate.cs @@ -1,8 +1,13 @@ -namespace Ardalis.SmartEnum.ProtoBufNet +namespace Ardalis.SmartEnum.ProtoBufNet { using System; using ProtoBuf; + /// + /// + /// + /// + /// [ProtoContract] public class SmartFlagEnumValueSurrogate where TEnum : SmartFlagEnum @@ -11,6 +16,10 @@ public class SmartFlagEnumValueSurrogate [ProtoMember(1, IsRequired = true)] TValue Value { get; set; } + /// + /// + /// + /// public static implicit operator SmartFlagEnumValueSurrogate(TEnum smartEnum) { if (smartEnum is null) @@ -19,6 +28,10 @@ public static implicit operator SmartFlagEnumValueSurrogate(TEnum return new SmartFlagEnumValueSurrogate { Value = smartEnum.Value }; } + /// + /// + /// + /// public static implicit operator TEnum(SmartFlagEnumValueSurrogate surrogate) { if (surrogate is null) diff --git a/src/SmartEnum.SystemTextJson/SmartEnumNameConverter.cs b/src/SmartEnum.SystemTextJson/SmartEnumNameConverter.cs index 79321c2b..2191e012 100644 --- a/src/SmartEnum.SystemTextJson/SmartEnumNameConverter.cs +++ b/src/SmartEnum.SystemTextJson/SmartEnumNameConverter.cs @@ -4,10 +4,23 @@ namespace Ardalis.SmartEnum.SystemTextJson using System.Text.Json; using System.Text.Json.Serialization; + /// + /// + /// + /// + /// public class SmartEnumNameConverter : JsonConverter where TEnum : SmartEnum where TValue : IEquatable, IComparable, IConvertible { + /// + /// + /// + /// + /// + /// + /// + /// public override TEnum Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { switch (reader.TokenType) @@ -20,7 +33,14 @@ public override TEnum Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSe } } - public override void Write(Utf8JsonWriter writer, TEnum value, JsonSerializerOptions options) + /// + /// + /// + /// + /// + /// + public override void Write(Utf8JsonWriter writer, TEnum value, + JsonSerializerOptions options) { if (value == null) writer.WriteNullValue(); @@ -40,6 +60,14 @@ private TEnum GetFromName(string name) } } + /// + /// + /// + /// + /// + /// + /// + /// public override TEnum ReadAsPropertyName(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { switch (reader.TokenType) @@ -52,6 +80,12 @@ public override TEnum ReadAsPropertyName(ref Utf8JsonReader reader, Type typeToC } } + /// + /// + /// + /// + /// + /// public override void WriteAsPropertyName(Utf8JsonWriter writer, TEnum value, JsonSerializerOptions options) { writer.WritePropertyName(value.Name); diff --git a/src/SmartEnum.SystemTextJson/SmartEnumValueConverter.cs b/src/SmartEnum.SystemTextJson/SmartEnumValueConverter.cs index cee03350..a2e9ee11 100644 --- a/src/SmartEnum.SystemTextJson/SmartEnumValueConverter.cs +++ b/src/SmartEnum.SystemTextJson/SmartEnumValueConverter.cs @@ -4,10 +4,22 @@ namespace Ardalis.SmartEnum.SystemTextJson using System.Text.Json; using System.Text.Json.Serialization; + /// + /// + /// + /// + /// public class SmartEnumValueConverter : JsonConverter where TEnum : SmartEnum where TValue : IEquatable, IComparable, IConvertible { + /// + /// + /// + /// + /// + /// + /// public override TEnum Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { if (reader.TokenType == JsonTokenType.Null) @@ -16,6 +28,12 @@ public override TEnum Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSe return GetFromValue(ReadValue(ref reader)); } + /// + /// + /// + /// + /// + /// public override void Write(Utf8JsonWriter writer, TEnum value, JsonSerializerOptions options) { if (value == null) @@ -84,6 +102,14 @@ private TValue ReadValue(ref Utf8JsonReader reader) throw new ArgumentOutOfRangeException(typeof(TValue).ToString(), $"{typeof(TValue).Name} is not supported."); } + /// + /// + /// + /// + /// + /// + /// + /// public override TEnum ReadAsPropertyName(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { if (reader.TokenType != JsonTokenType.PropertyName) @@ -94,6 +120,12 @@ public override TEnum ReadAsPropertyName(ref Utf8JsonReader reader, Type typeToC return GetFromValue(ReadPropertyName(ref reader)); } + /// + /// + /// + /// + /// + /// public override void WriteAsPropertyName(Utf8JsonWriter writer, TEnum value, JsonSerializerOptions options) { writer.WritePropertyName(value.Value?.ToString() ?? string.Empty); diff --git a/src/SmartEnum.SystemTextJson/SmartFlagEnumNameConverter.cs b/src/SmartEnum.SystemTextJson/SmartFlagEnumNameConverter.cs index c1da3e4d..6005c427 100644 --- a/src/SmartEnum.SystemTextJson/SmartFlagEnumNameConverter.cs +++ b/src/SmartEnum.SystemTextJson/SmartFlagEnumNameConverter.cs @@ -5,12 +5,28 @@ namespace Ardalis.SmartEnum.SystemTextJson using System.Text.Json; using System.Text.Json.Serialization; + /// + /// + /// + /// + /// public class SmartFlagEnumNameConverter : JsonConverter where TEnum : SmartFlagEnum where TValue : struct, IComparable, IEquatable { + /// + /// + /// public override bool HandleNull => true; + /// + /// + /// + /// + /// + /// + /// + /// public override TEnum Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { switch (reader.TokenType) @@ -35,6 +51,12 @@ TEnum GetFromName(string name) } } + /// + /// + /// + /// + /// + /// public override void Write(Utf8JsonWriter writer, TEnum value, JsonSerializerOptions options) { if (value == null) diff --git a/src/SmartEnum.SystemTextJson/SmartFlagEnumValueConverter.cs b/src/SmartEnum.SystemTextJson/SmartFlagEnumValueConverter.cs index 4bed0ad2..6c9e288d 100644 --- a/src/SmartEnum.SystemTextJson/SmartFlagEnumValueConverter.cs +++ b/src/SmartEnum.SystemTextJson/SmartFlagEnumValueConverter.cs @@ -1,97 +1,118 @@ -namespace Ardalis.SmartEnum.SystemTextJson + +using System; +using System.Linq; +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace Ardalis.SmartEnum.SystemTextJson; +/// +/// +/// +/// +/// +public class SmartFlagEnumValueConverter : JsonConverter +where TEnum : SmartFlagEnum +where TValue: struct, IEquatable, IComparable { - using System; - using System.Linq; - using System.Text.Json; - using System.Text.Json.Serialization; + /// + /// Defaults to true. + /// + public override bool HandleNull => true; - public class SmartFlagEnumValueConverter : JsonConverter - where TEnum : SmartFlagEnum - where TValue: struct, IEquatable, IComparable + /// + /// + /// + /// + /// + /// + /// + /// + public override TEnum Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { - public override bool HandleNull => true; + if (reader.TokenType == JsonTokenType.Null) + throw new JsonException($"Error converting value 'Null' to a smart flag enum."); - public override TEnum Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) - { - if (reader.TokenType == JsonTokenType.Null) - throw new JsonException($"Error converting value 'Null' to a smart flag enum."); + return GetFromValue(ReadValue(ref reader)); + } - return GetFromValue(ReadValue(ref reader)); + private TEnum GetFromValue(TValue value) + { + try + { + return SmartFlagEnum.DeserializeValue(value); } - - private TEnum GetFromValue(TValue value) + catch (Exception ex) { - try - { - return SmartFlagEnum.DeserializeValue(value); - } - catch (Exception ex) - { - throw new JsonException($"Error converting value '{value}' to a smart flag enum.", ex); - } + throw new JsonException($"Error converting value '{value}' to a smart flag enum.", ex); } + } - private TValue ReadValue(ref Utf8JsonReader reader) + private TValue ReadValue(ref Utf8JsonReader reader) + { + try { - try - { - if (typeof(TValue) == typeof(bool)) - return (TValue)(object)reader.GetBoolean(); - if (typeof(TValue) == typeof(byte)) - return (TValue)(object)reader.GetByte(); - if (typeof(TValue) == typeof(sbyte)) - return (TValue)(object)reader.GetSByte(); - if (typeof(TValue) == typeof(short)) - return (TValue)(object)reader.GetInt16(); - if (typeof(TValue) == typeof(ushort)) - return (TValue)(object)reader.GetUInt16(); - if (typeof(TValue) == typeof(int)) - return (TValue)(object)reader.GetInt32(); - if (typeof(TValue) == typeof(uint)) - return (TValue)(object)reader.GetUInt32(); - if (typeof(TValue) == typeof(long)) - return (TValue)(object)reader.GetInt64(); - if (typeof(TValue) == typeof(ulong)) - return (TValue)(object)reader.GetUInt64(); - if (typeof(TValue) == typeof(float)) - return (TValue)(object)reader.GetSingle(); - if (typeof(TValue) == typeof(double)) - return (TValue)(object)reader.GetDouble(); - if (typeof(TValue) == typeof(string)) - return (TValue)(object)reader.GetString(); - } - catch (InvalidOperationException ex) - { - throw new JsonException($"Error converting token '{reader.TokenType}' to a smart flag enum.", ex); - } - - throw new ArgumentOutOfRangeException(typeof(TValue).ToString(), $"{typeof(TValue).Name} is not supported."); + if (typeof(TValue) == typeof(bool)) + return (TValue)(object)reader.GetBoolean(); + if (typeof(TValue) == typeof(byte)) + return (TValue)(object)reader.GetByte(); + if (typeof(TValue) == typeof(sbyte)) + return (TValue)(object)reader.GetSByte(); + if (typeof(TValue) == typeof(short)) + return (TValue)(object)reader.GetInt16(); + if (typeof(TValue) == typeof(ushort)) + return (TValue)(object)reader.GetUInt16(); + if (typeof(TValue) == typeof(int)) + return (TValue)(object)reader.GetInt32(); + if (typeof(TValue) == typeof(uint)) + return (TValue)(object)reader.GetUInt32(); + if (typeof(TValue) == typeof(long)) + return (TValue)(object)reader.GetInt64(); + if (typeof(TValue) == typeof(ulong)) + return (TValue)(object)reader.GetUInt64(); + if (typeof(TValue) == typeof(float)) + return (TValue)(object)reader.GetSingle(); + if (typeof(TValue) == typeof(double)) + return (TValue)(object)reader.GetDouble(); + if (typeof(TValue) == typeof(string)) + return (TValue)(object)reader.GetString(); } - - public override void Write(Utf8JsonWriter writer, TEnum value, JsonSerializerOptions options) + catch (InvalidOperationException ex) { - if (value == null) - writer.WriteNullValue(); - else if (typeof(TValue) == typeof(bool)) - writer.WriteBooleanValue((bool)(object)value.Value); - else if (typeof(TValue) == typeof(short)) - writer.WriteNumberValue((int)(short)(object)value.Value); - else if (typeof(TValue) == typeof(int)) - writer.WriteNumberValue((int)(object)value.Value); - else if (typeof(TValue) == typeof(double)) - writer.WriteNumberValue((double)(object)value.Value); - else if (typeof(TValue) == typeof(decimal)) - writer.WriteNumberValue((decimal)(object)value.Value); - else if (typeof(TValue) == typeof(ulong)) - writer.WriteNumberValue((ulong)(object)value.Value); - else if (typeof(TValue) == typeof(uint)) - writer.WriteNumberValue((uint)(object)value.Value); - else if (typeof(TValue) == typeof(float)) - writer.WriteNumberValue((float)(object)value.Value); - else if (typeof(TValue) == typeof(long)) - writer.WriteNumberValue((long)(object)value.Value); - else - writer.WriteStringValue(value.Value.ToString()); + throw new JsonException($"Error converting token '{reader.TokenType}' to a smart flag enum.", ex); } + + throw new ArgumentOutOfRangeException(typeof(TValue).ToString(), $"{typeof(TValue).Name} is not supported."); + } + + /// + /// + /// + /// + /// + /// + public override void Write(Utf8JsonWriter writer, TEnum value, JsonSerializerOptions options) + { + if (value == null) + writer.WriteNullValue(); + else if (typeof(TValue) == typeof(bool)) + writer.WriteBooleanValue((bool)(object)value.Value); + else if (typeof(TValue) == typeof(short)) + writer.WriteNumberValue((int)(short)(object)value.Value); + else if (typeof(TValue) == typeof(int)) + writer.WriteNumberValue((int)(object)value.Value); + else if (typeof(TValue) == typeof(double)) + writer.WriteNumberValue((double)(object)value.Value); + else if (typeof(TValue) == typeof(decimal)) + writer.WriteNumberValue((decimal)(object)value.Value); + else if (typeof(TValue) == typeof(ulong)) + writer.WriteNumberValue((ulong)(object)value.Value); + else if (typeof(TValue) == typeof(uint)) + writer.WriteNumberValue((uint)(object)value.Value); + else if (typeof(TValue) == typeof(float)) + writer.WriteNumberValue((float)(object)value.Value); + else if (typeof(TValue) == typeof(long)) + writer.WriteNumberValue((long)(object)value.Value); + else + writer.WriteStringValue(value.Value.ToString()); } } diff --git a/src/SmartEnum.Utf8Json/SmartEnumNameFormatter.cs b/src/SmartEnum.Utf8Json/SmartEnumNameFormatter.cs index 84ad29b3..deec61fe 100644 --- a/src/SmartEnum.Utf8Json/SmartEnumNameFormatter.cs +++ b/src/SmartEnum.Utf8Json/SmartEnumNameFormatter.cs @@ -3,10 +3,21 @@ namespace Ardalis.SmartEnum.Utf8Json using global::Utf8Json; using System; + /// + /// + /// + /// + /// public class SmartEnumNameFormatter : IJsonFormatter where TEnum : SmartEnum where TValue : struct, IEquatable, IComparable { + /// + /// + /// + /// + /// + /// public void Serialize(ref JsonWriter writer, TEnum value, IJsonFormatterResolver formatterResolver) { if (value is null) @@ -18,6 +29,12 @@ public void Serialize(ref JsonWriter writer, TEnum value, IJsonFormatterResolver writer.WriteString(value.Name); } + /// + /// + /// + /// + /// + /// public TEnum Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver) { if (reader.ReadIsNull()) diff --git a/src/SmartEnum.Utf8Json/SmartEnumNameResolver.cs b/src/SmartEnum.Utf8Json/SmartEnumNameResolver.cs index 5f19b7ac..4aa5d213 100644 --- a/src/SmartEnum.Utf8Json/SmartEnumNameResolver.cs +++ b/src/SmartEnum.Utf8Json/SmartEnumNameResolver.cs @@ -3,14 +3,25 @@ namespace Ardalis.SmartEnum.Utf8Json using System; using global::Utf8Json; + /// + /// + /// public class SmartEnumNameResolver : IJsonFormatterResolver { + /// + /// + /// public static readonly SmartEnumNameResolver Instance = new SmartEnumNameResolver(); private SmartEnumNameResolver() { } + /// + /// + /// + /// + /// public IJsonFormatter GetFormatter() => FormatterCache.Formatter; diff --git a/src/SmartEnum.Utf8Json/SmartEnumValueFormatter.cs b/src/SmartEnum.Utf8Json/SmartEnumValueFormatter.cs index 1aaac722..a64a1c6f 100644 --- a/src/SmartEnum.Utf8Json/SmartEnumValueFormatter.cs +++ b/src/SmartEnum.Utf8Json/SmartEnumValueFormatter.cs @@ -3,10 +3,22 @@ namespace Ardalis.SmartEnum.Utf8Json using global::Utf8Json; using System; + /// + /// + /// + /// + /// public class SmartEnumValueFormatter : IJsonFormatter where TEnum : SmartEnum where TValue : struct, IEquatable, IComparable { + /// + /// + /// + /// + /// + /// + /// public void Serialize(ref JsonWriter writer, TEnum value, IJsonFormatterResolver formatterResolver) { if (value is null) @@ -41,6 +53,12 @@ public void Serialize(ref JsonWriter writer, TEnum value, IJsonFormatterResolver throw new ArgumentOutOfRangeException(typeof(TValue).ToString(), $"{typeof(TValue).Name} is not supported."); } + /// + /// + /// + /// + /// + /// public TEnum Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver) { if (reader.ReadIsNull()) diff --git a/src/SmartEnum.Utf8Json/SmartEnumValueResolver.cs b/src/SmartEnum.Utf8Json/SmartEnumValueResolver.cs index 9cee4c85..e0d6779e 100644 --- a/src/SmartEnum.Utf8Json/SmartEnumValueResolver.cs +++ b/src/SmartEnum.Utf8Json/SmartEnumValueResolver.cs @@ -3,8 +3,14 @@ namespace Ardalis.SmartEnum.Utf8Json using System; using global::Utf8Json; + /// + /// + /// public class SmartEnumValueResolver : IJsonFormatterResolver { + /// + /// + /// public static readonly SmartEnumValueResolver Instance = new SmartEnumValueResolver(); private SmartEnumValueResolver() diff --git a/src/SmartEnum.Utf8Json/SmartFlagEnumNameFormatter.cs b/src/SmartEnum.Utf8Json/SmartFlagEnumNameFormatter.cs index b2bd9621..e8f26b10 100644 --- a/src/SmartEnum.Utf8Json/SmartFlagEnumNameFormatter.cs +++ b/src/SmartEnum.Utf8Json/SmartFlagEnumNameFormatter.cs @@ -16,6 +16,12 @@ public class SmartFlagEnumNameFormatter : IJsonFormatter where TEnum : SmartFlagEnum where TValue : struct, IEquatable, IComparable { + /// + /// + /// + /// + /// + /// public void Serialize(ref JsonWriter writer, TEnum value, IJsonFormatterResolver formatterResolver) { if (value is null) @@ -27,6 +33,12 @@ public void Serialize(ref JsonWriter writer, TEnum value, IJsonFormatterResolver writer.WriteString(value.Name); } + /// + /// + /// + /// + /// + /// public TEnum Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver) { if (reader.ReadIsNull()) diff --git a/src/SmartEnum.Utf8Json/SmartFlagEnumNameResolver.cs b/src/SmartEnum.Utf8Json/SmartFlagEnumNameResolver.cs index 353fff44..39c88652 100644 --- a/src/SmartEnum.Utf8Json/SmartFlagEnumNameResolver.cs +++ b/src/SmartEnum.Utf8Json/SmartFlagEnumNameResolver.cs @@ -1,34 +1,44 @@ -namespace Ardalis.SmartEnum.Utf8Json +using System; +using global::Utf8Json; +using global::Utf8Json.Formatters; + +namespace Ardalis.SmartEnum.Utf8Json; + +/// +/// +/// +public class SmartFlagEnumNameResolver : IJsonFormatterResolver { - using System; - using global::Utf8Json; - using global::Utf8Json.Formatters; + /// + /// + /// + public static readonly SmartFlagEnumNameResolver Instance = new SmartFlagEnumNameResolver(); - public class SmartFlagEnumNameResolver : IJsonFormatterResolver + private SmartFlagEnumNameResolver() { - public static readonly SmartFlagEnumNameResolver Instance = new SmartFlagEnumNameResolver(); - - private SmartFlagEnumNameResolver() - { - } + } - public IJsonFormatter GetFormatter() => - FormatterCache.Formatter; + /// + /// + /// + /// + /// + public IJsonFormatter GetFormatter() => + FormatterCache.Formatter; - private static class FormatterCache - { - public static readonly IJsonFormatter Formatter; + private static class FormatterCache + { + public static readonly IJsonFormatter Formatter; #pragma warning disable S3963 // "static" fields should be initialized inline - static FormatterCache() + static FormatterCache() + { + if (typeof(T).IsSmartFlagEnum(out var genericArguments)) { - if (typeof(T).IsSmartFlagEnum(out var genericArguments)) - { - var formatterType = typeof(SmartFlagEnumNameFormatter<,>).MakeGenericType(genericArguments); - Formatter = (IJsonFormatter)Activator.CreateInstance(formatterType); - } + var formatterType = typeof(SmartFlagEnumNameFormatter<,>).MakeGenericType(genericArguments); + Formatter = (IJsonFormatter)Activator.CreateInstance(formatterType); } -#pragma warning restore S3963 // "static" fields should be initialized inline } +#pragma warning restore S3963 // "static" fields should be initialized inline } } \ No newline at end of file diff --git a/src/SmartEnum.Utf8Json/SmartFlagEnumValueFormatter.cs b/src/SmartEnum.Utf8Json/SmartFlagEnumValueFormatter.cs index 8a540bad..4cb0fbf0 100644 --- a/src/SmartEnum.Utf8Json/SmartFlagEnumValueFormatter.cs +++ b/src/SmartEnum.Utf8Json/SmartFlagEnumValueFormatter.cs @@ -4,10 +4,22 @@ namespace Ardalis.SmartEnum.Utf8Json using global::Utf8Json.Internal; using System; + /// + /// + /// + /// + /// public class SmartFlagEnumValueFormatter : IJsonFormatter where TEnum : SmartFlagEnum where TValue : struct, IEquatable, IComparable { + /// + /// + /// + /// + /// + /// + /// public void Serialize(ref JsonWriter writer, TEnum value, IJsonFormatterResolver formatterResolver) { if (value is null) @@ -38,6 +50,12 @@ public void Serialize(ref JsonWriter writer, TEnum value, IJsonFormatterResolver throw new ArgumentOutOfRangeException(typeof(TValue).ToString(), $"{typeof(TValue).Name} is not supported."); } + /// + /// + /// + /// + /// + /// public TEnum Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver) { if (reader.ReadIsNull()) diff --git a/src/SmartEnum.Utf8Json/SmartFlagEnumValueResolver.cs b/src/SmartEnum.Utf8Json/SmartFlagEnumValueResolver.cs index b3fc2286..05902b6c 100644 --- a/src/SmartEnum.Utf8Json/SmartFlagEnumValueResolver.cs +++ b/src/SmartEnum.Utf8Json/SmartFlagEnumValueResolver.cs @@ -1,4 +1,4 @@ -namespace Ardalis.SmartEnum.Utf8Json +namespace Ardalis.SmartEnum.Utf8Json { using System; using global::Utf8Json; @@ -14,6 +14,9 @@ public class SmartFlagEnumValueResolver : IJsonFormatterResolver /// public static readonly SmartFlagEnumValueResolver Instance = new SmartFlagEnumValueResolver(); + /// + /// + /// public SmartFlagEnumValueResolver() { } diff --git a/src/SmartEnum/AllowNegativeInputValuesAttribute.cs b/src/SmartEnum/AllowNegativeInputValuesAttribute.cs index 2e53847d..04e44098 100644 --- a/src/SmartEnum/AllowNegativeInputValuesAttribute.cs +++ b/src/SmartEnum/AllowNegativeInputValuesAttribute.cs @@ -1,9 +1,10 @@ -using System; -using System.Collections.Generic; -using System.Text; +using System; namespace Ardalis.SmartEnum { + /// + /// Marker attribute used to indicate that a SmartEnum allows negative values. + /// [AttributeUsage(AttributeTargets.Class)] public class AllowNegativeInputValuesAttribute : Attribute { diff --git a/src/SmartEnum/AllowUnsafeFlagEnumValuesAttribute.cs b/src/SmartEnum/AllowUnsafeFlagEnumValuesAttribute.cs index 6cc6b880..4f2f8c6e 100644 --- a/src/SmartEnum/AllowUnsafeFlagEnumValuesAttribute.cs +++ b/src/SmartEnum/AllowUnsafeFlagEnumValuesAttribute.cs @@ -1,9 +1,10 @@ -using System; -using System.Collections.Generic; -using System.Text; +using System; namespace Ardalis.SmartEnum { + /// + /// Marker attribute to indicate that a SmartEnum should allow unsafe flag enum values. + /// [AttributeUsage(AttributeTargets.Class)] public class AllowUnsafeFlagEnumValuesAttribute : Attribute { diff --git a/src/SmartEnum/Core/SmartEnumThen.cs b/src/SmartEnum/Core/SmartEnumThen.cs index 93469f56..903ed217 100644 --- a/src/SmartEnum/Core/SmartEnumThen.cs +++ b/src/SmartEnum/Core/SmartEnumThen.cs @@ -1,7 +1,12 @@ -using System; +using System; namespace Ardalis.SmartEnum.Core { + /// + /// + /// + /// + /// public readonly struct SmartEnumThen where TEnum : ISmartEnum where TValue : IEquatable, IComparable diff --git a/src/SmartEnum/Core/SmartEnumWhen.cs b/src/SmartEnum/Core/SmartEnumWhen.cs index 2fa1c0b8..e3a573cf 100644 --- a/src/SmartEnum/Core/SmartEnumWhen.cs +++ b/src/SmartEnum/Core/SmartEnumWhen.cs @@ -1,9 +1,14 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; namespace Ardalis.SmartEnum.Core { + /// + /// + /// + /// + /// public readonly struct SmartEnumWhen where TEnum : ISmartEnum where TValue : IEquatable, IComparable diff --git a/src/SmartEnum/Exceptions/InvalidFlagEnumValueParseException.cs b/src/SmartEnum/Exceptions/InvalidFlagEnumValueParseException.cs index 6ffd4556..598fcdf8 100644 --- a/src/SmartEnum/Exceptions/InvalidFlagEnumValueParseException.cs +++ b/src/SmartEnum/Exceptions/InvalidFlagEnumValueParseException.cs @@ -1,9 +1,10 @@ using System; -using System.Collections.Generic; -using System.Text; namespace Ardalis.SmartEnum.Exceptions { + /// + /// + /// [Serializable] public class InvalidFlagEnumValueParseException : Exception { diff --git a/src/SmartEnum/Exceptions/SmartFlagEnumContainsNegativeValueException.cs b/src/SmartEnum/Exceptions/SmartFlagEnumContainsNegativeValueException.cs index 1b0fb41f..cf2bb681 100644 --- a/src/SmartEnum/Exceptions/SmartFlagEnumContainsNegativeValueException.cs +++ b/src/SmartEnum/Exceptions/SmartFlagEnumContainsNegativeValueException.cs @@ -1,9 +1,10 @@ using System; -using System.Collections.Generic; -using System.Text; namespace Ardalis.SmartEnum.Exceptions { + /// + /// + /// [Serializable] public class SmartFlagEnumContainsNegativeValueException : Exception { diff --git a/src/SmartEnum/ISmartEnum.cs b/src/SmartEnum/ISmartEnum.cs index 75d9b0dd..53a0bad8 100644 --- a/src/SmartEnum/ISmartEnum.cs +++ b/src/SmartEnum/ISmartEnum.cs @@ -1,9 +1,8 @@ -using System; -using System.Collections.Generic; -using System.Text; - namespace Ardalis.SmartEnum { + /// + /// + /// public interface ISmartEnum { // Empty Interface diff --git a/src/SmartEnum/SmartEnum.cs b/src/SmartEnum/SmartEnum.cs index 3a686672..f1a5f012 100644 --- a/src/SmartEnum/SmartEnum.cs +++ b/src/SmartEnum/SmartEnum.cs @@ -20,6 +20,11 @@ public abstract class SmartEnum : SmartEnum where TEnum : SmartEnum { + /// + /// + /// + /// + /// protected SmartEnum(string name, int value) : base(name, value) { @@ -91,7 +96,11 @@ private static IEqualityComparer GetValueComparer() private readonly string _name; private readonly TValue _value; - + /// + /// + /// + /// + /// protected SmartEnum(string name, TValue value) { if (String.IsNullOrEmpty(name)) @@ -273,13 +282,26 @@ public static bool TryFromValue(TValue value, out TEnum result) return _fromValue.Value.TryGetValue(value, out result); } + /// + /// + /// + /// public override string ToString() => _name; + /// + /// + /// + /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public override int GetHashCode() => _value.GetHashCode(); + /// + /// + /// + /// + /// public override bool Equals(object obj) => (obj is SmartEnum other) && Equals(other); @@ -329,6 +351,12 @@ public SmartEnumThen When(params SmartEnum[] smart public SmartEnumThen When(IEnumerable> smartEnums) => new SmartEnumThen(smartEnums.Contains(this), false, this); + /// + /// + /// + /// + /// + /// public static bool operator ==(SmartEnum left, SmartEnum right) { // Handle null on left side @@ -339,6 +367,12 @@ public SmartEnumThen When(IEnumerable> s return left.Equals(right); } + /// + /// + /// + /// + /// + /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool operator !=(SmartEnum left, SmartEnum right) => !(left == right); @@ -352,28 +386,60 @@ public SmartEnumThen When(IEnumerable> s public virtual int CompareTo(SmartEnum other) => _value.CompareTo(other._value); + /// + /// + /// + /// + /// + /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool operator <(SmartEnum left, SmartEnum right) => left.CompareTo(right) < 0; + /// + /// + /// + /// + /// + /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool operator <=(SmartEnum left, SmartEnum right) => left.CompareTo(right) <= 0; + /// + /// + /// + /// + /// + /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool operator >(SmartEnum left, SmartEnum right) => left.CompareTo(right) > 0; + /// + /// + /// + /// + /// + /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool operator >=(SmartEnum left, SmartEnum right) => left.CompareTo(right) >= 0; + /// + /// + /// + /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static implicit operator TValue(SmartEnum smartEnum) => smartEnum is not null ? smartEnum._value : default; + /// + /// + /// + /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static explicit operator SmartEnum(TValue value) => FromValue(value); diff --git a/src/SmartEnum/SmartEnumComparerAttribute.cs b/src/SmartEnum/SmartEnumComparerAttribute.cs index f1589744..572dbc4b 100644 --- a/src/SmartEnum/SmartEnumComparerAttribute.cs +++ b/src/SmartEnum/SmartEnumComparerAttribute.cs @@ -1,55 +1,65 @@ using System; using System.Collections.Generic; -namespace Ardalis.SmartEnum +namespace Ardalis.SmartEnum; + +/// +/// Base class for an to specify the for a SmartEnum. +/// +/// +[AttributeUsage(AttributeTargets.Class)] +public class SmartEnumComparerAttribute : Attribute { + private readonly IEqualityComparer _comparer; + /// - /// Base class for an to specify the for a SmartEnum. + /// /// - /// - [AttributeUsage(AttributeTargets.Class)] - public class SmartEnumComparerAttribute : Attribute + /// + protected SmartEnumComparerAttribute(IEqualityComparer comparer) { - private readonly IEqualityComparer _comparer; - - protected SmartEnumComparerAttribute(IEqualityComparer comparer) - { - _comparer = comparer; - } - - public IEqualityComparer Comparer => _comparer; + _comparer = comparer; } /// - /// Attribute to apply to of type to specify how to compare - /// the enum values + /// /// - public class SmartEnumStringComparerAttribute : SmartEnumComparerAttribute + public IEqualityComparer Comparer => _comparer; +} + +/// +/// Attribute to apply to of type to specify how to compare +/// the enum values +/// +public class SmartEnumStringComparerAttribute : SmartEnumComparerAttribute +{ + /// + /// + /// + /// + public SmartEnumStringComparerAttribute(StringComparison comparison) + : base(GetComparer(comparison)) { - public SmartEnumStringComparerAttribute(StringComparison comparison) - : base(GetComparer(comparison)) - { - } + } - private static IEqualityComparer GetComparer(StringComparison comparison) + private static IEqualityComparer GetComparer(StringComparison comparison) + { + switch (comparison) { - switch (comparison) - { - case StringComparison.Ordinal: - return StringComparer.Ordinal; - case StringComparison.OrdinalIgnoreCase: - return StringComparer.OrdinalIgnoreCase; - case StringComparison.CurrentCulture: - return StringComparer.CurrentCulture; - case StringComparison.CurrentCultureIgnoreCase: - return StringComparer.CurrentCultureIgnoreCase; - case StringComparison.InvariantCulture: - return StringComparer.InvariantCulture; - case StringComparison.InvariantCultureIgnoreCase: - return StringComparer.InvariantCultureIgnoreCase; - } - - throw new ArgumentException($"StringComparison {comparison} is not supported", nameof(comparison)); + case StringComparison.Ordinal: + return StringComparer.Ordinal; + case StringComparison.OrdinalIgnoreCase: + return StringComparer.OrdinalIgnoreCase; + case StringComparison.CurrentCulture: + return StringComparer.CurrentCulture; + case StringComparison.CurrentCultureIgnoreCase: + return StringComparer.CurrentCultureIgnoreCase; + case StringComparison.InvariantCulture: + return StringComparer.InvariantCulture; + case StringComparison.InvariantCultureIgnoreCase: + return StringComparer.InvariantCultureIgnoreCase; } + + throw new ArgumentException($"StringComparison {comparison} is not supported", nameof(comparison)); } } diff --git a/src/SmartEnum/SmartEnumExtensions.cs b/src/SmartEnum/SmartEnumExtensions.cs index 39c446c8..e590c34a 100644 --- a/src/SmartEnum/SmartEnumExtensions.cs +++ b/src/SmartEnum/SmartEnumExtensions.cs @@ -1,17 +1,28 @@ -using System.Globalization; -using System.Linq; - namespace Ardalis.SmartEnum { using System; using System.Collections.Generic; using System.Reflection; + /// + /// + /// public static class SmartEnumExtensions { + /// + /// + /// + /// + /// public static bool IsSmartEnum(this Type type) => IsSmartEnum(type, out var _); + /// + /// + /// + /// + /// + /// public static bool IsSmartEnum(this Type type, out Type[] genericArguments) { if (type is null || type.IsAbstract || type.IsGenericTypeDefinition) @@ -37,6 +48,12 @@ public static bool IsSmartEnum(this Type type, out Type[] genericArguments) return false; } + /// + /// + /// + /// + /// + /// public static bool TryGetValues(this Type type, out IEnumerable enums) { while (type != null) diff --git a/src/SmartEnum/SmartEnumNameAttribute.cs b/src/SmartEnum/SmartEnumNameAttribute.cs index ebbfa268..2d609c9c 100644 --- a/src/SmartEnum/SmartEnumNameAttribute.cs +++ b/src/SmartEnum/SmartEnumNameAttribute.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; @@ -10,7 +10,7 @@ namespace Ardalis.SmartEnum { /// /// A that ensures the provided value matches the - /// of a /. + /// .Name of a /. /// Nulls and non- values are considered valid /// (add if you want the field to be required). /// @@ -52,6 +52,11 @@ public SmartEnumNameAttribute( ErrorMessage = string.Format(errorMessage, propertyName, string.Join(", ", GetValidSmartEnumNames())); } + /// + /// + /// + /// + /// public override bool IsValid(object value) { if (value is not string name) return true; diff --git a/src/SmartEnum/SmartFlagEngine.cs b/src/SmartEnum/SmartFlagEngine.cs index 47fba4fe..30b5478f 100644 --- a/src/SmartEnum/SmartFlagEngine.cs +++ b/src/SmartEnum/SmartFlagEngine.cs @@ -9,10 +9,18 @@ namespace Ardalis.SmartEnum { + /// + /// + /// + /// + /// public abstract class SmartFlagEngine where TEnum : SmartFlagEnum where TValue : IEquatable, IComparable { + /// + /// + /// protected SmartFlagEngine() { } /// @@ -199,10 +207,10 @@ private static int HighestFlagValue(IReadOnlyList enumList) /// /// Gets the largest possible value of the underlying type for the SmartFlagEnum. /// - /// If the underlying type + /// If the underlying type /// does not define a MaxValue field, this exception is thrown. /// - /// The value of the constant MaxValue field defined by the underlying type . + /// The value of the constant MaxValue field defined by the underlying type . private static TValue GetMaxValue() { FieldInfo maxValueField = typeof(TValue).GetField("MaxValue", BindingFlags.Public diff --git a/src/SmartEnum/SmartFlagEnum.cs b/src/SmartEnum/SmartFlagEnum.cs index 1bbe83bd..e76059d5 100644 --- a/src/SmartEnum/SmartFlagEnum.cs +++ b/src/SmartEnum/SmartFlagEnum.cs @@ -17,6 +17,11 @@ public abstract class SmartFlagEnum : SmartFlagEnum where TEnum : SmartFlagEnum { + /// + /// + /// + /// + /// protected SmartFlagEnum(string name, int value) : base(name, value) { @@ -85,6 +90,11 @@ private static IEnumerable GetAllOptions() public TValue Value => _value; + /// + /// + /// + /// + /// protected SmartFlagEnum(string name, TValue value) { if (String.IsNullOrEmpty(name)) @@ -101,13 +111,12 @@ protected SmartFlagEnum(string name, TValue value) /// /// The names of the item/s to get. /// true to ignore case during the comparison; otherwise, false. + /// /// - /// The items associated with the specified . + /// The items associated with the specified . /// /// is null. /// does not exist. - /// - /// public static IEnumerable FromName(string names, bool ignoreCase = false, bool deserialize = false) { if (String.IsNullOrEmpty(names)) @@ -139,8 +148,6 @@ IEnumerable FromName(Dictionary dictionary) /// true if the contains an item with the specified names; otherwise, false. /// /// is null. - /// - /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool TryFromName(string names, out IEnumerable result) => TryFromName(names, false, out result); @@ -157,8 +164,6 @@ public static bool TryFromName(string names, out IEnumerable result) => /// true if the contains an item with the specified names; otherwise, false. /// /// is null. - /// - /// public static bool TryFromName(string names, bool ignoreCase, out IEnumerable result) { if (String.IsNullOrEmpty(names)) @@ -178,8 +183,6 @@ public static bool TryFromName(string names, bool ignoreCase, out IEnumerable containing the item/s found. /// /// does not exist. - /// - /// public static IEnumerable FromValue(TValue value) { if (value == null) @@ -226,7 +229,6 @@ public static TEnum DeserializeValue(TValue value) /// If the specified value is not found, returns . /// /// - /// public static IEnumerable FromValue(TValue value, IEnumerable defaultValue) { if (value == null) @@ -246,7 +248,6 @@ public static IEnumerable FromValue(TValue value, IEnumerable defa /// true if the contains any items with the specified names; otherwise, false. /// /// - /// public static bool TryFromValue(TValue value, out IEnumerable result) { if (value == null || !int.TryParse(value.ToString(), out _)) @@ -316,9 +317,17 @@ private static string FormatEnumListString(IEnumerable enumInputList) return sb.ToString(); } + /// + /// Returns the _name of the . + /// + /// public override string ToString() => _name; + /// + /// Returns the GetHashCode of the value + /// + /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public override int GetHashCode() => _value.GetHashCode(); @@ -377,6 +386,12 @@ public SmartEnumThen When(params SmartFlagEnum[] s public SmartEnumThen When(IEnumerable> smartEnums) => new SmartEnumThen(smartEnums.Contains(this), false, this); + /// + /// + /// + /// + /// + /// public static bool operator ==(SmartFlagEnum left, SmartFlagEnum right) { // Handle null on left side @@ -387,6 +402,12 @@ public SmartEnumThen When(IEnumerable + /// + /// + /// + /// + /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool operator !=(SmartFlagEnum left, SmartFlagEnum right) => !(left == right); @@ -400,26 +421,58 @@ public SmartEnumThen When(IEnumerable other) => _value.CompareTo(other._value); + /// + /// + /// + /// + /// + /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool operator <(SmartFlagEnum left, SmartFlagEnum right) => left.CompareTo(right) < 0; + /// + /// + /// + /// + /// + /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool operator <=(SmartFlagEnum left, SmartFlagEnum right) => left.CompareTo(right) <= 0; + /// + /// + /// + /// + /// + /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool operator >(SmartFlagEnum left, SmartFlagEnum right) => left.CompareTo(right) > 0; + /// + /// + /// + /// + /// + /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool operator >=(SmartFlagEnum left, SmartFlagEnum right) => left.CompareTo(right) >= 0; + /// + /// + /// + /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static implicit operator TValue(SmartFlagEnum smartFlagEnum) => smartFlagEnum._value; + /// + /// + /// + /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static explicit operator SmartFlagEnum(TValue value) => FromValue(value).First(); diff --git a/src/SmartEnum/SmartFlagEnumExtensions.cs b/src/SmartEnum/SmartFlagEnumExtensions.cs index af3efbd4..3989db18 100644 --- a/src/SmartEnum/SmartFlagEnumExtensions.cs +++ b/src/SmartEnum/SmartFlagEnumExtensions.cs @@ -1,15 +1,19 @@ -using System; -using System.Collections; +using System; using System.Collections.Generic; -using System.Diagnostics; using System.Linq; -using System.Reflection; -using System.Text; namespace Ardalis.SmartEnum { + /// + /// + /// public static class SmartFlagEnumExtensions { + /// + /// + /// + /// + /// public static bool IsSmartFlagEnum(this Type type) => IsSmartFlagEnum(type, out var _); @@ -44,6 +48,15 @@ public static bool IsSmartFlagEnum(this Type type, out Type[] genericArguments) return false; } + /// + /// + /// + /// + /// + /// + /// + /// + /// public static bool TryGetFlagEnumValuesByName(this Dictionary dictionary, string names, out IEnumerable outputEnums) where TEnum : SmartFlagEnum where TValue : IEquatable, IComparable diff --git a/src/SmartEnum/TypeExtensions.cs b/src/SmartEnum/TypeExtensions.cs index c4aa6125..f3b2f9b1 100644 --- a/src/SmartEnum/TypeExtensions.cs +++ b/src/SmartEnum/TypeExtensions.cs @@ -1,18 +1,17 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Reflection; -namespace Ardalis.SmartEnum +namespace Ardalis.SmartEnum; + +internal static class TypeExtensions { - internal static class TypeExtensions + public static List GetFieldsOfType(this Type type) { - public static List GetFieldsOfType(this Type type) - { - return type.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy) - .Where(p => type.IsAssignableFrom(p.FieldType)) - .Select(pi => (TFieldType)pi.GetValue(null)) - .ToList(); - } + return type.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy) + .Where(p => type.IsAssignableFrom(p.FieldType)) + .Select(pi => (TFieldType)pi.GetValue(null)) + .ToList(); } } From b58aa46f9760c68f268edcaab0f123eddb8a8f41 Mon Sep 17 00:00:00 2001 From: Scott DePouw Date: Fri, 19 Jan 2024 14:41:08 -0500 Subject: [PATCH 2/2] 449: Support JSON serialization of ushort SmartEnums (#487) * Support JSON serialization of ushort SmartEnums * Re-implement change post-fix --------- Co-authored-by: Steve Smith --- .../SmartFlagEnumValueConverter.cs | 4 +++- test/SmartEnum.SystemTextJson.UnitTests/FlagTestEnums.cs | 7 +++++++ .../SmartFlagEnumValueConverterTests.cs | 8 ++++++-- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/SmartEnum.SystemTextJson/SmartFlagEnumValueConverter.cs b/src/SmartEnum.SystemTextJson/SmartFlagEnumValueConverter.cs index 6c9e288d..22e093de 100644 --- a/src/SmartEnum.SystemTextJson/SmartFlagEnumValueConverter.cs +++ b/src/SmartEnum.SystemTextJson/SmartFlagEnumValueConverter.cs @@ -97,7 +97,7 @@ public override void Write(Utf8JsonWriter writer, TEnum value, JsonSerializerOpt else if (typeof(TValue) == typeof(bool)) writer.WriteBooleanValue((bool)(object)value.Value); else if (typeof(TValue) == typeof(short)) - writer.WriteNumberValue((int)(short)(object)value.Value); + writer.WriteNumberValue((short)(object)value.Value); else if (typeof(TValue) == typeof(int)) writer.WriteNumberValue((int)(object)value.Value); else if (typeof(TValue) == typeof(double)) @@ -106,6 +106,8 @@ public override void Write(Utf8JsonWriter writer, TEnum value, JsonSerializerOpt writer.WriteNumberValue((decimal)(object)value.Value); else if (typeof(TValue) == typeof(ulong)) writer.WriteNumberValue((ulong)(object)value.Value); + else if (typeof(TValue) == typeof(ushort)) + writer.WriteNumberValue((ushort)(object)value.Value); else if (typeof(TValue) == typeof(uint)) writer.WriteNumberValue((uint)(object)value.Value); else if (typeof(TValue) == typeof(float)) diff --git a/test/SmartEnum.SystemTextJson.UnitTests/FlagTestEnums.cs b/test/SmartEnum.SystemTextJson.UnitTests/FlagTestEnums.cs index 07c06fc0..25bafd2b 100644 --- a/test/SmartEnum.SystemTextJson.UnitTests/FlagTestEnums.cs +++ b/test/SmartEnum.SystemTextJson.UnitTests/FlagTestEnums.cs @@ -1,5 +1,12 @@ namespace Ardalis.SmartEnum.SystemTextJson.UnitTests { + public sealed class FlagTestEnumUnsignedInt16 : SmartFlagEnum + { + public static readonly FlagTestEnumUnsignedInt16 Instance = new(nameof(Instance), 12); + + FlagTestEnumUnsignedInt16(string name, ushort value) : base(name, value) { } + } + public sealed class FlagTestEnumInt16 : SmartFlagEnum { public static readonly FlagTestEnumInt16 Instance = new FlagTestEnumInt16(nameof(Instance), 1); diff --git a/test/SmartEnum.SystemTextJson.UnitTests/SmartFlagEnumValueConverterTests.cs b/test/SmartEnum.SystemTextJson.UnitTests/SmartFlagEnumValueConverterTests.cs index 8395d0f8..801bc582 100644 --- a/test/SmartEnum.SystemTextJson.UnitTests/SmartFlagEnumValueConverterTests.cs +++ b/test/SmartEnum.SystemTextJson.UnitTests/SmartFlagEnumValueConverterTests.cs @@ -1,4 +1,3 @@ -using Ardalis.SmartEnum; using FluentAssertions; using System; using System.Text.Json; @@ -11,6 +10,9 @@ public class SmartFlagEnumValueConverterTests { public class TestClass { + [JsonConverter(typeof(SmartFlagEnumValueConverter))] + public FlagTestEnumUnsignedInt16 UnsignedInt16 { get; set; } + [JsonConverter(typeof(SmartFlagEnumValueConverter))] public FlagTestEnumInt16 Int16 { get; set; } @@ -21,8 +23,9 @@ public class TestClass public FlagTestEnumDouble Double { get; set; } } - static readonly TestClass TestInstance = new TestClass + static readonly TestClass TestInstance = new() { + UnsignedInt16 = FlagTestEnumUnsignedInt16.Instance, Int16 = FlagTestEnumInt16.Instance, Int32 = FlagTestEnumInt32.Instance2, Double = FlagTestEnumDouble.Instance @@ -30,6 +33,7 @@ public class TestClass static readonly string JsonString = JsonSerializer.Serialize(new { + UnsignedInt16 = 12, Int16 = 1, Int32 = 2, Double = 1