Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
28 changes: 27 additions & 1 deletion src/Sentry/StringOrRegex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,22 @@

namespace Sentry;

/// <summary>
/// The type of value stored by <see cref="StringOrRegex"/>.
/// </summary>
public enum StringOrRegexType
{
/// <summary>
/// A plain string.
/// </summary>
String,

/// <summary>
/// A regular expression.
/// </summary>
Regex,
}

/// <summary>
/// Stores either a plain string or a Regular Expression, typically to match against filters in the SentryOptions
/// </summary>
Expand All @@ -11,13 +27,19 @@ public class StringOrRegex
internal readonly Regex? _regex;
internal readonly string? _string;

/// <summary>
/// Gets the type of value stored by this instance.
/// </summary>
public StringOrRegexType Type { get; }
Comment thread
jamescrosswell marked this conversation as resolved.
Resolved

/// <summary>
/// Constructs a <see cref="StringOrRegex"/> instance.
/// </summary>
/// <param name="stringOrRegex">The prefix or regular expression pattern to match on.</param>
public StringOrRegex(string stringOrRegex)
{
_string = stringOrRegex;
Type = StringOrRegexType.String;
}

/// <summary>
Expand All @@ -27,7 +49,11 @@ public StringOrRegex(string stringOrRegex)
/// <remarks>
/// Use this constructor when you want the match to be performed using a regular expression.
/// </remarks>
public StringOrRegex(Regex regex) => _regex = regex;
public StringOrRegex(Regex regex)
{
_regex = regex;
Type = StringOrRegexType.Regex;
}

