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

Simplify how to call Guard methods #2894

Merged
merged 7 commits into from
Feb 16, 2022
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
Prev Previous commit
Next Next commit
if null or empty
  • Loading branch information
mic-max committed Feb 11, 2022
commit f3a9e7b0b92eafb49633dc50cbbac820d3e7024a
2 changes: 1 addition & 1 deletion src/OpenTelemetry.Api/Baggage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ public IReadOnlyDictionary<string, string> GetBaggage()
/// <returns>Baggage item or <see langword="null"/> if nothing was found.</returns>
public string GetBaggage(string name)
{
Guard.ThrowIfNullOrEmpty(name, nameof(name));
Guard.ThrowIfNullOrEmpty(name);

return this.baggage != null && this.baggage.TryGetValue(name, out string value)
? value
Expand Down
8 changes: 4 additions & 4 deletions src/OpenTelemetry.Api/Context/RuntimeContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static class RuntimeContext
/// <returns>The slot registered.</returns>
public static RuntimeContextSlot<T> RegisterSlot<T>(string slotName)
{
Guard.ThrowIfNullOrEmpty(slotName, nameof(slotName));
Guard.ThrowIfNullOrEmpty(slotName);

lock (Slots)
{
Expand All @@ -66,7 +66,7 @@ public static RuntimeContextSlot<T> RegisterSlot<T>(string slotName)
/// <returns>The slot previously registered.</returns>
public static RuntimeContextSlot<T> GetSlot<T>(string slotName)
{
Guard.ThrowIfNullOrEmpty(slotName, nameof(slotName));
Guard.ThrowIfNullOrEmpty(slotName);
var slot = GuardNotFound(slotName);
var contextSlot = Guard.ThrowIfNotOfType<RuntimeContextSlot<T>>(slot, nameof(slot));
return contextSlot;
Expand Down Expand Up @@ -127,7 +127,7 @@ public static T GetValue<T>(string slotName)
/// <param name="value">The value to be set.</param>
public static void SetValue(string slotName, object value)
{
Guard.ThrowIfNullOrEmpty(slotName, nameof(slotName));
Guard.ThrowIfNullOrEmpty(slotName);
var slot = GuardNotFound(slotName);
var runtimeContextSlotValueAccessor = Guard.ThrowIfNotOfType<IRuntimeContextSlotValueAccessor>(slot, nameof(slot));
runtimeContextSlotValueAccessor.Value = value;
Expand All @@ -140,7 +140,7 @@ public static void SetValue(string slotName, object value)
/// <returns>The value retrieved from the context slot.</returns>
public static object GetValue(string slotName)
{
Guard.ThrowIfNullOrEmpty(slotName, nameof(slotName));
Guard.ThrowIfNullOrEmpty(slotName);
var slot = GuardNotFound(slotName);
var runtimeContextSlotValueAccessor = Guard.ThrowIfNotOfType<IRuntimeContextSlotValueAccessor>(slot, nameof(slot));
return runtimeContextSlotValueAccessor.Value;
Expand Down
2 changes: 1 addition & 1 deletion src/OpenTelemetry.Api/Internal/Guard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public static void ThrowIfNull(object value, [CallerArgumentExpression("value")]
/// <param name="paramName">The parameter name to use in the thrown exception.</param>
[DebuggerHidden]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ThrowIfNullOrEmpty(string value, string paramName = DefaultParamName)
public static void ThrowIfNullOrEmpty(string value, [CallerArgumentExpression("value")] string paramName = DefaultParamName)
{
if (string.IsNullOrEmpty(value))
{
Expand Down
2 changes: 1 addition & 1 deletion src/OpenTelemetry/Resources/ResourceBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static ResourceBuilder AddService(
{
Dictionary<string, object> resourceAttributes = new Dictionary<string, object>();

Guard.ThrowIfNullOrEmpty(serviceName, nameof(serviceName));
Guard.ThrowIfNullOrEmpty(serviceName);

resourceAttributes.Add(ResourceSemanticConventions.AttributeServiceName, serviceName);

Expand Down
19 changes: 13 additions & 6 deletions test/OpenTelemetry.Tests/Internal/GuardTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ public void NullTest()
Assert.Equal("potato", ex1.ParamName);

object @event = null;
ex1 = Assert.Throws<ArgumentNullException>(() => Guard.ThrowIfNull(@event));
Assert.Contains("Must not be null", ex1.Message);
Assert.Equal("@event", ex1.ParamName);
var ex2 = Assert.Throws<ArgumentNullException>(() => Guard.ThrowIfNull(@event));
Assert.Contains("Must not be null", ex2.Message);
Assert.Equal("@event", ex2.ParamName);

Thing thing = null;
ex1 = Assert.Throws<ArgumentNullException>(() => Guard.ThrowIfNull(thing?.Bar));
Assert.Contains("Must not be null", ex1.Message);
Assert.Equal("thing?.Bar", ex1.ParamName);
var ex3 = Assert.Throws<ArgumentNullException>(() => Guard.ThrowIfNull(thing?.Bar));
Assert.Contains("Must not be null", ex3.Message);
Assert.Equal("thing?.Bar", ex3.ParamName);
}

[Fact]
Expand All @@ -69,9 +69,16 @@ public void NullOrEmptyTest()
// Invalid
var ex1 = Assert.Throws<ArgumentException>(() => Guard.ThrowIfNullOrEmpty(null));
Assert.Contains("Must not be null or empty", ex1.Message);
Assert.Equal("null", ex1.ParamName);

var ex2 = Assert.Throws<ArgumentException>(() => Guard.ThrowIfNullOrEmpty(string.Empty));
Assert.Contains("Must not be null or empty", ex2.Message);
Assert.Equal("string.Empty", ex2.ParamName);

var x = string.Empty;
var ex3 = Assert.Throws<ArgumentException>(() => Guard.ThrowIfNullOrEmpty(x));
Assert.Contains("Must not be null or empty", ex3.Message);
Assert.Equal("x", ex3.ParamName);
}

[Fact]
Expand Down