Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrated IPv6 scalar to source gen regex. #5875

Merged
merged 4 commits into from
Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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