/// <summary>
/// Implicitly converts a <see cref="string"/> to a <see cref="StringOrRegex"/>.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1358,12 +1358,18 @@ namespace Sentry
{
public StringOrRegex(System.Text.RegularExpressions.Regex regex) { }
public StringOrRegex(string stringOrRegex) { }
public Sentry.StringOrRegexType Type { get; }
public override bool Equals(object? obj) { }
public override int GetHashCode() { }
public override string ToString() { }
public static Sentry.StringOrRegex op_Implicit(System.Text.RegularExpressions.Regex regex) { }
public static Sentry.StringOrRegex op_Implicit(string stringOrRegex) { }
}
public enum StringOrRegexType
{
String = 0,
Regex = 1,
}
public class TransactionContext : Sentry.SpanContext, Sentry.ITransactionContext, Sentry.Protocol.ITraceContext
{
public TransactionContext(string name, string operation, Sentry.SpanId? spanId = default, Sentry.SpanId? parentSpanId = default, Sentry.SentryId? traceId = default, string? description = "", Sentry.SpanStatus? status = default, bool? isSampled = default, bool? isParentSampled = default, Sentry.TransactionNameSource nameSource = 0) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1358,12 +1358,18 @@ namespace Sentry
{
public StringOrRegex(System.Text.RegularExpressions.Regex regex) { }
public StringOrRegex(string stringOrRegex) { }
public Sentry.StringOrRegexType Type { get; }
public override bool Equals(object? obj) { }
public override int GetHashCode() { }
public override string ToString() { }
public static Sentry.StringOrRegex op_Implicit(System.Text.RegularExpressions.Regex regex) { }
public static Sentry.StringOrRegex op_Implicit(string stringOrRegex) { }
}
public enum StringOrRegexType
{
String = 0,
Regex = 1,
}
public class TransactionContext : Sentry.SpanContext, Sentry.ITransactionContext, Sentry.Protocol.ITraceContext
{
public TransactionContext(string name, string operation, Sentry.SpanId? spanId = default, Sentry.SpanId? parentSpanId = default, Sentry.SentryId? traceId = default, string? description = "", Sentry.SpanStatus? status = default, bool? isSampled = default, bool? isParentSampled = default, Sentry.TransactionNameSource nameSource = 0) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1358,12 +1358,18 @@ namespace Sentry
{
public StringOrRegex(System.Text.RegularExpressions.Regex regex) { }
public StringOrRegex(string stringOrRegex) { }
public Sentry.StringOrRegexType Type { get; }
public override bool Equals(object? obj) { }
public override int GetHashCode() { }
public override string ToString() { }
public static Sentry.StringOrRegex op_Implicit(System.Text.RegularExpressions.Regex regex) { }
public static Sentry.StringOrRegex op_Implicit(string stringOrRegex) { }
}
public enum StringOrRegexType
{
String = 0,
Regex = 1,
}
public class TransactionContext : Sentry.SpanContext, Sentry.ITransactionContext, Sentry.Protocol.ITraceContext
{
public TransactionContext(string name, string operation, Sentry.SpanId? spanId = default, Sentry.SpanId? parentSpanId = default, Sentry.SentryId? traceId = default, string? description = "", Sentry.SpanStatus? status = default, bool? isSampled = default, bool? isParentSampled = default, Sentry.TransactionNameSource nameSource = 0) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1339,12 +1339,18 @@ namespace Sentry
{
public StringOrRegex(System.Text.RegularExpressions.Regex regex) { }
public StringOrRegex(string stringOrRegex) { }
public Sentry.StringOrRegexType Type { get; }
public override bool Equals(object? obj) { }
public override int GetHashCode() { }
public override string ToString() { }
public static Sentry.StringOrRegex op_Implicit(System.Text.RegularExpressions.Regex regex) { }
public static Sentry.StringOrRegex op_Implicit(string stringOrRegex) { }
}
public enum StringOrRegexType
{
String = 0,
Regex = 1,
}
public class TransactionContext : Sentry.SpanContext, Sentry.ITransactionContext, Sentry.Protocol.ITraceContext
{
public TransactionContext(string name, string operation, Sentry.SpanId? spanId = default, Sentry.SpanId? parentSpanId = default, Sentry.SentryId? traceId = default, string? description = "", Sentry.SpanStatus? status = default, bool? isSampled = default, bool? isParentSampled = default, Sentry.TransactionNameSource nameSource = 0) { }
Expand Down Expand Up @@ -2065,4 +2071,4 @@ public static class SentryExceptionExtensions
public static void AddSentryContext(this System.Exception ex, string name, System.Collections.Generic.IReadOnlyDictionary<string, object> data) { }
public static void AddSentryTag(this System.Exception ex, string name, string value) { }
public static void SetSentryMechanism(this System.Exception ex, string type, string? description = null, bool? handled = default, bool? terminal = default) { }
}
}
20 changes: 18 additions & 2 deletions test/Sentry.Tests/StringOrRegexTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,33 @@ namespace Sentry.Tests;
public class StringOrRegexTests
{
[Fact]
public void StringOrRegex_ImplicitlyConvertsFromString()
public void Constructor_String_TypeIsString()
{
var target = new StringOrRegex("abc");
target.Type.Should().Be(StringOrRegexType.String);
}

[Fact]
public void Constructor_Regex_TypeIsRegex()
{
var target = new StringOrRegex(new Regex("^abc.*ghi$"));
target.Type.Should().Be(StringOrRegexType.Regex);
}

[Fact]
public void ImplicitConversion_String_PreservesValueAndType()
{
StringOrRegex target = "abc";
target.Type.Should().Be(StringOrRegexType.String);
target._string.Should().Be("abc");
target._regex.Should().BeNull();
}

[Fact]
public void StringOrRegex_ImplicitlyConvertsFromRegex()
public void ImplicitConversion_Regex_PreservesValueAndType()
{
StringOrRegex target = new Regex("^abc.*ghi$");
target.Type.Should().Be(StringOrRegexType.Regex);
target._string.Should().BeNull();
target._regex.Should().NotBeNull();
target._regex?.ToString().Should().Be("^abc.*ghi$");
Expand Down
Loading