Skip to content

Commit

Permalink
Migrated IPv6 scalar to source gen regex. (#5875)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib authored Feb 21, 2023
1 parent 77b15be commit eaae7e8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 19 deletions.
27 changes: 19 additions & 8 deletions src/HotChocolate/Core/src/Types.Scalars/IPv6Type.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Text.RegularExpressions;
using HotChocolate.Language;

Expand All @@ -7,7 +8,11 @@ namespace HotChocolate.Types;
/// The `IPv6` scalar type represents a valid a IPv6 address as defined in
/// <a href="https://tools.ietf.org/html/rfc8064">RFC8064</a>
/// </summary>
#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?" +
Expand All @@ -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

/// <summary>
/// Initializes a new instance of the <see cref="IPv6Type"/> class.
/// </summary>
Expand All @@ -52,22 +68,17 @@ public IPv6Type(
BindingBehavior bind = BindingBehavior.Explicit)
: base(
name,
_validationPattern,
CreateRegex(),
description,
RegexOptions.Compiled | RegexOptions.IgnoreCase,
bind)
{
}

/// <inheritdoc />
protected override SerializationException CreateParseLiteralError(IValueNode valueSyntax)
{
return ThrowHelper.IPv6Type_ParseLiteral_IsInvalid(this);
}
=> ThrowHelper.IPv6Type_ParseLiteral_IsInvalid(this);

/// <inheritdoc />
protected override SerializationException CreateParseValueError(object runtimeValue)
{
return ThrowHelper.IPv6Type_ParseValue_IsInvalid(this);
}
=> ThrowHelper.IPv6Type_ParseValue_IsInvalid(this);
}
12 changes: 4 additions & 8 deletions src/HotChocolate/Core/src/Types.Scalars/RegexType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace HotChocolate.Types;
/// </summary>
public class RegexType : StringType
{
private const int _defaultRegexTimeoutInMs = 100;
protected const int DefaultRegexTimeoutInMs = 100;

private readonly Regex _validationRegex;

Expand All @@ -28,7 +28,7 @@ public RegexType(
new Regex(
pattern,
regexOptions,
TimeSpan.FromMilliseconds(_defaultRegexTimeoutInMs)),
TimeSpan.FromMilliseconds(DefaultRegexTimeoutInMs)),
description,
bind)
{
Expand Down Expand Up @@ -101,13 +101,9 @@ public override bool TryDeserialize(object? resultValue, out object? runtimeValu

/// <inheritdoc />
protected override SerializationException CreateParseLiteralError(IValueNode valueSyntax)
{
return ThrowHelper.RegexType_ParseLiteral_IsInvalid(this, Name);
}
=> ThrowHelper.RegexType_ParseLiteral_IsInvalid(this, Name);

/// <inheritdoc />
protected override SerializationException CreateParseValueError(object runtimeValue)
{
return ThrowHelper.RegexType_ParseValue_IsInvalid(this, Name);
}
=> ThrowHelper.RegexType_ParseValue_IsInvalid(this, Name);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

namespace HotChocolate.Types;

// The current preview have bugs with regex.
#if !NET7_0
public class IPv6TypeTests : ScalarTypeTestBase
{
[Fact]
Expand Down Expand Up @@ -599,4 +597,3 @@ public void ParseResult_GivenObject_ThrowSerializationException(object value)
ExpectParseResultToThrowSerializationException<IPv6Type>(value);
}
}
#endif

0 comments on commit eaae7e8

Please sign in to comment.