From e5ca209fdf513f5800479e0994ac1e78014c47cf Mon Sep 17 00:00:00 2001 From: Pawel Gerr Date: Fri, 17 Nov 2023 17:55:26 +0100 Subject: [PATCH] Generate Map/Switch if the number of items is less than 1000 --- Directory.Build.props | 2 +- .../CodeAnalysis/SmartEnums/AllEnumSettings.cs | 8 ++++---- .../CodeAnalysis/SmartEnums/EnumSettings.cs | 4 ++-- .../CodeAnalysis/SmartEnums/SmartEnumCodeGenerator.cs | 6 ++++-- 4 files changed, 11 insertions(+), 9 deletions(-) diff --git a/Directory.Build.props b/Directory.Build.props index cf4a82eb..1c1a0beb 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -2,7 +2,7 @@ (c) $([System.DateTime]::Now.Year), Pawel Gerr. All rights reserved. - 6.5.1 + 6.5.2 Pawel Gerr true https://github.com/PawelGerr/Thinktecture.Runtime.Extensions diff --git a/src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/SmartEnums/AllEnumSettings.cs b/src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/SmartEnums/AllEnumSettings.cs index 49bf4042..bc1274a1 100644 --- a/src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/SmartEnums/AllEnumSettings.cs +++ b/src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/SmartEnums/AllEnumSettings.cs @@ -12,8 +12,8 @@ public sealed class AllEnumSettings : IEquatable public OperatorsGeneration EqualityComparisonOperators { get; } public bool SkipIFormattable { get; } public bool SkipToString { get; } - public bool SkipSwitchMethods { get; } - public bool SkipMapMethods { get; } + public bool? SkipSwitchMethods { get; } + public bool? SkipMapMethods { get; } public AllEnumSettings(AttributeData? attribute, bool isValidatable) { @@ -25,8 +25,8 @@ public AllEnumSettings(AttributeData? attribute, bool isValidatable) EqualityComparisonOperators = attribute?.FindEqualityComparisonOperators() ?? OperatorsGeneration.Default; SkipIFormattable = attribute?.FindSkipIFormattable() ?? false; SkipToString = attribute?.FindSkipToString() ?? false; - SkipSwitchMethods = attribute?.FindSkipSwitchMethods() ?? false; - SkipMapMethods = attribute?.FindSkipMapMethods() ?? false; + SkipSwitchMethods = attribute?.FindSkipSwitchMethods(); + SkipMapMethods = attribute?.FindSkipMapMethods(); // Comparison operators depend on the equality comparison operators if (ComparisonOperators > EqualityComparisonOperators) diff --git a/src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/SmartEnums/EnumSettings.cs b/src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/SmartEnums/EnumSettings.cs index 56af1b0e..14e1bfa3 100644 --- a/src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/SmartEnums/EnumSettings.cs +++ b/src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/SmartEnums/EnumSettings.cs @@ -7,8 +7,8 @@ namespace Thinktecture.CodeAnalysis.SmartEnums; public bool IsValidatable => _settings.IsValidatable; public bool SkipToString => _settings.SkipToString; - public bool SkipSwitchMethods => _settings.SkipSwitchMethods; - public bool SkipMapMethods => _settings.SkipMapMethods; + public bool? SkipSwitchMethods => _settings.SkipSwitchMethods; + public bool? SkipMapMethods => _settings.SkipMapMethods; public bool HasStructLayoutAttribute => _attributeInfo.HasStructLayoutAttribute; public IReadOnlyList DesiredFactories => _attributeInfo.DesiredFactories; diff --git a/src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/SmartEnums/SmartEnumCodeGenerator.cs b/src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/SmartEnums/SmartEnumCodeGenerator.cs index a9f25b53..a6d81e0b 100644 --- a/src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/SmartEnums/SmartEnumCodeGenerator.cs +++ b/src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/SmartEnums/SmartEnumCodeGenerator.cs @@ -166,7 +166,9 @@ public override int GetHashCode() if (!_state.Settings.SkipToString) GenerateToString(); - if (!_state.Settings.SkipSwitchMethods) + var hasSaneNumberOfItems = _state.ItemNames.Count < 1000; + + if (!_state.Settings.SkipSwitchMethods.GetValueOrDefault() && hasSaneNumberOfItems) { GenerateSwitchForAction(false); GenerateSwitchForAction(true); @@ -174,7 +176,7 @@ public override int GetHashCode() GenerateSwitchForFunc(true); } - if (!_state.Settings.SkipMapMethods) + if (!_state.Settings.SkipMapMethods.GetValueOrDefault() && hasSaneNumberOfItems) GenerateMap(); GenerateGetLookup();