From eaae7e80a56521f0f34c3e2b92ce6de23ec13371 Mon Sep 17 00:00:00 2001 From: Michael Staib Date: Tue, 21 Feb 2023 12:14:20 +0100 Subject: [PATCH] Migrated IPv6 scalar to source gen regex. (#5875) --- .../Core/src/Types.Scalars/IPv6Type.cs | 27 +++++++++++++------ .../Core/src/Types.Scalars/RegexType.cs | 12 +++------ .../test/Types.Scalars.Tests/IPv6TypeTests.cs | 3 --- 3 files changed, 23 insertions(+), 19 deletions(-) diff --git a/src/HotChocolate/Core/src/Types.Scalars/IPv6Type.cs b/src/HotChocolate/Core/src/Types.Scalars/IPv6Type.cs index 29033406db4..262cc6e2292 100644 --- a/src/HotChocolate/Core/src/Types.Scalars/IPv6Type.cs +++ b/src/HotChocolate/Core/src/Types.Scalars/IPv6Type.cs @@ -1,3 +1,4 @@ +using System; using System.Text.RegularExpressions; using HotChocolate.Language; @@ -7,7 +8,11 @@ namespace HotChocolate.Types; /// The `IPv6` scalar type represents a valid a IPv6 address as defined in /// RFC8064 /// +#if NET7_0_OR_GREATER +public partial class IPv6Type : RegexType +#else public class IPv6Type : RegexType +#endif { private const string _validationPattern = "(^(?:(?:(?:[0-9A-Fa-f]{1,4}:){6}(?:[0-9A-Fa-f]{1,4}:[0-9A-Fa-f]{1,4}|(?:(?:0?0?" + @@ -33,6 +38,17 @@ public class IPv6Type : RegexType "]{1,4}|(?:(?:[0-9A-Fa-f]{1,4}:){0,6}[0-9A-Fa-f]{1,4})?::)(?:\\/(?:0?0?[0-9]|0?[" + "1-9][0-9]|1[01][0-9]|12[0-8]))?)$)"; +#if NET7_0_OR_GREATER + [GeneratedRegex(_validationPattern, RegexOptions.IgnoreCase, DefaultRegexTimeoutInMs)] + private static partial Regex CreateRegex(); +#else + private static Regex CreateRegex() + => new Regex( + _validationPattern, + RegexOptions.Compiled | RegexOptions.IgnoreCase, + TimeSpan.FromMilliseconds(DefaultRegexTimeoutInMs)); +#endif + /// /// Initializes a new instance of the class. /// @@ -52,22 +68,17 @@ public IPv6Type( BindingBehavior bind = BindingBehavior.Explicit) : base( name, - _validationPattern, + CreateRegex(), description, - RegexOptions.Compiled | RegexOptions.IgnoreCase, bind) { } /// protected override SerializationException CreateParseLiteralError(IValueNode valueSyntax) - { - return ThrowHelper.IPv6Type_ParseLiteral_IsInvalid(this); - } + => ThrowHelper.IPv6Type_ParseLiteral_IsInvalid(this); /// protected override SerializationException CreateParseValueError(object runtimeValue) - { - return ThrowHelper.IPv6Type_ParseValue_IsInvalid(this); - } + => ThrowHelper.IPv6Type_ParseValue_IsInvalid(this); } diff --git a/src/HotChocolate/Core/src/Types.Scalars/RegexType.cs b/src/HotChocolate/Core/src/Types.Scalars/RegexType.cs index 036babe5c2e..90431acbba5 100644 --- a/src/HotChocolate/Core/src/Types.Scalars/RegexType.cs +++ b/src/HotChocolate/Core/src/Types.Scalars/RegexType.cs @@ -10,7 +10,7 @@ namespace HotChocolate.Types; /// public class RegexType : StringType { - private const int _defaultRegexTimeoutInMs = 100; + protected const int DefaultRegexTimeoutInMs = 100; private readonly Regex _validationRegex; @@ -28,7 +28,7 @@ public RegexType( new Regex( pattern, regexOptions, - TimeSpan.FromMilliseconds(_defaultRegexTimeoutInMs)), + TimeSpan.FromMilliseconds(DefaultRegexTimeoutInMs)), description, bind) { @@ -101,13 +101,9 @@ public override bool TryDeserialize(object? resultValue, out object? runtimeValu /// protected override SerializationException CreateParseLiteralError(IValueNode valueSyntax) - { - return ThrowHelper.RegexType_ParseLiteral_IsInvalid(this, Name); - } + => ThrowHelper.RegexType_ParseLiteral_IsInvalid(this, Name); /// protected override SerializationException CreateParseValueError(object runtimeValue) - { - return ThrowHelper.RegexType_ParseValue_IsInvalid(this, Name); - } + => ThrowHelper.RegexType_ParseValue_IsInvalid(this, Name); } diff --git a/src/HotChocolate/Core/test/Types.Scalars.Tests/IPv6TypeTests.cs b/src/HotChocolate/Core/test/Types.Scalars.Tests/IPv6TypeTests.cs index 8e6390f03c5..2723fabf1fa 100644 --- a/src/HotChocolate/Core/test/Types.Scalars.Tests/IPv6TypeTests.cs +++ b/src/HotChocolate/Core/test/Types.Scalars.Tests/IPv6TypeTests.cs @@ -5,8 +5,6 @@ namespace HotChocolate.Types; -// The current preview have bugs with regex. -#if !NET7_0 public class IPv6TypeTests : ScalarTypeTestBase { [Fact] @@ -599,4 +597,3 @@ public void ParseResult_GivenObject_ThrowSerializationException(object value) ExpectParseResultToThrowSerializationException(value); } } -#endif